annotate py/tempserver.py @ 174:c49d87bb81b9

fix to absolute path for lockfile, --daemon does chdir("/")
author Matt Johnston <matt@ucc.asn.au>
date Thu, 25 Jul 2013 08:03:14 +0800
parents 5821c5fab919
children be579bcf3c7f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
1 #!/home/matt/templog/venv/bin/python
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
6
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 import gevent
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
8 import gevent.monkey
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
9 import lockfile.pidlockfile
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
10 import daemon
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 import utils
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
13 from utils import L,D,EX,W
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
14 import fridge
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
15 import config
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
16 import sensor_ds18b20
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
17 import params
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
18 import uploader
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
19
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 class Tempserver(object):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 def __init__(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 self.readings = []
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
24 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
25 self.fridge = None
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
26
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
27 # don't patch os, fork() is used by daemonize
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
28 gevent.monkey.patch_all(os=False, thread=False)
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
30 def __enter__(self):
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
31 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
32 self.fridge = fridge.Fridge(self)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
33 self.uploader = uploader.Uploader(self)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
34 self.params.load()
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
35 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
36 return self
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
38 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
39 L("Exiting, cleanup handler");
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
40 self.fridge.off()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
42 def run(self):
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
43
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
44 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
45 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
46
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
47 # 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
48 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
49 self.fridge.start()
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
50 self.sensors.start()
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
51 self.uploader.start()
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
52
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
53 # won't return.
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
54 while True:
157
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
55 try:
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
56 gevent.sleep(60)
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
57 except KeyboardInterrupt:
6d799bee3508 reload config on SIGHUP
Matt Johnston <matt@ucc.asn.au>
parents: 155
diff changeset
58 break
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
59
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
60 def now(self):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
61 return utils.monotonic_time()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
62
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
63 def set_sensors(self, sensors):
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
64 if hasattr(self, 'sensors'):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
65 self.sensors.kill()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
66 self.sensors = sensors
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
67 self.wort_name = sensors.wort_name()
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
68 self.fridge_name = sensors.fridge_name()
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 def take_readings(self):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 ret = self.readings
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 self.readings = []
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 return ret
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 def pushfront(self, readings):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 """ used if a caller of take_readings() fails """
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
77 self.readings = readings + self.readings
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
79 # a reading is a map of {sensorname: value}. temperatures
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
80 # are float degrees
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 def add_reading(self, reading):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 """ adds a reading at the current time """
155
d03157c7ad60 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
83 D("add_reading(%s)" % str(reading))
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
84 self.readings.append( (reading, self.now()))
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
85 self.current = (reading.get(self.wort_name, None),
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
86 reading.get(self.fridge_name, None))
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
87 if len(self.readings) > config.MAX_READINGS:
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 157
diff changeset
88 self.readings = self.readings[-config.MAX_READINGS:]
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 def current_temps(self):
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 143
diff changeset
91 """ returns (wort_temp, fridge_temp) tuple """
148
b32e5a11a4cb few updates, seems to run
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
92 return self.current
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
93
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
94 def setup_logging():
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
95 logging.basicConfig(format='%(asctime)s %(message)s',
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
96 datefmt='%m/%d/%Y %I:%M:%S %p',
163
bf2a17873ba1 less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 162
diff changeset
97 level=logging.INFO)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
98
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
99 def start():
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
100 with Tempserver() as server:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
101 server.run()
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
102
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
103 def main():
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 148
diff changeset
104 setup_logging()
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
105
174
c49d87bb81b9 fix to absolute path for lockfile, --daemon does chdir("/")
Matt Johnston <matt@ucc.asn.au>
parents: 172
diff changeset
106 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
107 pidpath = os.path.join(heredir, 'tempserver.pid')
172
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
108 pidf = lockfile.pidlockfile.PIDLockFile(pidpath, threaded=False)
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
109 try:
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
110 pidf.acquire(0)
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
111 pidf.release()
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
112 except lockfile.AlreadyLocked, e:
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
113 pid = pidf.read_pid()
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
114 print>>sys.stderr, "Locked by PID %d" % pid
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
115 if pid > 0:
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
116 try:
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
117 os.kill(pid, 0)
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
118 # must still be running PID
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
119 raise e
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
120 except OSError:
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
121 # isn't still running, steal the lock
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
122 print>>sys.stderr, "Unlinking stale lockfile %s for pid %d" % (pidpath, pid)
5821c5fab919 use better pidlockfile
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
123 pidf.break_lock()
162
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
124
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
125 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
126 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
127 logf = open(logpath, 'a+')
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
128 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
129 start()
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
130 else:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
131 with pidf:
d73077e8cd67 Add daemon mode with locking, add "disabled" parameter
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
132 start()
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
133
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
134 if __name__ == '__main__':
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
135 main()