-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtt_board_client.py
57 lines (51 loc) · 1.94 KB
/
mqtt_board_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
'''
Used to display game on local mqtt network
-- write board.svg file displying current position and last move
'''
import chess
import chess.svg
import paho.mqtt.client as mqtt
import defaults
board = chess.Board()
board.push_uci("e2e4")
#pgr = pygame_render.PygameRender(640)
#pgr.render(board, False, colors=defaults.colors)
svg = chess.svg.board(board,size=640, flipped=False,
colors=defaults.colors)
open(".board.svg", 'w').write(svg)
class MqttMessageHandler:
def __init__(self):
self.board = chess.Board()
self.colors = defaults.colors
def on_connect(self, client, userdata=None, flags=None, rc=None):
client.subscribe("capture_queen.reset")
client.subscribe('capture_queen.position')
print('connected')
def on_message(self, client, userdata, msg):
topic = msg.topic
subtopic = topic.split('.')[1]
print(topic) # , msg.payload)
if subtopic == 'reset':
self.board = chess.Board()
if subtopic == 'position':
payload = msg.payload.decode('utf-8')
lastmove, fen = payload.split('//')
if lastmove != 'None':
lastmove = chess.Move(chess.parse_square(lastmove[:2]),
chess.parse_square(lastmove[2:4]))
else:
lastmove = None
board.set_fen(fen)
svg = chess.svg.board(board,size=640, flipped=False,
lastmove=lastmove,
colors=self.colors)
open("board.svg", 'w').write(svg)
#print('wrote .board.svg')
client = mqtt.Client()
handler = MqttMessageHandler()
client.on_connect = handler.on_connect
client.on_message = handler.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)
client.loop_forever()