annotate py/utils.py @ 582:cebec9b40ad2

merge
author Matt Johnston <matt@ucc.asn.au>
date Mon, 30 Nov 2015 21:45:17 +0800
parents ccfaa4351fd8
children 02aff9ff8d24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import sys
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
3 import ctypes
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import time
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import select
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import logging
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
7 import binascii
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
8 import json
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
9 import datetime
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
10 import collections
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
448
fe729664a5e6 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
12 D = logging.debug
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 L = logging.info
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 W = logging.warning
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 E = logging.error
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 DEFAULT_TRIES = 3
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 READLINE_SELECT_TIMEOUT = 1
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
443
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
20 def EX(msg, *args, **kwargs):
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
21 kwargs['exc_info'] = True
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
22 logging.error(msg, *args, **kwargs)
bca470d153fd a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 439
diff changeset
23
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 clock_gettime = None
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 no_clock_gettime = True
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 def monotonic_time():
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 global clock_gettime
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 global no_clock_gettime
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 if no_clock_gettime:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 return time.time()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 class timespec(ctypes.Structure):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 _fields_ = [
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 ('tv_sec', ctypes.c_long),
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 ('tv_nsec', ctypes.c_long)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 ]
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 if not clock_gettime:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 try:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 librt = ctypes.CDLL('librt.so.0', use_errno=True)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 clock_gettime = librt.clock_gettime
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 except:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 W("No clock_gettime(), using fake fallback.")
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 no_clock_gettime = True
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 return time.time()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 t = timespec()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 CLOCK_MONOTONIC = 1 # see <linux/time.h>
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 errno_ = ctypes.get_errno()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 raise OSError(errno_, os.strerror(errno_))
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 return t.tv_sec + t.tv_nsec * 1e-9
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 # decorator, tries a number of times, returns None on failure, sleeps between
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 # Must be used as "@retry()" if arguments are defaulted
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 def retry(retries=DEFAULT_TRIES, try_time = 1):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 def inner(func):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 def new_f(*args, **kwargs):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 for i in range(retries):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 d = func(*args, **kwargs)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 if d is not None:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 return d
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 time.sleep(try_time)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 return None
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
67 new_f.__name__ = func.__name__
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 return new_f
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 return inner
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 def readline(sock):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 timeout = READLINE_SELECT_TIMEOUT
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 buf = ''
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 while True:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 (rlist, wlist, xlist) = select.select([sock], [], [], timeout)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 if sock not in rlist:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 # hit timeout
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 return None
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 c = sock.recv(1)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 if c == '':
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 # lightblue timeout
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 return None
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 if c == '\r':
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 continue
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 buf += c
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 if c == '\n':
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 return buf
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 def crc16(buff, crc = 0, poly = 0x8408):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 l = len(buff)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 i = 0
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 while i < l:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 ch = ord(buff[i])
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 uc = 0
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 while uc < 8:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 if (crc & 1) ^ (ch & 1):
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 crc = (crc >> 1) ^ poly
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 else:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 crc >>= 1
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 ch >>= 1
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 uc += 1
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 i += 1
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 return crc
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 def cheap_daemon():
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 L("Daemonising.")
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 sys.stdout.flush()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 sys.stderr.flush()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 out = file('/dev/null', 'a+')
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 os.dup2(out.fileno(), sys.stdout.fileno())
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 os.dup2(out.fileno(), sys.stderr.fileno())
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 try:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 pid = os.fork()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 if pid > 0:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 sys.exit(0)
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
120 except OSError as e:
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 E("Bad fork()")
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 sys.exit(1)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 os.setsid()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 try:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 pid = os.fork()
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 if pid > 0:
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 sys.exit(0)
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
130 except OSError as e:
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 E("Bad fork()")
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 sys.exit(1)
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
134 def uptime():
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
135 try:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
136 return float(open('/proc/uptime', 'r').read().split(' ', 1)[0])
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 448
diff changeset
137 except Exception as e:
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 443
diff changeset
138 return -1
439
31ac84425a2d python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
140
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
141 def json_load_round_float(s, **args):
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 551
diff changeset
142 return json.loads(s,parse_float = lambda f: round(float(f), 2), **args)
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
143
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
144 class NotTooOften(object):
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
145 """ prevents things happening more than once per limit.
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
146 Isn't monotonic, good enough for logging. eg
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
147 self.logfailure = NotTooOften(180) # 3 minutes
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
148 ...
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
149 if self.logfailure():
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
150 L("blah")
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
151 """
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
152 def __init__(self, limit):
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
153 """ limit is a delay in seconds or TimeDelta """
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
154 if type(limit) is datetime.timedelta:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
155 self.limit = limit
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
156 else:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
157 self.limit = datetime.timedelta(seconds=limit)
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
158
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
159 # must be positive
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
160 assert self.limit > datetime.timedelta(0)
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
161 self.last = datetime.datetime(10, 1, 1)
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
162
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
163 def __call__(self):
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
164 if datetime.datetime.now() - self.last > self.limit:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
165 self.last = datetime.datetime.now()
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
166 return True
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
167
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
168 def log(self, msg):
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
169 """ calls L(msg) if it isn't too often, otherwise D(msg)
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
170 """
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
171 if self():
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
172 L(msg + " (log interval %s)" % str(self.limit))
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
173 else:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
174 D(msg)
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
175
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
176 Period = collections.namedtuple('Period', 'start end')
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
177 class StepIntegrator(object):
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
178 """
570
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
179 Takes on/off events and a monotonically increasing timefn. Returns the integral
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
180 of (now-limittime, now) over those events.
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
181
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
182 >>> s = StepIntegrator(lambda: t, 40)
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
183 >>> t = 1
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
184 >>> s.turn(1)
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
185 >>> t = 10
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
186 >>> s.turn(0)
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
187 >>> t = 20
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
188 >>> s.turn(1)
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
189 >>> t = 30
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
190 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
191 19
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
192 >>> s.turn(0)
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
193 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
194 19
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
195 >>> t = 35
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
196 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
197 19
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
198 >>> t = 42
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
199 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
200 18
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
201 >>> t = 52
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
202 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
203 10
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
204 >>> t = 69
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
205 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
206 1
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
207 >>> t = 70
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
208 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
209 0
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
210 >>> t = 170
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
211 >>> print(s.integrate())
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
212 0
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
213 """
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
214 def __init__(self, timefn, limittime):
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
215 # _on_periods is a list of [period]. End is None if still on
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
216 self._on_periods = []
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
217 self._timefn = timefn
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
218 self._limittime = limittime
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
219
570
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
220 def set_limit(self, limittime):
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
221 if self._limittime == limittime:
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
222 return
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
223 self._limittime = limittime
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
224 self._trim()
570
afe2eb17723e untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 569
diff changeset
225
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
226 def turn(self, value):
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
227 if not self._on_periods:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
228 if value:
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
229 self._on_periods.append(Period(self._timefn(), None))
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
230 return
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
231
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
232 # state hasn't changed
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
233 on_now = (self._on_periods[-1].end is None)
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
234 if value == on_now:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
235 return
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
236
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
237 if value:
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
238 self._on_periods.append(Period(self._timefn(), None))
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
239 else:
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
240 self._on_periods[-1] = self._on_periods[-1]._replace(end = self._timefn())
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
241
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
242 def _trim(self):
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
243 begin = self._timefn() - self._limittime
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
244 # shortcut, first start is after begin
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
245 if not self._on_periods or self._on_periods[0].start >= begin:
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
246 return
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
247
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
248 new_periods = []
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
249 for s, e in self._on_periods:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
250 if s == e:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
251 continue
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
252 elif s >= begin:
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
253 new_periods.append(Period(s,e))
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
254 elif e is not None and e < begin:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
255 continue
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
256 else:
571
ccfaa4351fd8 scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 570
diff changeset
257 new_periods.append(Period(begin, e))
569
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
258 self._on_periods = new_periods
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
259
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
260 def integrate(self):
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
261 self._trim()
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
262 tot = 0
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
263 for s, e in self._on_periods:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
264 if e is None:
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
265 e = self._timefn()
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
266 tot += (e-s)
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
267 return tot
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
268
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
269
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
270
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
271
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
272
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
273
d7575050e4be StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 564
diff changeset
274