|
| 1 | +* [Event Reporting and Multi-Event Reporting](#Event-Reporting-and-Multi-Event-Reporting) |
| 2 | + * [Publishing to topic for reporting event](#Publishing-to-topic-for-reporting-event) |
| 3 | + * [Publishing to topic for reporting multiple events](#Publishing-to-topic-for-reporting-multiple-events) |
| 4 | + |
| 5 | +# Event Reporting and Multi-Event Reporting |
| 6 | + |
| 7 | +This document describes how a device publishes to topics for event reporting and multi-event reporting. |
| 8 | + |
| 9 | +## Publishing to topic for reporting event |
| 10 | + |
| 11 | +Run [TemplateSample.py](../../explorer/sample/template/example_template.py). After the device is connected successfully, it will initialize the data template, call the `templateEventPost()` API for event reporting, and publish to the event topic: |
| 12 | +`$thing/up/event/{ProductID}/{DeviceName}` |
| 13 | + |
| 14 | +Below is the sample code: |
| 15 | +```python |
| 16 | +# Construct QcloudExplorer |
| 17 | +qcloud = QcloudExplorer(device_file="explorer/sample/device_info.json", tls=True) |
| 18 | +# Initialize the log |
| 19 | +logger = qcloud.logInit(qcloud.LoggerLevel.DEBUG, enable=True) |
| 20 | + |
| 21 | +# Register the MQTT callback |
| 22 | +qcloud.registerMqttCallback(on_connect, on_disconnect, |
| 23 | + on_message, on_publish, |
| 24 | + on_subscribe, on_unsubscribe) |
| 25 | +# Get the product ID and device name |
| 26 | +product_id = qcloud.getProductID() |
| 27 | +device_name = qcloud.getDeviceName() |
| 28 | + |
| 29 | +# Connect to MQTT |
| 30 | +qcloud.connect() |
| 31 | + |
| 32 | +# Initialize the data template |
| 33 | +qcloud.templateInit(product_id, device_name, on_template_property, |
| 34 | + on_template_action, on_template_event, on_template_service) |
| 35 | +qcloud.templateSetup(product_id, device_name, "sample/template/template_config.json") |
| 36 | + |
| 37 | +# Construct an event |
| 38 | +timestamp = int(round(time.time() * 1000)) |
| 39 | +event = { |
| 40 | + "events": [ |
| 41 | + { |
| 42 | + "eventId": "status_report", |
| 43 | + "type": "info", |
| 44 | + "timestamp": timestamp, |
| 45 | + "params": { |
| 46 | + "status":0, |
| 47 | + "message":"event test" |
| 48 | + } |
| 49 | + } |
| 50 | + ] |
| 51 | +} |
| 52 | +# Report the event |
| 53 | +qcloud.templateEventPost(product_id, device_name, event) |
| 54 | + |
| 55 | +# Disconnect from MQTT |
| 56 | +qcloud.disconnect() |
| 57 | +``` |
| 58 | + |
| 59 | +Observe the output log. |
| 60 | +``` |
| 61 | +2021-07-21 16:59:37,208.208 [log.py:35] - DEBUG - [event post] {'events': [{'eventId': 'status_report', 'type': 'info', 'timestamp': 1626857977209, 'params': {'status': 0, 'message': 'event test'}}]} |
| 62 | +2021-07-21 16:59:37,209.209 [client.py:2165] - DEBUG - Sending PUBLISH (d0, q1, r0, m5), 'b'$thing/up/event/xxx/dev1'', ... (182 bytes) |
| 63 | +2021-07-21 16:59:37,209.209 [log.py:35] - DEBUG - publish success |
| 64 | +2021-07-21 16:59:37,261.261 [client.py:2165] - DEBUG - Received PUBACK (Mid: 5) |
| 65 | +2021-07-21 16:59:37,262.262 [log.py:35] - DEBUG - on_publish:mid:5,userdata:None |
| 66 | +2021-07-21 16:59:37,286.286 [client.py:2165] - DEBUG - Received PUBLISH (d0, q0, r0, m0), '$thing/down/event/xxx/dev1', ... (85 bytes) |
| 67 | +2021-07-21 16:59:37,287.287 [log.py:35] - DEBUG - product_1:on_template_event:payload:{'method': 'events_reply', 'clientToken': 'xxx-0', 'code': 0, 'status': '', 'data': {}},userdata:None |
| 68 | +``` |
| 69 | +The above log represents the process in which the device publishes to the topic for reporting a single event successfully. As can be seen, the device publishes an event successfully and receives the `event_reply` from the cloud. In the information of the device created in the console, you can view the corresponding device event. If the `type` passed in is `info`, the event is of the information type. For more information on how to view device events in the console, please see [Device Debugging](https://cloud.tencent.com/document/product/1081/34741). |
| 70 | + |
| 71 | +## Publishing to topic for reporting multiple events |
| 72 | + |
| 73 | +Run [TemplateSample.py](../../explorer/sample/template/example_template.py). After the device is connected successfully, it will initialize the data template, call the `templateEventPost()` API for event reporting, and publish to the event topic: |
| 74 | +`$thing/up/event/{ProductID}/{DeviceName}` |
| 75 | + |
| 76 | +Below is the sample code: |
| 77 | +```python |
| 78 | +# Construct a JSON message |
| 79 | +def report_json_construct_events(event_list): |
| 80 | + # deal events and add your real value |
| 81 | + status = 1 |
| 82 | + message = "test" |
| 83 | + voltage = 20.0 |
| 84 | + name = "memory" |
| 85 | + error_code = 0 |
| 86 | + timestamp = int(round(time.time() * 1000)) |
| 87 | + |
| 88 | + format_string = '"%s":"%s",' |
| 89 | + format_int = '"%s":%d,' |
| 90 | + events = [] |
| 91 | + for event in event_list: |
| 92 | + string = '{' |
| 93 | + string += format_string % ("eventId", event.event_name) |
| 94 | + string += format_string % ("type", event.type) |
| 95 | + string += format_int % ("timestamp", timestamp) |
| 96 | + string += '"params":{' |
| 97 | + for prop in event.events_prop: |
| 98 | + if (prop.type == "int" or prop.type == "float" |
| 99 | + or prop.type == "bool" or prop.type == "enum"): |
| 100 | + if prop.key == "status": |
| 101 | + string += format_int % (prop.key, status) |
| 102 | + elif prop.key == "voltage": |
| 103 | + string += format_int % (prop.key, voltage) |
| 104 | + elif prop.key == "error_code": |
| 105 | + string += format_int % (prop.key, error_code) |
| 106 | + elif prop.type == "string": |
| 107 | + if prop.key == "message": |
| 108 | + string += format_string % (prop.key, message) |
| 109 | + elif prop.key == "name": |
| 110 | + string += format_string % (prop.key, name) |
| 111 | + |
| 112 | + string = string[:len(string) - 1] |
| 113 | + string += "}}" |
| 114 | + events.append(json.loads(string)) |
| 115 | + |
| 116 | + json_out = '{"events":%s}' % json.dumps(events) |
| 117 | + |
| 118 | + return json.loads(json_out) |
| 119 | + |
| 120 | +# Construct QcloudExplorer |
| 121 | +qcloud = QcloudExplorer(device_file="explorer/sample/device_info.json", tls=True) |
| 122 | +# Initialize the log |
| 123 | +logger = qcloud.logInit(qcloud.LoggerLevel.DEBUG, enable=True) |
| 124 | + |
| 125 | +# Register the MQTT callback |
| 126 | +qcloud.registerMqttCallback(on_connect, on_disconnect, |
| 127 | + on_message, on_publish, |
| 128 | + on_subscribe, on_unsubscribe) |
| 129 | +# Get the product ID and device name |
| 130 | +product_id = qcloud.getProductID() |
| 131 | +device_name = qcloud.getDeviceName() |
| 132 | + |
| 133 | +# Connect to MQTT |
| 134 | +qcloud.connect() |
| 135 | + |
| 136 | +# Initialize the data template |
| 137 | +qcloud.templateInit(product_id, device_name, on_template_property, |
| 138 | + on_template_action, on_template_event, on_template_service) |
| 139 | +qcloud.templateSetup(product_id, device_name, "sample/template/template_config.json") |
| 140 | + |
| 141 | +# Get the event list from the configuration file |
| 142 | +event_list = qcloud.getEventsList(product_id, device_name) |
| 143 | +# Construct a JSON event structure based on the event list |
| 144 | +events = report_json_construct_events(event_list) |
| 145 | +# Report the event |
| 146 | +qcloud.templateEventPost(product_id, device_name, events) |
| 147 | + |
| 148 | +# Disconnect from MQTT |
| 149 | +qcloud.disconnect() |
| 150 | +``` |
| 151 | + |
| 152 | +Observe the output log. |
| 153 | +``` |
| 154 | +2021-07-21 16:48:28,464.464 [log.py:35] - DEBUG - [event post] {'events': [{'eventId': 'status_report', 'type': 'info', 'timestamp': 1626857308464, 'params': {'status': 1, 'message': 'test'}}, {'eventId': 'low_voltage', 'type': 'alert', 'timestamp': 1626857308464, 'params': {'voltage': 20}}, {'eventId': 'hardware_fault', 'type': 'fault', 'timestamp': 1626857308464, 'params': {'name': 'memory', 'error_code': 0}}]} |
| 155 | +2021-07-21 16:48:28,464.464 [client.py:2165] - DEBUG - Sending PUBLISH (d0, q1, r0, m5), 'b'$thing/up/event/xxx/dev1'', ... (409 bytes) |
| 156 | +2021-07-21 16:48:28,464.464 [log.py:35] - DEBUG - publish success |
| 157 | +2021-07-21 16:48:28,508.508 [client.py:2165] - DEBUG - Received PUBACK (Mid: 5) |
| 158 | +2021-07-21 16:48:28,508.508 [log.py:35] - DEBUG - on_publish:mid:5,userdata:None |
| 159 | +2021-07-21 16:48:28,538.538 [client.py:2165] - DEBUG - Received PUBLISH (d0, q0, r0, m0), '$thing/down/event/xxx/dev1', ... (85 bytes) |
| 160 | +2021-07-21 16:48:28,539.539 [log.py:35] - DEBUG - on_template_event:payload:{'method': 'events_reply', 'clientToken': 'xxx-0', 'code': 0, 'status': '', 'data': {}},userdata:None |
| 161 | +``` |
| 162 | +The above log represents the process in which the device publishes to the topic for reporting multiple events successfully. As can be seen, the device publishes events successfully and receives the `events_reply` from the cloud. In the information of the device created in the console, you can view the corresponding device events. If the `type` passed in is `info`, the event is of the information type; if `alert`, the alarm type; if `fault`, the fault type. For more information on how to view device events in the console, please see [Device Debugging](https://cloud.tencent.com/document/product/1081/34741). |
0 commit comments