-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThermostatService.py
More file actions
163 lines (136 loc) · 6.08 KB
/
ThermostatService.py
File metadata and controls
163 lines (136 loc) · 6.08 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
__author__ = 'matt'
import threading
import time
from GPIOManager import TestGPIOManager
# from RaspberryPiManager import RaspberryPiManager
# from BeagleBoneBlackGPIOManager import BeagleBoneBlackGPIOManager
class ThermostatService(threading.Thread):
def __init__(self, threadId, name, configuration, fileManager, temperatureReader):
threading.Thread.__init__(self)
self.threadId = threadId
self.name = name
self.configuration = configuration
self.fileManager = fileManager
self.temperatureReader = temperatureReader
self.currentMode = "off"
self.currentTemperature = 70.0
self.manager = TestGPIOManager(configuration)
# self.manager = RaspberryPiManager(configuration)
# self.manager = BeagleBoneBlackGPIOManager(configuration)
def run(self):
print("Starting service")
self.turn_off_heat()
self.turn_off_ac()
#comment for testing
#time.sleep(300)
self.turn_off_fan()
while True:
print(time.strftime("%H:%M:%S ") + "thermostat service polling...")
# check current
newValues = self.fileManager.read_current()
try:
newMode = newValues["mode"]
newTemperature = newValues["temperature"]
# if mode is changing
if newMode != self.currentMode:
# if new mode is off
if "off" == newMode:
# turn off unit
# if currently fan, simply turn off the fan
if "fan" == self.currentMode:
self.turn_off_fan()
# else, turn off heat or ac, wait 5 minutes, then turn off fan
else:
if "heat" == self.currentMode:
self.turn_off_heat()
elif "ac" == self.currentMode:
self.turn_off_ac()
time.sleep(300)
self.turn_off_fan()
elif "fan" == newMode:
self.turn_on_fan()
if "heat" == self.currentMode:
self.turn_off_heat()
elif "ac" == self.currentMode:
self.turn_off_ac()
time.sleep(300)
elif "heat" == self.currentMode:
self.turn_off_heat()
time.sleep(300)
elif "ac" == self.currentMode:
self.turn_off_ac()
time.sleep(300)
except:
#TODO: handle an error in mode changing
# error! maybe try handling at some point, but first just turn off
# everything except the fan.
self.turn_on_fan()
self.turn_off_heat()
self.turn_off_ac()
# check current
newValues = self.fileManager.read_current()
self.currentMode = newValues["mode"]
self.currentTemperature = round(float(newValues["temperature"]), 1)
temperature = self.temperatureReader.CurrentTemperature()
if "heat" == self.currentMode:
self.turn_off_ac()
if temperature < self.currentTemperature -\
self.configuration.inactiveHysteresis:
if "heat" not in self.configuration.running:
self.turn_on_fan()
self.turn_on_heat()
elif temperature > self.currentTemperature +\
self.configuration.activeHysteresis:
if "heat" in self.configuration.running:
self.turn_off_heat()
time.sleep(300)
self.turn_off_fan()
elif "ac" == self.currentMode:
self.turn_off_heat()
if temperature > self.currentTemperature +\
self.configuration.inactiveHysteresis:
if "ac" not in self.configuration.running:
self.turn_on_fan()
self.turn_on_ac()
elif temperature < self.currentTemperature -\
self.configuration.activeHysteresis:
if "ac" in self.configuration.running:
self.turn_off_ac()
time.sleep(300)
self.turn_off_fan()
elif "fan" == self.currentMode:
self.turn_on_fan()
self.turn_off_heat()
self.turn_off_ac()
time.sleep(5)
#TODO: create another helper to manipulate GPIO ports appropriately
def turn_off_fan(self):
if "fan" in self.configuration.running:
self.configuration.running.remove("fan")
self.manager.turn_off_fan()
def turn_on_fan(self):
if "fan" not in self.configuration.running:
self.configuration.running.append("fan")
self.manager.turn_on_fan()
def turn_off_ac(self):
if "ac" in self.configuration.running:
self.configuration.running.remove("ac")
self.manager.turn_off_ac()
def turn_on_ac(self):
if "fan" not in self.configuration.running:
self.configuration.running.append("fan")
self.manager.turn_on_fan()
if "ac" not in self.configuration.running:
self.configuration.running.append("ac")
self.manager.turn_on_ac()
def turn_off_heat(self):
if "heat" in self.configuration.running:
self.configuration.running.remove("heat")
self.manager.turn_off_heat()
def turn_on_heat(self):
if "fan" not in self.configuration.running:
self.configuration.running.append("fan")
self.manager.turn_on_fan()
if "heat" not in self.configuration.running:
self.configuration.running.append("heat")
self.manager.turn_on_heat()