Skip to content

Commit 9f14d59

Browse files
committed
relay managment added
1 parent 7bac00c commit 9f14d59

5 files changed

Lines changed: 86 additions & 14 deletions

File tree

boiler

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def db2local(b, db):
1414
if b.state['target']!=float(i.get('target',-1)) or b.state['heating']!=int(i.get('heating',-1)):
1515
print (b.write_state(float(i.get('target',-1)),int(i.get('heating',-1))))
1616
else:
17-
print ("No boiler '{0}' targets in cloud".format(b.boiler_name),file=sys.stderr)
17+
print ("No boiler '{0}' state change in cloud".format(b.boiler_name),file=sys.stderr)
1818
sys.exit(1)
1919
return b.state
2020

2121

2222
thermo_name=tconf.thermo_name
2323
if len(sys.argv)<2:
2424
print ("usage: boiler <cmd> [<target_temperature> <heating> [<thermo_name>]]",file=sys.stderr)
25-
print (" <cmd> - sync,loop,local,db,read",file=sys.stderr)
25+
print (" <cmd> - sync, loop, local, dbw, read",file=sys.stderr)
2626
sys.exit(1)
2727
else:
2828
cmd=sys.argv[1]
@@ -49,11 +49,12 @@ elif cmd=='loop': #sync local state from db in loop
4949
sys.sleep(tconf.therm_rate*1.5)
5050
elif cmd=='local': #write values to local state file only
5151
print (b.write_state(target,heating))
52-
elif cmd=='db': #write values to db and locally
52+
elif cmd=='dbw': #write values to db and locally
5353
response=db.thermo_cmd(target,heating,b.boiler_name,b.boiler_name)
5454
print (b.write_state(target,heating))
5555
elif cmd=='read':
5656
print ('Local file: ', b.read_state())
57+
print ('Boiler: ', b.boiler_name )
5758
response=db.get_cmd(b.boiler_name,b.boiler_name)
5859
if response.has_key('Item'):
5960
i=response['Item']

dyndb.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ def __init__(self,url="http://localhost:8000",region="eu-central-1"):
1212
self.table_log = self.dynamodb.Table('Temperature')
1313
self.table_cmd = self.dynamodb.Table('Commands')
1414

