-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt_clock_client.py
118 lines (97 loc) · 3.78 KB
/
mqtt_clock_client.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
import chess
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
group = 'capture_queen'
# 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("capture_queen.turn")
client.subscribe("capture_queen.reset_pi")
client.subscribe("capture_queen.position")
client.subscribe("capture_queen.goback")
client.subscribe("capture_queen.goforward")
client.subscribe("capture_queen.quit")
client.subscribe("capture_queen.resign")
client.subscribe("capture_queen.draw")
client.subscribe("capture_queen.underpromote")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
for cb in subscribers:
cb(msg)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
#client.connect("192.168.212.186", 1883, keepalive=60)
#client.connect("192.168.7.130", 1883, keepalive=60)
client.connect("localhost", 1883, keepalive=60)
# 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.
#publish.single(group + '.reset_clock', "", hostname="localhost")
def mqtt_start():
### non-blocking threaded mqtt loop
client.loop_start()
def mqtt_goback():
payload = 1
publish.single(group + '.goback', payload, hostname="localhost")
def mqtt_goforward():
payload = 1
publish.single(group + '.goforward', payload, hostname="localhost")
def mqtt_draw():
payload = 'draw'
publish.single(group + '.draw', payload, hostname="localhost")
def mqtt_resign(color):
payload = color
publish.single(group + '.resign', payload, hostname="localhost")
def mqtt_game_over(result):
payload = result
publish.single(group + '.game_over', payload, hostname="localhost")
def mqtt_quit():
payload = 1
publish.single(group + '.quit', payload, hostname="localhost")
subscribers = []
def mqtt_subscribe(callback):
subscribers.append(callback)
def mqtt_publish_position(fen, lastmove_uci=None):
payload = f'{lastmove_uci}//{fen}'
publish.single(group + '.position', payload, hostname="localhost")
def mqtt_sethalfmove(halfmove):
publish.single(group + '.sethalfmove', halfmove,
hostname="localhost")
def mqtt_setblack_ms(ms):
publish.single(group + '.setblack_ms', ms,
hostname="localhost")
def mqtt_setwhite_ms(ms):
publish.single(group + '.setwhite_ms', ms,
hostname="localhost")
def mqtt_clock_pause(paused):
paused = int(paused)
publish.single(group + '.paused', paused,
hostname="localhost")
def mqtt_underpromote_to(piece):
print(f'mqtt_underpromote_to("{piece}")')
publish.single(group + '.underpromote', piece,
hostname="localhost")
def mqtt_clock_reset(initial, increment):
publish.single(group + '.initial_seconds', initial,
hostname="localhost")
publish.single(group + '.increment_seconds', increment,
hostname="localhost")
publish.single(group + '.reset', 1, hostname="localhost")
print('mqtt_clock reset')
class MqttRenderer:
def render(board, side, colors=None):
'''
Colors not used
'''
if board is None:
board = chess.Board()
fen = board.fen()
if len(board.move_stack) > 0:
lastmove_uci = board.move_stack[-1].uci()
else:
lastmove_uci = None
mqtt_publish_position(fen, lastmove_uci)