From 117578f3c93d0bb61c73ffd470b167ea77697511 Mon Sep 17 00:00:00 2001 From: gongjiale <942894820@qq.com> Date: Mon, 14 Oct 2024 23:29:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=A0=E6=84=9F=E5=99=A8=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=85=A5=E5=BA=93=E6=8E=A5=E5=8F=A3+=E6=AF=8F=E6=9C=881?= =?UTF-8?q?=E6=97=A5=E5=87=8C=E6=99=A8=E5=AE=9A=E6=97=B6=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/config/SecurityConfig.java | 2 +- .../outside/controller/OutSideController.java | 43 +++++ .../project/outside/domain/SensorData.java | 159 ++++++++++++++++++ .../project/outside/domain/SensorMonth.java | 55 ++++++ .../project/outside/mapper/OutSideMapper.java | 29 ++++ .../outside/service/OutSideService.java | 17 ++ .../service/impl/OutSideServiceImpl.java | 127 ++++++++++++++ .../mybatis/outside/OutSideMapper.xml | 67 ++++++++ 8 files changed, 498 insertions(+), 1 deletion(-) create mode 100644 RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java create mode 100644 RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java create mode 100644 RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorMonth.java create mode 100644 RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/mapper/OutSideMapper.java create mode 100644 RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java create mode 100644 RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java create mode 100644 RuoYi-Vue-Oracle/src/main/resources/mybatis/outside/OutSideMapper.xml diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index 511842b..3ee8565 100644 --- a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -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() diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java new file mode 100644 index 0000000..ad8eac1 --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/controller/OutSideController.java @@ -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(); +// } +// +// } + + } + diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java new file mode 100644 index 0000000..9825355 --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorData.java @@ -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 + '\'' + + '}'; + } +} diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorMonth.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorMonth.java new file mode 100644 index 0000000..f95f9cd --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/domain/SensorMonth.java @@ -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; + } +} diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/mapper/OutSideMapper.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/mapper/OutSideMapper.java new file mode 100644 index 0000000..966544d --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/mapper/OutSideMapper.java @@ -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 queryMonth(String lastMonth); + + /** + * 插入月度表 + * @param list + */ + void insertMonth(@Param("sensorMonthList") List list); +} diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java new file mode 100644 index 0000000..9915f14 --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/OutSideService.java @@ -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); +} diff --git a/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java new file mode 100644 index 0000000..1fbd021 --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/java/com/ruoyi/project/outside/service/impl/OutSideServiceImpl.java @@ -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 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 "未知状态"; // 如果有其他未定义的状态,返回默认值 + } + } +} diff --git a/RuoYi-Vue-Oracle/src/main/resources/mybatis/outside/OutSideMapper.xml b/RuoYi-Vue-Oracle/src/main/resources/mybatis/outside/OutSideMapper.xml new file mode 100644 index 0000000..77d872f --- /dev/null +++ b/RuoYi-Vue-Oracle/src/main/resources/mybatis/outside/OutSideMapper.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + 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 ALL + + INTO th_device_report_month1 (sn, dept_id, avg_value, year, month) + VALUES (#{sensor.sn}, #{sensor.deptId}, #{sensor.avgValue}, #{sensor.year}, #{sensor.month}) + + SELECT * FROM dual + + \ No newline at end of file