-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_iot_mqtt.py
395 lines (333 loc) · 11.6 KB
/
cloud_iot_mqtt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
To connect the device you must have downloaded Google's CA root certificates,
and a copy of your private key file. Run this script with the corresponding
algorithm flag.
$ python cloud_iot_mqtt.py \
--project_id=my-project-id \
--registry_id=example-my-registry-id \
--device_id=my-device-id \
--private_key_file=rsa_private.pem \
--algorithm=RS256
With a single server, you can run multiple instances of the device with
different device ids, and the server will distinguish them. Try creating a few
devices and running them all at the same time.
"""
import argparse
import datetime
import json
import logging
import os
import random
import ssl
import time
from dataclasses import dataclass
from contextlib import contextmanager
from random import randint
import jwt
import paho.mqtt.client as mqtt
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(module)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
logger = logging.getLogger(__name__)
def create_jwt(project_id, private_key_file, algorithm):
"""
Creates a JWT to establish an MQTT connection.
"""
token = {
"iat": datetime.datetime.utcnow(),
"exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=60),
"aud": project_id
}
with open(private_key_file, "r") as f:
private_key = f.read()
logger.debug(f"Creating JWT using '{algorithm}' "
f"from private key file '{private_key_file}'")
return jwt.encode(token, private_key, algorithm=algorithm)
def error_str(rc):
"""
Converts a Paho error to a human readable string.
"""
return f"{rc}: {mqtt.error_string(rc)}"
class Device:
"""
Represents the state of a single device.
"""
# The maximum backoff time before giving up, in seconds.
MAX_BACKOFF_TIME = 32
def __init__(self, project_id, cloud_region, registry_id, device_id):
# params that form client_id
self._project_id = project_id
self._cloud_region = cloud_region
self._registry_id = registry_id
self._device_id = device_id
self._client = mqtt.Client(client_id=self.client_id)
self._client.on_connect = self.on_connect
self._client.on_publish = self.on_publish
self._client.on_disconnect = self.on_disconnect
self._client.on_subscribe = self.on_subscribe
self._client.on_message = self.on_message
# Whether to wait with exponential backoff before publishing.
self._should_backoff = True
# The initial backoff time after a disconnection occurs, in seconds.
self._min_backoff_time = 1
@classmethod
def create_from_client_id(cls, client_id):
"""
Creates a Device from client_id string.
:param client_id: client_id as a string.
"""
return cls(*client_id.split("/")[1::2])
@property
def client_id(self):
return (
f"projects/{self._project_id}/"
f"locations/{self._cloud_region}/"
f"registries/{self._registry_id}/"
f"devices/{self._device_id}"
)
@property
def project_id(self):
return self._project_id
@property
def cloud_region(self):
return self._cloud_region
@property
def registry_id(self):
return self._registry_id
@property
def device_id(self):
return self._device_id
def authenticate(self, token):
self._client.username_pw_set(username="unused", password=token)
def tls_set(self, ca_certs, tls_version, **kwargs):
self._client.tls_set(
ca_certs=ca_certs,
tls_version=tls_version,
**kwargs
)
def connect(self, mqtt_bridge_hostname, mqtt_bridge_port):
logger.info(f"Connecting... {mqtt_bridge_hostname}:{mqtt_bridge_port}")
if self._should_backoff:
# If backoff time is too large, give up.
if self._min_backoff_time > self.MAX_BACKOFF_TIME:
logger.error(
f"Exceeded maximum backoff time for the device with "
f"ID '{self.device_id}'. Giving up."
)
return
delay = self._min_backoff_time + random.randint(0, 1000) / 1000.0
time.sleep(delay)
self._min_backoff_time *= 2
self._client.connect(
host=mqtt_bridge_hostname,
port=mqtt_bridge_port
)
def disconnect(self):
self._client.disconnect()
def publish(self, topic, payload=None, qos=0, retain=False, properties=None): # noqa
self._client.publish(
topic,
payload=payload,
qos=qos,
retain=retain,
properties=properties
)
def subscribe(self, topic, qos=0, options=None, properties=None):
self._client.subscribe(
topic,
qos=qos,
options=options,
properties=properties
)
def loop_start(self):
self._client.loop_start()
def loop_stop(self):
self._client.loop_stop()
def on_connect(self, unused_client, unused_userdata, unused_flags, rc):
"""
Callback for when a device connects.
"""
logger.info(f"on_connect {mqtt.connack_string(rc)}")
# After a successful connect, reset backoff time and stop backing off.
self._should_backoff = False
self._min_backoff_time = 1
def on_disconnect(self, unused_client, unused_userdata, rc):
"""
Callback for when a device disconnects.
"""
logger.info(f"Disconnected: {error_str(rc)}")
# Since a disconnect occurred, the next loop iteration will wait with
# exponential backoff.
self._should_backoff = True
def on_publish(self, unused_client, unused_userdata, unused_mid):
"""
Callback when the device receives a PUBACK from the MQTT bridge.
"""
logger.info('Published message acked.')
def on_subscribe(self, unused_client, unused_userdata, unused_mid, granted_qos): # noqa
"""
Callback when the device receives a SUBACK from the MQTT bridge.
"""
logger.info(f"Subscribed: {granted_qos}")
if granted_qos[0] == 128:
logger.info("Subscription failed.")
def on_message(self, unused_client, unused_userdata, message):
"""
Callback when the device receives a message on a subscription.
"""
payload = message.payload.decode("utf-8")
logger.info(
f"Received message '{payload}' on topic '{message.topic}' "
f"with Qos {str(message.qos)}."
)
# The device will receive its latest config when it subscribes to the
# config topic. If there is no configuration for the device, the device
# will receive a config with an empty payload.
if not payload:
logger.info("No payload provided.")
return
# The config is passed in the payload of the message.
data = json.loads(payload)
logger.info(f"Received payload: {data}")
def parse_command_line_args():
"""
Parse command line arguments.
"""
parser = argparse.ArgumentParser(
description="Fake Google Cloud IoT MQTT device."
)
parser.add_argument(
"--project_id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT"),
required=True,
help="GCP cloud project name."
)
parser.add_argument(
"--registry_id",
required=True,
help="Cloud IoT registry ID."
)
parser.add_argument(
"--device_id",
required=True,
help="Cloud IoT device id"
)
parser.add_argument(
"--private_key_file",
required=True,
help="Path to private key file."
)
parser.add_argument(
"--algorithm",
choices=("RS256", "ES256"),
default="RS256",
help="Encryption algorithm to generate the JWT. Defaults to RS256."
)
parser.add_argument(
"--cloud_region",
default="us-central1",
help="GCP Cloud Region"
)
parser.add_argument(
"--ca_certs",
default="roots.pem",
help="CA root certificate. Get from https://pki.google.com/roots.pem"
)
parser.add_argument(
"--num_messages",
type=int,
default=20,
help="Number of messages to publish.")
parser.add_argument(
"--mqtt_bridge_hostname",
default="mqtt.googleapis.com",
help="MQTT bridge hostname."
)
parser.add_argument(
"--mqtt_bridge_port",
type=int,
default=8883,
help="MQTT bridge port."
)
parser.add_argument(
"-v", "--verbose",
action="store_true",
help="increase output verbosity"
)
return parser.parse_args()
@dataclass
class MqttConnectionConfig:
ca_certs: str
tls_version: int = ssl.PROTOCOL_TLSv1_2
mqtt_bridge_hostname: str = "mqtt.googleapis.com"
mqtt_bridge_port: int = 8883
@contextmanager
def managed_device(
client_id: str,
conn_cfg: MqttConnectionConfig,
token: str = None
):
device = Device.create_from_client_id(client_id)
if token:
device.authenticate(token)
if conn_cfg.ca_certs:
device.tls_set(
ca_certs=conn_cfg.ca_certs,
tls_version=conn_cfg.tls_version
)
device.connect(
conn_cfg.mqtt_bridge_hostname,
conn_cfg.mqtt_bridge_port
)
device.loop_start()
yield device
device.disconnect()
device.loop_stop()
def main():
args = parse_command_line_args()
if args.verbose:
logger.setLevel(logging.DEBUG)
client_id = (f"projects/{args.project_id}/"
f"locations/{args.cloud_region}/"
f"registries/{args.registry_id}/"
f"devices/{args.device_id}")
conn_cfg = MqttConnectionConfig(
ca_certs=args.ca_certs,
tls_version=ssl.PROTOCOL_TLSv1_2,
mqtt_bridge_hostname=args.mqtt_bridge_hostname,
mqtt_bridge_port=args.mqtt_bridge_port
)
token = create_jwt(args.project_id, args.private_key_file, args.algorithm)
# This is the topic that the device will publish telemetry events
# (e.g. temperature data, power consumption etc.) to.
mqtt_telemetry_topic = f"/devices/{args.device_id}/events"
# This is the topic that the device will receive configuration updates on.
mqtt_config_topic = f"/devices/{args.device_id}/config"
# This is the topic that the device will receive commands from IoT Core.
mqtt_commands_topic = f"/devices/{args.device_id}/commands/#"
with managed_device(client_id, conn_cfg, token) as device:
# Subscribe to the config topic.
device.subscribe(mqtt_config_topic, qos=1)
# Subscribe to the commands topic
device.subscribe(mqtt_commands_topic, qos=1)
# Update and publish telemetry readings.
for i in range(1, args.num_messages + 1):
payload = f"{device.registry_id}/{device.device_id}-payload-{i}"
logger.info(
f"Publishing message '{i}' '{args.num_messages}': '{payload}'"
)
device.publish(mqtt_telemetry_topic, payload, qos=1)
# Send events every second.
time.sleep(randint(1, 4))
# This is the topic that the device will send state updates on.
mqtt_state_topic = f"/devices/{device.device_id}/state"
payload = "Fake state"
logger.info(
f"Publishing {device.device_id} STATE: '{payload}'"
)
device.publish(mqtt_state_topic, payload)
logger.info("Finished loop successfully. Goodbye!")
if __name__ == "__main__":
main()