-
Notifications
You must be signed in to change notification settings - Fork 3
/
mqttexample.gd
114 lines (95 loc) · 4.14 KB
/
mqttexample.gd
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
extends Control
var receivedmessagecount = 0
func _ready():
connectedactionsactive(false)
func _on_brokerprotocol_item_selected(index):
# port defaults for [tcp, ssl, ws, wss] used at https://test.mosquitto.org/
$VBox/HBoxBroker/brokerport.text = "%d" % ([ 1883, 8886, 8080, 8081 ][index])
func _on_button_connect_toggled(button_pressed):
if button_pressed:
randomize()
$MQTT.client_id = "s%d" % randi()
if $VBox/HBoxLastwill/lastwilltopic.text:
$MQTT.set_last_will($VBox/HBoxLastwill/lastwilltopic.text,
$VBox/HBoxLastwill/lastwillmessage.text,
$VBox/HBoxLastwill/lastwillretain.button_pressed)
else:
$MQTT.set_last_will("", "", false)
if $VBox/HBoxBroker/brokeruser.text:
$MQTT.set_user_pass($VBox/HBoxBroker/brokeruser.text,
$VBox/HBoxBroker/brokerpswd.text)
else:
$MQTT.set_user_pass(null, null)
$VBox/HBoxBrokerControl/status.text = "connecting..."
var brokerurl = $VBox/HBoxBroker/brokeraddress.text
var protocol = $VBox/HBoxBroker/brokerprotocol.get_item_text($VBox/HBoxBroker/brokerprotocol.selected)
var retval = $MQTT.connect_to_broker("%s%s:%s" % [protocol, brokerurl, $VBox/HBoxBroker/brokerport.text])
if not retval:
$VBox/HBoxBrokerControl/status.text = "error."
else:
#$VBox/HBoxBrokerControl/status.text = "disconnecting..."
$MQTT.disconnect_from_server()
func brokersettingsactive(active):
$VBox/HBoxBroker/brokeraddress.editable = active
$VBox/HBoxBroker/brokerport.editable = active
$VBox/HBoxBroker/brokerprotocol.disabled = not active
$VBox/HBoxLastwill/lastwilltopic.editable = active
$VBox/HBoxLastwill/lastwillmessage.editable = active
$VBox/HBoxLastwill/lastwillretain.disabled = not active
$VBox/HBoxBrokerControl/ButtonConnect.button_pressed = not active
func connectedactionsactive(active):
$VBox/HBoxSubscriptions/subscribetopic.editable = active
$VBox/HBoxSubscriptions/subscribe.disabled = not active
$VBox/HBoxPublish/publishtopic.editable = active
$VBox/HBoxPublish/publishmessage.editable = active
$VBox/HBoxPublish/publishretain.disabled = not active
$VBox/HBoxPublish/publish.disabled = not active
if not active:
$VBox/HBoxSubscriptions/subscriptions.clear()
$VBox/HBoxSubscriptions/subscriptions.disabled = true
$VBox/HBoxSubscriptions/unsubscribe.disabled = true
func _on_mqtt_broker_connected():
$VBox/HBoxBrokerControl/status.text = "connected."
brokersettingsactive(false)
connectedactionsactive(true)
receivedmessagecount = 0
func _on_mqtt_broker_disconnected():
$VBox/HBoxBrokerControl/status.text = "disconnected."
brokersettingsactive(true)
connectedactionsactive(false)
func _on_mqtt_broker_connection_failed():
$VBox/HBoxBrokerControl/status.text = "failed."
brokersettingsactive(true)
connectedactionsactive(false)
func _on_mqtt_received_message(topic, message):
if receivedmessagecount == 0:
$VBox/subscribedmessages.clear()
receivedmessagecount += 1
$VBox/subscribedmessages.append_text("[b]%s[/b] %s\n" % [topic, message])
func _on_publish_pressed():
var qos = 0
$MQTT.publish($VBox/HBoxPublish/publishtopic.text,
$VBox/HBoxPublish/publishmessage.text,
$VBox/HBoxPublish/publishretain.button_pressed,
qos)
func _on_subscribe_pressed():
var qos = 0
var topic = $VBox/HBoxSubscriptions/subscribetopic.text.strip_edges()
$MQTT.subscribe(topic, qos)
for i in range($VBox/HBoxSubscriptions/subscriptions.item_count):
if topic == $VBox/HBoxSubscriptions/subscriptions.get_item_text(i):
return
$VBox/HBoxSubscriptions/subscriptions.add_item(topic)
$VBox/HBoxSubscriptions/subscriptions.select($VBox/HBoxSubscriptions/subscriptions.item_count - 1)
$VBox/HBoxSubscriptions/subscriptions.disabled = false
$VBox/HBoxSubscriptions/unsubscribe.disabled = false
func _on_unsubscribe_pressed():
var seloptbutt = $VBox/HBoxSubscriptions/subscriptions
var sel = seloptbutt.selected
var topic = seloptbutt.get_item_text(sel)
$MQTT.unsubscribe(topic)
seloptbutt.remove_item(seloptbutt.selected)
$VBox/HBoxSubscriptions/subscriptions.disabled = (seloptbutt.item_count == 0)
$VBox/HBoxSubscriptions/unsubscribe.disabled = (seloptbutt.item_count == 0)
if seloptbutt.item_count != 0:
seloptbutt.select(min(sel, seloptbutt.item_count - 1))