用能空调代码提交 终端相关代码修改
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.njcn.energy.handler;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
||||
import com.github.tocrhz.mqtt.annotation.MqttSubscribe;
|
||||
import com.github.tocrhz.mqtt.annotation.NamedValue;
|
||||
@@ -15,11 +16,15 @@ import com.njcn.energy.pojo.po.EleLogs;
|
||||
import com.njcn.energy.service.IEleLogsService;
|
||||
import com.njcn.energy.service.IModelService;
|
||||
import com.njcn.redis.utils.RedisUtil;
|
||||
import io.swagger.models.auth.In;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.json.JSON;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -37,7 +42,7 @@ import java.util.concurrent.TimeUnit;
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class MqttMessageHandler {
|
||||
|
||||
private final MqttPublisher publisher;
|
||||
@@ -48,6 +53,18 @@ public class MqttMessageHandler {
|
||||
|
||||
private final IEleLogsService eleLogsService;
|
||||
|
||||
@Value("${mqtt.client-id}")
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* 用于记录错误指令发送次数,默认发送三次停止
|
||||
* @author cdf
|
||||
* @date 2022/6/28
|
||||
*/
|
||||
private Integer mqttSendCount = 0;
|
||||
private String mqttJsonMsg;
|
||||
private String mqttSendTopic;
|
||||
|
||||
// /**
|
||||
// * 设备注册
|
||||
// */
|
||||
@@ -179,11 +196,8 @@ public class MqttMessageHandler {
|
||||
// }
|
||||
|
||||
|
||||
@MqttSubscribe(value = "/platform/devack/#",qos = 1)
|
||||
public void airOperation(String topic, MqttMessage message, @Payload String payload){
|
||||
System.out.println(message.toString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
@@ -13,12 +14,15 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.github.tocrhz.mqtt.annotation.MqttSubscribe;
|
||||
import com.github.tocrhz.mqtt.annotation.Payload;
|
||||
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
|
||||
import com.njcn.common.pojo.enums.common.DataStateEnum;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.db.constant.DbConstant;
|
||||
import com.njcn.energy.handler.MqttMessageHandler;
|
||||
import com.njcn.energy.mapper.AirSetMapper;
|
||||
import com.njcn.energy.mapper.AirStrategyMapper;
|
||||
import com.njcn.energy.mapper.LogicAccessMapper;
|
||||
@@ -43,7 +47,9 @@ import com.xxl.job.core.biz.model.ReturnT;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -74,6 +80,13 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
|
||||
private final AirStrategyMapper airStrategyMapper;
|
||||
|
||||
private final MqttMessageHandler mqttMessageHandler;
|
||||
|
||||
@Value("${mqtt.client-id}")
|
||||
private String clientId;
|
||||
|
||||
private Integer xxlGroup = 5;
|
||||
|
||||
|
||||
@Override
|
||||
public Page<AirStrategy> listAirStrategy(AirStrategyParam.AirStrategyQueryParam queryParam) {
|
||||
@@ -148,7 +161,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
*/
|
||||
XxlJobInfo xxlJobInfo = new XxlJobInfo();
|
||||
//执行器ID,手动指定,此处设置为3,对应xxl_job_group中的executor--测试执行器
|
||||
xxlJobInfo.setJobGroup(5);
|
||||
xxlJobInfo.setJobGroup(xxlGroup);
|
||||
//任务描述
|
||||
xxlJobInfo.setJobDesc("开启空调");
|
||||
xxlJobInfo.setAuthor("cdf");
|
||||
@@ -175,7 +188,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
*/
|
||||
XxlJobInfo xxlJobInfoEnd = new XxlJobInfo();
|
||||
//执行器ID,手动指定,此处设置为3,对应xxl_job_group中的executor--测试执行器
|
||||
xxlJobInfoEnd.setJobGroup(5);
|
||||
xxlJobInfoEnd.setJobGroup(xxlGroup);
|
||||
//任务描述
|
||||
xxlJobInfoEnd.setJobDesc("关闭空调");
|
||||
xxlJobInfoEnd.setAuthor("cdf");
|
||||
@@ -236,7 +249,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
XxlJobInfo xxlJobInfo = new XxlJobInfo();
|
||||
//执行器ID,手动指定,此处设置为3,对应xxl_job_group中的executor--测试执行器
|
||||
xxlJobInfo.setId(Integer.parseInt(jobIds[0]));
|
||||
xxlJobInfo.setJobGroup(5);
|
||||
xxlJobInfo.setJobGroup(xxlGroup);
|
||||
//任务描述
|
||||
xxlJobInfo.setJobDesc("开启空调");
|
||||
xxlJobInfo.setAuthor("cdf");
|
||||
@@ -264,7 +277,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
XxlJobInfo xxlJobInfoEnd = new XxlJobInfo();
|
||||
xxlJobInfoEnd.setId(Integer.parseInt(jobIds[1]));
|
||||
//执行器ID,手动指定,此处设置为3,对应xxl_job_group中的executor--测试执行器
|
||||
xxlJobInfoEnd.setJobGroup(5);
|
||||
xxlJobInfoEnd.setJobGroup(xxlGroup);
|
||||
//任务描述
|
||||
xxlJobInfoEnd.setJobDesc("关闭空调");
|
||||
xxlJobInfoEnd.setAuthor("cdf");
|
||||
@@ -418,6 +431,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
AirStrategyDTO airStrategyDTO = new AirStrategyDTO();
|
||||
airStrategyDTO.setMid(1);
|
||||
airStrategyDTO.setDeviceId(netAndDevByLineDTO.getDevId());
|
||||
airStrategyDTO.setUserId(clientId);
|
||||
airStrategyDTO.setTimestamp(Calendar.getInstance().getTimeInMillis());
|
||||
airStrategyDTO.setExpire(-1);
|
||||
airStrategyDTO.setType("CMD_DEV_CTRL");
|
||||
@@ -430,12 +444,24 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
System.out.println(json);
|
||||
String topic = "/platform/devcmd/v1/" + netAndDevByLineDTO.getNdid();
|
||||
publisher.send(topic, json, 1, false);
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
System.out.println("睡了10s");
|
||||
|
||||
|
||||
//mqttMessageHandler.setMqttParam(0,topic,json);
|
||||
this.mqttSendCount = 0;
|
||||
this.mqttJsonMsg = json;
|
||||
this.mqttSendTopic = topic;
|
||||
|
||||
flag = true;
|
||||
while(flag){
|
||||
|
||||
}
|
||||
/* try {
|
||||
Thread.sleep(2000);
|
||||
System.out.println("切换命令睡了1s");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
*/
|
||||
}
|
||||
} else {
|
||||
//关闭空调
|
||||
@@ -443,6 +469,7 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
AirStrategyDTO airStrategyDTO = new AirStrategyDTO();
|
||||
airStrategyDTO.setMid(1);
|
||||
airStrategyDTO.setDeviceId(netAndDevByLineDTO.getDevId());
|
||||
airStrategyDTO.setUserId(clientId);
|
||||
airStrategyDTO.setTimestamp(Calendar.getInstance().getTimeInMillis());
|
||||
airStrategyDTO.setExpire(-1);
|
||||
airStrategyDTO.setType("CMD_DEV_CTRL");
|
||||
@@ -455,12 +482,22 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
System.out.println(json);
|
||||
String topic = "/platform/devcmd/v1/" + netAndDevByLineDTO.getNdid();
|
||||
publisher.send(topic, json, 1, false);
|
||||
try {
|
||||
Thread.sleep(10000);
|
||||
System.out.println("切换监测点睡了10s");
|
||||
this.mqttSendCount = 0;
|
||||
this.mqttJsonMsg = json;
|
||||
this.mqttSendTopic = topic;
|
||||
flag = true;
|
||||
while(flag){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*try {
|
||||
Thread.sleep(2000);
|
||||
System.out.println("切换监测点睡了1s");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
@@ -485,6 +522,37 @@ public class EleAirStrategyServiceImpl extends ServiceImpl<AirStrategyMapper, Ai
|
||||
}
|
||||
|
||||
|
||||
private Boolean flag = false;
|
||||
private Integer mqttSendCount = 0;
|
||||
private String mqttJsonMsg;
|
||||
private String mqttSendTopic;
|
||||
@MqttSubscribe(value = "/platform/devack/#",qos = 1)
|
||||
public void airOperation(String topic, MqttMessage message, @Payload String payload){
|
||||
System.out.println(message.toString());
|
||||
JSONObject jsonObject = new JSONObject(message.toString());
|
||||
String str = jsonObject.getStr("userId");
|
||||
|
||||
if(str.equals(clientId)){
|
||||
|
||||
if(!jsonObject.getStr("code").equals("200")){
|
||||
mqttSendCount++;
|
||||
if(mqttSendCount < 3){
|
||||
System.out.println("进入错误重发++++++");
|
||||
System.out.println("错误重发详情"+mqttJsonMsg);
|
||||
publisher.send(mqttSendTopic, mqttJsonMsg, 1, false);
|
||||
}else{
|
||||
flag = false;
|
||||
}
|
||||
}else{
|
||||
flag = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 校验参数,检查是否存在相同名称的空调策略
|
||||
*/
|
||||
|
||||
@@ -47,8 +47,8 @@ mybatis-plus:
|
||||
#别名扫描
|
||||
type-aliases-package: com.njcn.energy.pojo
|
||||
|
||||
#mqtt:
|
||||
# uri: tcp://192.168.1.14:1883
|
||||
# client-id: energy_online
|
||||
# username: t_user
|
||||
# password: njcnpqs
|
||||
mqtt:
|
||||
uri: tcp://192.168.1.14:1883
|
||||
client-id: energy_cdf
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
Reference in New Issue
Block a user