15-
def put_values(self,temperature,humidity,thermo_name="InsideHall",target_t=100,is_heating=0):
15+
def put_values(self,temperature1,temperature2,thermo_name="InsideHall",target_t=100,is_heating=0):
1616
try:
1717
response=self.table_log.put_item(
1818
Item={
1919
'Thermometer': thermo_name,
2020
'GetDate': get_now(),
21-
'val': get_val(temperature),
22-
'hum': get_val(humidity),
21+
'val1': get_val(temperature1),
22+
'val2': get_val(temperature2),
2323
'target': get_val(target_t),
2424
'heating': get_val(is_heating),
2525
}
@@ -29,8 +29,8 @@ def put_values(self,temperature,humidity,thermo_name="InsideHall",target_t=100,i
2929
Item={
3030
'Thermometer': thermo_name,
3131
'GetDate': get_now(),
32-
'val': float_to_decimal(temperature),
33-
'hum': float_to_decimal(humidity),
32+
'val1': float_to_decimal(temperature1),
33+
'val2': float_to_decimal(temperature2),
3434
'target': float_to_decimal(target_t),
3535
'heating': get_val(int(is_heating)),
3636
}

get_thermo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def default(self, o):
3434

3535

3636
for i in response['Items']:
37-
print(time.ctime(float(i['GetDate'])),'temp',float(i['val']), 'hum', float(i['hum']),
37+
print(time.ctime(float(i['GetDate'])),'temp1',float(i.get('val1', -1)), 'temp2', float(i.get('val2', -1)),
3838
int(i.get('target',-1)),int(i.get('heating',-1)))
3939

4040

tconf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@
77
therm_rate=120
88
thermo_name="InsideHall"
99
state_prefix="."
10+
def_temp = 5
11+
relay="959BI_1"
12+
relay_on = "1"
13+
relay_off = "0"
14+
relay_cmd = "/usr/local/bin/usbrelay"

thermd

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,75 @@
11
#!/usr/bin/python
22

3+
from __future__ import print_function
34
import sys
45
import time
6+
import datetime
57
import dyndb
68
import tconf
79
import bstate
810
from w1thermsensor import W1ThermSensor
11+
import subprocess
12+
913

1014
sensor = W1ThermSensor()
1115

16+
def db2local(b, db):
17+
response=db.get_cmd(b.boiler_name,b.boiler_name)
18+
if response.has_key('Item'):
19+
i=response['Item']
20+
if b.state['target']!=float(i.get('target',-1)) or b.state['heating']!=int(i.get('heating',-1)):
21+
print ("Remote state change for {0}".format(b.boiler_name))
22+
print (b.write_state(float(i.get('target',-1)),int(i.get('heating',-1))))
23+
24+
return b.state
25+
26+
27+
def boiler_state():
28+
out = subprocess.check_output([tconf.relay_cmd])
29+
if out:
30+
for l in out.splitlines():
31+
state = l.split('=')
32+
if len(state) == 1 and state[0] == tconf.relay:
33+
return state[1]
34+
return None
35+
36+
def boiler_on():
37+
cur_s = boiler_state()
38+
if cur_s == tconf.relay_on:
39+
return
40+
41+
subprocess.check_output([tconf.relay_cmd, "{0}={1}".format(tconf.relay,tconf.relay_on)])
42+
43+
def boiler_off():
44+
cur_s = boiler_state()
45+
if cur_s == tconf.relay_off:
46+
return
47+
48+
subprocess.check_output([tconf.relay_cmd, "{0}={1}".format(tconf.relay,tconf.relay_off)])
49+
50+
def now_day():
51+
now = datetime.datetime.now()
52+
if now.hour >= 23 or now.hour<7:
53+
return 0
54+
else:
55+
return 1
56+
57+
58+
def run_cmd(cur_t, cmd):
59+
target_t = cmd.get('target', tconf.def_temp)
60+
# command mode: 0 - always off; 1 - maintain target; 2 - target at night, 5 at day
61+
mode = cmd.get('heating', 1)
62+
if mode == 0:
63+
boiler_off();
64+
else:
65+
if mode == 2 and now_day():
66+
target_t = tconf.def_temp
67+
if cur_t < target_t:
68+
boiler_on()
69+
else:
70+
boiler_off()
71+
72+
1273

1374
if len(sys.argv)==3:
1475
tty_name=sys.argv[1]
@@ -27,13 +88,18 @@ db=dyndb.Tempdb(tconf.url,tconf.region)
2788

2889
while True:
2990
boiler.read_state()
30-
temp_in_c = sensor.get_temperature()
31-
if temp_in_c:
32-
print time.time(),temp_in_c, 0.0, boiler.state['target'],boiler.state['heating']
33-
db.put_values(temp_in_c, 0.0, thermo_name,boiler.state['target'],
91+
temp_in_c1 = sensor.get_temperature()
92+
temp_in_c2 = sensor.get_temperature()
93+
if temp_in_c1:
94+
print (time.time(),temp_in_c1, temp_in_c2, boiler.state['target'],boiler.state['heating'])
95+
db.put_values(temp_in_c1, temp_in_c2, thermo_name,boiler.state['target'],
3496
boiler.state['heating'])
3597
else:
36-
print ("ERR read invalid data from serial: '")
98+
print ("ERR read invalid data from wired thermometr: '")
99+
100+
boiler_cmd = db2local(boiler, db)
101+
run_cmd(temp_in_c1, boiler_cmd)
102+
37103
time.sleep(tconf.therm_rate)
38104

39105

0 commit comments

Comments
 (0)