package com.ard.sample.component.mqtt.handler;
|
|
import com.ard.sample.component.mqtt.model.Chan;
|
import com.ard.sample.component.mqtt.model.ChannelName;
|
import com.ard.sample.component.mqtt.model.CommonTopicReceiver;
|
import com.ard.sample.component.websocket.enums.MessageType;
|
import com.ard.sample.component.websocket.handler.DockWebSocketHandler;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.integration.annotation.ServiceActivator;
|
import org.springframework.integration.mqtt.support.MqttHeaders;
|
import org.springframework.messaging.Message;
|
import org.springframework.stereotype.Component;
|
|
import java.io.IOException;
|
|
/**
|
* @author sean
|
* @version 1.2
|
* @date 2022/9/9
|
*/
|
@Component
|
public class PropertySetReplyHandler {
|
|
@Autowired
|
private ObjectMapper mapper;
|
|
@Autowired
|
private DockWebSocketHandler _dockWebSocketHandler;
|
|
/**
|
* Handle the reply message from the pilot side to the on-demand video.
|
* @param message reply message
|
* @throws IOException
|
*/
|
@ServiceActivator(inputChannel = ChannelName.INBOUND_PROPERTY_SET_REPLY)
|
public void serviceReply(Message<?> message) throws IOException {
|
// 解析消息的主题,从中提取设备序列号等信息
|
String topic = message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC).toString();
|
// thing/product/7CTDLCE00A1UPE/property/set_reply
|
String[] parts = topic.split("/");
|
// 目标子串是分割后数组的第3个元素(索引2)
|
String deviceSn = parts.length > 2 ? parts[2] : ""; // 加判断避免数组越界
|
|
byte[] payload = (byte[])message.getPayload();
|
|
CommonTopicReceiver receiver = mapper.readValue(payload, new TypeReference<CommonTopicReceiver>() {});
|
Chan<CommonTopicReceiver<?>> chan = Chan.getInstance();
|
// Put the message to the chan object.
|
chan.put(receiver);
|
|
receiver.setGateway(deviceSn);
|
receiver.setType(MessageType.CONTROL.getType());
|
// receiver.setMethod();
|
|
// 设备属性设置后的处理逻辑
|
|
// WebSocket 接口,发给前端
|
String dockControl="";
|
dockControl=mapper.writeValueAsString(receiver);
|
|
_dockWebSocketHandler.sendMessageToSubscribers(deviceSn, MessageType.CONTROL.getType(),dockControl, "");
|
}
|
}
|