-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngcController.py
49 lines (37 loc) · 1.13 KB
/
ngcController.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
# python 3.11
import random
import time
from paho.mqtt import client as mqtt_client
broker = '10.1.25.108'
port = 1883
topic = "ngc/parameter/1/datasrc"
# Generate a Client ID with a random number suffix
client_id = f'controller-{random.randint(0, 1000)}'
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Controller connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, client_id)
# client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def publish(client):
time.sleep(1)
msg = f"https://cataas.com/cat/gif"
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
client.loop_stop()
if __name__ == '__main__':
run()