From 8f91a75244ab7c1d4919b368921a6f833fbb1226 Mon Sep 17 00:00:00 2001 From: 18650502300 <18650502300@163.com> Date: Sun, 31 Mar 2024 20:30:26 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E8=A7=A3=E6=9E=90=E6=97=A5=E5=BF=97?= =?UTF-8?q?=EF=BC=8C=E6=98=AF=E5=90=A6=E5=85=A5=E5=BA=93=E6=97=A5=E5=BF=97?= =?UTF-8?q?=202.=20=E5=AE=9A=E6=97=B6=E5=99=A8=E4=BF=AE=E6=94=B9=E6=9B=B4?= =?UTF-8?q?=E6=96=B0onenet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/onenet/DatapointValue.java | 2 +- AutoSprout-ui/src/api/plant/analysisLog.js | 44 ++++ .../src/views/plant/analysisLog/index.vue | 211 +++++++++++++++ .../com/ruoyi/device/app/AppController.java | 1 + .../com/ruoyi/device/domain/AsDevice.java | 3 + .../device/domain/AsPlantAnalysisLog.java | 4 + .../java/com/ruoyi/device/domain/AsTimer.java | 3 + .../service/impl/AsTimerServiceImpl.java | 243 ++++++++++++------ .../service/impl/AsUserServiceImpl.java | 22 ++ .../mapper/plant/AsPlantAnalysisLogMapper.xml | 18 +- 10 files changed, 460 insertions(+), 91 deletions(-) create mode 100644 AutoSprout-ui/src/api/plant/analysisLog.js create mode 100644 AutoSprout-ui/src/views/plant/analysisLog/index.vue diff --git a/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/onenet/DatapointValue.java b/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/onenet/DatapointValue.java index a8d10f4..5e7688c 100644 --- a/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/onenet/DatapointValue.java +++ b/AutoSprout-common/src/main/java/com/ruoyi/common/core/domain/onenet/DatapointValue.java @@ -25,7 +25,7 @@ public class DatapointValue { private int sec;//秒 private Boolean sw;// 开关 private int week;//周转成二进制 bit0位开始为周一 0110 (week&1<<0) 1<<1 1<<2 00000110 - + private int id; } // 内部类 Mc 脉冲 diff --git a/AutoSprout-ui/src/api/plant/analysisLog.js b/AutoSprout-ui/src/api/plant/analysisLog.js new file mode 100644 index 0000000..7b8c079 --- /dev/null +++ b/AutoSprout-ui/src/api/plant/analysisLog.js @@ -0,0 +1,44 @@ +import request from '@/utils/request' + +// 查询植物解析日志列表 +export function listAnalysisLog(query) { + return request({ + url: '/plant/analysis/list', + method: 'get', + params: query + }) +} + +// 查询植物解析日志详细 +export function getAnalysisLog(id) { + return request({ + url: '/plant/analysis/' + id, + method: 'get' + }) +} + +// 新增植物解析日志 +export function addAnalysisLog(data) { + return request({ + url: '/plant/analysis', + method: 'post', + data: data + }) +} + +// 修改植物解析日志 +export function updateAnalysisLog(data) { + return request({ + url: '/plant/analysis', + method: 'put', + data: data + }) +} + +// 删除植物解析日志 +export function delAnalysisLog(id) { + return request({ + url: '/plant/analysis/' + id, + method: 'delete' + }) +} diff --git a/AutoSprout-ui/src/views/plant/analysisLog/index.vue b/AutoSprout-ui/src/views/plant/analysisLog/index.vue new file mode 100644 index 0000000..5509ab5 --- /dev/null +++ b/AutoSprout-ui/src/views/plant/analysisLog/index.vue @@ -0,0 +1,211 @@ + + + diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java index d48f73b..37f2c62 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java @@ -211,6 +211,7 @@ public class AppController extends BaseController /** 记录植物库中没有该植物,用于后期补充植物库*/ AsPlantAnalysisLog asPlantAnalysisLog = AsPlantAnalysisLog.builder() .plantName(name) + .isExist("0") .identifyId(asPlantIdentifyLog.getId()) .msg("没有该植物信息") .build(); diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsDevice.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsDevice.java index 72a56af..125a336 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsDevice.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsDevice.java @@ -83,6 +83,9 @@ public class AsDevice extends BaseEntity // @Excel(name = "定时浇水规则json") private String regularWatering; + /** 下次浇水json */ + private String nextDs; + /** 启动土壤湿度值 */ @Excel(name = "启动土壤湿度值") private Integer soilMoistureOpen; diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsPlantAnalysisLog.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsPlantAnalysisLog.java index 58ae71a..f7e6b86 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsPlantAnalysisLog.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsPlantAnalysisLog.java @@ -33,6 +33,10 @@ public class AsPlantAnalysisLog extends BaseEntity @Excel(name = "名称") private String plantName; + /** 是否存在植物库:0-不存在;1-存在 */ + @Excel(name = "是否入库") + private String isExist; + /** 消息 */ @Excel(name = "消息") private String msg; diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsTimer.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsTimer.java index fee0342..b5cb7a9 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsTimer.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/domain/AsTimer.java @@ -31,6 +31,9 @@ public class AsTimer extends BaseEntity @Excel(name = "定时模式:1:-单次浇水;2-循环浇水") private String mode; + /** 定时器索引 */ + private Long index; + /** 启动时间 */ @Excel(name = "启动时间", width = 30, dateFormat = "HH:mm") private Time startTime; diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsTimerServiceImpl.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsTimerServiceImpl.java index f696491..8dd9fc5 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsTimerServiceImpl.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsTimerServiceImpl.java @@ -1,16 +1,5 @@ package com.ruoyi.device.service.impl; -import java.io.UnsupportedEncodingException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.sql.Time; -import java.text.SimpleDateFormat; -import java.time.Instant; -import java.time.LocalDateTime; -import java.time.ZoneId; -import java.util.Date; -import java.util.List; - import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.constant.HttpStatus; @@ -18,25 +7,37 @@ import com.ruoyi.common.constant.IotConstants; import com.ruoyi.common.core.domain.onenet.*; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.common.utils.onenet.IotUtil; import com.ruoyi.common.utils.onenet.Token; import com.ruoyi.device.domain.AsDevice; -import com.ruoyi.device.domain.AsWateringRecord; +import com.ruoyi.device.domain.AsTimer; +import com.ruoyi.device.mapper.AsTimerMapper; import com.ruoyi.device.service.IAsDeviceService; +import com.ruoyi.device.service.IAsTimerService; import lombok.SneakyThrows; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import com.ruoyi.device.mapper.AsTimerMapper; -import com.ruoyi.device.domain.AsTimer; -import com.ruoyi.device.service.IAsTimerService; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionTemplate; import javax.annotation.Resource; +import java.sql.Time; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; /** * 定时器Service业务层处理 @@ -85,10 +86,39 @@ public class AsTimerServiceImpl implements IAsTimerService * @param asTimer 定时器 * @return 定时器 */ + @SneakyThrows @Override public List selectAsTimerList(AsTimer asTimer) { - return asTimerMapper.selectAsTimerList(asTimer); + /** 根据设备id请求onenet获取到该设备的定时器列表*/ + String token = Token.getToken(); + Long deviceId = asTimer.getDeviceId(); + AsDevice device = asDeviceService.selectAsDeviceByDeviceId(deviceId); + String mac = device.getMac(); + if(StringUtils.isEmpty(mac)){ + throw new ServiceException("设备无mac号"); + } + /**请求onenet获取到该设备的定时器列表*/ + List dsArray = getDs("add",asTimer, token, device); + /**组装定时器信息返回给前端*/ + List asTimers = new ArrayList<>(); + for (int i = 0; i < dsArray.size(); i++) { + AsTimer asTimer1 = new AsTimer(); + DatapointValue.Ds ds = dsArray.get(i); + Boolean once = ds.getOnce(); + asTimer1.setMode(once?"1":"2"); + asTimer1.setDeviceId(asTimer.getDeviceId()); + asTimer1.setIndex((long)i); + //将week将10进制转成2进制 + asTimer1.setWeek(Integer.toBinaryString(ds.getWeek())); + asTimer1.setIsSwitch(ds.getSw()); + // 将时分秒,hour,min,sec,转成java.sql.Time类型 + LocalTime localTime = LocalTime.of(ds.getHour(), ds.getMin(), ds.getSec()); + asTimer1.setStartTime(Time.valueOf(localTime)); + asTimers.add(asTimer1); + } +// List asTimers = asTimerMapper.selectAsTimerList(asTimer); + return asTimers; } /** @@ -106,75 +136,111 @@ public class AsTimerServiceImpl implements IAsTimerService Time sqlTime = new Time(timeFormat.parse(asTimer.getStartTimeStr()).getTime()); asTimer.setStartTime(sqlTime); asTimer.setCreateTime(DateUtils.getNowDate()); -// -// Long deviceId = asTimer.getDeviceId(); -// AsDevice device = asDeviceService.selectAsDeviceByDeviceId(deviceId); -// -// /**1.请求onenet获取到该设备的定时器信息*/ -// String param = "device_name=" + device.getMac() + "&product_id=" + productId; -// String sendUrl = iotUrl+ IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param; -// -// String token = Token.getToken(); -// logger.info("IOT获取到Authorization:【{}】",token); -// String result = HttpUtils.sendGetWithToken(sendUrl, null, token); -// -// logger.info("IOT返回的结果【{}】",result); -// /**2.处理返回的参数*/ -// DataPointRes dataPointRes = JSONObject.parseObject(result, DataPointRes.class); -// Data data = dataPointRes.getData(); -// List datastreams = data.getDatastreams(); -// List dsArray = null; -// for (Datastream datastream: datastreams) { -// if(datastream.getId().equals("jj")){ -// List datapoints = datastream.getDatapoints(); -// for (Datapoint obj:datapoints) { -// DatapointValue value = obj.getValue(); -// dsArray = value.getDsArray(); -// DatapointValue.Ds ds = new DatapointValue.Ds(); -// //将week(二进制)转成十进制 -// String week = asTimer.getWeek(); -// ds.setWeek(Integer.parseInt(week, 2)); -// //将startTimeStr(HH:mm)转成time提取小时、分钟、秒 -// Date parse = timeFormat.parse(asTimer.getStartTimeStr()); -// Instant instant = parse.toInstant(); -// LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); -// //time提取出小时,分钟,秒 -// ds.setHour(localDateTime.getHour()); -// ds.setMin(localDateTime.getMinute()); -// ds.setSec(localDateTime.getSecond()); -// String mode = asTimer.getMode(); -// ds.setOnce("1".equals(mode)?true:false); -// ds.setSw(asTimer.getIsSwitch()); -// dsArray.add(ds); -// } -// } -// } -// -// /** 向onenet发送编辑指令 更新设备定时器*/ -// /** 1.获取参数*/ -// String command = getTimerCommand(dsArray); -// logger.info("IOT获取到下发命令:【{}】",command); -// -// /** 2.发送请求*/ -// String deviceName = device.getMac();//mac地址就是产品名称 -// String param2 = "device_name=" + deviceName + "&product_id=" + productId +"&timeout=" + timeout; -// String sendUrl2 = iotUrl+ IotConstants.ADDS_COMMAND + "?"+param2; -// -// logger.info("IOT获取到Authorization:【{}】",token); -// -// String result2 = HttpUtils.sendPostWithToken(sendUrl2, command, token); -// -// /** 2.返回结果处理*/ -// JSONObject paramsObj = JSON.parseObject(result2); -// String code = paramsObj.getString("code"); -// if (!HttpStatus.IOT_SUCCESS.equals(code)) -// { -// throw new ServiceException(code+"-----"+ IotUtil.formatMsg(code)); -// } -// logger.info("IOT请求调用结果:【{}】",result2); + + String token = Token.getToken(); + Long deviceId = asTimer.getDeviceId(); + AsDevice device = asDeviceService.selectAsDeviceByDeviceId(deviceId); + + /**请求onenet获取到该设备的定时器列表*/ + List dsArray = getDs("add",asTimer, token, device); + + /**向onenet发送编辑指令*/ + sendCommand(token, device, dsArray); return asTimerMapper.insertAsTimer(asTimer); } + private void sendCommand(String token, AsDevice device, List dsArray) { + /** 向onenet发送编辑指令 更新设备定时器*/ + /** 1.获取参数*/ + String command = getTimerCommand(dsArray); + logger.info("IOT获取到下发命令:【{}】",command); + + /** 2.发送请求*/ + String deviceName = device.getMac();//mac地址就是产品名称 + String param2 = "device_name=" + deviceName + "&product_id=" + productId +"&timeout=" + timeout; + String sendUrl2 = iotUrl+ IotConstants.ADDS_COMMAND + "?"+param2; + + logger.info("IOT获取到Authorization:【{}】", token); + + String result2 = HttpUtils.sendPostWithToken(sendUrl2, command, token); + + /** 3.返回结果处理*/ + JSONObject paramsObj = JSON.parseObject(result2); + String code = paramsObj.getString("code"); + if (!HttpStatus.IOT_SUCCESS.equals(code)) + { + throw new ServiceException(code+"-----"+ IotUtil.formatMsg(code)); + } + logger.info("IOT请求调用结果:【{}】",result2); + } + + /** + * 请求onenet获取到该设备的定时器信息 + * + * @param asTimer 定时器 + * @return 结果 + */ + @Nullable + private List getDs(String type, AsTimer asTimer, String token, AsDevice device) throws ParseException { + /**1.请求onenet获取到该设备的定时器信息*/ + String param = "device_name=" + device.getMac() + "&product_id=" + productId; + String sendUrl = iotUrl+ IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param; + + logger.info("IOT获取到Authorization:【{}】", token); + String result = HttpUtils.sendGetWithToken(sendUrl, null, token); + + logger.info("IOT返回的结果【{}】",result); + /**2.处理返回的参数*/ + DataPointRes dataPointRes = JSONObject.parseObject(result, DataPointRes.class); + Data data = dataPointRes.getData(); + List datastreams = data.getDatastreams(); + List dsArray = null; + for (Datastream datastream: datastreams) { + if(datastream.getId().equals("jj")){ + List datapoints = datastream.getDatapoints(); + for (Datapoint obj:datapoints) { + DatapointValue value = obj.getValue(); + dsArray = value.getDsArray(); + DatapointValue.Ds ds = getDsItem(asTimer); + if("add".equals(type)){ + //添加定时器 + dsArray.add(ds); + }else if("edit".equals(type)){ + //编辑定时器 + for (int i = 0; i < dsArray.size(); i++) { + if(i == asTimer.getIndex() ){ + dsArray.remove(i); + dsArray.add(ds); + } + } + } + } + } + } + logger.info("请求onenet获取到定时器列表【{}】",JSON.toJSON(dsArray)); + return dsArray; + } + + @NotNull + private DatapointValue.Ds getDsItem(AsTimer asTimer) throws ParseException { + DatapointValue.Ds ds = new DatapointValue.Ds(); + //将week(二进制)转成十进制 + String week = asTimer.getWeek(); + ds.setWeek(Integer.parseInt(week, 2)); + //将startTimeStr(HH:mm)转成time提取小时、分钟、秒 + Date parse = new SimpleDateFormat("HH:mm").parse(asTimer.getStartTimeStr()); + Instant instant = parse.toInstant(); + LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); + //time提取出小时,分钟,秒 + ds.setHour(localDateTime.getHour()); + ds.setMin(localDateTime.getMinute()); + ds.setSec(localDateTime.getSecond()); + String mode = asTimer.getMode(); + ds.setOnce("1".equals(mode)?true:false); + ds.setSw(asTimer.getIsSwitch()); + return ds; + } + //拼接定时器命令 private String getTimerCommand(List dsArray) { if (dsArray == null || dsArray.size() == 0) @@ -192,7 +258,6 @@ public class AsTimerServiceImpl implements IAsTimerService .append("hour").append(ds.getHour()).append(IotConstants.COMMAND_SEPARATOR) .append("min").append(ds.getMin()).append(IotConstants.COMMAND_SEPARATOR) .append("sec").append(ds.getSec()).append(IotConstants.COMMAND_SEPARATOR); - } return command.toString(); } @@ -203,9 +268,21 @@ public class AsTimerServiceImpl implements IAsTimerService * @param asTimer 定时器 * @return 结果 */ + @SneakyThrows @Override public int updateAsTimer(AsTimer asTimer) { + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); + Time sqlTime = new Time(timeFormat.parse(asTimer.getStartTimeStr()).getTime()); + asTimer.setStartTime(sqlTime); + String token = Token.getToken(); + Long deviceId = asTimer.getDeviceId(); + AsDevice device = asDeviceService.selectAsDeviceByDeviceId(deviceId); + /** 请求onenet获取到该设备的定时器列表*/ + List dsArray = getDs("edit",asTimer, token, device); + /** 根据定时器索引修改定时器 */ + /**向onenet发送编辑指令*/ + sendCommand(token, device, dsArray); asTimer.setUpdateTime(DateUtils.getNowDate()); return asTimerMapper.updateAsTimer(asTimer); } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java index cf8a795..9e5c7a9 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsUserServiceImpl.java @@ -8,6 +8,7 @@ import com.ruoyi.common.annotation.DataScope; import com.ruoyi.common.constant.HttpStatus; import com.ruoyi.common.constant.IotConstants; import com.ruoyi.common.constant.UserConstants; +import com.ruoyi.common.core.domain.onenet.*; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; @@ -426,8 +427,29 @@ public class AsUserServiceImpl implements IAsUserService String code = paramsObj.getString("code"); if (!HttpStatus.IOT_SUCCESS.equals(code)) { asDevice1.setOnlineStatus("不在线"); + return asDevices; }else{ asDevice1.setOnlineStatus("在线"); + // 查询onenet设备参数 + String datapointsUrl = iotUrl+ IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param; + log.info("IOT获取到Authorization:【{}】", token); + String result2 = HttpUtils.sendGetWithToken(datapointsUrl, null, token); + DataPointRes dataPointRes = JSONObject.parseObject(result2, DataPointRes.class); + Data data = dataPointRes.getData(); + List datastreams = data.getDatastreams(); + for (Datastream datastream: datastreams) { + if(datastream.getId().equals("jj")){ + List datapoints = datastream.getDatapoints(); + Datapoint datapoint = datapoints.get(0); + DatapointValue value = datapoint.getValue(); + asDevice1.setWaterIntensity(value.getJiaoshui_qiangdu()); + DatapointValue.Tr tr = value.getTr(); + asDevice1.setSoilMoistureClose(tr.getEnd_sd()); + asDevice1.setSoilMoistureOpen(tr.getStart_sd()); + asDevice1.setPulseModeParam(JSON.toJSONString(value.getMc())); + asDevice1.setNextDs(JSON.toJSONString(value.getNext_ds())); + } + } } } return asDevices; diff --git a/AutoSprout-watering/src/main/resources/mapper/plant/AsPlantAnalysisLogMapper.xml b/AutoSprout-watering/src/main/resources/mapper/plant/AsPlantAnalysisLogMapper.xml index 9c619ac..f729027 100644 --- a/AutoSprout-watering/src/main/resources/mapper/plant/AsPlantAnalysisLogMapper.xml +++ b/AutoSprout-watering/src/main/resources/mapper/plant/AsPlantAnalysisLogMapper.xml @@ -3,41 +3,44 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + + - select id, identify_id, plant_name, create_time, msg from as_plant_analysis_log + select id, identify_id, plant_name, is_exist, create_time, msg from as_plant_analysis_log - + - + insert into as_plant_analysis_log identify_id, plant_name, + is_exist, create_time, msg, #{identifyId}, #{plantName}, + #{isExist}, #{createTime}, #{msg}, @@ -48,6 +51,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" identify_id = #{identifyId}, plant_name = #{plantName}, + is_exist = #{isExist}, create_time = #{createTime}, msg = #{msg}, @@ -59,9 +63,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - delete from as_plant_analysis_log where id in + delete from as_plant_analysis_log where id in #{id} - \ No newline at end of file +