first commit

This commit is contained in:
zc
2025-12-08 10:40:43 +08:00
commit 871ae8be0a
410 changed files with 38212 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package org.dromara.mica.mqtt.client.solon.test;
import org.dromara.mica.mqtt.client.solon.MqttClientTemplate;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventListener;
import java.nio.charset.StandardCharsets;
/**
* <b>(ClientTest)</b>
*
* @author Peigen
* @version 1.0.0
* @since 2023/7/15
*/
@Component
public class ClientTest implements EventListener<AppLoadEndEvent> {
public static void main(String[] args) {
Solon.start(ClientTest.class, args);
}
@Inject
MqttClientTemplate client;
@Override
public void onEvent(AppLoadEndEvent event) throws Throwable {
client.publish("mica", "hello".getBytes(StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.client.solon.test.listener;
import org.dromara.mica.mqtt.client.solon.event.MqttConnectedEvent;
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.EventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 客户端连接状态监听
*
* @author L.cm
*/
@Component
public class MqttClientConnectListener implements EventListener<MqttConnectedEvent> {
private static final Logger logger = LoggerFactory.getLogger(MqttClientConnectListener.class);
@Inject
private MqttClientCreator mqttClientCreator;
@Override
public void onEvent(MqttConnectedEvent mqttConnectedEvent) throws Throwable {
logger.info("MqttConnectedEvent:{}", mqttConnectedEvent);
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.mica.mqtt.client.solon.test.listener;
import org.dromara.mica.mqtt.client.solon.event.MqttDisconnectEvent;
import org.dromara.mica.mqtt.core.client.MqttClientCreator;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.EventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 客户端连接状态监听
*
* @author L.cm
*/
@Component
public class MqttClientDisConnectListener implements EventListener<MqttDisconnectEvent> {
private static final Logger logger = LoggerFactory.getLogger(MqttClientDisConnectListener.class);
@Inject
private MqttClientCreator mqttClientCreator;
@Override
public void onEvent(MqttDisconnectEvent mqttDisconnectEvent) throws Throwable {
logger.info("MqttDisconnectEvent:{}", mqttDisconnectEvent);
// 在断线时更新 clientId、username、password
mqttClientCreator.clientId("newClient" + System.currentTimeMillis())
.username("newUserName")
.password("newPassword");
}
}

View File

@@ -0,0 +1,26 @@
package org.dromara.mica.mqtt.client.solon.test.listener;
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
import org.dromara.mica.mqtt.codec.message.MqttPublishMessage;
import org.dromara.mica.mqtt.core.client.IMqttClientMessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import java.nio.charset.StandardCharsets;
/**
* 客户端消息监听的另一种方式
*
* @author L.cm
*/
@MqttClientSubscribe("${topic1}")
public class MqttClientMessageListener implements IMqttClientMessageListener {
private static final Logger logger = LoggerFactory.getLogger(MqttClientMessageListener.class);
@Override
public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,38 @@
package org.dromara.mica.mqtt.client.solon.test.listener;
import org.dromara.mica.mqtt.core.annotation.MqttClientSubscribe;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.noear.solon.annotation.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
/**
* 客户端消息监听
*
* @author L.cm
*/
@Component
public class MqttClientSubscribeListener {
private static final Logger logger = LoggerFactory.getLogger(MqttClientSubscribeListener.class);
@MqttClientSubscribe("/test/#")
public void subQos0(String topic, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.QOS1)
public void subQos1(String topic, byte[] payload) {
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
@MqttClientSubscribe("/sys/${productKey}/${deviceName}/thing/sub/register")
public void thingSubRegister(String topic, byte[] payload) {
// 1.3.8 开始支持,@MqttClientSubscribe 注解支持 ${} 变量替换,会默认替换成 +
// 注意mica-mqtt 会先从 Spring boot 配置中替换参数 ${},如果存在配置会优先被替换。
logger.info("topic:{} payload:{}", topic, new String(payload, StandardCharsets.UTF_8));
}
}

View File

@@ -0,0 +1,27 @@
server:
port: 30036
# mqtt-client 配置
mqtt:
client:
enabled: true # 是否开启客户端默认true
ip: 127.0.0.1 # 连接的服务端 ip 默认127.0.0.1
port: 18889 # 端口默认1883
# clientId: 000001 # 客户端Id非常重要一般为设备 sn不可重复
# username: mica # 认证的用户名
# password: mica # 认证的密码
# timeout: 5 # 超时时间单位默认5秒
# reconnect: true # 是否重连默认true
# re-interval: 5000 # 重连时间,默认 5000 毫秒
# version: mqtt_3_1_1 # mqtt 协议版本,可选 MQTT_3_1、mqtt_3_1_1、mqtt_5默认mqtt_3_1_1
# read-buffer-size: 8KB # 接收数据的 buffer size默认8k
# max-bytes-in-message: 10MB # 消息解析最大 bytes 长度默认10M
# keep-alive-secs: 60 # keep-alive 时间,单位:秒
# clean-session: true # mqtt clean session默认true
# ssl:
# enabled: false # 是否开启 ssl 认证2.1.0 开始支持双向认证
# keystore-path: # 可选参数ssl 双向认证 keystore 目录,支持 classpath:/ 路径。
# keystore-pass: # 可选参数ssl 双向认证 keystore 密码
# truststore-path: # 可选参数ssl 双向认证 truststore 目录,支持 classpath:/ 路径。
# truststore-pass: # 可选参数ssl 双向认证 truststore 密码
topic1: /test2/#