forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgateway.py
More file actions
348 lines (285 loc) · 12.2 KB
/
gateway.py
File metadata and controls
348 lines (285 loc) · 12.2 KB
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
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import datetime
import json
import os
import socket
import ssl
import time
from time import ctime
import jwt
import paho.mqtt.client as mqtt
# Hostname of '' means using the IP address of the machine.
HOST = ''
PORT = 10000
BUFF_SIZE = 2048
ADDR = (HOST, PORT)
udpSerSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSerSock.setblocking(False)
udpSerSock.bind(ADDR)
class GatewayState:
# This is the topic that the device will receive configuration updates on.
mqtt_config_topic = ''
# Host the gateway will connect to
mqtt_bridge_hostname = ''
mqtt_bridge_port = 8883
# For all PUBLISH messages which are waiting for PUBACK. The key is 'mid'
# returned by publish().
pending_responses = {}
# For all SUBSCRIBE messages which are waiting for SUBACK. The key is
# 'mid'.
pending_subscribes = {}
# for all SUBSCRIPTIONS. The key is subscription topic.
subscriptions = {}
# Indicates if MQTT client is connected or not
connected = False
gateway_state = GatewayState()
# [START iot_mqtt_jwt]
def create_jwt(project_id, private_key_file, algorithm, jwt_expires_minutes):
"""Creates a JWT (https://jwt.io) to establish an MQTT connection.
Args:
project_id: The cloud project ID this device belongs to
private_key_file: A path to a file containing either an RSA256 or
ES256 private key.
algorithm: Encryption algorithm to use. Either 'RS256' or 'ES256'
jwt_expires_minutes: The time in minutes before the JWT expires.
Returns:
An MQTT generated from the given project_id and private key,
which expires in 20 minutes. After 20 minutes, your client will
be disconnected, and a new JWT will have to be generated.
Raises:
ValueError: If the private_key_file does not contain a known
key.
"""
token = {
# The time that the token was issued at
'iat': datetime.datetime.now(tz=datetime.timezone.utc),
# The time the token expires.
'exp': (
datetime.datetime.now(tz=datetime.timezone.utc) +
datetime.timedelta(minutes=jwt_expires_minutes)),
# The audience field should always be set to the GCP project id.
'aud': project_id
}
# Read the private key file.
with open(private_key_file, 'r') as f:
private_key = f.read()
print('Creating JWT using {} from private key file {}'.format(
algorithm, private_key_file))
return jwt.encode(token, private_key, algorithm=algorithm)
# [END iot_mqtt_jwt]
# [START iot_mqtt_config]
def error_str(rc):
"""Convert a Paho error to a human readable string."""
return '{}: {}'.format(rc, mqtt.error_string(rc))
def on_connect(client, unused_userdata, unused_flags, rc):
"""Callback for when a device connects."""
print('on_connect', mqtt.connack_string(rc))
gateway_state.connected = True
# Subscribe to the config topic.
client.subscribe(gateway_state.mqtt_config_topic, qos=1)
def on_disconnect(client, unused_userdata, rc):
"""Paho callback for when a device disconnects."""
print('on_disconnect', error_str(rc))
gateway_state.connected = False
# re-connect
# NOTE: should implement back-off here, but it's a tutorial
client.connect(
gateway_state.mqtt_bridge_hostname, gateway_state.mqtt_bridge_port)
def on_publish(unused_client, userdata, mid):
"""Paho callback when a message is sent to the broker."""
print('on_publish, userdata {}, mid {}'.format(
userdata, mid))
try:
client_addr, message = gateway_state.pending_responses.pop(mid)
udpSerSock.sendto(message.encode(), client_addr)
print('Pending response count {}'.format(
len(gateway_state.pending_responses)))
except KeyError:
print('Unable to find key {}'.format(mid))
def on_subscribe(unused_client, unused_userdata, mid, granted_qos):
print('on_subscribe: mid {}, qos {}'.format(mid, granted_qos))
try:
client_addr, response = gateway_state.pending_subscribes[mid]
udpSerSock.sendto(response.encode(), client_addr)
except KeyError:
print('Unable to find mid: {}'.format(mid))
def on_message(unused_client, unused_userdata, message):
"""Callback when the device receives a message on a subscription."""
payload = message.payload
qos = message.qos
print('Received message \'{}\' on topic \'{}\' with Qos {}'.format(
payload.decode("utf-8"), message.topic, qos))
try:
client_addr = gateway_state.subscriptions[message.topic]
udpSerSock.sendto(payload, client_addr)
print('Sent message to device')
except KeyError:
print('Nobody subscribes to topic {}'.format(message.topic))
def get_client(
project_id, cloud_region, registry_id, gateway_id, private_key_file,
algorithm, ca_certs, mqtt_bridge_hostname, mqtt_bridge_port,
jwt_expires_minutes):
"""Create our MQTT client. The client_id is a unique string that
identifies this device. For Google Cloud IoT Core, it must be in the
format below."""
client_template = 'projects/{}/locations/{}/registries/{}/devices/{}'
client_id = client_template.format(
project_id, cloud_region, registry_id, gateway_id)
client = mqtt.Client(client_id)
# With Google Cloud IoT Core, the username field is ignored, and the
# password field is used to transmit a JWT to authorize the device.
client.username_pw_set(
username='unused',
password=create_jwt(
project_id, private_key_file, algorithm,
jwt_expires_minutes))
# Enable SSL/TLS support.
client.tls_set(ca_certs=ca_certs, tls_version=ssl.PROTOCOL_TLSv1_2)
# Register message callbacks.
# https://eclipse.org/paho/clients/python/docs/
# describes additional callbacks that Paho supports. In this example,
# the callbacks just print to standard out.
client.on_connect = on_connect
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.on_message = on_message
client.on_subscribe = on_subscribe
# Connect to the Google MQTT bridge.
client.connect(mqtt_bridge_hostname, mqtt_bridge_port)
return client
# [END iot_mqtt_config]
def parse_command_line_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description=(
'Example Google Cloud IoT Core MQTT device connection code.'))
parser.add_argument(
'--project_id',
default=os.environ.get('GOOGLE_CLOUD_PROJECT'),
help='GCP cloud project name')
parser.add_argument(
'--registry_id', required=True,
help='Cloud IoT Core registry id')
parser.add_argument(
'--gateway_id', required=True,
help='Cloud IoT Core gateway id')
parser.add_argument(
'--private_key_file',
required=True, help='Path to private key file.')
parser.add_argument(
'--algorithm',
choices=('RS256', 'ES256'),
required=True,
help='Which encryption algorithm to use to generate the JWT.')
parser.add_argument(
'--cloud_region', default='us-central1',
help='GCP cloud region')
parser.add_argument(
'--ca_certs',
default='roots.pem',
help=('CA root from https://pki.google.com/roots.pem'))
parser.add_argument(
'--mqtt_bridge_hostname',
default='mqtt.googleapis.com',
help='MQTT bridge hostname.')
parser.add_argument(
'--mqtt_bridge_port',
choices=(8883, 443),
default=8883,
type=int,
help='MQTT bridge port.')
parser.add_argument(
'--jwt_expires_minutes',
default=1200,
type=int,
help=('Expiration time, in minutes, for JWT tokens.'))
return parser.parse_args()
def attach_device(client, device_id):
attach_topic = '/devices/{}/attach'.format(device_id)
print(attach_topic)
return client.publish(attach_topic, "", qos=1)
def detatch_device(client, device_id):
detach_topic = '/devices/{}/detach'.format(device_id)
print(detach_topic)
return client.publish(detach_topic, "", qos=1)
# [START iot_mqtt_run]
def main():
global gateway_state
args = parse_command_line_args()
gateway_state.mqtt_config_topic = '/devices/{}/config'.format(
parse_command_line_args().gateway_id)
gateway_state.mqtt_bridge_hostname = args.mqtt_bridge_hostname
gateway_state.mqtt_bridge_port = args.mqtt_bridge_hostname
client = get_client(
args.project_id, args.cloud_region, args.registry_id,
args.gateway_id, args.private_key_file, args.algorithm,
args.ca_certs, args.mqtt_bridge_hostname, args.mqtt_bridge_port,
args.jwt_expires_minutes)
while True:
client.loop()
if gateway_state.connected is False:
print('connect status {}'.format(gateway_state.connected))
time.sleep(1)
continue
try:
data, client_addr = udpSerSock.recvfrom(BUFF_SIZE)
except socket.error:
continue
print('[{}]: From Address {}:{} receive data: {}'.format(
ctime(), client_addr[0], client_addr[1], data.decode("utf-8")))
command = json.loads(data.decode('utf-8'))
if not command:
print('invalid json command {}'.format(data))
continue
action = command["action"]
device_id = command["device"]
template = '{{ "device": "{}", "command": "{}", "status" : "ok" }}'
if action == 'event':
print('Sending telemetry event for device {}'.format(device_id))
payload = command["data"]
mqtt_topic = '/devices/{}/events'.format(device_id)
print('Publishing message to topic {} with payload \'{}\''.format(
mqtt_topic, payload))
_, event_mid = client.publish(mqtt_topic, payload, qos=1)
response = template.format(device_id, 'event')
print('Save mid {} for response {}'.format(event_mid, response))
gateway_state.pending_responses[event_mid] = (
client_addr, response)
elif action == 'attach':
_, attach_mid = attach_device(client, device_id)
response = template.format(device_id, 'attach')
print('Save mid {} for response {}'.format(attach_mid, response))
gateway_state.pending_responses[attach_mid] = (
client_addr, response)
elif action == 'detach':
_, detach_mid = detatch_device(client, device_id)
response = template.format(device_id, 'detach')
print('Save mid {} for response {}'.format(detach_mid, response))
gateway_state.pending_responses[detach_mid] = (
client_addr, response)
elif action == "subscribe":
print('subscribe config for {}'.format(device_id))
subscribe_topic = '/devices/{}/config'.format(device_id)
_, mid = client.subscribe(subscribe_topic, qos=1)
response = template.format(device_id, 'subscribe')
gateway_state.subscriptions[subscribe_topic] = (client_addr)
print('Save mid {} for response {}'.format(mid, response))
gateway_state.pending_subscribes[mid] = (client_addr, response)
else:
print('undefined action: {}'.format(action))
print('Finished.')
# [END iot_mqtt_run]
if __name__ == '__main__':
main()