-
Notifications
You must be signed in to change notification settings - Fork 0
/
heater_control.py
executable file
·321 lines (242 loc) · 6.92 KB
/
heater_control.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import time
import serial
import os
import sys
import csv
import HandlePlanning
from datetime import datetime
import logging
import HandlePlanning
from queue import Queue
from threading import Thread
import presence_automation
from configparser import ConfigParser
# ---- Config File Read/Init ----
config = ConfigParser()
config.read("/home/osmc/Conde_Home_Automation/config.ini")
# print(config)
# print(config.get('heaterstates', 'lastmode'))
globalMode = config.get('heaterstates', 'lastmode')
modeZ1 = config.get('heaterstates', 'laststate1')
modeZ2 = config.get('heaterstates', 'laststate2')
modeZ3 = config.get('heaterstates', 'laststate3')
season = config.get('heaterstates', 'season')
# globalMode = "Auto"
# modeZ1 = "Moon"
# modeZ2 = "Moon"
# modeZ3 = "Moon"
tempRemaining = 0 # minutes
X2DEventQueue = Queue()
def SaveHeaterStatesToFile():
global globalMode
global modeZ1
global modeZ2
global modeZ3
if (globalMode=="Temp"):
config.set('heaterstates', 'lastmode', "Auto")
else:
config.set('heaterstates', 'lastmode', globalMode)
config.set('heaterstates', 'laststate1', modeZ1)
config.set('heaterstates', 'laststate2', modeZ2)
config.set('heaterstates', 'laststate3', modeZ3)
with open('/home/osmc/Conde_Home_Automation/config.ini', 'w') as configfile:
config.write(configfile)
logging.info("Heater State saved to INI File")
def StartTempModeThread():
worker = Thread(target=StartTempMode)
worker.setDaemon(False)
worker.start()
def StartTempMode():
global globalMode
global modeZ1
global modeZ2
global modeZ3
global tempRemaining
while ((globalMode == "Temp") & (tempRemaining > 0)):
logging.info("Heater Temporary Mode is ON ! Refreshing states")
logging.info("Temporary Mode time remaining : "+str(tempRemaining)+" minutes")
UpdateHeatersStates()
tempRemaining = tempRemaining - 1;
if (tempRemaining < 0):
tempRemaining = 0
time.sleep(60)
globalMode = "Auto"
UpdateHeatersStates()
def SetHeaterCommand(command):
global globalMode
global modeZ1
global modeZ2
global modeZ3
global tempRemaining
logging.info("Heater Command received : "+command)
if command.startswith("Temp"):
# format
# Temp Hg1 60
tempSplit = command.split()
globalMode = "Temp"
tempRemaining = float(tempSplit[2])
modeZ1 = str(tempSplit[1])[:-1]
modeZ2 = str(tempSplit[1])[:-1]
modeZ3 = str(tempSplit[1])[:-1]
StartTempModeThread()
elif command.endswith("1"):
modeZ1 = str(command)[:-1]
globalMode = "Manual"
SendState(command)
elif command.endswith("2"):
modeZ2 = str(command)[:-1]
globalMode = "Manual"
SendState(command)
elif command.endswith("3"):
modeZ3 = str(command)[:-1]
globalMode = "Manual"
#SendState(command)
elif (command == "Auto"):
globalMode = "Auto"
UpdateHeatersStates()
elif (command == "Manual"):
globalMode = "Manual"
UpdateHeatersStates()
def SendState(state):
modeNum = state[-1:]
if state.startswith("Sun"):
SendX2DCommand(state)
SendX2DCommand("On"+modeNum)
if state.startswith("Moon"):
SendX2DCommand(state)
SendX2DCommand("On"+modeNum)
if state.startswith("On"):
SendX2DCommand(state)
if state.startswith("Hg"):
SendX2DCommand(state)
if state.startswith("Off"):
SendX2DCommand(state)
def UpdateHeatersStates():
global globalMode
global modeZ1
global modeZ2
global modeZ3
global season
if (globalMode == "Auto"):
logging.info("Heaters are in Auto Mode")
if presence_automation.IsThereAnybody() == False:
logging.info("Nobody is at home - Putting the heaters in Moon mode")
modeZ1 = "Moon"
SendX2DCommand("Moon"+"1")
SendX2DCommand("On1")
SendX2DCommand("Moon"+"2")
SendX2DCommand("On2")
else:
path=os.path.dirname(__file__)+'/'
if path=='/' :
path='./'
data=list()
dayNames=['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche']
stateNames=['Moon','Sun']
if (season == "Summer"):
csvFile = "/home/osmc/Conde_Home_Automation/chauffage-summer.csv"
else:
csvFile = "/home/osmc/Conde_Home_Automation/chauffage.csv"
with open(csvFile, 'rt') as f:
reader = csv.reader(f, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL)
for row in reader:
# print row
data.append(row)
zone1=HandlePlanning.HandlePlanning()
zone2=HandlePlanning.HandlePlanning()
zone1.setData(data)
zone2.setData(data)
horaires=zone1.getHoraires()
zone2.getHoraires()
zone1.getZone("Zone 1")
zone2.getZone("Zone 2")
d=datetime.now()
dayName=dayNames[d.weekday()]
#print(dayName)
timeStr=d.strftime("%H:%M:%S")
#print(timeStr)
'''
# Some tests
# timeStr="07:35:00"
# timeStr="07:00:00"
# timeStr="06:35:00"
'''
#timeStr="23:31:00"
stat1=stateNames[zone1.getStatus(dayName, timeStr)]
logging.info("Heaters Status for Zone 1 : "+stat1)
stat2=stateNames[zone2.getStatus(dayName, timeStr)]
logging.info("Heaters Status for Zone 2 : "+stat2)
#os.system("python3 %spyX2DCmd.py %s1"%(path,stat1))
#time.sleep(15)
#os.system("python3 %spyX2DCmd.py %s2"%(path,stat2))
modeZ1 = stat1
SendX2DCommand(stat1+"1")
SendX2DCommand("On1")
modeZ2 = stat2
SendX2DCommand(stat2+"2")
SendX2DCommand("On2")
else:
logging.info("Heaters are NOT in auto mode (manual or Temporary Mode). Refreshing manual States")
SendState(modeZ1+"1")
SendState(modeZ2+"2")
# SendState(modeZ3+"3")
def SendX2DCommandThread():
worker = Thread(target=SendX2DCommandQueue, args=(X2DEventQueue,))
worker.setDaemon(False)
worker.start()
def SendX2DCommand(command):
X2DEventQueue.put(command)
def SendX2DCommandQueue(q):
while True:
logging.info("X2D Command Queue waiting ...")
command = q.get()
logging.info("X2D Command in queue : "+command)
SaveHeaterStatesToFile()
logging.info("Opening Serial Port")
try:
if os.name == 'nt':
ser = serial.Serial(
port='COM10',
baudrate=9600
)
ser.rts = False
ser.dtr = False
else:
ser = serial.Serial(
port='/dev/USB868',
baudrate=9600
)
ser.rts = False
ser.dtr = False
#ser.open()
if ser.isOpen() == False:
ser.open()
logging.info("Serial Port Opened")
except:
logging.info("ERROR WHILE OPENING SERIAL PORT !")
q.task_done()
continue
out = ''
# wait for RFbee init
time.sleep(3)
while ser.inWaiting() > 0:
out += str(ser.read(1).decode("ascii"))
if out != '':
#print(">>" + out)
logging.info("Arduino/CC1101 initialization Complete")
logging.debug(out)
time.sleep(3)
ser.write(bytearray(command+"\n",'ascii'))
#print(">>%s"%(command))
logging.info(command+" sent to Arduino")
out = ''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
out += str(ser.read(1).decode("ascii"))
if "ok" in out :
logging.info("Arduino message sent successfuly")
logging.debug(out)
else:
logging.error("Error sending command")
q.task_done()