-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
207 lines (163 loc) · 7.57 KB
/
Copy pathmain.py
File metadata and controls
207 lines (163 loc) · 7.57 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python
'''
Created on 1 Nov 2015
@author: stephen H
test change
'''
from __future__ import division
__updated__ = "2016-07-31"
import logging
from logging.handlers import RotatingFileHandler
from database import DbUtils
import threading
import multiprocessing
import neopixelserial
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer
from requesthandler import MyRequestHandler
from heatinggpio import setupGPIO, buttonCheckHeat, hBeat
from variables import Variables
from sys import platform as _platform
from os import system
import hardware
import time
#from max import checkHeat, initialiseNeoPixel
from max import MaxInterface
input_queue = multiprocessing.Queue()
output_queue = multiprocessing.Queue()
reboot_Timer = 0.0
offTime = 0.0
shutdown_Timer = 0.0
shutOff_Timer = 0.0
def mainCheckHeat(self):
MaxInterface().checkHeat(input_queue)
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests is separate thread"""
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True,
queue=None):
self.queue = queue
HTTPServer.__init__(self, server_address, RequestHandlerClass,
bind_and_activate=bind_and_activate)
class Main():
def __init__(self):
#Initialise the Logger
logLevel = Variables().readVariables(['LoggingLevel']).rstrip('\r')
useNeoPixel = Variables().readVariables(['UseNeoPixel'])
self.logger = logging.getLogger("main")
level = logging.getLevelName(logLevel)
self.logger.setLevel(level)
fh = RotatingFileHandler("heating.log",
maxBytes=1000000, # 1Mb I think
backupCount=5)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
self.logger.addHandler(fh)
self.logger.info("Main has started __init__ has run logger level is %s" % logLevel)
try:
cube = DbUtils().getCubes()
self.logger.info("Database OK cube %s found" % (cube[1]))
except Exception, err:
DbUtils().initialiseDB()
self.logger.exception("Database Initialised %s" % err)
self.logger.info("Free Memory at Boot %s MB" % hardware.getRAM())
self.logger.info("CPU Usage at Boot %s" % hardware.getCPUUse())
# Start Serial connection worker if using NeoPixel
if useNeoPixel:
self.arduino = neopixelserial.SerialProcess(input_queue, output_queue)
self.arduino.daemon = True
self.arduino.start()
# Or start the GPIO
else:
setupGPIO(input_queue)
# Start Web UI
self.startKioskServer()
# Start Main Loop
self.doLoop()
def doLoop(self):
nextLoopCheck = time.time()
while True:
loopStartTime = time.time()
if loopStartTime >= nextLoopCheck:
print "running loop"
checkInterval, boiler_enabled, useNeoPixel = Variables().readVariables(['Interval', 'BoilerEnabled', 'UseNeoPixel'])
if boiler_enabled != 1:
checkInterval = checkInterval * 2
if _platform == "linux" or _platform == "linux2":
if not useNeoPixel:
self.logger.info("checking heat levels")
buttonCheckHeat("main")
self.logger.info("starting heartbeat")
hBeat(checkInterval)
self.logger.info("Memory free this loop %s MB" % hardware.getRAM())
self.logger.info("CPU Usage this loop %s" % hardware.getCPUUse())
self.logger.debug( "loop interval : %s" %(checkInterval))
self.doLoop()
else:
self.logger.info("checking heat levels")
MaxInterface().checkHeat(input_queue)
self.logger.info('Running NeoPixel timer')
self.logger.info("Memory free this loop %s MB" % hardware.getRAM())
self.logger.info("CPU Usage this loop %s" % hardware.getCPUUse())
self.logger.debug( "loop interval : %s" %(checkInterval))
else:
MaxInterface().checkHeat(input_queue)
self.logger.info('Running Windows timer')
nextLoopCheck = loopStartTime + checkInterval
if not output_queue.empty():
serialData = output_queue.get().rstrip()
self.processSerial(serialData)
time.sleep(.2)
def processSerial(self, data):
global reboot_Timer
global offTime
global shutdown_Timer
global shutOff_Timer
self.logger.info("Processing Serial Data : %s" % data)
if data == "checkSW_ON":
MaxInterface().checkHeat(input_queue)
elif data == "boilerSW_ON":
boilerEnabled = Variables().readVariables(['BoilerEnabled'])
if boilerEnabled:
boilerEnabled = 0
else:
boilerEnabled = 1
Variables().writeVariable([['BoilerEnabled', boilerEnabled]])
MaxInterface().checkHeat(input_queue)
elif data == "rebootSW_ON":
reboot_Timer = time.time()
MaxInterface().setNeoPixel(0, input_queue, 2)
elif data == "rebootSW_OFF":
offTime = time.time()
if offTime - reboot_Timer >= 3:
self.logger.info("Rebooting")
system("sudo reboot")
elif data == "shutdownSW_ON":
shutdown_Timer = time.time()
MaxInterface().setNeoPixel(0, input_queue, 1)
elif data == "shutdownSW_OFF":
shutOff_Timer = time.time()
if shutOff_Timer - shutdown_Timer >= 3:
self.logger.info("Shutting Down Python")
system("sudo shutdown -h now")
elif data == "overSW_ON":
boiler_override = Variables().readVariables(['BoilerOverride'])
boiler_override += 1
if boiler_override > 2:
boiler_override = 0
print boiler_override
Variables().writeVariable([['BoilerOverride', boiler_override]])
MaxInterface().setNeoPixel(0, input_queue, 0)
def startKioskServer(self):
webIP, webPort = Variables().readVariables(['WebIP', 'WebPort'])
self.logger.info("Web UI Starting on : %s %s" %( webIP, webPort))
try:
server = ThreadingHTTPServer((webIP, webPort), MyRequestHandler, queue=input_queue)
uithread = threading.Thread(target=server.serve_forever)
uithread.setDaemon(True)
uithread.start()
except Exception, err:
self.logger.error("Unable to start Web Server on %s %s %s" %(webIP, webPort, err))
self.logger.critical("Killing all Python processes so cron can restart")
system("sudo pkill python")
if __name__=='__main__':
Main()