From b8d0c9acb91509746e77fa76afd87fcb8410a44f Mon Sep 17 00:00:00 2001 From: 18650502300 <18650502300@163.com> Date: Wed, 10 Apr 2024 16:09:43 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=B5=87=E8=8A=B1=E5=99=A8=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/onenet/DatapointValue.java | 2 +- .../com/ruoyi/device/app/AppController.java | 1 + .../com/ruoyi/device/domain/AsDevice.java | 9 +++ .../ruoyi/device/service/IAsTimerService.java | 10 +++ .../service/impl/AsDeviceServiceImpl.java | 61 ++++++++++++------- .../service/impl/AsTimerServiceImpl.java | 2 +- .../service/impl/AsUserServiceImpl.java | 38 +++++++++--- .../mapper/device/AsDeviceMapper.xml | 6 +- 8 files changed, 95 insertions(+), 34 deletions(-) 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 abfa1be..b6a853c 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 @@ -59,7 +59,7 @@ public class DatapointValue { private int hour; private int min; private int sec; - private Boolean sw;//开关 + private Boolean sw;//定时总开关,先根据总开关判断定时功能是否关闭,如果打开,再判断定时数组中有没有有效的定时器 private int week;//周转成二进制 bit0位开始为周一 0110 (week&1<<0) 1<<1 1<<2 00000110 private String date;//日期 } 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 de3a562..74edb1b 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 @@ -134,6 +134,7 @@ public class AppController extends BaseController @PutMapping("/device") public AjaxResult edit(@RequestBody AsDevice asDevice) { + logger.info("接收到修改设备参数请求:【{}】",JSON.toJSON(asDevice)); return toAjax(asDeviceService.updateAsDevice(asDevice)); } 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 2a5f6ed..4c15666 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 @@ -1,6 +1,7 @@ package com.ruoyi.device.domain; import java.util.Date; +import java.util.List; import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.annotation.JsonFormat; @@ -143,4 +144,12 @@ public class AsDevice extends BaseEntity /** 是否配网,0-未配网,1-已配网 */ @Excel(name = "是否配网") private String isNetwork; + + /** 浇水模式:1-定时;2-智能土壤湿度;3-手动 */ + @Excel(name = "浇水模式") + private List mode; + + /** 浇水模式:1-定时;2-智能土壤湿度;3-手动 */ + @Excel(name = "浇水模式") + private String modeStr; } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsTimerService.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsTimerService.java index 21e4a0b..b671d31 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsTimerService.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/IAsTimerService.java @@ -1,6 +1,8 @@ package com.ruoyi.device.service; import java.util.List; + +import com.ruoyi.common.core.domain.onenet.DatapointValue; import com.ruoyi.device.domain.AsTimer; import org.springframework.web.bind.annotation.PathVariable; @@ -68,4 +70,12 @@ public interface IAsTimerService * @return 结果 */ public Boolean isSwitch(AsTimer asTimer); + + /** + * 清除无效定时器 + * + * @param dsArray 定时器数组 + * @return 结果 + */ + public List cleanInvalidDs(List dsArray); } diff --git a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsDeviceServiceImpl.java b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsDeviceServiceImpl.java index 3827a04..f388d87 100644 --- a/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsDeviceServiceImpl.java +++ b/AutoSprout-watering/src/main/java/com/ruoyi/device/service/impl/AsDeviceServiceImpl.java @@ -25,6 +25,8 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static com.ruoyi.common.utils.SecurityUtils.getUsername; @@ -195,12 +197,6 @@ public class AsDeviceServiceImpl extends ServiceImpl i private String getCommand(AsDevice asDevice) { - String regularWateringSwitch = "true".equals(asDevice.getRegularWatering()) ? "1" : "0";//定时浇水开关 - - JSONObject regularWateringJsonObj = JSON.parseObject(asDevice.getRegularWateringJson()); - String startHour = regularWateringJsonObj.getString("startHour");//启动时间-小时 - String startMin = regularWateringJsonObj.getString("startMin");//启动时间-分钟 - String sprayTime = regularWateringJsonObj.getString("sprayTime");//喷洒时间 String soilMoistureSwitch = "true".equals(asDevice.getSoilMoisture()) ? "1" : "0";//土壤湿度开关 Integer soilMoistureOpen = asDevice.getSoilMoistureOpen();//启动土壤湿度 @@ -208,34 +204,53 @@ public class AsDeviceServiceImpl extends ServiceImpl i String pulseModeSwitch = "true".equals(asDevice.getPulseMode()) ? "1" : "0";//脉冲模式开关 JSONObject pulseModeParamObj = JSON.parseObject(asDevice.getPulseModeParam()); - String sprayTime1 = pulseModeParamObj.getString("sprayTime");//喷洒时间 - String intervalTime = pulseModeParamObj.getString("intervalTime");//间隔时间 + //将‘0分3秒’这样的字符串转换成秒 + int sprayTime1 = convertStringToSeconds(pulseModeParamObj.getString("sprayTime"));//喷洒时间 + int intervalTime = convertStringToSeconds(pulseModeParamObj.getString("intervalTime"));//间隔时间 Integer waterIntensity = asDevice.getWaterIntensity(); - String min = asDevice.getScreenRestTime().replace("min", "");//息屏时间转换成分钟数 - Integer minNum = Integer.parseInt(min.replace("hour", "")) * 60; - String screenRestTime = min.contains("hour") ? minNum.toString() : min; + String min = asDevice.getScreenRestTime().replace("min", "");//息屏时间转换成秒 + Integer minNum = Integer.parseInt(min) * 60; + Integer hourNum = Integer.parseInt(min.replace("hour", "")) * 60 * 60; + String screenRestTime = min.contains("hour") ? hourNum.toString() : minNum.toString(); String screenRestTimeSwitch = "99".equals(asDevice.getScreenRestTime()) ? "0" : "1";//息屏开关 - StringBuilder command = new StringBuilder() - .append("dsjs_sec").append(sprayTime).append(IotConstants.COMMAND_SEPARATOR) - .append("dskaiguan").append(regularWateringSwitch).append(IotConstants.COMMAND_SEPARATOR) - .append("dsstart_hour").append(startHour).append(IotConstants.COMMAND_SEPARATOR) - .append("dsstart_min").append(startMin).append(IotConstants.COMMAND_SEPARATOR) - .append("jiaoshui_qiangdu").append(waterIntensity).append(IotConstants.COMMAND_SEPARATOR) + .append("qiangdu").append(waterIntensity).append(IotConstants.COMMAND_SEPARATOR) .append("mckaiguan").append(pulseModeSwitch).append(IotConstants.COMMAND_SEPARATOR) - .append("mcjg_sec").append(intervalTime).append(IotConstants.COMMAND_SEPARATOR) - .append("mcjs_sec").append(sprayTime1).append(IotConstants.COMMAND_SEPARATOR) + .append("jg_sec").append(intervalTime).append(IotConstants.COMMAND_SEPARATOR) + .append("js_sec").append(sprayTime1).append(IotConstants.COMMAND_SEPARATOR) .append("setkaiguan").append(screenRestTimeSwitch).append(IotConstants.COMMAND_SEPARATOR) - .append("setxp_min").append(screenRestTime).append(IotConstants.COMMAND_SEPARATOR) + .append("xp_min").append(screenRestTime).append(IotConstants.COMMAND_SEPARATOR) .append("trkaiguan").append(soilMoistureSwitch).append(IotConstants.COMMAND_SEPARATOR) - .append("trstart_sd").append(soilMoistureOpen).append(IotConstants.COMMAND_SEPARATOR) - .append("trend_sd").append(soilMoistureClose).append(IotConstants.COMMAND_SEPARATOR); - + .append("start_sd").append(soilMoistureOpen).append(IotConstants.COMMAND_SEPARATOR) + .append("end_sd").append(soilMoistureClose).append(IotConstants.COMMAND_SEPARATOR); return command.toString(); } + public int convertStringToSeconds(String timeString) { + // 定义正则表达式,用于匹配分钟和秒数 + String regex = "(\\d+)分(\\d+)秒"; + + // 使用 Pattern 和 Matcher 对象进行匹配 + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(timeString); + + // 确保字符串匹配预期格式 + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid time string format. Expected format: '秒'"); + } + + // 提取分钟和秒数 + int minutes = Integer.parseInt(matcher.group(1)); + int seconds = Integer.parseInt(matcher.group(2)); + + // 计算总秒数 + int totalSeconds = minutes * 60 + seconds; + + return totalSeconds; + } + /** * 批量删除设备列表 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 b471ab9..9f1d383 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 @@ -329,7 +329,7 @@ public class AsTimerServiceImpl implements IAsTimerService * @return */ @NotNull - private List cleanInvalidDs(List dsArray) { + public List cleanInvalidDs(List dsArray) { List filteredDsList = dsArray.stream() .filter(ds -> ds.getSw()) .collect(Collectors.toList()); 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 01db596..f5e2c19 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 @@ -21,11 +21,12 @@ import com.ruoyi.common.core.domain.entity.AsUser; import com.ruoyi.device.domain.AsWateringRecord; import com.ruoyi.device.mapper.AsDeviceMapper; import com.ruoyi.device.mapper.AsUserMapper; +import com.ruoyi.device.service.IAsDeviceService; +import com.ruoyi.device.service.IAsTimerService; import com.ruoyi.device.service.IAsUserService; import com.ruoyi.device.service.IAsWateringRecordService; import com.ruoyi.system.service.ISysConfigService; import lombok.SneakyThrows; -import org.apache.commons.lang3.ObjectUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -37,10 +38,7 @@ import javax.annotation.Resource; import javax.validation.Validator; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.List; +import java.util.*; /** * 用户 业务层处理 @@ -61,6 +59,12 @@ public class AsUserServiceImpl implements IAsUserService @Resource private AsDeviceMapper asDeviceMapper; + @Resource + private IAsDeviceService asDeviceService; + + @Resource + private IAsTimerService asTimerService; + @Resource private IAsWateringRecordService asWateringRecordService; @@ -437,10 +441,10 @@ public class AsUserServiceImpl implements IAsUserService JSONObject paramsObj = JSON.parseObject(result); String code = paramsObj.getString("code"); if (!HttpStatus.IOT_SUCCESS.equals(code)) { - asDevice1.setOnlineStatus("不在线"); + asDevice1.setOnlineStatus("0");//在线状态:0-不在线;1-在线 return asDevices; }else{ - asDevice1.setOnlineStatus("在线"); + asDevice1.setOnlineStatus("1"); // 查询onenet设备参数 String datapointsUrl = iotUrl+ IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param; log.info("IOT获取到Authorization:【{}】", token); @@ -462,12 +466,32 @@ public class AsUserServiceImpl implements IAsUserService DatapointValue.nextDs next_ds = value.getNext_ds(); /**设置下次浇水时间日期*/ setNextDs(asDevice1, next_ds); + /**设置浇水模式*/ + List modeList = new ArrayList<>(); + Boolean sw = next_ds.getSw(); + List dsArray = value.getDsArray(); + //清除无效定时器 + List ds = asTimerService.cleanInvalidDs(dsArray); + if(sw && ds.size()>0){//如果定时器开启且定时器列表不为空 + modeList.add("1"); + } + Boolean kaiguan = tr.getKaiguan(); + if(kaiguan){//土壤模式开关 + modeList.add("2"); + } + asDevice1.setMode(modeList); + asDevice1.setModeStr(JSON.toJSONString(modeList)); asDevice1.setCurrentSoilMoisture(value.getTuran_show()); List asWateringRecords = asWateringRecordService.selectAsWateringRecordList(AsWateringRecord.builder().deviceId(asDevice1.getDeviceId()).build()); // 根据asWateringRecords查询最后一次浇水时间 if(asWateringRecords.size()>0){ asDevice1.setLastWatering(asWateringRecords.get(0).getWaterTime()); } + /**将value转成device对象并保存到数据库*/ + int i = asDeviceMapper.updateAsDevice(asDevice1); + if(i>0){ + log.info("保存设备参数成功:【{}】", JSON.toJSONString(asDevice1)); + } } } } diff --git a/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml b/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml index 613fc77..e3da874 100644 --- a/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml +++ b/AutoSprout-watering/src/main/resources/mapper/device/AsDeviceMapper.xml @@ -36,10 +36,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + - select device_id, picture, device_name, classify_id, classify_name, model_id, model, mac, activation_time, online_status, user_id, user_name, nick_name, regular_watering, soil_moisture_open, soil_moisture_close, water_intensity, pulse_mode, pulse_mode_param, screen_rest_time, version, version_id, create_by, create_time, update_by, update_time, remark, regular_watering_json, soil_moisture, water_intensity_switch,is_network from as_device + select device_id, picture, device_name, classify_id, classify_name, model_id, model, mac, activation_time, online_status, user_id, user_name, nick_name, regular_watering, soil_moisture_open, soil_moisture_close, water_intensity, pulse_mode, pulse_mode_param, screen_rest_time, version, version_id, create_by, create_time, update_by, update_time, remark, regular_watering_json, soil_moisture, water_intensity_switch,is_network,mode_str from as_device