|
|
|
package com.zhonglai.luhui.device.protocol.plc004.control;
|
|
|
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.ruoyi.common.utils.GsonConstructor;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.util.ByteUtil;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.util.DateUtils;
|
|
|
|
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;
|
|
...
|
...
|
@@ -12,13 +13,14 @@ import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto; |
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指令控制监听实现
|
|
|
|
*/
|
|
|
|
public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFactory {
|
|
|
|
private static final String topicModel = "/{{roleid}}/{{username}}/{{topicType}}/{{clientid}}";
|
|
|
|
private static final String topicModel = "/{{roleid}}/{{username}}/{{clientid}}/{{payloadtype}}/{{topicType}}";
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class);
|
|
|
|
@Override
|
|
...
|
...
|
@@ -34,17 +36,12 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto |
|
|
|
NoticeMessageDto noticeMessageDto = new NoticeMessageDto();
|
|
|
|
noticeMessageDto.setTopic(topic);
|
|
|
|
noticeMessageDto.setTopicconfig(topicModel);
|
|
|
|
if (jsonObject.has("1"))
|
|
|
|
{
|
|
|
|
JsonObject data = jsonObject.get("1").getAsJsonObject();
|
|
|
|
if (data.has("3") && data.has("106"))
|
|
|
|
{
|
|
|
|
byte[] bytes = new byte[]{(byte) 0xA5, 0x07, 0x01, 0x20, (byte) data.get("3").getAsInt(), (byte) (data.get("106").getAsInt() - 1)};
|
|
|
|
bytes = Arrays.copyOf(bytes, bytes.length + 1);
|
|
|
|
bytes[bytes.length - 1] = ByteUtil.calculateChecksum(bytes);
|
|
|
|
noticeMessageDto.setCommd(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//将jsonObject里面的两层键值比如:{"1":{"key","value","key1",1}}提取成一层放在commdjson里面如{"key":"value","key1":1}
|
|
|
|
JsonObject commdjson = flattenJsonObject(jsonObject);
|
|
|
|
|
|
|
|
noticeMessageDto.setCommd(commdjson.toString().getBytes());
|
|
|
|
|
|
|
|
return noticeMessageDto;
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -53,6 +50,39 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto |
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 将嵌套的JsonObject扁平化。
|
|
|
|
*
|
|
|
|
* @param jsonObject 嵌套的JsonObject
|
|
|
|
* @return 扁平化的JsonObject
|
|
|
|
*/
|
|
|
|
public JsonObject flattenJsonObject(JsonObject jsonObject) {
|
|
|
|
// 使用Gson将嵌套的JsonObject转换为Map
|
|
|
|
Type typeOfMap = new TypeToken<Map<String, JsonElement>>(){}.getType();
|
|
|
|
Map<String, JsonElement> map = GsonConstructor.get().fromJson(jsonObject, typeOfMap);
|
|
|
|
|
|
|
|
// 创建一个新的JsonObject用于存储扁平化的结果
|
|
|
|
JsonObject flatJson = new JsonObject();
|
|
|
|
|
|
|
|
// 遍历Map中的所有键值对
|
|
|
|
for (Map.Entry<String, JsonElement> entry : map.entrySet()) {
|
|
|
|
JsonElement value = entry.getValue();
|
|
|
|
|
|
|
|
// 检查值是否为JsonObject
|
|
|
|
if (value.isJsonObject()) {
|
|
|
|
// 如果是JsonObject,则将其键值对添加到flatJson中
|
|
|
|
JsonObject innerJson = value.getAsJsonObject();
|
|
|
|
for (Map.Entry<String, JsonElement> innerEntry : innerJson.entrySet()) {
|
|
|
|
flatJson.add(innerEntry.getKey(), innerEntry.getValue());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 如果不是JsonObject,则直接添加到flatJson中
|
|
|
|
flatJson.add(entry.getKey(), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flatJson;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Topic getWriteTopic(String deviceId, IotProduct iotProduct)
|
|
|
|
{
|
|
...
|
...
|
@@ -61,6 +91,7 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto |
|
|
|
topic.setClientid(deviceId);
|
|
|
|
topic.setRoleid(iotProduct.getRole_id()+"");
|
|
|
|
topic.setUsername(iotProduct.getMqtt_username());
|
|
|
|
topic.setPayloadtype("Json");
|
|
|
|
return topic;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|