作者 钟来

设备控制功能优化

正在显示 16 个修改的文件 包含 303 行增加16 行删除
... ... @@ -29,7 +29,7 @@ public class StringModelOutput extends ThingsModelItemBase<String>
@Override
public boolean checkValue() {
if(null != getValue() && getValue().length()<maxLength)
if(null != getValue() && getValue().length()<=maxLength)
{
return true;
}
... ...
... ... @@ -121,7 +121,7 @@ public class ControlDeviceConreoller extends BaseController {
String body = new String(bodyBytes, request.getCharacterEncoding());
DeviceCommand deviceCommand = new DeviceCommand();
deviceCommand.setDeviceId(deviceId);
deviceCommand.setCommandType(CommandType.write);
deviceCommand.setCommandType(CommandType.read);
deviceCommand.setData(GsonConstructor.get().fromJson(body,JsonObject.class));
return deviceControl(deviceCommand);
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zhonglai.luhui</groupId>
<artifactId>lh-device-protocol-agreement</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>lh-device-6wp</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.zhonglai.luhui</groupId>
<artifactId>lh-device-protocol-factory</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.zhonglai.luhui.device.protocol.wp6.analysis;
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
import com.zhonglai.luhui.device.analysis.util.TopicUtil;
import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory;
import com.zhonglai.luhui.device.protocol.factory.analysis.topic.Online;
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
import com.zhonglai.luhui.device.protocol.wp6.analysis.topic.*;
public class WP6ProtocolParserFactoryImpl implements ProtocolParserFactory {
private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}";
@Override
public Topic analysisTopic(String topicStr) {
Topic topic = TopicUtil.initTopicFromModelStr(topicStr,topicModel);
return topic;
}
@Override
public AnalysisResult analysisPayload(Topic topic, byte[] payload) {
switch (topic.getTopicType())
{
case "online":
return new Online().analysisPayload(payload);
case "ONLINE":
return new Online().analysisPayload(payload);
case "ALL_POST":
topic.setAllup(true);
return new AllPost().analysisPayload(payload);
case "ADD_POST":
return new AddPost().analysisPayload(payload);
case "GET":
return new Get().analysisPayload(payload);
case "PUT_REQ":
return new PutReq().analysisPayload(payload);
case "READ_REQ":
return new ReadReq().analysisPayload(payload);
default:
return new AnalysisResult(false,false,null);
}
}
}
... ...
package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
public class AddPost {
public AnalysisResult analysisPayload( byte[] payload)
{
return analysisPayload(new String(payload));
}
public AnalysisResult analysisPayload(String payload)
{
JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
return new AnalysisResult(false,false,jsonObject);
}
}
... ...
package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
public class AllPost {
public AnalysisResult analysisPayload( byte[] payload)
{
return analysisPayload(new String(payload));
}
public AnalysisResult analysisPayload(String payload)
{
JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
return new AnalysisResult(true,false,jsonObject);
}
}
... ...
package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
public class Get {
public AnalysisResult analysisPayload( byte[] payload)
{
return analysisPayload(new String(payload));
}
public AnalysisResult analysisPayload( String payload)
{
JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
return new AnalysisResult(false,false,jsonObject);
}
}
... ...
package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.analysis.dto.MessageCode;
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
public class PutReq {
public AnalysisResult analysisPayload(byte[] payload)
{
return analysisPayload(new String(payload));
}
public AnalysisResult analysisPayload(String payload)
{
JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
return new AnalysisResult(false, true, jsonObject, message -> {
if(jsonObject.has("code") && "1".equals(jsonObject.get("code").getAsString()))
{
message.setCode(MessageCode.DEFAULT_SUCCESS_CODE);
message.setMessage("操作成功");
}
});
}
}
... ...
package com.zhonglai.luhui.device.protocol.wp6.analysis.topic;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.analysis.dto.MessageCode;
import com.zhonglai.luhui.device.protocol.factory.dto.AnalysisResult;
import java.util.HashMap;
public class ReadReq {
public AnalysisResult analysisPayload(byte[] payload)
{
return analysisPayload(new String(payload));
}
public AnalysisResult analysisPayload(String payload)
{
JsonObject jsonObject = GsonConstructor.get().fromJson(payload, JsonObject.class);
JsonObject rJsonObject = null;
if(jsonObject.has("code") && "1".equals(jsonObject.get("code").getAsString()) && jsonObject.has("data") && null != jsonObject.get("data")&& jsonObject.get("data").isJsonObject())
{
rJsonObject = jsonObject.getAsJsonObject("data");
}
return new AnalysisResult(false, true, rJsonObject, message -> {
if(jsonObject.has("code") && "1".equals(jsonObject.get("code").getAsString()))
{
if(jsonObject.has("data") && null != jsonObject.get("data") && jsonObject.get("data").isJsonObject())
{
message.setData(GsonConstructor.get().fromJson(jsonObject.getAsJsonObject("data").toString(), HashMap.class));
}
message.setCode(MessageCode.DEFAULT_SUCCESS_CODE);
message.setMessage("操作成功");
}
});
}
}
... ...
package com.zhonglai.luhui.device.protocol.wp6.control;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.DateUtils;
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
import com.zhonglai.luhui.device.domain.IotProduct;
import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach;
import com.zhonglai.luhui.device.protocol.factory.control.DeviceCommandServiceFactory;
import com.zhonglai.luhui.device.protocol.factory.dto.NoticeMessageDto;
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 指令控制监听实现
*/
public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFactory {
private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}";
private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class);
@Override
public NoticeMessageDto read(String deviceId, JsonObject jsonObject) {
ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
Topic topic = getReadTopic(deviceId,parserDeviceHostDto.getIotProduct());
NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
noticeMessageDto.setTopic(topic);
noticeMessageDto.setTopicconfig(topicModel);
noticeMessageDto.setCommd(jsonObject.toString().getBytes());
return noticeMessageDto;
}
@Override
public NoticeMessageDto write(String deviceId, JsonObject jsonObject) {
ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
Topic topic = getWriteTopic(deviceId,parserDeviceHostDto.getIotProduct());
NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
noticeMessageDto.setTopic(topic);
noticeMessageDto.setTopicconfig(topicModel);
noticeMessageDto.setCommd(jsonObject.toString().getBytes());
return noticeMessageDto;
}
@Override
public NoticeMessageDto notice(String deviceId, JsonObject jsonObject) {
return null;
}
private Topic getWriteTopic(String deviceId, IotProduct iotProduct)
{
Topic topic = new Topic();
topic.setTopicType("PUT");
topic.setClientid(deviceId);
topic.setRoleid(iotProduct.getRole_id()+"");
topic.setUsername(iotProduct.getMqtt_username());
topic.setPayloadtype("Json");
topic.setMessageid(DateUtils.getNowTimeMilly()+"");
return topic;
}
private Topic getReadTopic(String deviceId, IotProduct iotProduct)
{
Topic topic = new Topic();
topic.setTopicType("READ");
topic.setClientid(deviceId);
topic.setRoleid(iotProduct.getRole_id()+"");
topic.setUsername(iotProduct.getMqtt_username());
topic.setPayloadtype("Json");
topic.setMessageid(DateUtils.getNowTimeMilly()+"");
return topic;
}
}
... ...
... ... @@ -26,7 +26,14 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class);
@Override
public NoticeMessageDto read(String deviceId, JsonObject jsonObject) {
return null;
ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
Topic topic = getReadTopic(deviceId,parserDeviceHostDto.getIotProduct());
NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
noticeMessageDto.setTopic(topic);
noticeMessageDto.setTopicconfig(topicModel);
noticeMessageDto.setCommd(jsonObject.toString().getBytes());
return noticeMessageDto;
}
@Override
... ... @@ -59,4 +66,16 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
topic.setMessageid(DateUtils.getNowTimeMilly()+"");
return topic;
}
private Topic getReadTopic(String deviceId, IotProduct iotProduct)
{
Topic topic = new Topic();
topic.setTopicType("READ");
topic.setClientid(deviceId);
topic.setRoleid(iotProduct.getRole_id()+"");
topic.setUsername(iotProduct.getMqtt_username());
topic.setPayloadtype("Json");
topic.setMessageid(DateUtils.getNowTimeMilly()+"");
return topic;
}
}
... ...
... ... @@ -23,5 +23,6 @@
<module>lh-device-plc004</module>
<module>lh-device-modbus</module>
<module>lh-device-defaul</module>
<module>lh-device-6wp</module>
</modules>
</project>
\ No newline at end of file
... ...
... ... @@ -165,7 +165,7 @@ public class DeviceCommandListenService implements RocketMQReplyListener<Message
//设置通知渠道
ClienConnectionImpl clienConnection = new ClienConnectionImpl();
log.info("设置通知渠道 {} {}",topic.getClientid(),clienConnection);
log.info("设置通知渠道 {} ",topic.getClientid());
clienConnection.setCommd(noticeMessageDomain.getCommd());
clienConnectionMap.put(topic.getClientid(),clienConnection);
clienNoticeServiceFactory.sendMessage(noticeMessageDomain);
... ...
... ... @@ -245,19 +245,22 @@ public class IotThingsModelService {
{
for(String vkey:vjsonObject.keySet())
{
JSONObject jsData = vjsonObject.getJSONObject(vkey);
for(String key:jsData.keySet())
if(GsonConstructor.get().fromJson(vjsonObject.get(vkey).toString(),JsonElement.class).isJsonObject())
{
IotThingsModel thingsModel = getThingsModelBase(product_id,key);
String data_type = thingsModel.getData_type().toUpperCase();
if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type))
JSONObject jsData = vjsonObject.getJSONObject(vkey);
for(String key:jsData.keySet())
{
data_type = ThingsModelDataTypeEnum.STRING.name();
IotThingsModel thingsModel = getThingsModelBase(product_id,key);
String data_type = thingsModel.getData_type().toUpperCase();
if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type))
{
data_type = ThingsModelDataTypeEnum.STRING.name();
}
ThingsModelItemBase thingsModelBase = ThingsModelItemBase.newhingsModelReverse(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
jsData.put(key,thingsModelBase.getValue());
}
ThingsModelItemBase thingsModelBase = ThingsModelItemBase.newhingsModelReverse(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
jsData.put(key,thingsModelBase.getValue());
vjsonObject.put(vkey,jsData);
}
vjsonObject.put(vkey,jsData);
}
return vjsonObject;
}
... ...
... ... @@ -92,6 +92,10 @@ public class AllPostTopic implements BusinessAgreement<AllPostDto> {
if(!newDataValue.equals(odlDataValue))
{
jsonObject.put(deviceDataConfig.getPlc_data_type().sensorDataType,newDataValue); //更新数据
if(deviceDataConfig.getPlc_data_type().sensorDataType.equals("104"))
{
jsonObject.put("9",newDataValue); //更新数据
}
deviceInfo.setDataValue(jsonObject.toJSONString());
String describe = deviceDataConfig.getAttribute_describe()+newDataValue;
... ...
... ... @@ -19,8 +19,8 @@ spring:
mqtt:
#链接地址
# broker: tcp://175.24.61.68:1883
broker: tcp://8.129.82.37:1883
broker: tcp://175.24.61.68:1883
# broker: tcp://8.129.82.37:1883
#唯一标识
clientId: ${random.uuid}
topicconfig: "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}/{{messageid}}"
... ... @@ -29,7 +29,7 @@ mqtt:
client:
#客户端操作时间
operationTime: 10
productids: 35
productids: 9
#rocketmq配置信息
rocketmq:
... ...