perf: 调整mqtt根据装置model动态处理topic
parent
0aa2a7e704
commit
e3f7a20c17
@ -1,35 +1,53 @@
|
|||||||
package com.shxy.xymanager_service.mqtt;
|
package com.shxy.xymanager_service.mqtt;
|
||||||
|
|
||||||
|
import com.shxy.xymanager_common.entity.Terminals;
|
||||||
|
import com.shxy.xymanager_service.service.NewCacheService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.shxy.xymanager_common.constant.Constants.*;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MqttSubscriberService {
|
public class MqttSubscriberService {
|
||||||
|
|
||||||
@Value("${mqtt.resptopic}")
|
|
||||||
private String resptopic;
|
|
||||||
@Value("${mqtt.datatopic}")
|
|
||||||
private String datatopic;
|
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private MqttClient mqttClient;
|
private MqttClient mqttClient;
|
||||||
@Resource
|
@Resource
|
||||||
DataMessageListener dataMessageListener;
|
DataMessageListener dataMessageListener;
|
||||||
@Resource
|
@Resource
|
||||||
RespMessageListener respMessageListener;
|
RespMessageListener respMessageListener;
|
||||||
|
@Resource
|
||||||
|
NewCacheService newCacheService;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() throws Exception {
|
public void init() throws Exception {
|
||||||
|
List<String> modelList = new ArrayList<>();
|
||||||
|
Map<Integer, Terminals> terminalMap = newCacheService.getTerminalMap();
|
||||||
|
Iterator<Integer> it = terminalMap.keySet().iterator();
|
||||||
|
while (it.hasNext()) {
|
||||||
|
Integer termId = it.next();
|
||||||
|
Terminals term = terminalMap.get(termId);
|
||||||
|
if (StringUtils.isNotBlank(term.getModel())) {
|
||||||
|
if (!modelList.contains(term.getModel())) {
|
||||||
|
modelList.add(term.getModel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// 订阅主题
|
// 订阅主题
|
||||||
mqttClient.subscribe(resptopic, respMessageListener);
|
for (String model : modelList) {
|
||||||
mqttClient.subscribe(datatopic, dataMessageListener);
|
mqttClient.subscribe(TopicPrefix + model + RespSuffix, respMessageListener);
|
||||||
|
mqttClient.subscribe(TopicPrefix + model + DataSuffix, dataMessageListener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue