add:接入mqtt(td:项目启停时设备信号读写)

This commit is contained in:
zhangzq
2024-02-01 17:27:03 +08:00
parent 1dbc4e0098
commit 86935740fc
15 changed files with 807 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package org.nl.config.agvconfig;
import io.netty.buffer.ByteBuf;
/*
* @author ZZQ
* @Date 2024/1/3 17:18
*/
public class ProtocolCodec {
public static void readEmp(ByteBuf buf, int size){
buf.readBytes(size);
}
public static byte[] readBody(ByteBuf buf){
byte[] body = new byte[buf.readableBytes()];
buf.readBytes(body);
if (body[0]<<8+body[1] == 0X87CD){
return body;
}
return new byte[0];
}
}

View File

@@ -0,0 +1,46 @@
package org.nl.config.lucene;
/**
* @Author: lyd
* @Description: 定义lucene相关常量
* @Date: 2023/8/25
*/
public class LogMessageConstant {
/** */
public final static String SORT_NAME = "time";
/** 级别 */
public final static String FIELD_LEVEL = "level";
/** 时间 */
public final static String FIELD_TIMESTAMP = "timestamp";
/** 类的限定名 */
public final static String FIELD_CLASS_NAME = "logger";
/** 线程名 */
public final static String FIELD_THREAD = "thread";
/** 日志内容 */
public final static String FIELD_MESSAGE = "message";
public final static String FIELD_TRACEID = "tlogTraceId";
// 定义颜色值
/** 文本颜色:黑色 */
public final static String COLOR_BLACK = "\u001B[30m";
/** 文本颜色:红色 */
public final static String COLOR_RED = "\u001B[31m";
/** 文本颜色:绿色 */
public final static String COLOR_GREEN = "\u001B[32m";
/** 文本颜色:黄色 */
public final static String COLOR_YELLOW = "\u001B[33m";
/** 文本颜色:蓝色 */
public final static String COLOR_BLUE = "\u001B[34m";
/** 文本颜色:品红色 */
public final static String COLOR_MAGENTA = "\u001B[35m";
/** 文本颜色:青色 */
public final static String COLOR_CYAN = "\u001B[36m";
/** 文本颜色:白色 */
public final static String COLOR_WHITE = "\u001B[37m";
/** 文本颜色重置 */
public final static String COLOR_RESET = "\u001B[0m";
/** 背景颜色:黄色 */
public final static String BACKGROUND_YELLOW = "\u001B[43m";
/** 索引路径 */
public final static String INDEX_DIR = "E:\\lucene\\index";
}

View File

@@ -0,0 +1,85 @@
package org.nl.config.lucene;
/**
* @author ldjun
* @version 1.0
* @date 2023年08月24日 13:00
* @desc desc
*/
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKAnalyzer;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;
public class LuceneAppender extends AppenderBase<ILoggingEvent> {
public LuceneProperties properties;
public static Directory index;
private List<LucenePropertyAndEncoder> encoders;
public static IndexWriter indexWriter;
@Override
public void start() {
super.start();
try {
// 读取配置文件
Properties properties = YmlConfigFileUtil.readConfig("config/application.yml");
// 获取配置值
String luceneDir = properties.getProperty("lucene.index.path");
index = FSDirectory.open(Paths.get(luceneDir));
// 初始化 Lucene 索引
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(index, config);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void append(ILoggingEvent event) {
Document doc = new Document();
for (Property property : this.properties.getProperties()) {
LucenePropertyAndEncoder encoder = new LucenePropertyAndEncoder(property, this.context);
doc.add(new StringField(property.getName(), encoder.encode(event), Field.Store.YES));
}
doc.add(new TextField(LogMessageConstant.FIELD_MESSAGE, event.getFormattedMessage(), Field.Store.YES));
doc.add(new StringField(LogMessageConstant.FIELD_TIMESTAMP, String.valueOf(event.getTimeStamp()),Field.Store.YES));
doc.add(new NumericDocValuesField(LogMessageConstant.SORT_NAME, event.getTimeStamp()));
try {
indexWriter.addDocument(doc);
indexWriter.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void stop() {
super.stop();
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setProperties(LuceneProperties properties) {
this.properties = properties;
}
}

View File

@@ -0,0 +1,25 @@
package org.nl.config.lucene;
import java.util.ArrayList;
import java.util.List;
public class LuceneProperties {
private List<Property> properties;
public LuceneProperties() {
this.properties = new ArrayList<Property>();
}
public List<Property> getProperties() {
return properties;
}
public void addProperty(Property property) {
properties.add(property);
}
}

View File

@@ -0,0 +1,38 @@
package org.nl.config.lucene;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.pattern.PatternLayoutBase;
/*
* @author ZZQ
* @Date 2023/12/22 18:11
*/
public class LucenePropertyAndEncoder {
private Property property;
private PatternLayoutBase layout = new PatternLayout();
public LucenePropertyAndEncoder(Property property, Context context) {
this.property = property;
this.layout.setContext(context);
this.layout.setPattern(String.valueOf(property.getValue()));
this.layout.setPostCompileProcessor(null);
this.layout.start();
}
public String encode(ILoggingEvent event) {
return layout.doLayout(event);
}
public String getName() {
return property.getName();
}
public boolean allowEmpty() {
return property.isAllowEmpty();
}
}

View File

@@ -0,0 +1,44 @@
package org.nl.config.lucene;
/*
* @author ZZQ
* @Date 2023/12/26 15:30
*/
public class Property {
private String name;
private String value;
private boolean allowEmpty;
public Property() {
}
public Property(String name, String value, boolean allowEmpty) {
this.name = name;
this.value = value;
this.allowEmpty = allowEmpty;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public boolean isAllowEmpty() {
return allowEmpty;
}
public void setAllowEmpty(boolean allowEmpty) {
this.allowEmpty = allowEmpty;
}
}

View File

@@ -0,0 +1,28 @@
package org.nl.config.lucene;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.Properties;
/**
* @Author: lyd
* @Description: 配置文件获取方法
* @Date: 2023/12/6
*/
public class YmlConfigFileUtil {
public static Properties readConfig(String configFile) {
// 创建 Resource 对象
Resource resource = new ClassPathResource(configFile);
// 创建 YamlPropertiesFactoryBean
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(resource);
// 获取 Properties 对象
Properties properties = yamlPropertiesFactoryBean.getObject();
return properties;
}
}

View File

@@ -0,0 +1,79 @@
package org.nl.config.mqtt;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.nl.config.mqtt.callback.PublishCallback;
import org.nl.config.mqtt.callback.SubsribeCallback;
import org.nl.config.mqtt.config.MqttConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.UUID;
/*
* @author ZZQ
* @Date 2024/1/31 14:07
*
*/
@Service
@ConditionalOnProperty(name = "mqtt.active", havingValue = "true")
public class MQServer {
@Autowired
private MqttConfig mqttConfig;
public static MqttClient subsribeClient;
public static MqttClient publishClient;
@PostConstruct
public void init() throws Exception {
this.initSubsribe();
this.initPublish();
}
public static void sendMsg(String topic,String body,int qos,Boolean retained){
try {
if (publishClient!=null && StringUtils.isNotEmpty(body)){
MqttMessage message = new MqttMessage();
message.setQos(qos);
message.setRetained(retained);
message.setPayload(body.getBytes());
publishClient.publish(topic,message);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
public void shutdown() throws Exception {
subsribeClient.disconnect();
publishClient.disconnect();
}
private void initSubsribe() throws Exception {
if (subsribeClient!=null){
System.out.println("重新连接");
subsribeClient.disconnect();
}
subsribeClient = new MqttClient(mqttConfig.getUrl(), mqttConfig.getClientId(), new MemoryPersistence());
subsribeClient.connect(mqttConfig.getOption());
subsribeClient.setCallback(new SubsribeCallback());
subsribeClient.subscribe(mqttConfig.getTopics());
}
private void initPublish() throws Exception {
if (publishClient!=null){
System.out.println("重新连接");
publishClient.disconnect();
}
publishClient = new MqttClient(mqttConfig.getUrl(), UUID.randomUUID().toString(), new MemoryPersistence());
publishClient.connect(mqttConfig.getOption());
publishClient.setCallback(new PublishCallback());
}
}

View File

@@ -0,0 +1,29 @@
package org.nl.config.mqtt;
import cn.dev33.satoken.annotation.SaIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @author ZZQ
* @Date 2024/1/31 17:12
*/
@RestController
public class PublishDemo {
@Autowired
MQServer server;
@RequestMapping("/publish")
@SaIgnore
public void send(String topic,String msg){
MQServer.sendMsg(topic,msg,1,true);
}
@RequestMapping("/init")
@SaIgnore
public void d222() throws Exception {
server.init();
}
}

View File

@@ -0,0 +1,31 @@
package org.nl.config.mqtt.callback;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.nl.config.mqtt.msg.MsgWorker;
import org.nl.config.mqtt.msg.MsgPoolManager;
/*
* @author ZZQ
* @Date 2024/1/31 13:55
*/
public class PublishCallback implements MqttCallback {
@Override
public void connectionLost(Throwable throwable) {
//重连调用服务初始化init
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
//接收到消息先放队列根据不同topic处理不同处理逻辑
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
//消息接收成功后
boolean complete = iMqttDeliveryToken.isComplete();
System.out.println("消息处理结果:"+complete);
}
}

View File

@@ -0,0 +1,31 @@
package org.nl.config.mqtt.callback;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.nl.config.mqtt.msg.MsgPoolManager;
import org.nl.config.mqtt.msg.MsgWorker;
/*
* @author ZZQ
* @Date 2024/1/31 13:55
*/
public class SubsribeCallback implements MqttCallback {
@Override
public void connectionLost(Throwable throwable) {
//重连调用服务初始化init
}
@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
//接收到消息先放队列根据不同topic处理不同处理逻辑
System.out.println("接收到消息topic:"+topic+"——"+new String(mqttMessage.getPayload()));
MsgPoolManager.hander(new MsgWorker(topic,new String(mqttMessage.getPayload())));
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
//消息接收如果处理失败允许在这里重试
}
}

View File

@@ -0,0 +1,63 @@
package org.nl.config.mqtt.config;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/*
* @author ZZQ
* @Date 2024/1/31 14:07
*/
@Configuration
@ConfigurationProperties(prefix = "spring.mqtt")
@ConditionalOnProperty(name = "mqtt.active", havingValue = "true")
@Data
public class MqttConfig {
private String username;
private String password;
private String url;
private String clientId;
private String[] topics;
private int timeout;
private int keepalive;
private MqttConnectOptions option;
@PostConstruct
public void initOption(){
MqttConnectOptions options = new MqttConnectOptions();
// 设定清除会话信息true时每次连接都会建立新的会话false时服务端会保留会话信息
options.setCleanSession(true);
//options.setCleanSession(true);
// 设定重连机制设定为true时,mqtt的重连机制会启动,当mqtt client掉线之后它会进入重连
options.setAutomaticReconnect(true);
if (!username.equals("a")){
options.setUserName(username);
}
if (!password.equals("a")){
options.setPassword(password.toCharArray());
}
options.setConnectionTimeout(timeout);
options.setKeepAliveInterval(keepalive);
// 设置“遗嘱”消息的话题,若客户端与服务器之间的连接意外中断,服务器将发布客户端的“遗嘱”消息。
//options.setWill("willTopic", WILL_DATA, 2, false);
option=options;
}
}

View File

@@ -0,0 +1,83 @@
package org.nl.config.mqtt.msg;
import org.checkerframework.checker.units.qual.C;
import org.nl.config.mqtt.MQServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.SmartLifecycle;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Queue;
import java.util.concurrent.*;
/*
* @author ZZQ
* @Date 2024/1/31 15:19
* 待确认1.所有消走线程还是先走队列2.同一个topic消息处理需要保证消费顺序3.消息写时订阅也会订阅毁掉会拿到自己写的信息
*/
@Component
@ConditionalOnBean(value = MQServer.class)
public class MsgPoolManager {
@Autowired
MQServer mqServer;
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 2;
// 线程池维护线程的最大数量
private final static int MAX_POOL_SIZE = 20 ;
// 线程池维护线程所允许的空闲时间
private final static int KEEP_ALIVE_TIME = 10;
// 线程池 所使用的缓存队列大小
private final static int WORK_QUEUE_SIZE = 1000;
private final static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);
static Queue<Runnable> msgQueue = new LinkedBlockingQueue<>();
static ThreadPoolExecutor threadPool =null;
static ScheduledFuture scheduledFuture =null;
static {
}
public static void hander(MsgWorker r){
threadPool.execute(r);
}
@PostConstruct
public void start() {
threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS,new ArrayBlockingQueue<>(WORK_QUEUE_SIZE), (r, executor) -> msgQueue.add(r));
// scheduledFuture = scheduler.scheduleAtFixedRate(() -> {
// // 判断缓存队列是否存在记录
// System.out.println("定时任务启动"+msgQueue.size());
// if(!msgQueue.isEmpty()){
// if(threadPool.getQueue().size() < WORK_QUEUE_SIZE){
// Runnable worker = msgQueue.poll();
// threadPool.execute(worker);
// }
// }
// },0,1,TimeUnit.SECONDS);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("----关闭线程池-----");
try {
mqServer.shutdown();
threadPool.shutdown();
// scheduler.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}));
}
}

View File

@@ -0,0 +1,27 @@
package org.nl.config.mqtt.msg;
/*
* @author ZZQ
* @Date 2024/1/31 15:29
* 消息处理线程通过线程池处理如果需要用到具体topic的handler可以传入
*/
public class MsgWorker implements Runnable{
/**
* topic
*/
public String topic;
/**
* 消息体
*/
private String body;
@Override
public void run() {
System.out.println("接收到消息"+topic+"-"+body);
}
public MsgWorker(String topic, String body) {
this.topic = topic;
this.body = body;
}
}

View File

@@ -0,0 +1,176 @@
server:
port: 8010
tomcat:
accept-count: 1000
max-connections: 10000
max-threads: 800
min-spare-threads: 100
shutdown: graceful
#配置数据源
spring:
lifecycle:
timeout-per-shutdown-phase: 60s
datasource:
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
# url: jdbc:log4jdbc:mysql://${DB_HOST:10.1.3.91}:${DB_PORT:3306}/${DB_NAME:acs}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
# url: jdbc:log4jdbc:mysql://${DB_HOST:192.168.81.252}:${DB_PORT:3306}/${DB_NAME:hl_one_acs}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:hl_one_acs}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true&allowPublicKeyRetrieval=true
username: ${DB_USER:root}
# password: ${DB_PWD:P@ssw0rd}
# password: ${DB_PWD:Root.123456}
password: ${DB_PWD:942464Yy}
# 初始连接数
initial-size: 5
# 最小连接数
min-idle: 15
# 最大连接数
max-active: 30
# 超时时间(以秒数为单位)
remove-abandoned-timeout: 180
# 获取连接超时时间
max-wait: 3000
# 连接有效性检测时间
time-between-eviction-runs-millis: 60000
# 连接在池中最小生存的时间
min-evictable-idle-time-millis: 300000
# 连接在池中最大生存的时间
max-evictable-idle-time-millis: 900000
# 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
test-while-idle: true
# 指明是否在从池中取出连接前进行检验,如果检验失败, 则从池中去除连接并尝试取出另一个
test-on-borrow: true
# 是否在归还到池中前进行检验
test-on-return: false
# 检测连接是否有效
validation-query: select 1
# 配置监控统计
webStatFilter:
enabled: true
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false
filter:
stat:
enabled: true
# 记录慢SQL
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
redis:
#数据库索引
database: ${REDIS_DB:15}
host: ${REDIS_HOST:127.0.0.1}
port: ${REDIS_PORT:6379}
password: ${REDIS_PWD:}
mqtt:
username: a
password: a
url: tcp://10.211.55.3:1884
clientId: mqttdemo001
topics:
- test01
- test02
timeout: 10
keepalive: 100
# 登录相关配置
login:
# 登录缓存
cache-enable: true
# 是否限制单用户登录
single-login: false
# 验证码
login-code:
# 验证码类型配置 查看 LoginProperties 类
code-type: arithmetic
# 登录图形验证码有效时间/分钟
expiration: 2
# 验证码高度
width: 111
# 验证码宽度
heigth: 36
# 内容长度
length: 2
# 字体名称,为空则使用默认字体
font-name:
# 字体大小
font-size: 25
#jwt
jwt:
header: Authorization
# 令牌前缀
token-start-with: Bearer
# 必须使用最少88位的Base64对该令牌进行编码
base64-secret: ZmQ0ZGI5NjQ0MDQwY2I4MjMxY2Y3ZmI3MjdhN2ZmMjNhODViOTg1ZGE0NTBjMGM4NDA5NzYxMjdjOWMwYWRmZTBlZjlhNGY3ZTg4Y2U3YTE1ODVkZDU5Y2Y3OGYwZWE1NzUzNWQ2YjFjZDc0NGMxZWU2MmQ3MjY1NzJmNTE0MzI=
# 令牌过期时间 此处单位/毫秒 默认4小时可在此网站生成 https://www.convertworld.com/zh-hans/time/milliseconds.html
token-validity-in-seconds: 14400000
# 在线用户key
online-key: online-token-
# 验证码
code-key: code-key-
# token 续期检查时间范围默认30分钟单位毫秒在token即将过期的一段时间内用户操作了则给用户的token续期
detect: 1800000
# 续期时间范围默认1小时单位毫秒
renew: 3600000
#是否允许生成代码生产环境设置为false
generator:
enabled: true
#是否开启 swagger-ui
swagger:
enabled: true
# IP 本地解析
ip:
local-parsing: true
# 文件存储路径
file:
mac:
path: ~/file/
avatar: ~/avatar/
linux:
path: /home/eladmin/file/
avatar: /home/eladmin/avatar/
windows:
path: C:\eladmin\file\
avatar: C:\eladmin\avatar\
# 文件大小 /M
maxSize: 100
avatarMaxSize: 5
logging:
file:
path: C:\logs\nlwms\
config: classpath:logback-spring.xml
lucene:
index:
path: C:\logs\index
# Sa-Token配置
sa-token:
# token 名称 (同时也是cookie名称)
token-name: Authorization
# token 有效期单位s 默认30天, -1代表永不过期
timeout: 2592000
# token 临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
activity-timeout: -1
# 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
is-concurrent: true
# 在多人登录同一账号时是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
is-share: false
# token风格
token-style: random-128
# 是否输出操作日志
is-log: false
jwt-secret-key: opsjajisdnnca0sdkksdfaaasdfwwq
# token 前缀
token-prefix: Bearer