Mercurial > templog
annotate py/tempserver.py @ 484:4a51e2bf689b
a few fixes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 06 Feb 2014 20:35:43 +0800 |
parents | bd29ddb360a5 |
children | be579bcf3c7f |
rev | line source |
---|---|
447 | 1 #!/home/matt/templog/venv/bin/python |
439 | 2 |
3 import sys | |
4 import os | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
5 import logging |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
6 |
439 | 7 import gevent |
444 | 8 import gevent.monkey |
471 | 9 import lockfile.pidlockfile |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
10 import daemon |
439 | 11 |
12 import utils | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
13 from utils import L,D,EX,W |
444 | 14 import fridge |
15 import config | |
16 import sensor_ds18b20 | |
17 import params | |
459 | 18 import uploader |
444 | 19 |
439 | 20 |
21 class Tempserver(object): | |
22 def __init__(self): | |
23 self.readings = [] | |
444 | 24 self.current = (None, None) |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
25 self.fridge = None |
444 | 26 |
27 # don't patch os, fork() is used by daemonize | |
28 gevent.monkey.patch_all(os=False, thread=False) | |
439 | 29 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
30 def __enter__(self): |
444 | 31 self.params = params.Params() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
32 self.fridge = fridge.Fridge(self) |
459 | 33 self.uploader = uploader.Uploader(self) |
444 | 34 self.params.load() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
35 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
36 return self |
439 | 37 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
38 def __exit__(self, exc_type, exc_value, traceback): |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
39 L("Exiting, cleanup handler"); |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
40 self.fridge.off() |
439 | 41 |
444 | 42 def run(self): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
43 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
44 if self.fridge is None: |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
45 raise Exception("Tempserver.run() must be within 'with Tempserver() as server'") |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
46 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
47 # XXX do these go here or in __enter_() ? |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
48 self.start_time = self.now() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
49 self.fridge.start() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
50 self.sensors.start() |
459 | 51 self.uploader.start() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
52 |
444 | 53 # won't return. |
54 while True: | |
456 | 55 try: |
56 gevent.sleep(60) | |
57 except KeyboardInterrupt: | |
58 break | |
444 | 59 |
60 def now(self): | |
61 return utils.monotonic_time() | |
62 | |
443 | 63 def set_sensors(self, sensors): |
447 | 64 if hasattr(self, 'sensors'): |
443 | 65 self.sensors.kill() |
66 self.sensors = sensors | |
67 self.wort_name = sensors.wort_name() | |
68 self.fridge_name = sensors.fridge_name() | |
439 | 69 |
70 def take_readings(self): | |
71 ret = self.readings | |
72 self.readings = [] | |
73 return ret | |
74 | |
75 def pushfront(self, readings): | |
76 """ used if a caller of take_readings() fails """ | |
459 | 77 self.readings = readings + self.readings |
439 | 78 |
443 | 79 # a reading is a map of {sensorname: value}. temperatures |
80 # are float degrees | |
439 | 81 def add_reading(self, reading): |
82 """ adds a reading at the current time """ | |
454 | 83 D("add_reading(%s)" % str(reading)) |
444 | 84 self.readings.append( (reading, self.now())) |
443 | 85 self.current = (reading.get(self.wort_name, None), |
86 reading.get(self.fridge_name, None)) | |
459 | 87 if len(self.readings) > config.MAX_READINGS: |
88 self.readings = self.readings[-config.MAX_READINGS:] | |
439 | 89 |
90 def current_temps(self): | |
443 | 91 """ returns (wort_temp, fridge_temp) tuple """ |
447 | 92 return self.current |
444 | 93 |
94 def setup_logging(): | |
95 logging.basicConfig(format='%(asctime)s %(message)s', | |
96 datefmt='%m/%d/%Y %I:%M:%S %p', | |
462 | 97 level=logging.INFO) |
444 | 98 |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
99 def start(): |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
100 with Tempserver() as server: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
101 server.run() |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
102 |
444 | 103 def main(): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
104 setup_logging() |
444 | 105 |
473
bd29ddb360a5
fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents:
471
diff
changeset
|
106 heredir = os.path.abspath(os.path.dirname(__file__)) |
bd29ddb360a5
fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents:
471
diff
changeset
|
107 pidpath = os.path.join(heredir, 'tempserver.pid') |
471 | 108 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False) |
109 try: | |
110 pidf.acquire(0) | |
111 pidf.release() | |
112 except lockfile.AlreadyLocked, e: | |
113 pid = pidf.read_pid() | |
114 print>>sys.stderr, "Locked by PID %d" % pid | |
115 if pid > 0: | |
116 try: | |
117 os.kill(pid, 0) | |
118 # must still be running PID | |
119 raise e | |
120 except OSError: | |
121 # isn't still running, steal the lock | |
122 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid) | |
123 pidf.break_lock() | |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
124 |
444 | 125 if '--daemon' in sys.argv: |
461
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
126 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
127 logf = open(logpath, 'a+') |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
128 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
129 start() |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
130 else: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
131 with pidf: |
1eb68df9f8ab
Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents:
459
diff
changeset
|
132 start() |
444 | 133 |
134 if __name__ == '__main__': | |
135 main() |