-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spacestate_sign.py
129 lines (95 loc) · 3.84 KB
/
Spacestate_sign.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
119
120
121
122
123
124
125
126
127
128
129
#! /usr/bin/python3
import paho.mqtt.client
import struct
import socket
import time
import os
from PIL import Image, ImageFont, ImageDraw
class doorPixel():
client = paho.mqtt.client.Client()
debugShow = False
width = 64
height = 32
pixelBuffer = []
rgbMode = struct.pack("<?", 0)
versionBit = struct.pack("<B", 1)
pixelvlut = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
font = ImageFont.truetype("Comfortaa-Regular.ttf", 12)
state = False
def __init__(self):
self.setup_mqtt()
self.client.loop_forever()
def display_debug(self, image):
import numpy
import cv2
image = cv2.cvtColor(numpy.array(image), cv2.COLOR_RGB2BGR)
cv2.imshow('nurd sign', image)
cv2.waitKey(0)
def send_bufferfull(self, pixelBuffer):
buffer = self.versionBit + self.rgbMode
for buf in pixelBuffer:
buffer += buf
if len(buffer) >= 1400 and len(buffer) < 1510:
self.pixelvlut.sendto(buffer, ('esp32-07B018.dhcp.nurd.space', 5004))
return True
return False
def show_image(self, image):
pixelBuffer = []
for x in range(image.width):
for y in range(image.height):
r, g, b = image.getpixel((x, y ))
pixelBuffer.append(bytes(struct.pack("<2H3B", x, y, r, g, b)))
if self.send_bufferfull(pixelBuffer):
pixelBuffer = []
if self.debugShow:
self.display_debug(image)
def spaceopen(self):
image = Image.new("RGB", (self.width, self.height))
draw = ImageDraw.Draw(image)
draw.text((10,0), "NURDs\nare in!", font=self.font, alignment="center",fill=(0, 255, 0,))
self.show_image(image)
def spaceclosed(self):
image = Image.new("RGB", (self.width, self.height))
font = ImageFont.truetype("Comfortaa-Regular.ttf", 12)
draw = ImageDraw.Draw(image)
draw.text((16,0), "", font=self.font, alignment="center",fill=(255, 0, 0,))
self.show_image(image)
def doorbell_spaceclosed(self):
image = Image.new("RGB", (self.width, self.height))
font = ImageFont.truetype("Comfortaa-Regular.ttf", 12)
draw = ImageDraw.Draw(image)
draw.text((16,0), "Er is\nniemand!",font=self.font, alignment="center",fill=(255, 255, 0,))
self.show_image(image)
def doorbell_wait(self):
image = Image.new("RGB", (self.width, self.height))
font = ImageFont.truetype("Comfortaa-Regular.ttf", 10)
draw = ImageDraw.Draw(image)
draw.text((1,0), "Een\nmoment", font=self.font, alignment="center",fill=(255, 0, 255,))
self.show_image(image)
def setup_mqtt(self):
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_disconnect = self.on_disconnect
self.client.connect("mqtt.vm.nurd.space")
def on_disconnect(self, client, userdata, rc):
self.setup_mqtt()
def on_connect(self, client, userdata, flags, rc):
self.client.subscribe("space/state")
self.client.subscribe("deurbel")
def on_message(self, client, userdata, msg):
print(time.ctime(), f'mqtt for {msg.topic}')
if msg.topic == "space/state":
self.spacestate = True if msg.payload.decode("utf-8").lower() == "true" else False
if self.spacestate == True:
self.spaceopen()
else:
self.spaceclosed()
print(time.ctime(), self.spacestate)
elif msg.topic == 'deurbel':
print(time.ctime(), 'Deurbel')
if self.spacestate == False:
self.doorbell_spaceclosed()
else:
self.doorbell_wait()
time.sleep(10)
doorPixel()