您的位置:首页 > 移动开发

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: