|
|
|
package com.zhonglai.luhui.device.protocol.plc004.control;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指令控制监听实现
|
|
|
|
*/
|
|
|
|
public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFactory {
|
|
|
|
private static final String topicModel = "/{{roleid}}/{{username}}/{{topicType}}/{{clientid}}";
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenServiceImpl.class);
|
|
|
|
@Override
|
|
|
|
public NoticeMessageDto read(String deviceId, JsonObject jsonObject) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return noticeMessageDto;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean notice(String deviceId, JsonObject jsonObject) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
return topic;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|