# jetlinks-spring-boot-starter **Repository Path**: caixiadao/jetlinks-spring-boot-starter ## Basic Information - **Project Name**: jetlinks-spring-boot-starter - **Description**: 一个基于springboot的快速集成jetlinks-community (当前仅支持社区版websocket协议)消息订阅与发送的启动器; - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2021-12-13 - **Last Updated**: 2024-08-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # jetlinks-spring-boot-starter #### 介绍 一个基于springboot的快速集成jetlinks-community (当前仅支持社区版websocket协议)消息订阅与发送的启动器; ### 快速使用 #### 1. 添加工程pom.xl依赖 ``` io.gitee.caixiadao jetlinks-iot-spring-boot-starter 1.0.1 ``` #### 2. 在application.properties或者application.yml配置
yml 配置
```yaml spring: jetlinks: auth: # jetlinks 服务地址; url: http://localhost:8848 # 登陆token 过去时间,-1 永不过期 expires: -1 # 用户名; username: admin # 密码; password: admin websocket: # websocket 协议订阅消息的地址; jetlink-websocket-url: ws://localhost:8848/messaging # jetlinks 配置文件加载开关 enable: true # 订阅topic 开关,为false 时不订阅任何topic auto-sub: true # 根据添加的产品填写订阅产品ID product[0]: productId: project-1 topics: /device/{productId}/*/online,/device/{productId}/*/offline,/device/{productId}/*/message/property/report,/rule-engine/device/alarm/{productId}/** ##订阅产品2 根据添加的产品填写订阅产品ID product[1]: # 根据添加的产品填写订阅产品ID productId: project—2 #默认订阅所有topic,配置后订阅指定topic topics: /device/{productId}/*/online,/device/{productId}/*/offline,/device/{productId}/*/message/property/report,/rule-engine/device/alarm/{productId}/** ```
properties方式
```properties # jetlinks 服务授权登陆地址头; spring.jetlinks.auth.url=http://localhost:8848 # 登陆token 过去时间,-1 永不过期 spring.jetlinks.auth.expires=-1 # 用户名; spring.jetlinks.auth.username=admin # 密码; spring.jetlinks.auth.password=admin # websocket 协议订阅消息的地址; spring.jetlinks.websocket.jetlink-websocket-url=ws://localhost:8848/messaging # jetlinks 配置参数加载开关 spring.jetlinks.enable=true # 订阅topic 开关,为false 时不订阅任何topic spring.jetlinks.auto-sub=true # 根据添加的产品填写订阅产品ID spring.jetlinks.product[0].productId=project-1 #指定订阅topic,默认订阅所有topic spring.jetlinks.product[0].topics=/device/{productId}/*/online,/device/{productId}/*/offline,/device/{productId}/*/message/property/report,/rule-engine/device/alarm/{productId}/** # 根据添加的产品填写订阅产品ID spring.jetlinks.product[1].productId=project—2 #指定订阅topic,默认订阅所有topic spring.jetlinks.product[1].topics=/device/{productId}/*/online,/device/{productId}/*/offline,/device/{productId}/*/message/property/report,/rule-engine/device/alarm/{productId}/** ``` #### 3. 添加订阅TOPIC类型注解 1.添加扫描路径 @IotScan(basePackages = "com.xxx.xx") ```java @SpringBootApplication @IotScan(basePackages = "com.iot.demo.device")//配置事件处理监听扫描目录 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class); } } ``` 2.定义消息处理类 ```java /** * 功能描述 * 设备上线 **/ @Component @MessageListener(JetlinkTopicContants.ONLINE) @Slf4j public class OnlineDeviceHandler { @MessageHandler public void handler(String msg){ log.info("设备上线{}",msg); } } ``` ```java /** * 功能描述 * 设备离线 */ @Component @MessageListener(JetlinkTopicContants.OFFLINE) @Slf4j public class OfflineDeviceHandler { @MessageHandler public void handler(String msg){ log.info("设备离线{}",msg); } } ``` ```java /** * 功能描述 * 属性消息获取 * @date 2021/11/21 16:19 */ @Component @MessageListener(JetlinkTopicContants.REPORT_PROPERTY) @Slf4j public class ReportDeviceHandler { @MessageHandler public void handler(String msg){ log.info("设备消息更新属性数据{}",msg); } } ``` 3.发送消息例子 ```java /** * @author caixiadao * @version 1.0 * 发送消息实例 */ @Component @Slf4j public class MessageHandlerManager { //发送消息处理类 @Autowired SingleEndPointAsyncIotMsgService singleEndPointAsyncIotMsgService; /** * 下发消息到 sendComm功能,命令格式:01 03 04 03 9A 00 EF 9B D4 * @param projectId 产品ID * @throws Exception */ public void sendCommandHandler(String projectId) throws Exception { FunctionInvokeMessage functionInvokeMessage=new FunctionInvokeMessage(); functionInvokeMessage.setFunctionId("sendComm");// 功能ID functionInvokeMessage.addInput(FunctionParameter.builder().name("cmd").value("01 03 04 03 9A 00 EF 9B D4").build()); JetlinkSend js= JetlinkSend.builder() .type("sub").parameter(functionInvokeMessage) .id(UUID.randomUUID().toString()) .topic("/device-message-sender/"+projectId+"/*").build(); Future future= singleEndPointAsyncIotMsgService.send(JSON.toJSONString(js)); } } ``` #### 问题