perf: 调整mqtt根据装置model动态处理topic
parent
0aa2a7e704
commit
e3f7a20c17
@ -1,35 +1,53 @@
|
||||
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 org.apache.commons.lang3.StringUtils;
|
||||
import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
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
|
||||
@Slf4j
|
||||
public class MqttSubscriberService {
|
||||
|
||||
@Value("${mqtt.resptopic}")
|
||||
private String resptopic;
|
||||
@Value("${mqtt.datatopic}")
|
||||
private String datatopic;
|
||||
|
||||
@Resource
|
||||
private MqttClient mqttClient;
|
||||
@Resource
|
||||
DataMessageListener dataMessageListener;
|
||||
@Resource
|
||||
RespMessageListener respMessageListener;
|
||||
@Resource
|
||||
NewCacheService newCacheService;
|
||||
|
||||
@PostConstruct
|
||||
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);
|
||||
mqttClient.subscribe(datatopic, dataMessageListener);
|
||||
for (String model : modelList) {
|
||||
mqttClient.subscribe(TopicPrefix + model + RespSuffix, respMessageListener);
|
||||
mqttClient.subscribe(TopicPrefix + model + DataSuffix, dataMessageListener);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue