作者 钟来

设备控制功能优化

正在显示 16 个修改的文件 包含 303 行增加16 行删除
@@ -29,7 +29,7 @@ public class StringModelOutput extends ThingsModelItemBase<String> @@ -29,7 +29,7 @@ public class StringModelOutput extends ThingsModelItemBase<String>
29 29
30 @Override 30 @Override
31 public boolean checkValue() { 31 public boolean checkValue() {
32 - if(null != getValue() && getValue().length()<maxLength) 32 + if(null != getValue() && getValue().length()<=maxLength)
33 { 33 {
34 return true; 34 return true;
35 } 35 }
@@ -121,7 +121,7 @@ public class ControlDeviceConreoller extends BaseController { @@ -121,7 +121,7 @@ public class ControlDeviceConreoller extends BaseController {
121 String body = new String(bodyBytes, request.getCharacterEncoding()); 121 String body = new String(bodyBytes, request.getCharacterEncoding());
122 DeviceCommand deviceCommand = new DeviceCommand(); 122 DeviceCommand deviceCommand = new DeviceCommand();
123 deviceCommand.setDeviceId(deviceId); 123 deviceCommand.setDeviceId(deviceId);
124 - deviceCommand.setCommandType(CommandType.write); 124 + deviceCommand.setCommandType(CommandType.read);
125 deviceCommand.setData(GsonConstructor.get().fromJson(body,JsonObject.class)); 125 deviceCommand.setData(GsonConstructor.get().fromJson(body,JsonObject.class));
126 return deviceControl(deviceCommand); 126 return deviceControl(deviceCommand);
127 } 127 }
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <modelVersion>4.0.0</modelVersion>
  6 + <parent>
  7 + <groupId>com.zhonglai.luhui</groupId>
  8 + <artifactId>lh-device-protocol-agreement</artifactId>
  9 + <version>1.0-SNAPSHOT</version>
  10 + </parent>
  11 +
  12 + <artifactId>lh-device-6wp</artifactId>
  13 +
  14 + <properties>
  15 + <maven.compiler.source>8</maven.compiler.source>
  16 + <maven.compiler.target>8</maven.compiler.target>
  17 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18 + </properties>
  19 +
  20 + <dependencies>
  21 + <dependency>
  22 + <groupId>com.zhonglai.luhui</groupId>
  23 + <artifactId>lh-device-protocol-factory</artifactId>
  24 + </dependency>
  25 + </dependencies>
  26 +</project>
  1 +package com.zhonglai.luhui.device.protocol.wp6.analysis;
  2 +
  3 +import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
  4 +import com.zhonglai.luhui.device.analysis.util.TopicUtil;
  5 +import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory;
  6 +import com.zhonglai.luhui.device.protocol.factory.analysis.topic.Online;
  7 +import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
  8 +import com.zhonglai.luhui.device.protocol.wp6.analysis.topic.*;
  9 +
  10 +public class WP6ProtocolParserFactoryImpl implements ProtocolParserFactory {
  11 + private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}";
  12 +
  13 + @Override
  14 + public Topic analysisTopic(String topicStr) {
  15 + Topic topic = TopicUtil.initTopicFromModelStr(topicStr,topicModel);
  16 + return topic;
  17 + }
  18 +
  19 + @Override
  20 + public AnalysisResult analysisPayload(Topic topic, byte[] payload) {
  21 + switch (topic.getTopicType())
  22 + {
  23 + case "online":
  24 + return new Online().analysisPayload(payload);
  25 + case "ONLINE":
  26 + return new Online().analysisPayload(payload);
  27 + case "ALL_POST":
  28 + topic.setAllup(true);
  29 + return new AllPost().analysisPayload(payload);
  30 + case "ADD_POST":
  31 + return new AddPost().analysisPayload(payload);
  32 + case "GET":
  33 + return new Get().analysisPayload(payload);
  34 + case "PUT_REQ":
  35 + return new PutReq().analysisPayload(payload);
  36 + case "READ_REQ":
  37 + return new ReadReq().analysisPayload(payload);
  38 + default:
  39 + return new AnalysisResult(false,false,null);
  40 + }
  41 +
  42 + }
  43 +
  44 +}
  1 +package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
  2 +
  3 +import com.google.gson.JsonObject;
  4 +import com.ruoyi.common.utils.GsonConstructor;
  5 +import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
  6 +
  7 +public class AddPost {
  8 + public AnalysisResult analysisPayload( byte[] payload)
  9 + {
  10 + return analysisPayload(new String(payload));
  11 + }
  12 +
  13 + public AnalysisResult analysisPayload(String payload)
  14 + {
  15 + JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
  16 + return new AnalysisResult(false,false,jsonObject);
  17 + }
  18 +}
  1 +package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
  2 +
  3 +import com.google.gson.JsonObject;
  4 +import com.ruoyi.common.utils.GsonConstructor;
  5 +import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
  6 +
  7 +public class AllPost {
  8 + public AnalysisResult analysisPayload( byte[] payload)
  9 + {
  10 + return analysisPayload(new String(payload));
  11 + }
  12 +
  13 + public AnalysisResult analysisPayload(String payload)
  14 + {
  15 + JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
  16 + return new AnalysisResult(true,false,jsonObject);
  17 + }
  18 +}
  1 +package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
  2 +
  3 +import com.google.gson.JsonObject;
  4 +import com.ruoyi.common.utils.GsonConstructor;
  5 +import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
  6 +
  7 +public class Get {
  8 + public AnalysisResult analysisPayload( byte[] payload)
  9 + {
  10 + return analysisPayload(new String(payload));
  11 + }
  12 +
  13 + public AnalysisResult analysisPayload( String payload)
  14 + {
  15 + JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
  16 + return new AnalysisResult(false,false,jsonObject);
  17 + }
  18 +}
  1 +package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
  2 +
  3 +import com.google.gson.JsonObject;
  4 +import com.ruoyi.common.utils.GsonConstructor;
  5 +import com.zhonglai.luhui.device.analysis.dto.MessageCode;
  6 +import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
  7 +
  8 +public class PutReq {
  9 + public AnalysisResult analysisPayload(byte[] payload)
  10 + {
  11 + return analysisPayload(new String(payload));
  12 + }
  13 +
  14 + public AnalysisResult analysisPayload(String payload)
  15 + {
  16 + JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
  17 + return new AnalysisResult(false, true, jsonObject, message -> {
  18 + if(jsonObject.has("code") && "1".equals(jsonObject.get("code").getAsString()))
  19 + {
  20 + message.setCode(MessageCode.DEFAULT_SUCCESS_CODE);
  21 + message.setMessage("操作成功");
  22 + }
  23 + });
  24 + }
  25 +}
  1 +package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
  2 +
  3 +import com.google.gson.JsonObject;
  4 +import com.ruoyi.common.utils.GsonConstructor;
  5 +import com.zhonglai.luhui.device.analysis.dto.MessageCode;
  6 +import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
  7 +
  8 +import java.util.HashMap;
  9 +
  10 +public class ReadReq {
  11 + public AnalysisResult analysisPayload(byte[] payload)
  12 + {
  13 + return analysisPayload(new String(payload));
  14 + }
  15 +
  16 + public AnalysisResult analysisPayload(String payload)
  17 + {
  18 + JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
  19 + JsonObject rJsonObject = null;
  20 + if(jsonObject.has("code") && "1".equals(jsonObject.get("code").getAsString()) && jsonObject.has("data") && null != jsonObject.get("data")&& jsonObject.get("data").isJsonObject())
  21 + {
  22 + rJsonObject = jsonObject.getAsJsonObject("data");
  23 + }
  24 + return new AnalysisResult(false, true, rJsonObject, message -> {
  25 + if(jsonObject.has("code") && "1".equals(jsonObject.get("code").getAsString()))
  26 + {
  27 + if(jsonObject.has("data") && null != jsonObject.get("data") && jsonObject.get("data").isJsonObject())
  28 + {
  29 + message.setData(GsonConstructor.get().fromJson(jsonObject.getAsJsonObject("data").toString(), HashMap.class));
  30 + }
  31 + message.setCode(MessageCode.DEFAULT_SUCCESS_CODE);
  32 + message.setMessage("操作成功");
  33 + }
  34 + });
  35 + }
  36 +}
  1 +package com.zhonglai.luhui.device.protocol.wp6.control;
  2 +
  3 +import com.google.gson.JsonObject;
  4 +import com.ruoyi.common.utils.DateUtils;
  5 +import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
  6 +import com.zhonglai.luhui.device.domain.IotProduct;
  7 +import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach;
  8 +import com.zhonglai.luhui.device.protocol.factory.control.DeviceCommandServiceFactory;
  9 +import com.zhonglai.luhui.device.protocol.factory.dto.NoticeMessageDto;
  10 +import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
  11 +import org.slf4j.Logger;
  12 +import org.slf4j.LoggerFactory;
  13 +
  14 +/**
  15 + * 指令控制监听实现
  16 + */
  17 +public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFactory {
  18 + private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}";
  19 +
  20 + private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class);
  21 + @Override
  22 + public NoticeMessageDto read(String deviceId, JsonObject jsonObject) {
  23 + ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
  24 + Topic topic = getReadTopic(deviceId,parserDeviceHostDto.getIotProduct());
  25 + NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
  26 + noticeMessageDto.setTopic(topic);
  27 + noticeMessageDto.setTopicconfig(topicModel);
  28 +
  29 + noticeMessageDto.setCommd(jsonObject.toString().getBytes());
  30 + return noticeMessageDto;
  31 + }
  32 +
  33 + @Override
  34 + public NoticeMessageDto write(String deviceId, JsonObject jsonObject) {
  35 + ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
  36 +
  37 + Topic topic = getWriteTopic(deviceId,parserDeviceHostDto.getIotProduct());
  38 + NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
  39 + noticeMessageDto.setTopic(topic);
  40 + noticeMessageDto.setTopicconfig(topicModel);
  41 +
  42 + noticeMessageDto.setCommd(jsonObject.toString().getBytes());
  43 +
  44 + return noticeMessageDto;
  45 + }
  46 +
  47 + @Override
  48 + public NoticeMessageDto notice(String deviceId, JsonObject jsonObject) {
  49 + return null;
  50 + }
  51 +
  52 + private Topic getWriteTopic(String deviceId, IotProduct iotProduct)
  53 + {
  54 + Topic topic = new Topic();
  55 + topic.setTopicType("PUT");
  56 + topic.setClientid(deviceId);
  57 + topic.setRoleid(iotProduct.getRole_id()+"");
  58 + topic.setUsername(iotProduct.getMqtt_username());
  59 + topic.setPayloadtype("Json");
  60 + topic.setMessageid(DateUtils.getNowTimeMilly()+"");
  61 + return topic;
  62 + }
  63 +
  64 + private Topic getReadTopic(String deviceId, IotProduct iotProduct)
  65 + {
  66 + Topic topic = new Topic();
  67 + topic.setTopicType("READ");
  68 + topic.setClientid(deviceId);
  69 + topic.setRoleid(iotProduct.getRole_id()+"");
  70 + topic.setUsername(iotProduct.getMqtt_username());
  71 + topic.setPayloadtype("Json");
  72 + topic.setMessageid(DateUtils.getNowTimeMilly()+"");
  73 + return topic;
  74 + }
  75 +}
