第一次提交
This commit is contained in:
24
fastbee-gateway/gateway-boot/pom.xml
Normal file
24
fastbee-gateway/gateway-boot/pom.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>fastbee-gateway</artifactId>
|
||||
<groupId>com.fastbee</groupId>
|
||||
<version>3.8.5</version>
|
||||
</parent>
|
||||
<artifactId>gateway-boot</artifactId>
|
||||
|
||||
<description>网关模块</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fastbee</groupId>
|
||||
<artifactId>fastbee-mq</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
@ -0,0 +1,70 @@
|
||||
package com.fastbee.gateway.boot.start;
|
||||
|
||||
import com.fastbee.mq.redischannel.listen.*;
|
||||
import com.fastbee.mqttclient.PubMqttClient;
|
||||
import com.fastbee.protocol.service.IProtocolManagerService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 启动类
|
||||
*
|
||||
* @author bill
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@Order(2)
|
||||
public class StartBoot implements ApplicationRunner {
|
||||
|
||||
|
||||
@Autowired
|
||||
private PubMqttClient mqttClient;
|
||||
@Autowired
|
||||
private DeviceReplyListen replyListen;
|
||||
@Autowired
|
||||
private DeviceReportListen reportListen;
|
||||
@Autowired
|
||||
private DeviceStatusListen statusListen;
|
||||
@Autowired
|
||||
private DevicePropFetchListen propFetchListen;
|
||||
@Autowired
|
||||
private UpgradeListen upgradeListen;
|
||||
@Autowired
|
||||
private FunctionInvokeListen invokeListen;
|
||||
@Resource
|
||||
private DeviceOtherListen otherListen;
|
||||
@Resource
|
||||
private DeviceTestListen testListen;
|
||||
@Resource
|
||||
private IProtocolManagerService protocolManagerService;
|
||||
@Resource
|
||||
private IMqttMessageListener subscribeCallback;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
try {
|
||||
replyListen.listen();
|
||||
reportListen.listen();
|
||||
statusListen.listen();
|
||||
propFetchListen.listen();
|
||||
upgradeListen.listen();
|
||||
invokeListen.listen();
|
||||
otherListen.listen();
|
||||
testListen.listen();
|
||||
/*启动内部客户端,用来下发客户端服务*/
|
||||
mqttClient.setListener(subscribeCallback);
|
||||
mqttClient.initialize();
|
||||
protocolManagerService.getAllProtocols();
|
||||
log.info("=>设备监听队列启动成功");
|
||||
} catch (Exception e) {
|
||||
log.error("=>客户端启动失败:{}", e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package com.fastbee.gateway.boot.start;
|
||||
|
||||
import com.fastbee.common.core.mq.DeviceReportBo;
|
||||
import com.fastbee.common.enums.ServerType;
|
||||
import com.fastbee.common.utils.DateUtils;
|
||||
import com.fastbee.common.utils.StringUtils;
|
||||
import com.fastbee.common.utils.gateway.mq.TopicsUtils;
|
||||
import com.fastbee.iot.ruleEngine.MsgContext;
|
||||
import com.fastbee.iot.ruleEngine.RuleProcess;
|
||||
import com.fastbee.mqttclient.IEmqxMessageProducer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class subscribeCallback implements IMqttMessageListener {
|
||||
@Resource
|
||||
private RuleProcess ruleProcess;
|
||||
@Resource
|
||||
private TopicsUtils topicsUtils;
|
||||
@Resource
|
||||
private IEmqxMessageProducer emqxMessageSerice;
|
||||
|
||||
@Override
|
||||
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
|
||||
String message = new String(mqttMessage.getPayload());
|
||||
log.info("接收消息主题 : " + topic);
|
||||
log.info("接收消息Qos : " + mqttMessage.getQos());
|
||||
log.info("接收消息内容 : " + message);
|
||||
|
||||
//这里默认设备编号长度超过9位
|
||||
String[] split = topic.split("/");
|
||||
String sn = Arrays.stream(split).filter(imei -> imei.length() > 9).findFirst().get();
|
||||
// 规则引擎脚本处理,完成后返回结果
|
||||
MsgContext context = ruleProcess.processRuleScript(sn, 1, topic, message);
|
||||
if (!Objects.isNull(context) && StringUtils.isNotEmpty(context.getPayload())
|
||||
&& StringUtils.isNotEmpty(context.getTopic())) {
|
||||
topic = context.getTopic();
|
||||
message = context.getPayload();
|
||||
}
|
||||
String serialNumber = topicsUtils.parseSerialNumber(topic);
|
||||
Long productId = topicsUtils.parseProductId(topic);
|
||||
String name = topicsUtils.parseTopicName(topic);
|
||||
DeviceReportBo reportBo = DeviceReportBo.builder()
|
||||
.serialNumber(serialNumber)
|
||||
.productId(productId)
|
||||
.data(message.getBytes(StandardCharsets.UTF_8))
|
||||
.platformDate(DateUtils.getNowDate())
|
||||
.topicName(topic)
|
||||
.serverType(ServerType.MQTT)
|
||||
.build();
|
||||
/*将mqtt的消息发送至MQ队列处理消息 ,减轻mqtt客户端消息压力*/
|
||||
emqxMessageSerice.sendEmqxMessage(name,reportBo);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user