-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermd
executable file
·111 lines (84 loc) · 3.28 KB
/
thermd
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
#!/usr/bin/python
from __future__ import print_function
import sys
import time
import datetime
import dyndb
import tconf
import bstate
from w1thermsensor import W1ThermSensor
import subprocess
import os
import sys
devnull = open(os.devnull, 'w')
logfile = open(tconf.log,'a+')
def db2local(b, db):
response=db.get_cmd(b.boiler_name,b.boiler_name)
if response.has_key('Item'):
i=response['Item']
if b.state['target']!=float(i.get('target',-1)) or b.state['heating']!=int(i.get('heating',-1)):
print ("Remote state change for {0}".format(b.boiler_name), flush=true, file=logfile)
print (b.write_state(float(i.get('target',-1)),int(i.get('heating',-1))))
return b.state
def boiler_state():
out = subprocess.check_output([tconf.relay_cmd], stderr=devnull)
if out:
for l in out.splitlines():
state = l.split('=')
if len(state) == 2 and state[0] == tconf.relay:
return state[1]
return -1
def boiler_on(cur_s):
if cur_s == tconf.relay_on:
return
subprocess.check_output([tconf.relay_cmd, "{0}={1}".format(tconf.relay,tconf.relay_on)],
stderr=devnull)
def boiler_off(cur_s):
if cur_s == tconf.relay_off:
return
subprocess.check_output([tconf.relay_cmd, "{0}={1}".format(tconf.relay,tconf.relay_off)],
stderr=devnull)
def now_day():
now = datetime.datetime.now()
if now.hour >= 23 or now.hour<7:
return 0
else:
return 1
def run_cmd(cur_t, cmd, cur_s):
target_t = cmd.get('target', tconf.def_temp)
# command mode: 0 - always off; 1 - maintain target; 2 - target at night, 5 at day
mode = cmd.get('heating', 1)
if mode == 0:
boiler_off(cur_s);
else:
if mode == 2 and now_day():
target_t = tconf.def_temp
if cur_t < target_t:
boiler_on(cur_s)
else:
boiler_off(cur_s)
if len(sys.argv)==3:
tty_name=sys.argv[1]
thermo_name=sys.argv[2]
else:
tty_name=tconf.tty
thermo_name=tconf.thermo_name
#read boiler state file
boiler = bstate.Boiler(thermo_name)
# Connect to Dynamodb
db=dyndb.Tempdb(tconf.url,tconf.region)
sensor = W1ThermSensor()
while True:
boiler.read_state()
temp_in_c1 = sensor.get_temperature()
temp_in_c2 = sensor.get_temperature()
cur_s = boiler_state()
if temp_in_c1:
print (time.time(),temp_in_c1, temp_in_c2, boiler.state['target'],boiler.state['heating'], cur_s,flush=true, file=logfile)
db.put_values(temp_in_c1, temp_in_c2, thermo_name,boiler.state['target'],
boiler.state['heating'], cur_s)
else:
print ("ERR read invalid data from wired thermometr: '", flush=true, file=logfile)
boiler_cmd = db2local(boiler, db)
run_cmd(temp_in_c1, boiler_cmd, cur_s)
time.sleep(tconf.therm_rate)