-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcdDisplay.py
127 lines (101 loc) · 3.72 KB
/
lcdDisplay.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
#!/usr/bin/env python
import lcddriver
from time import *
import pika
import sys
from threading import Thread
import threading
import time
import logging
import os
lcd = None
rabbitMqHost = os.environ['RABBIT_MQ_HOST']
rabbitMqQueue = os.environ['RABBIT_MQ_QUEUE']
demoMode = eval(os.environ.get('DEMO_MODE', False))
rowData = [" "," "," "," "," "]
connection = pika.BlockingConnection
# -------------------------------------------------------------------------------------------------------
def writeDataToDisplay():
global lcd
global rowData
global demoMode
logging.info("writeDataToDisplay Thread startet...")
ct = threading.currentThread()
lcd = lcddriver.lcd()
while getattr(ct, "do_checking", True):
if not demoMode:
lcd.lcd_clear()
else:
logging.info("----------------------------------")
rowCount = 0
for row in rowData:
if not demoMode:
lcd.lcd_display_string(row, rowCount)
else:
logging.info("Write: " + row + " on LCD row:" + str(rowCount))
rowCount +=1
time.sleep(10)
# -------------------------------------------------------------------------------------------------------
def openConnection():
global connection
global rabbitMqHost
global rabbitMqQueue
connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitMqHost))
channel = connection.channel()
channel.basic_consume(queue=rabbitMqQueue,on_message_callback=callback)
logging.info("Waiting for Messages on Queue:" + rabbitMqQueue)
channel.start_consuming()
# -------------------------------------------------------------------------------------------------------
def closeConnection():
global connection
connection.close()
# -------------------------------------------------------------------------------------------------------
def callback(ch, method, properties, body):
global rabbitMqQueue
global rowData
try:
ch.basic_ack(delivery_tag=method.delivery_tag)
logging.debug("Message arrived")
stringBody = "".join(map(chr, body))
if stringBody.find("=") != -1:
if demoMode:
logging.info("Rceived:" + stringBody)
row = stringBody[0:stringBody.find("=")]
rowData[int(row)] = stringBody[stringBody.find("=")+1:]
else:
logging.info("Message on Channel: " + rabbitMqQueue +
" has an unexpectedly Format -> expecting ROW=DATA")
except BaseException as error:
logging.error(error)
# -------------------------------------------------------------------------------------------------------
def main():
global lcd
logging.basicConfig(level=logging.INFO)
logging.info("---------------------------------------------")
rowData[0] = " "
rowData[1] = os.environ.get('WELCOME_TEXT', "booting...")
rowData[2] = " "
rowData[3] = " "
if not demoMode:
logging.info("LCD initialised...")
lcd = lcddriver.lcd()
lcd.lcd_clear()
# Start anf join the output Thread
outputThread = Thread(target=writeDataToDisplay)
outputThread.join()
outputThread.daemon = True
outputThread.start()
else:
logging.info("lcdDisplay Connector Started in DEMO MODE")
openConnection()
logging.info('Finished')
closeConnection()
logging.info("---------------------------------------------")
if __name__ == '__main__':
try:
main()
except:
logging.info("---------------------------------------------")
logging.info("-- CRITICAL ERROR OCCURED...")
logging.info("---------------------------------------------")
sys.exit(2)