1. 浇花器命令调整

This commit is contained in:
邱贞招 2024-04-10 16:09:43 +08:00
parent de808f9c16
commit b8d0c9acb9
8 changed files with 95 additions and 34 deletions

View File

@ -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;//日期
}

View File

@ -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));
}

View File

@ -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<String> mode;
/** 浇水模式1-定时2-智能土壤湿度3-手动 */
@Excel(name = "浇水模式")
private String modeStr;
}

View File

@ -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<DatapointValue.Ds> cleanInvalidDs(List<DatapointValue.Ds> dsArray);
}

View File

@ -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<AsDeviceMapper, AsDevice> 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<AsDeviceMapper, AsDevice> 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: '<minutes>分<seconds>秒'");
}
// 提取分钟和秒数
int minutes = Integer.parseInt(matcher.group(1));
int seconds = Integer.parseInt(matcher.group(2));
// 计算总秒数
int totalSeconds = minutes * 60 + seconds;
return totalSeconds;
}
/**
* 批量删除设备列表

View File

@ -329,7 +329,7 @@ public class AsTimerServiceImpl implements IAsTimerService
* @return
*/
@NotNull
private List<DatapointValue.Ds> cleanInvalidDs(List<DatapointValue.Ds> dsArray) {
public List<DatapointValue.Ds> cleanInvalidDs(List<DatapointValue.Ds> dsArray) {
List<DatapointValue.Ds> filteredDsList = dsArray.stream()
.filter(ds -> ds.getSw())
.collect(Collectors.toList());

View File

@ -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<String> modeList = new ArrayList<>();
Boolean sw = next_ds.getSw();
List<DatapointValue.Ds> dsArray = value.getDsArray();
//清除无效定时器
List<DatapointValue.Ds> 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<AsWateringRecord> 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));
}
}
}
}

View File

@ -36,10 +36,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="soilMoisture" column="soil_moisture" />
<result property="waterIntensitySwitch" column="water_intensity_switch" />
<result property="isNetwork" column="is_network" />
<result property="modeStr" column="mode_str" />
</resultMap>
<sql id="selectAsDeviceVo">
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
</sql>
<select id="selectAsDeviceList" parameterType="AsDevice" resultMap="AsDeviceResult">
@ -171,7 +172,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="soilMoisture != null">soil_moisture = #{soilMoisture},</if>
<if test="waterIntensitySwitch != null">water_intensity_switch = #{waterIntensitySwitch},</if>
<if test="status != null">status = #{status},</if>
<if test="isNetwork != null">is_network = #{isNetwork}</if>
<if test="isNetwork != null">is_network = #{isNetwork},</if>
<if test="modeStr != null">mode_str = #{modeStr}</if>
</trim>
where device_id = #{deviceId}
</update>