feat:每日流量写入;登录接口优化路由器是否可以连接路由器
This commit is contained in:
parent
da32fc3231
commit
24d45e48f0
19
pom.xml
19
pom.xml
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
<lombok.version>1.18.30</lombok.version>
|
||||
<hutool.version>5.8.26</hutool.version>
|
||||
<commons.version>2.11.0</commons.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
@ -61,11 +62,27 @@
|
|||
<artifactId>sa-token-spring-boot-starter</artifactId>
|
||||
<version>${sa-token.version}</version>
|
||||
</dependency>
|
||||
<!-- 数据库相关 -->
|
||||
<dependency>
|
||||
<groupId>org.xerial</groupId>
|
||||
<artifactId>sqlite-jdbc</artifactId>
|
||||
<version>3.45.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
<version>7.15.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.3.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.11.0</version>
|
||||
<version>${commons.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- HuTool -->
|
||||
|
|
|
|||
|
|
@ -1,14 +1,18 @@
|
|||
package com.bcrjl.miwifi;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* 项目启动类
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@EnableScheduling
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.bcrjl.miwifi.mapper")
|
||||
public class WebApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WebApplication.class, args);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
package com.bcrjl.miwifi.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.DependsOn;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 数据库初始化
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@DependsOn("myBatisPlusConfig")
|
||||
@Configuration
|
||||
public class FlywayDbInitializer {
|
||||
@Resource
|
||||
private DataSource dataSource;
|
||||
|
||||
|
||||
/**
|
||||
* 启动时根据当前数据库类型执行数据库初始化
|
||||
*/
|
||||
@PostConstruct
|
||||
public void migrateFlyway() {
|
||||
try {
|
||||
String databaseProductName = dataSource.getConnection().getMetaData().getDatabaseProductName();
|
||||
String dbType = databaseProductName.toLowerCase(Locale.ROOT);
|
||||
|
||||
// 检查当前数据库类型是否支持
|
||||
if (!StrUtil.equalsAnyIgnoreCase(dbType, "sqlite")) {
|
||||
throw new RuntimeException("不支持的数据库类型 [" + dbType + "]");
|
||||
}
|
||||
Flyway load = Flyway.configure().dataSource(dataSource).outOfOrder(true).locations("db/migration-sqlite").load();
|
||||
load.migrate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package com.bcrjl.miwifi.config;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* mybatis-plus 配置类
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class MyBatisPlusConfig {
|
||||
@Resource
|
||||
private DataSource dataSource;
|
||||
|
||||
@Value("${spring.datasource.driver-class-name}")
|
||||
private String datasourceDriveClassName;
|
||||
|
||||
@Value("${spring.datasource.url}")
|
||||
private String datasourceUrl;
|
||||
|
||||
/**
|
||||
* 如果是 sqlite 数据库,自动创建数据库文件所在目录
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
if (StrUtil.equals(datasourceDriveClassName, "org.sqlite.JDBC")) {
|
||||
String path = datasourceUrl.replace("jdbc:sqlite:", "");
|
||||
String folderPath = FileUtil.getParent(path, 1);
|
||||
log.info("SQLite 数据库文件所在目录: [{}]", folderPath);
|
||||
if (!FileUtil.exist(folderPath)) {
|
||||
FileUtil.mkdir(folderPath);
|
||||
log.info("检测到 SQLite 数据库文件所在目录不存在, 已自动创建.");
|
||||
} else {
|
||||
log.info("检测到 SQLite 数据库文件所在目录已存在, 无需自动创建.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* mybatis plus 分页插件配置
|
||||
*/
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() throws SQLException {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
String databaseProductName = dataSource.getConnection().getMetaData().getDatabaseProductName();
|
||||
DbType dbType = DbType.getDbType(databaseProductName);
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,9 @@ package com.bcrjl.miwifi.controller;
|
|||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.bcrjl.miwifi.common.response.R;
|
||||
import com.bcrjl.miwifi.common.util.MiWifiUtil;
|
||||
import com.bcrjl.miwifi.model.param.LoginParam;
|
||||
import com.bcrjl.miwifi.service.DeviceDayTrafficService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
|
@ -11,6 +13,10 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 鉴权服务
|
||||
*
|
||||
|
|
@ -21,6 +27,11 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
||||
@Resource
|
||||
private DeviceDayTrafficService deviceDayTrafficService;
|
||||
@Resource
|
||||
private MiWifiUtil miWifiUtil;
|
||||
|
||||
/**
|
||||
* 系统密码
|
||||
*/
|
||||
|
|
@ -36,7 +47,12 @@ public class AuthController {
|
|||
@PostMapping("/login")
|
||||
public R login(@RequestBody LoginParam param) {
|
||||
if (SecureUtil.md5(PASSWORD).equals(param.getPassword())) {
|
||||
Map<String, Object> loginInfo = miWifiUtil.getLoginInfo();
|
||||
if (Objects.isNull(loginInfo)) {
|
||||
return R.fail("无法链接路由器信息");
|
||||
}
|
||||
StpUtil.login(10001);
|
||||
deviceDayTrafficService.initTraffic();
|
||||
return R.data(StpUtil.getTokenInfo());
|
||||
} else {
|
||||
return R.fail("密码错误:");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
package com.bcrjl.miwifi.job;
|
||||
|
||||
import com.bcrjl.miwifi.service.DeviceDayTrafficService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 设备任务
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DeviceJob {
|
||||
@Resource
|
||||
private DeviceDayTrafficService deviceDayTrafficService;
|
||||
|
||||
@Scheduled(cron = "0 0 0 * * ?")
|
||||
public void writeEverydayDeviceTraffic() {
|
||||
log.info("每日凌晨执行写入设备流量开始");
|
||||
try {
|
||||
deviceDayTrafficService.writeEverydayDeviceTraffic();
|
||||
} catch (Exception e) {
|
||||
log.error("每日写入数据异常", e);
|
||||
}
|
||||
log.info("每日凌晨执行写入设备流量结束");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package com.bcrjl.miwifi.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.bcrjl.miwifi.model.domain.DeviceDayTraffic;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 设备每日统计表 Mapper 接口
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@Mapper
|
||||
public interface DeviceDayTrafficMapper extends BaseMapper<DeviceDayTraffic> {
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bcrjl.miwifi.mapper.DeviceDayTrafficMapper">
|
||||
<resultMap id="BaseResultMap" type="com.bcrjl.miwifi.model.domain.DeviceDayTraffic">
|
||||
<id column="id" jdbcType="INTEGER" property="id"/>
|
||||
<result column="date" jdbcType="VARCHAR" property="date"/>
|
||||
<result column="ip" jdbcType="VARCHAR" property="ip"/>
|
||||
<result column="mac" jdbcType="VARCHAR" property="mac"/>
|
||||
<result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
|
||||
<result column="upload" jdbcType="VARCHAR" property="upload"/>
|
||||
<result column="download" jdbcType="VARCHAR" property="download"/>
|
||||
<result column="total" jdbcType="VARCHAR" property="total"/>
|
||||
</resultMap>
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package com.bcrjl.miwifi.model.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 设备每日流量统计表
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@Data
|
||||
@TableName("device_day_traffic")
|
||||
public class DeviceDayTraffic implements Serializable {
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
@TableField("date")
|
||||
private String date;
|
||||
@TableField("ip")
|
||||
private String ip;
|
||||
@TableField("mac")
|
||||
private String mac;
|
||||
@TableField("device_name")
|
||||
private String deviceName;
|
||||
@TableField("upload")
|
||||
private String upload;
|
||||
@TableField("download")
|
||||
private String download;
|
||||
@TableField("total")
|
||||
private String total;
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package com.bcrjl.miwifi.service;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.bcrjl.miwifi.common.util.MiWifiUtil;
|
||||
import com.bcrjl.miwifi.mapper.DeviceDayTrafficMapper;
|
||||
import com.bcrjl.miwifi.model.domain.DeviceDayTraffic;
|
||||
import com.bcrjl.miwifi.model.result.StatusResult;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 设备每日流量 Service
|
||||
*
|
||||
* @author yanqs
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class DeviceDayTrafficService extends ServiceImpl<DeviceDayTrafficMapper, DeviceDayTraffic> {
|
||||
|
||||
private final MiWifiUtil miWifiUtil;
|
||||
|
||||
/**
|
||||
* 定时任务写入每日数据
|
||||
*/
|
||||
public void writeEverydayDeviceTraffic() {
|
||||
dayTraffic();
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统启动初始化数据
|
||||
*/
|
||||
public void initTraffic() {
|
||||
List<DeviceDayTraffic> list = this.list();
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
dayTraffic();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 每日流量写入
|
||||
*/
|
||||
private void dayTraffic() {
|
||||
StatusResult status = miWifiUtil.getStatus();
|
||||
if (Objects.nonNull(status) && CollUtil.isNotEmpty(status.getDev())) {
|
||||
List<DeviceDayTraffic> dbList = new ArrayList<>();
|
||||
Date date = DateUtil.date();
|
||||
String dateStr = DateUtil.format(date, "yyyy-MM-dd");
|
||||
status.getDev().forEach(dev -> {
|
||||
DeviceDayTraffic deviceDayTraffic = new DeviceDayTraffic();
|
||||
deviceDayTraffic.setMac(dev.getMac());
|
||||
deviceDayTraffic.setDate(dateStr);
|
||||
deviceDayTraffic.setDeviceName(dev.getDevName());
|
||||
deviceDayTraffic.setUpload(dev.getUpload());
|
||||
deviceDayTraffic.setDownload(dev.getDownload());
|
||||
Long total = Long.parseLong(dev.getUpload()) + Long.valueOf(dev.getDownload());
|
||||
deviceDayTraffic.setTotal(String.valueOf(total));
|
||||
dbList.add(deviceDayTraffic);
|
||||
});
|
||||
this.saveBatch(dbList);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,3 +2,10 @@ bmw:
|
|||
url: 192.168.31.1
|
||||
password: 123456
|
||||
web-password: 123456
|
||||
db:
|
||||
path: ${user.home}/.miwifi-agent/db/agent
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:sqlite:${bmw.db.path}.db
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,9 @@ spring:
|
|||
web:
|
||||
resources:
|
||||
static-locations: classpath:/static/
|
||||
datasource:
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
|
||||
|
||||
############## Sa-Token 配置 (文档: https://sa-token.cc) ##############
|
||||
sa-token:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
CREATE TABLE "device_day_traffic"
|
||||
(
|
||||
"id" integer NOT NULL,
|
||||
"date" text,
|
||||
"mac" TEXT,
|
||||
"device_name" TEXT,
|
||||
"ip" TEXT,
|
||||
"upload" TEXT,
|
||||
"download" TEXT,
|
||||
"total" TEXT,
|
||||
PRIMARY KEY ("id")
|
||||
);
|
||||
Loading…
Reference in New Issue