IoT service,mqtt,publish topic different from subscribe topic
来源:互联网 发布:手机python编程软件 编辑:程序博客网 时间:2024/06/05 15:14
万万没想到,万万没想到~~
没想到的是IoT service不仅仅是一个MQTT Broker,”他“还”好心“帮你多做了一件事情,就是topic转换,哈哈,没想到吧,subscribe的topic竟然和publish的topic不一样!
神马?!是的,你没听错!当然,多加topic转换的一步是有一定原因的(至于为什么会有这样的顶层设计,我目前还无暇思考),也是容易实现的,因为app和device都是注册到”他“那里的,app和device的认证信息都在”他“那,做个topic转换还不简单!
至于”MQTT 连接成功订阅不成功“的难题也顺利解决了。因为IoT service不认识你publish的topic,所以MQTTListener不给你subscribe!所以有”一连一断(不断的循环往复connect和disconnect,就是不执行subscribe()方法)“。这个问题是不是socket closed by remote peer呢?好像还不一样。
MQTTGataway回调函数里subscribe的topic如下:
String topic = "iot-2/type/" + MqttUtil.DEFAULT_DEVICE_TYPE+ "/id/+/evt/"
+ MqttUtil.DEFAULT_EVENT_ID + "/fmt/json";
MQTTListener的监听的onPublish中(用来监听device或者sensor端的publish的topic)的topic如下:
Pattern pattern = Pattern.compile("iot-2/type/"
+ MqttUtil.DEFAULT_DEVICE_TYPE + "/id/(.+)/evt/"
+ MqttUtil.DEFAULT_EVENT_ID + "/fmt/json");
XXDevice或者XXSensor端publish的topic如下(重要的事情说三遍,是的,你没看错!):
connection.publish("iot-2/evt/eid/fmt/json", data.getBytes(),
QoS.AT_LEAST_ONCE, false);
终于搞定了,监听端的console log:
debug: Connecting
debug: Transport connected
send: MQTTFrame { type: CONNECT, qos: AT_MOST_ONCE, dup:false }
debug: Logging in
recv: MQTTFrame { type: CONNACK, qos: AT_MOST_ONCE, dup:false }
debug: MQTT login accepted
subscribe topic: iot-2/type/devicecbs1/id/+/evt/eid/fmt/json
send: MQTTFrame { type: SUBSCRIBE, qos: AT_LEAST_ONCE, dup:false }
mqtt connected succeed
recv: MQTTFrame { type: SUBACK, qos: AT_MOST_ONCE, dup:false }
subscribe successfully[B@30ddb86
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
recv: MQTTFrame { type: PUBLISH, qos: AT_LEAST_ONCE, dup:false }
mqtt onPublic----------- :iot-2/type/devicecbs1/id/cbsdevicea/evt/eid/fmt/json
mqtt onPublic----------- :{"type" : "Feature", "geometry": {"coordinates": [0.4079238821522253, 51.58289257363429], "type":"Point"}} }
mqtt save data ....onPublish
send: MQTTFrame { type: PUBACK, qos: AT_MOST_ONCE, dup:false }
----------------------------------saveToDB--------------------------------------- = {"type" : "Feature", "geometry": {"coordinates": [0.4079238821522253, 51.58289257363429], "type":"Point"}} }
save to cloudant DB succeeded!
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
send: MQTTFrame { type: PINGREQ, qos: AT_MOST_ONCE, dup:false }
recv: MQTTFrame { type: PINGRESP, qos: AT_MOST_ONCE, dup:false }
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
recv: MQTTFrame { type: PUBLISH, qos: AT_LEAST_ONCE, dup:false }
mqtt onPublic----------- :iot-2/type/devicecbs1/id/cbsdevicea/evt/eid/fmt/json
mqtt onPublic----------- :{"type" : "Feature", "geometry": {"coordinates": [0.4079238821522253, 51.58289257363429], "type":"Point"}} }
mqtt save data ....onPublish
send: MQTTFrame { type: PUBACK, qos: AT_MOST_ONCE, dup:false }
----------------------------------saveToDB--------------------------------------- = {"type" : "Feature", "geometry": {"coordinates": [0.4079238821522253, 51.58289257363429], "type":"Point"}} }
save to cloudant DB succeeded!
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
send: MQTTFrame { type: PINGREQ, qos: AT_MOST_ONCE, dup:false }
recv: MQTTFrame { type: PINGRESP, qos: AT_MOST_ONCE, dup:false }
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
send: MQTTFrame { type: PINGREQ, qos: AT_MOST_ONCE, dup:false }
recv: MQTTFrame { type: PINGRESP, qos: AT_MOST_ONCE, dup:false }
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
----------------------------------saveToDB--------------------------------------- = null
补充一点:
一般情况下在app端subscribe(是个listener),在device或者sensor端publish。
但是在特殊情况下,可能需要在同一端需要subscribe和publish,这里也说一下。
1. 如果subscribe和publish都在device端:
//Subscribe the Command events
//iot-2/cmd/<cmd-type>/fmt/<format-id>
handler.subscribe("iot-2/cmd/" + MqttUtil.DEFAULT_CMD_ID + "/fmt/json",0);
//Publish device events to the app
//iot-2/evt/<event-id>/fmt/<format>
handler.publish("iot-2/evt/" + MqttUtil.DEFAULT_EVENT_ID
+ "/fmt/json", jsonObj.toString(), false, 0);
2. 如果subscribe和publish都在app端:
//Subscribe Device Events
//iot-2/type/<type-id>/id/<device-id>/evt/<event-id>/fmt/<format-id>
handler.subscribe("iot-2/type/" + MqttUtil.DEFAULT_DEVICE_TYPE
+ "/id/+/evt/" + MqttUtil.DEFAULT_EVENT_ID + "/fmt/json", 0);
//Publish command to one specific device
//iot-2/type/<type-id>/id/<device-id>/cmd/<cmd-id>/fmt/<format-id>
handler.publish("iot-2/type/" + MqttUtil.DEFAULT_DEVICE_TYPE
+ "/id/" + deviceid + "/cmd/" + MqttUtil.DEFAULT_CMD_ID
+ "/fmt/json", jsonObj.toString(), false, 0);
Note:这里仅指IoT service(一种特殊的MQTT Broker)!
- IoT service,mqtt,publish topic different from subscribe topic
- MQTT publish ,Does your topic contain '+' or '#'?
- topic
- MQTT topic匹配规则基础
- MQTT--topic(主题)设计
- ROS Service vs Topic
- service和topic区别
- MQTT topic中的分隔符与通配符
- topic和service的区别
- Publish~Subscribe
- ROS之topic和service通信比较
- Service Broker实现发布-订阅(Publish-Subscribe)框架(3)
- Service Broker实现发布-订阅(Publish-Subscribe)框架
- Traffic topic
- Cplusplus topic
- Topic Model
- JMS Topic
- Topic Model
- Eclipse技巧:[5]eclipse js提示
- 【哈希表除留余数法+BSGS算法求离散对数】POJ Discrete Logging 2417
- Struts2笔记——ONGL表达式语言
- javascript float转int
- Cheering up the Cows,2008nov,usaco
- IoT service,mqtt,publish topic different from subscribe topic
- NOIP 2014 day1第二题 联合权值
- 各种页面高度
- VPN原理-举例说明
- php会话控制
- TCP协议滑动窗口协议以及拥塞控制算法
- socket套接字的使用
- GDT、GDTR、LDT、LDTR的理解 [zz]
- 【ztree系列】完美实现复选框功能