IoT service,mqtt,publish topic different from subscribe topic
2015-08-27 19:59
363 查看
万万没想到,万万没想到~~
没想到的是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 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)!
相关文章推荐
- javascript设计模式之工厂(Factory)模式
- 杭电 HDU ACM 1015 Safecracker (dfs 枚举排列)
- 解决CSS中float:left后需要clear:both清空的繁琐步骤
- 使用CSS实现DIV成半透明效果,CSS实现层与背景半透明效果。
- JavaScript 闭包
- Java 调用 Javascript 函数的范例
- js中substring与substr的学习。
- Web前端成长日记--JQuery选择器的妙用
- ExtJs 中获取 radiobutton 的值
- ExtJs 中获取 radiobutton 的值
- CI框架引入外部css和js文件
- C#【js利用ajax去访问后台代码】
- 优秀的前端开发需要注意的素养
- JavaScript ArrayBuffer浅析
- React学习笔记(4)---react属性与状态
- 各种页面高度
- javascript float转int
- javascript对象的几种创建方式
- css3用图片作边框
- JSF 2 checkboxes example