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

155 lines
5.4 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
from threading import Thread
2017-01-27 18:21:49 +00:00
global mode
mode="standby"
global timeout
timeout=0
global flipthread
flipthread=None
2016-12-23 00:33:23 +00:00
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("raum2/flipdot/#") #subscribe to every subtopic
2017-01-27 18:21:49 +00:00
#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
global timeout
global flipthread
2016-12-23 00:33:23 +00:00
if mode=="standby" or mode=="inuse":
2017-01-27 18:21:49 +00:00
if msg.topic == "raum2/flipdot/scroll/set":
mode="inuse"
updateTimeout()
2017-01-27 18:21:49 +00:00
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 flipthread is not None and flipthread.isAlive():
flipdot.stopAnimation()
while flipthread.isAlive():
flipdot.stopAnimation() #try to stop animation
time.sleep(0.1)
flipthread.join() #wait for thread to finish
flipthread=Thread(target=flipdot.send_marquee, args=(text,speed))
flipthread.start()
if mode!="inuse" and msg.topic == "raum2/flipdot/text/set": #used to display information regulary (time etc). only available if flipdot not in use
2017-01-27 18:21:49 +00:00
payload = msg.payload.decode("utf-8")
if flipthread is not None and flipthread.isAlive():
flipdot.stopAnimation()
while flipthread.isAlive():
flipdot.stopAnimation() #try to stop animation
time.sleep(0.1)
flipthread.join()
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
flipthread=Thread(target=flipdot.send_text, args=(payload,64))
else:
#flipdot.send_text(payload) #without animation
flipthread=Thread(target=flipdot.send_text, args=(payload,))
flipthread.start()
2017-02-16 21:30:01 +00:00
if msg.topic == "raum2/flipdot/textFull/set": #scale/break text automatically
mode="inuse"
updateTimeout()
2017-02-16 21:30:01 +00:00
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":
mode="inuse"
updateTimeout()
2017-01-27 18:21:49 +00:00
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()
updateTimeout()
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
updateTimeout()
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")
def updateTimeout():
global timeout
timeout=int(round(time.time() * 1000))
2017-01-27 18:21:49 +00:00
2016-12-23 00:33:23 +00:00
#flipdot = FlipdotSender("2001:67c:275c:a9::c", 2323)
flipdot = FlipdotSender("localhost", 2323)
#hangman= Hangman("2001:67c:275c:a9::c", 2323,flipdot)
hangman= Hangman("localhost", 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 (timeout!=0) and (mode=="hangman"):
if millis-120000 > timeout: #timeout
2017-02-16 21:30:01 +00:00
print("Game Timeout")
mode="standby"
timeout=0
if (timeout!=0) and (mode=="inuse"): #inuse: flipdot was manually controlled, do not update time,pwr etc for a while
if millis-10000 > timeout: #timeout
mode="standby"
timeout=0
2017-02-16 21:30:01 +00:00
2016-12-23 00:33:23 +00:00
time.sleep(2)