@@ -26,7 +26,14 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto @@ -26,7 +26,14 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
26 private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class); 26 private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class);
27 @Override 27 @Override
28 public NoticeMessageDto read(String deviceId, JsonObject jsonObject) { 28 public NoticeMessageDto read(String deviceId, JsonObject jsonObject) {
29 - return null; 29 + ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
  30 + Topic topic = getReadTopic(deviceId,parserDeviceHostDto.getIotProduct());
  31 + NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
  32 + noticeMessageDto.setTopic(topic);
  33 + noticeMessageDto.setTopicconfig(topicModel);
  34 +
  35 + noticeMessageDto.setCommd(jsonObject.toString().getBytes());
  36 + return noticeMessageDto;
30 } 37 }
31 38
32 @Override 39 @Override
@@ -59,4 +66,16 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto @@ -59,4 +66,16 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
59 topic.setMessageid(DateUtils.getNowTimeMilly()+""); 66 topic.setMessageid(DateUtils.getNowTimeMilly()+"");
60 return topic; 67 return topic;
61 } 68 }
  69 +
  70 + private Topic getReadTopic(String deviceId, IotProduct iotProduct)
  71 + {
  72 + Topic topic = new Topic();
  73 + topic.setTopicType("READ");
  74 + topic.setClientid(deviceId);
  75 + topic.setRoleid(iotProduct.getRole_id()+"");
  76 + topic.setUsername(iotProduct.getMqtt_username());
  77 + topic.setPayloadtype("Json");
  78 + topic.setMessageid(DateUtils.getNowTimeMilly()+"");
  79 + return topic;
  80 + }
