-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt.py
60 lines (45 loc) · 1.76 KB
/
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
import paho.mqtt.client as mqtt
import uuid
import time
# Test code to trig the gateway to update with mqtt.
# Requirements:
# - pip install paho-mqtt
stopMqttClient = False
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print('Connected with result code '+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe('graphql-gateway/update')
client.publish('graphql-gateway/update', 'Update gateway')
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global stopMqttClient
print(msg.topic+': '+str(msg.payload.decode('utf-8')))
stopMqttClient = True
def on_disconnect(client, userdata: object, rc: int):
print('Disconnected with result code '+str(rc))
def get_client():
clientId = f'mqtt-test-{uuid.uuid1()}'
print(clientId)
client = mqtt.Client(client_id=clientId, transport='websockets')
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
host = 'localhost'
port = 15675
client.ws_set_options(path='/ws', headers=None)
client.username_pw_set('amqp', password='amqp')
client.connect(host, port=port, keepalive=3)
return client
client = get_client()
client.loop_start()
print('Waiting until mqtt message is published and received.')
while not stopMqttClient:
time.sleep(0.1)
client.loop_stop(force=True)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
# client.loop_forever()