mqtt-to-flipdot/mqtt-to-flipdot.py

115 lines
3.7 KiB
Python
Raw Normal View History

2016-12-23 00:33:23 +00:00
#!/usr/bin/env python3
import time
import paho.mqtt.client as mqtt
from FlipdotSender import FlipdotSender
import time
2017-01-27 18:21:49 +00:00
from hangman import Hangman
global mode
mode="standby"
2017-02-16 21:30:01 +00:00
global gametimeout
gametimeout=0
2016-12-23 00:33:23 +00:00
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
2017-01-27 18:21:49 +00:00
client.subscribe("raum2/flipdot/#")
#client.subscribe("raum2/flipdot/text")
#client.subscribe("raum2/flipdot/scroll")
#client.subscribe("raum2/flipdot/image")
2016-12-23 00:33:23 +00:00
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload.decode("utf-8")))
2017-01-27 18:21:49 +00:00
global mode
2017-02-16 21:30:01 +00:00
global gametimeout
2016-12-23 00:33:23 +00:00
2017-01-27 18:21:49 +00:00
if mode=="standby":
2017-02-16 21:30:01 +00:00
gametimeout=0
2017-01-27 18:21:49 +00:00
if msg.topic == "raum2/flipdot/scroll/set":
payload = msg.payload.decode("utf-8")
2016-12-23 01:39:36 +00:00
2017-02-16 21:30:01 +00:00
if len(payload)>0 and (payload[0]).isdigit():
2017-01-27 18:21:49 +00:00
speed = int(payload[0])
text = payload[1:]
else:
speed = 3
text = payload
flipdot.send_marquee(text, speed)
if msg.topic == "raum2/flipdot/text/set":
payload = msg.payload.decode("utf-8")
2017-02-16 21:30:01 +00:00
if len(payload)>0 and payload[0]=='~':
payload=payload[1:] #remove first char
flipdot.send_text(payload,25) #send_text with animation
flipdot.send_text(payload) #without animation
if msg.topic == "raum2/flipdot/textFull/set":
payload = msg.payload.decode("utf-8")
if len(payload)>0 and payload[0]=='~':
payload=payload[1:]
flipdot.send_textFull(payload,50)
flipdot.send_textFull(payload)
2017-01-27 18:21:49 +00:00
if msg.topic == "raum2/flipdot/image/set":
payload = msg.payload.decode("utf-8")
print(payload)
flipdot.send_bytes(payload)
if msg.topic == "raum2/flipdot/hangman/set":
2017-01-17 22:28:21 +00:00
payload = msg.payload.decode("utf-8")
2016-12-23 00:33:23 +00:00
2017-01-27 18:21:49 +00:00
if payload=="#start":
hangman.setup()
2017-02-16 21:30:01 +00:00
gametimeout=int(round(time.time() * 1000))
2017-01-27 18:21:49 +00:00
client.publish("raum2/flipdot/hangman","started")
mode="hangman"
elif (mode=="hangman") and (len(payload)>0): #try entered character
2017-02-16 21:30:01 +00:00
gametimeout=int(round(time.time() * 1000))
2017-01-27 18:21:49 +00:00
trychar=payload[-1]
gamestatus=hangman.step(trychar)
2017-02-16 21:30:01 +00:00
client.publish("raum2/flipdot/hangman","char="+trychar.upper())
2017-01-27 18:21:49 +00:00
if gamestatus!=1: #game has ended
mode="standby"
if gamestatus==2: #gameover
client.publish("raum2/flipdot/hangman","gameover")
elif gamestatus==3: #won
client.publish("raum2/flipdot/hangman","won")
elif gamestatus==0: #ended in another way
client.publish("raum2/flipdot/hangman","ended")
if payload=="#stop":
mode="standby"
client.publish("raum2/flipdot/hangman","ended")
2016-12-23 00:33:23 +00:00
flipdot = FlipdotSender("2001:67c:275c:a9::c", 2323)
2017-02-16 21:30:01 +00:00
hangman= Hangman("2001:67c:275c:a9::c", 2323,flipdot)
2017-01-27 18:21:49 +00:00
2016-12-23 00:33:23 +00:00
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("raum.ctdo.de", 1883, 60)
client.loop_start()
while True:
2017-02-16 21:30:01 +00:00
millis = int(round(time.time() * 1000))
if gametimeout!=0:
if millis-120000 > gametimeout: #timeout
print("Game Timeout")
mode="standby"
gametimeout=0
2016-12-23 00:33:23 +00:00
time.sleep(2)