62 } 81 }
@@ -23,5 +23,6 @@ @@ -23,5 +23,6 @@
23 <module>lh-device-plc004</module> 23 <module>lh-device-plc004</module>
24 <module>lh-device-modbus</module> 24 <module>lh-device-modbus</module>
25 <module>lh-device-defaul</module> 25 <module>lh-device-defaul</module>
  26 + <module>lh-device-6wp</module>
26 </modules> 27 </modules>
27 </project> 28 </project>
@@ -165,7 +165,7 @@ public class DeviceCommandListenService implements RocketMQReplyListener<Message @@ -165,7 +165,7 @@ public class DeviceCommandListenService implements RocketMQReplyListener<Message
165 165
166 //设置通知渠道 166 //设置通知渠道
167 ClienConnectionImpl clienConnection = new ClienConnectionImpl(); 167 ClienConnectionImpl clienConnection = new ClienConnectionImpl();
168 - log.info("设置通知渠道 {} {}",topic.getClientid(),clienConnection); 168 + log.info("设置通知渠道 {} ",topic.getClientid());
169 clienConnection.setCommd(noticeMessageDomain.getCommd()); 169 clienConnection.setCommd(noticeMessageDomain.getCommd());
170 clienConnectionMap.put(topic.getClientid(),clienConnection); 170 clienConnectionMap.put(topic.getClientid(),clienConnection);
171 clienNoticeServiceFactory.sendMessage(noticeMessageDomain); 171 clienNoticeServiceFactory.sendMessage(noticeMessageDomain);
@@ -245,19 +245,22 @@ public class IotThingsModelService { @@ -245,19 +245,22 @@ public class IotThingsModelService {
245 { 245 {
246 for(String vkey:vjsonObject.keySet()) 246 for(String vkey:vjsonObject.keySet())
247 { 247 {
248 - JSONObject jsData = vjsonObject.getJSONObject(vkey);  
249 - for(String key:jsData.keySet()) 248 + if(GsonConstructor.get().fromJson(vjsonObject.get(vkey).toString(),JsonElement.class).isJsonObject())
250 { 249 {
251 - IotThingsModel thingsModel = getThingsModelBase(product_id,key);  
252 - String data_type = thingsModel.getData_type().toUpperCase();  
253 - if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type)) 250 + JSONObject jsData = vjsonObject.getJSONObject(vkey);
  251 + for(String key:jsData.keySet())
254 { 252 {
255 - data_type = ThingsModelDataTypeEnum.STRING.name(); 253 + IotThingsModel thingsModel = getThingsModelBase(product_id,key);
  254 + String data_type = thingsModel.getData_type().toUpperCase();
  255 + if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type))
  256 + {
  257 + data_type = ThingsModelDataTypeEnum.STRING.name();
  258 + }
  259 + ThingsModelItemBase thingsModelBase = ThingsModelItemBase.newhingsModelReverse(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
  260 + jsData.put(key,thingsModelBase.getValue());
256 } 261 }
257 - ThingsModelItemBase thingsModelBase = ThingsModelItemBase.newhingsModelReverse(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));  
258 - jsData.put(key,thingsModelBase.getValue()); 262 + vjsonObject.put(vkey,jsData);
259 } 263 }
260 - vjsonObject.put(vkey,jsData);  
261 } 264 }
262 return vjsonObject; 265 return vjsonObject;
263 } 266 }
@@ -92,6 +92,10 @@ public class AllPostTopic implements BusinessAgreement<AllPostDto> { @@ -92,6 +92,10 @@ public class AllPostTopic implements BusinessAgreement<AllPostDto> {
92 if(!newDataValue.equals(odlDataValue)) 92 if(!newDataValue.equals(odlDataValue))
93 { 93 {
94 jsonObject.put(deviceDataConfig.getPlc_data_type().sensorDataType,newDataValue); //更新数据 94 jsonObject.put(deviceDataConfig.getPlc_data_type().sensorDataType,newDataValue); //更新数据
  95 + if(deviceDataConfig.getPlc_data_type().sensorDataType.equals("104"))
  96 + {
  97 + jsonObject.put("9",newDataValue); //更新数据
  98 + }
95 deviceInfo.setDataValue(jsonObject.toJSONString()); 99 deviceInfo.setDataValue(jsonObject.toJSONString());
96 100
97 String describe = deviceDataConfig.getAttribute_describe()+newDataValue; 101 String describe = deviceDataConfig.getAttribute_describe()+newDataValue;
@@ -19,8 +19,8 @@ spring: @@ -19,8 +19,8 @@ spring:
19 19
20 mqtt: 20 mqtt:
21 #链接地址 21 #链接地址
22 -# broker: tcp://175.24.61.68:1883  
23 - broker: tcp://8.129.82.37:1883 22 + broker: tcp://175.24.61.68:1883
  23 +# broker: tcp://8.129.82.37:1883
24 #唯一标识 24 #唯一标识
25 clientId: ${random.uuid} 25 clientId: ${random.uuid}
26 topicconfig: "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}" 26 topicconfig: "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}"
@@ -29,7 +29,7 @@ mqtt: @@ -29,7 +29,7 @@ mqtt:
29 client: 29 client:
30 #客户端操作时间 30 #客户端操作时间
31 operationTime: 10 31 operationTime: 10
32 - productids: 35 32 + productids: 9
33 33
34 #rocketmq配置信息 34 #rocketmq配置信息
35 rocketmq: 35 rocketmq: