传感器设备入库接口+每月1日凌晨定时方法位置

This commit is contained in:
gongjiale 2024-10-14 23:29:22 +08:00
parent f3c39d4a13
commit 117578f3c9
8 changed files with 498 additions and 1 deletions

View File

@ -111,7 +111,7 @@ public class SecurityConfig
.authorizeHttpRequests((requests) -> {
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
requests.antMatchers("/login", "/register", "/captchaImage").permitAll()
requests.antMatchers("/login", "/register", "/captchaImage","/outside/*").permitAll()
// 静态资源可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()

View File

@ -0,0 +1,43 @@
package com.ruoyi.project.outside.controller;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.outside.service.OutSideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/outside")
public class OutSideController extends BaseController {
@Autowired
private OutSideService outSideService;
@PostMapping("/sensorData")
public AjaxResult receiveSensorData(@RequestBody String data) {
try {
Integer succ=outSideService.insertDevice(data);
return AjaxResult.success(succ);
} catch (Exception e) {
System.out.println(e);
return AjaxResult.error();
}
}
// @PostMapping("/monthTask")
// public AjaxResult monthTask(@RequestBody String date) {
// try {
// Integer succ=outSideService.monthTask(date);
// return AjaxResult.success(succ);
// } catch (Exception e) {
// System.out.println(e);
// return AjaxResult.error();
// }
//
// }
}

View File

@ -0,0 +1,159 @@
package com.ruoyi.project.outside.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class SensorData {
/**
* 上报时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date reportTime;
/**
* 设备编码
*/
private String sn;
/**
* 探测器标号
*/
private String flag;
/**
* 气体浓度 gas_value
*/
private String ds;
/**
* 检测精度 gas_dec
*/
private int jcjd;
/**
* 状态 gas_status
*/
private String zt;
/**
* 种类 gas_type
*/
private String zl;
/**
* 单位 gas_unit
*/
private String dw;
/**
* 一级报警值alarml
*/
private String dbz;
/**
* 二级报警值 alarmh
*/
private String gbz;
/**
* 量程 gas_range
*/
private String lc;
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getDs() {
return ds;
}
public void setDs(String ds) {
this.ds = ds;
}
public int getJcjd() {
return jcjd;
}
public void setJcjd(int jcjd) {
this.jcjd = jcjd;
}
public String getZt() {
return zt;
}
public void setZt(String zt) {
this.zt = zt;
}
public String getZl() {
return zl;
}
public Date getReportTime() {
return reportTime;
}
public void setReportTime(Date reportTime) {
this.reportTime = reportTime;
}
public void setZl(String zl) {
this.zl = zl;
}
public String getDw() {
return dw;
}
public void setDw(String dw) {
this.dw = dw;
}
public String getDbz() {
return dbz;
}
public void setDbz(String dbz) {
this.dbz = dbz;
}
public String getGbz() {
return gbz;
}
public void setGbz(String gbz) {
this.gbz = gbz;
}
public String getLc() {
return lc;
}
public void setLc(String lc) {
this.lc = lc;
}
@Override
public String toString() {
return "SensorData{" +
"sn='" + sn + '\'' +
", flag='" + flag + '\'' +
", ds='" + ds + '\'' +
", jcjd=" + jcjd +
", zt='" + zt + '\'' +
", zl='" + zl + '\'' +
", dw=" + dw +
", dbz='" + dbz + '\'' +
", gbz='" + gbz + '\'' +
", lc='" + lc + '\'' +
'}';
}
}

View File

@ -0,0 +1,55 @@
package com.ruoyi.project.outside.domain;
public class SensorMonth {
/* * 设备编码
*/
private String sn;
private String deptId;
private String avgValue;
private String year;
private String month;
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getDeptId() {
return deptId;
}
public void setDeptId(String deptId) {
this.deptId = deptId;
}
public String getAvgValue() {
return avgValue;
}
public void setAvgValue(String avgValue) {
this.avgValue = avgValue;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
}

View File

@ -0,0 +1,29 @@
package com.ruoyi.project.outside.mapper;
import com.ruoyi.project.outside.domain.SensorData;
import com.ruoyi.project.outside.domain.SensorMonth;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface OutSideMapper {
/**
* 插入设备信息
* @param data
*/
void insertDevice(SensorData data);
/**
* 查询上月的平均值
* @param lastMonth
* @return
*/
List<SensorMonth> queryMonth(String lastMonth);
/**
* 插入月度表
* @param list
*/
void insertMonth(@Param("sensorMonthList") List<SensorMonth> list);
}

View File

@ -0,0 +1,17 @@
package com.ruoyi.project.outside.service;
public interface OutSideService {
/**
* 插入设备表
* @param data
* @return
*/
Integer insertDevice(String data);
/**
* 每月定时任务 - :2024-04
* @param date
* @return
*/
Integer monthTask(String date);
}

View File

@ -0,0 +1,127 @@
package com.ruoyi.project.outside.service.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.outside.domain.SensorData;
import com.ruoyi.project.outside.domain.SensorMonth;
import com.ruoyi.project.outside.mapper.OutSideMapper;
import com.ruoyi.project.outside.service.OutSideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
public class OutSideServiceImpl implements OutSideService {
@Autowired
private OutSideMapper outSideMapper;
@Override
public Integer insertDevice(String data) {
try {
SensorData sensorData = new SensorData();
// 假设 ID 长度固定为 16
String sn = data.substring(0, 16);
String jsonData = data.substring(16);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(jsonData);
JsonNode sensorNode = rootNode.get("sensorDatas").get(0); // 获取第一个 sensorData
sensorData.setSn(sn);
sensorData.setFlag(sensorNode.get("flag").asText());
sensorData.setDs(sensorNode.get("gas_value").asText());
sensorData.setJcjd(sensorNode.get("gas_dec").asInt());
// gas_status 转换为中文状态描述
int gasStatus = sensorNode.get("gas_status").asInt();
String statusDescription = getStatusDescription(gasStatus); // 调用转换方法
sensorData.setZt(statusDescription);
// sensorData.setZl(sensorNode.get("gas_type").asText()); 写死了 都是VOCS
sensorData.setZl("VOCS");
// gas_unit 转换为对应单位
int gasUnit = sensorNode.get("gas_unit").asInt();
String unitDescription = getUnitDescription(gasUnit); // 调用转换方法
sensorData.setDw(unitDescription); // 将中文单位设置到 dw 字段
sensorData.setDbz(sensorNode.get("alarml").asText());
sensorData.setGbz(sensorNode.get("alarmh").asText());
sensorData.setLc(sensorNode.get("gas_range").asText());
sensorData.setJcjd(sensorNode.get("gas_dec").asInt());//检测精度
sensorData.setReportTime(DateUtils.getNowDate());
outSideMapper.insertDevice(sensorData);
return 1;
}catch (Exception e) {
System.out.println(e);
return 0;
}
}
@Override
public Integer monthTask(String date) {
// 使用 YearMonth 来解析字符串日期
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
YearMonth yearMonth = YearMonth.parse(date, formatter);
// 计算上个月
YearMonth previousMonth = yearMonth.minusMonths(1);
// 格式化回字符串
String lastMonth = previousMonth.format(formatter);
List<SensorMonth> list=outSideMapper.queryMonth(lastMonth);
outSideMapper.insertMonth(list);
return 1;
}
// gas_unit 值转换为对应的单位描述
private static String getUnitDescription(int gasUnit) {
switch (gasUnit) {
case 1:
return "%LEL";
case 2:
return "%VOL";
case 3:
return "PPM";
case 4:
return "umol/mol";
case 5:
return "mg/m3";
case 6:
return "ug/m3";
case 7:
return "";
case 8:
return "%";
case 9:
return "%RH";
default:
return "未知单位"; // 如果有其他未定义的单位返回默认值
}
}
// gas_status 值转换为对应的中文描述
private static String getStatusDescription(int gasStatus) {
switch (gasStatus) {
case 0:
return "预热";
case 1:
return "正常";
case 2:
return "标定错误";
case 3:
return "传感器故障";
case 4:
return "预警";
case 5:
return "一级报警";
case 6:
return "二级报警";
case 7:
return "故障";
case 8:
return "禁止使用";
default:
return "未知状态"; // 如果有其他未定义的状态返回默认值
}
}
}

View File

@ -0,0 +1,67 @@
<?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.ruoyi.project.outside.mapper.OutSideMapper">
<resultMap id="SensorMonthResultMap" type="com.ruoyi.project.outside.domain.SensorMonth">
<result property="sn" column="sn" />
<result property="deptId" column="dept_id" />
<result property="avgValue" column="avg_value" />
<result property="year" column="year" />
<result property="month" column="month" />
</resultMap>
<insert id="insertDevice" >
INSERT INTO TH_DEVICE_REPORT (
SN,
REPORT_TIME,
DS,
GBZ,
DBZ,
ZT,
ZL,
DW,
JCJD,
LC
) VALUES (
#{sn, jdbcType=NVARCHAR},
#{reportTime, jdbcType=DATE},
#{ds, jdbcType=NVARCHAR},
#{gbz, jdbcType=NVARCHAR},
#{dbz, jdbcType=NVARCHAR},
#{zt, jdbcType=VARCHAR},
#{zl, jdbcType=VARCHAR},
#{dw, jdbcType=VARCHAR},
#{jcjd, jdbcType=NUMERIC},
#{lc, jdbcType=VARCHAR}
)
</insert>
<select id="queryMonth" resultMap="SensorMonthResultMap">
SELECT
r.SN,
AVG(DS) AS "avg_value",
dept.dept_id,
TO_CHAR(r.REPORT_TIME, 'YYYY') AS year,
TO_CHAR(r.REPORT_TIME, 'MM') AS month
FROM
th_device_report r JOIN th_device t on r.sn=t.sn join sys_dept dept on t.dept_id=dept.dept_id
WHERE
TO_CHAR(r.REPORT_TIME, 'YYYY-MM') = #{lastMonth}
GROUP BY
r.SN,
dept.dept_id,
TO_CHAR(r.REPORT_TIME, 'YYYY'),
TO_CHAR(r.REPORT_TIME, 'MM')
</select>
<insert id="insertMonth">
INSERT ALL
<foreach collection="sensorMonthList" item="sensor" separator="">
INTO th_device_report_month1 (sn, dept_id, avg_value, year, month)
VALUES (#{sensor.sn}, #{sensor.deptId}, #{sensor.avgValue}, #{sensor.year}, #{sensor.month})
</foreach>
SELECT * FROM dual
</insert>
</mapper>