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