11#!/usr/bin/python
22
3+ from __future__ import print_function
34import sys
45import time
6+ import datetime
57import dyndb
68import tconf
79import bstate
810from w1thermsensor import W1ThermSensor
11+ import subprocess
12+
913
1014sensor = 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
1374if len (sys .argv )== 3 :
1475 tty_name = sys .argv [1 ]
@@ -27,13 +88,18 @@ db=dyndb.Tempdb(tconf.url,tconf.region)
2788
2889while 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