IoTF service(a special MQTT Broker) Connectivity for Applications and Devices
2015-09-01 15:16
513 查看
这里先说一下特殊情况,可能需要在同一端需要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 theapp
//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 onespecific 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);
其实正是这两个特殊情况,我们可以扩展出来下面的两种情况:
1.订阅端在device,发布端在app;
2.订阅端在App,发布端在device。
Note:实际上,完全可以把两端(设备和服务器)都当做app,或者多当做device,技术上完全是可行的,只要是同一个IoTBroker(其他的MQTTBroker不符合这里的topic规则),就是topic怎么指定的事儿。但是这样有点不符合大众的逻辑思维,一般情况下,用的比较多的scenario是设备采集数据,向服务器端发送数据(在这里叫发布消息),即第2种情况。不论谁是订阅端,在订阅时都要有一个监听器,不断的监听发布端传过来的消息数据,拿到后根据需求做相应处理。
1.Device/Sensor send message to App(订阅端在App,发布端在device):
Subscribingto device events
An application can subscribe toevents from oneor more devices.
Subscribe to topic iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string
Note
The MQTT “any” wildcard character (+) may be usedfor any of the following components if you want to subscribe to more than onetype of event, or events from more than a single device.
device_type
device_id
event_id
format_string
eg. Subscribeto topic iot-2/type/device_type/id/+/evt/event_id/fmt/format_string
Publishingdevice events
An application can publish events asif they camefrom any registered device.
Publish to topic iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string
Fox example:
App end subscribe topic:
String topic =
"iot-2/type/" + MqttUtil.DEFAULT_DEVICE_TYPE +
"/id/+/evt/"+ MqttUtil.DEFAULT_EVENT_ID +
"/fmt/json";
subscribetopic: iot-2/type/devicecbs1/id/+/evt/eid/fmt/json
App end onPublish Listener topic:
Pattern pattern = Pattern.compile("iot-2/type/"
+MqttUtil.DEFAULT_DEVICE_TYPE +
"/id/(.+)/evt/"
+MqttUtil.DEFAULT_EVENT_ID +
"/fmt/json");
mqtt onPublic-----------:iot-2/type/devicecbs1/id/cbsdevicea/evt/eid/fmt/json
//Publish device events tothe app
//iot-2/evt/<event-id>/fmt/<format>
handler.publish("iot-2/evt/"+ MqttUtil.DEFAULT_EVENT_ID
+"/fmt/json", jsonObj.toString(), false, 0);
That means, the device and app to communicate only by “evt”!!
Note:
wildcard character only available in subscribe topic!
Publish topic is not allowed!
2.App send message to Device/Sensor(订阅端在device,发布端在app):
Subscribingto device commands
An application can subscribe tocommands beingsent to one or more devices.
Subscribe to topic: iot-2/type/device_type/id/device_id/cmd/command_id/fmt/format_string
Note
The MQTT “any” wildcard character(+) may be used for any of the following components if you want to subscribe tomore than one type of event, or events from more than a single device.
device_type
device_id
cmd_id
format_string
Publishingdevice commands
An application can publish a commandto anyregistered device.
Publish to topic iot-2/type/device_type/id/device_id/cmd/command_id/fmt/format_string
//Subscribe the Command events
//iot-2/cmd/<cmd-type>/fmt/<format-id>
handler.subscribe("iot-2/cmd/"+ MqttUtil.DEFAULT_CMD_ID + "/fmt/json",0);
references:
https://docs.internetofthings.ibmcloud.com/messaging/applications.html
https://docs.internetofthings.ibmcloud.com/messaging/devices.html
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 theapp
//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 onespecific 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);
其实正是这两个特殊情况,我们可以扩展出来下面的两种情况:
1.订阅端在device,发布端在app;
2.订阅端在App,发布端在device。
Note:实际上,完全可以把两端(设备和服务器)都当做app,或者多当做device,技术上完全是可行的,只要是同一个IoTBroker(其他的MQTTBroker不符合这里的topic规则),就是topic怎么指定的事儿。但是这样有点不符合大众的逻辑思维,一般情况下,用的比较多的scenario是设备采集数据,向服务器端发送数据(在这里叫发布消息),即第2种情况。不论谁是订阅端,在订阅时都要有一个监听器,不断的监听发布端传过来的消息数据,拿到后根据需求做相应处理。
1.Device/Sensor send message to App(订阅端在App,发布端在device):
Subscribingto device events
An application can subscribe toevents from oneor more devices.
Subscribe to topic iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string
Note
The MQTT “any” wildcard character (+) may be usedfor any of the following components if you want to subscribe to more than onetype of event, or events from more than a single device.
device_type
device_id
event_id
format_string
eg. Subscribeto topic iot-2/type/device_type/id/+/evt/event_id/fmt/format_string
Publishingdevice events
An application can publish events asif they camefrom any registered device.
Publish to topic iot-2/type/device_type/id/device_id/evt/event_id/fmt/format_string
Fox example:
App end subscribe topic:
String topic =
"iot-2/type/" + MqttUtil.DEFAULT_DEVICE_TYPE +
"/id/+/evt/"+ MqttUtil.DEFAULT_EVENT_ID +
"/fmt/json";
subscribetopic: iot-2/type/devicecbs1/id/+/evt/eid/fmt/json
App end onPublish Listener topic:
Pattern pattern = Pattern.compile("iot-2/type/"
+MqttUtil.DEFAULT_DEVICE_TYPE +
"/id/(.+)/evt/"
+MqttUtil.DEFAULT_EVENT_ID +
"/fmt/json");
mqtt onPublic-----------:iot-2/type/devicecbs1/id/cbsdevicea/evt/eid/fmt/json
//Publish device events tothe app
//iot-2/evt/<event-id>/fmt/<format>
handler.publish("iot-2/evt/"+ MqttUtil.DEFAULT_EVENT_ID
+"/fmt/json", jsonObj.toString(), false, 0);
That means, the device and app to communicate only by “evt”!!
Note:
wildcard character only available in subscribe topic!
Publish topic is not allowed!
2.App send message to Device/Sensor(订阅端在device,发布端在app):
Subscribingto device commands
An application can subscribe tocommands beingsent to one or more devices.
Subscribe to topic: iot-2/type/device_type/id/device_id/cmd/command_id/fmt/format_string
Note
The MQTT “any” wildcard character(+) may be used for any of the following components if you want to subscribe tomore than one type of event, or events from more than a single device.
device_type
device_id
cmd_id
format_string
Publishingdevice commands
An application can publish a commandto anyregistered device.
Publish to topic iot-2/type/device_type/id/device_id/cmd/command_id/fmt/format_string
//Subscribe the Command events
//iot-2/cmd/<cmd-type>/fmt/<format-id>
handler.subscribe("iot-2/cmd/"+ MqttUtil.DEFAULT_CMD_ID + "/fmt/json",0);
references:
https://docs.internetofthings.ibmcloud.com/messaging/applications.html
https://docs.internetofthings.ibmcloud.com/messaging/devices.html
相关文章推荐
- 【IOS开发】实时显示摄像头内容
- Android中EditTex的密文和明文切换
- Android微信支付快速集成案例及有效解决返回值-1
- 关于android中BaseAdapter的理解
- Unity学习疑问记录之Quaternion
- Cocos2d-x从入门到精通第八课《坐标系统》
- 25条提升iOS App性能的建议
- swift 总结3
- Unity3D架构系列之- FSM有限状态机设计六(总结)
- Android 自定义ViewGroup手把手教你实现ArcMenu
- Android富文本图文混排、粗体、斜体、图片、下划线、删除线等
- Android OnLowMemory和OnTrimMemory
- Android OnLowMemory和OnTrimMemory
- Android OnLowMemory和OnTrimMemory
- Unity3D架构系列之- FSM有限状态机设计五
- 女神画廊App (Swift1.2)
- TUTK p2p视频流H.264 iOS平台解码成功
- 【腾讯开放平台】Android、IOS实现指定QQ临时会话功能
- Unity3D架构系列之- FSM有限状态机设计四
- 为什么你的APP不如人?因为你没有让它“活”起来