kupa/kupa.py
Maciej Kozlowski 910ce1b86b kod od kupy
2019-05-24 17:53:11 +02:00

212 lines
4.9 KiB
Python
Executable file

import paho.mqtt.client as mqtt
import requests
import time
import os
import flaschen
import threading
import time
state = {
'startup': 0,
'last_change': None,
}
p=None
UDP_IP = '10.8.0.146'
UDP_PORT = 1337
isKupa=True
letter_k=[
4,
["1001",
"1010",
"1100",
"1010",
"1001"]
]
letter_u=[
4,
["1001",
"1001",
"1001",
"1001",
"0110"]
]
letter_p=[
4,
["1110",
"1001",
"1110",
"1000",
"1000"]
]
letter_a=[
4,
["0110",
"1001",
"1111",
"1001",
"1001"]
]
letter_space=[
4,
["0000",
"0000",
"0000",
"0000",
"0000"]
]
ft = flaschen.Flaschen(UDP_IP, UDP_PORT,8, 5)
current_text_offset=0
def get_char_pixel_length(ch):
l = {
'k': letter_k[0],
'u': letter_u[0],
'p': letter_p[0],
'a': letter_a[0],
' ': letter_space[0]
}.get(ch, 0)
return l + 1
def get_char_pixel_bitmap(ch):
l = {
'k': letter_k[1],
'u': letter_u[1],
'p': letter_p[1],
'a': letter_a[1],
' ': letter_space[1]
}.get(ch, ["0000","0000","0000","0000","0000"])
return l
def get_string_pixel_length(st):
l=0
for c in st:
l += get_char_pixel_length(c)
return l
def print_letter(ch):
white=(255,255,255)
for x in range(0,ch[0]):
for y in range(0,5):
if ch[1][y][x]=='1':
ft.set(x,y,white)
def get_text_vline(st, i):
done=False
letter=0
offset=0
previous_offset = 0
if i >= get_string_pixel_length(st):
return['0','0','0','0','0']
while not done:
offset +=get_char_pixel_length(st[letter])
if i < offset-1:
done=True
vline = [get_char_pixel_bitmap(st[letter])[0][i-previous_offset],get_char_pixel_bitmap(st[letter])[1][i-previous_offset],get_char_pixel_bitmap(st[letter])[2][i-previous_offset],get_char_pixel_bitmap(st[letter])[3][i-previous_offset], get_char_pixel_bitmap(st[letter])[4][i-previous_offset]]
return vline
elif i == offset-1:
vline = ['0','0','0','0','0']
done=True
else:
letter += 1
previous_offset = offset
return vline
def prepare_text_block(st):
l=get_string_pixel_length(st)
#l+=8 # add full clear screen for text rotation
block=[]
i=0
for vline in range(0,l):
block.append(get_text_vline(st, i))
i+=1
return block
def print_text(block):
for x in range(current_text_offset, current_text_offset+8):
for y in range(0, ft.height):
if block[x][y] == '1':
ft.set(x-current_text_offset, y, (255,255,255))
def render_thread(name):
global current_text_offset
text=" kupa "
print("we start sraning")
block = prepare_text_block(text)
while isKupa:
for y in range(0, ft.height):
for x in range(0, ft.width):
ft.set(x, y, (255, 0, 0))
print_text(block)
#print_letter(letter_k)
ft.send()
time.sleep(0.125)
current_text_offset+=1
if (current_text_offset >= get_string_pixel_length(text)-8):
current_text_offset = 0
# 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))
global state
state['startup'] = time.time()
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("iot/247a1d/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global state
global isKupa
if time.time() - state['startup'] > 1.0:
print(msg.topic+" "+str(msg.payload))
if msg.topic.endswith('shitting/state'):
print(msg.payload.decode())
s = msg.payload.decode() == 'true'
toilet_state = 'OCCUPIED' if s else 'FREE'
shitting_period = time.time() - state['last_change'] if state['last_change'] else None
try:
if s == True:
p = threading.Thread(target=render_thread, args=(1,))
isKupa=True
print("isKupa is on")
p.start()
else:
print("isKupa is off")
isKupa=False
except Exception as exc:
print(exc)
state['last_change'] = time.time()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("iot.waw.hackerspace.pl", 1883, 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.
client.loop_forever()