-
Notifications
You must be signed in to change notification settings - Fork 21
/
cryo_notifier.py
executable file
·415 lines (377 loc) · 15.9 KB
/
cryo_notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/python
# Copyright (C) 2013 Evan Jeffrey
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
### BEGIN NODE INFO
[info]
name = Cryo Notifier
version = 2.2
description = Send reminders to fill cryos
[startup]
cmdline = %PYTHON% %FILE%
timeout = 20
[shutdown]
message = 987654321
timeout = 5
### END NODE INFO
"""
from labrad import util, types as T
from labrad.server import LabradServer, setting
from labrad.units import Unit, mV, ns, deg, MHz, V, GHz, rad, s
import datetime, time
from twisted.python import log
from twisted.internet import defer, reactor
from twisted.internet.task import LoopingCall
from twisted.internet.defer import inlineCallbacks, returnValue
DEBUG = False
def td_to_seconds(td):
'''
Takes a timedelta object and returns the time interval in seconds
'''
return td.microseconds*1e-6 + td.seconds + td.days * 24.0 * 3600.0
'''
Registry keys:
[ "", "Servers", "Cryo Notifier" ]
timers = [ ("name1", interval[s]),
("name2", interval[s]),
("name3", interval[s])]
name1_reset = timestamp
notify_users = ["user1, "user2"]
timers_enabled = True
'''
@inlineCallbacks
def start_server(cxn, node_name, server_name):
"""Start an external server
Use this to make sure dependencies are running, ie the telecom server.
"""
if server_name in cxn.servers:
returnValue(True)
if node_name in cxn.servers:
p = cxn[node_name].packet()
p.start(server_name)
yield p.send()
returnValue(True)
raise RuntimeError("Unable to start server %s" % server_name)
class CryoNotifier(LabradServer):
"""Mass email when someone forgets to fill cryos
Todo: subclass DeviceServer and allow multiple notifyer devices per server.
This is a real problem. There are several places in this server at which we
make LabRAD calls to device servers without explicitly stating which device
we'd like to talk to. For example, we ask the lakeshore diode controller
for temperature data after a selectDevice() call with no arguments. This
means we get whatever the first lakeshare diode box on our LabRAD network
happens to be.
"""
name = 'Cryo Notifier'
def temperatureCheckFunc(self, channel, temp):
try:
#pylabrad units are more broke than Ted on a Thursday night. You
#can't do comparison without first going to float in a common unit.
return temp['K'] > self.temperatureBounds[channel]['K']
except KeyError:
print("Channel %s has no bound, unable to check"%channel)
return False
def sleepyTimerCheckFunc(self, channel, timeLeft):
''' check if the time will run out tonight, i.e. if it's currently
after 10PM and cryos will run out before 8 AM. '''
now = time.localtime()
now = datetime.time(now.tm_hour, now.tm_min)
night, morn = datetime.time(*self.sleepyTime[0]), datetime.time(*self.sleepyTime[1])
nightTime = (night > morn and (now >= night or now < morn)) or \
(night < morn and (now >= night and now < morn))
if not nightTime:
return False
# now check if we have enough time left
if now < morn:
minsTillMorn = (morn.hour - now.hour) * 60 + morn.minute - now.minute
else:
minsTillMorn = (morn.hour - now.hour + 24) * 60 + morn.minute - now.minute
if timeLeft < minsTillMorn*60:
print "%.1f hours until morning, %.1f hours remaining!" % (minsTillMorn/60.0, timeLeft/3600.0)
return timeLeft < minsTillMorn*60
@inlineCallbacks
def matchNode(self):
''' Try to determine what fridge we're on by matching a node name to a
folder in the registry. '''
# pull out node names
nodes = [x for x in self.client.servers if x.lower().startswith('node')]
nodes = [x.partition(' ')[2] for x in nodes]
p = self.reg.packet()
p.cd(self.path)
p.dir()
ans = yield p.send()
dirs = ans['dir'][0]
matches = [x for x in nodes if x in dirs]
if not matches:
print "No node <--> registry directory matches found. Using %s" % self.path
returnValue(self.path)
elif len(matches) > 1:
print "Multiple node matches found! %s Sticking with %s" % (str(matches), self.path)
returnValue(self.path)
else:
print "Found node: %s" % matches[0]
yield self.reg.cd(matches[0])
self.path.append(matches[0])
returnValue(self.path)
@inlineCallbacks
def initServer(self):
self.sent_notifications = set()
self.cold = False
self.reg = self.client.registry
self.ruox = self.client.lakeshore_ruox
self.diodes = self.client.lakeshore_diodes
#type of parameter -> {'data'->data, 'func'->func to check if notification needed}
self.thingsToCheck = {"timers":
{"data": None,
"func": lambda channel, x: x<0},
"temperatures":
{"data": None,
"func": self.temperatureCheckFunc},
"sleepyTimers":
{"data": None,
"func": self.sleepyTimerCheckFunc}
}
self.path = ['', 'Servers', self.name]
self.path = yield self.matchNode()
if self.path[0]==self.name:
self.node = 'node_vince'
else:
self.node = 'node_'+self.path[0]
print self.path
yield start_server(self.client, self.node, 'Telecomm Server')
self.cb = LoopingCall(self.checkForAndSendAlerts)
self.cb.start(interval=10.0, now=True)
@setting(5, returns='*(sv[s])')
def query_timers(self, c):
'''
Returns the list of timers and their time remaining. Expired
timers will have a negative time remaining. If timing is
disabled all timers will list zero.
'''
if self.enabled:
yield self.loadRegistryInfo()
yield self.update_timers()
rv = self.thingsToCheck["timers"]["data"]
else:
rv = [(t, 0) for t in self.timers]
returnValue(rv)
@setting(6, returns='*(sv[s])')
def query_temperatures(self, c):
'''
Returns the list of temperatures and their current values.
'''
rv = self.thingsToCheck['temperatures']['data']
returnValue(rv)
@setting(10, timer_name='s', message='s', returns='v[s]')
def reset_timer(self, c, timer_name, message=''):
if timer_name not in self.timers:
raise KeyError("Timer %s unknown" % name)
else:
dt = datetime.datetime.now()
p = self.reg.packet()
p.set("%s_reset" % timer_name, dt)
p.get("%s_count" % timer_name, False, -1)
p.cd('log')
p.set(dt.isoformat(), (timer_name, message))
p.cd(self.path)
rv = yield p.send()
counter_val = rv['get']
if counter_val > -1:
print('Incrementing counter %s to %d' % (timer_name, counter_val+1))
p = self.reg.packet()
p.set('%s_count' % timer_name, rv['get'] + 1)
yield p.send()
returnValue(self.timers[timer_name][0])
@setting(11, name='s', val='w', returns='w')
def counter(self, c, name, val=None):
if name not in self.timers:
raise KeyError("Counter %s unknown" % name)
p = self.reg.packet()
if val is not None:
p.set('%s_count' % name, val)
p.get('%s_count' % name, False, 0)
rv = yield p.send()
returnValue(rv['get'])
@setting(12, returns='*(s,w)')
def query_counters(self, c):
if self.enabled:
yield self.loadRegistryInfo()
yield self.update_timers()
rv = self.counters.items()
returnValue(rv)
@setting(15, username='s', returns='b')
def validate_user(self, c, username):
'''
Check to see if user is on list of valid users.
If not, but they are known by SMS server, add them
to list of users.
'''
if not self.cold: # Allow anyone when fridge is warm
return True
if self.enabled and (username.lower() in [x.lower() for x in self.users]):
return True
else:
return False
@setting(20, enable='b')
def enable_timers(self,c,enable):
'''
Turn on or off timing. This should be turned on when the fridge is cold.
'''
p = self.reg.packet()
p.set("timers_enabled", enable)
yield p.send()
self.enabled = enable
@setting(25, returns='*s')
def allowed_users(self, c):
'''Return list of users currently enabled for notification.
Only these users should be able to use qubit sequencer.'''
return self.users
@inlineCallbacks
def loadRegistryInfo(self):
p = self.reg.packet()
p.cd(self.path)
p.get('temperatures', key='temperatures')
p.get('timers', key='timers') #List of timers for all fridges
p.get('timers_enabled', key='enabled') #global bool
p.get('notify_users', key='users') #List of who receives notifications
p.get('notify_email', False, [], key='email') #...and their emails.
p.get('sleepyTime', key='sleepyTime') # night time: ((HH, MM), (HH, MM))
ans = yield p.send()
self.users = ans['users']
self.email = ans['email']
self.enabled = ans['enabled']
self.sleepyTime = ans['sleepyTime']
self.timerSettings = dict(ans['timers'])
self.temperatureBounds = dict(ans['temperatures'])
@inlineCallbacks
def update_temperatures(self):
data = {}
#Check to see if we're still cold
try:
p = self.client.lakeshore_diodes.packet()
p.select_device()
p.temperatures()
ans = yield p.send()
# only keep the ones we're interested in
names = ["4Kin", "4Kout", "77K",]# "Ret", "Mix", "Xchg", "Still", "Pot"]
data.update(dict(zip(names, ans['tempetratures'])))
self.cold = ans['temperatures'][1]['K'] < 10.0
except Exception:
#Assume we are cold if we can't reach the lakeshore server
self.cold = True
p = self.ruox.packet()
p.select_device(key='device') #This is BAD. We should actually pick a device
try:
p.named_temperatures(key='temps')
resp = yield p.send()
data.update(dict([(x[0],x[1][0]) for x in resp['temps']]))
except AttributeError:
p.temperatures(key='temps')
resp = yield p.send()
data.update(dict(zip(['Mix1', 'Mix2', 'Still', 'Pot', 'Xchg'],
[x[0] for x in resp['temps']])))
nodeName = resp['device'].split(' ')[0]
#Now we do something really ugly. We need to be able to handle the fact
#that the temperature bound data in the registry comes with cryostat
#names attached, eg Vince:Mix1. However the named temperatures from the
#ruox server come as Mix1, without the cryostat name. To handle this we
#figure out what _node_ the ruox server is on and add that to the name.
#This is really pretty bad but I can't think of a good way to fix it.
#The next person to service this server (heh) can take up where I left
#off. Upgrade the temperature checking facilities to more rationally
#sort the data.
data = dict([(nodeName+':'+k,v) for k,v in data.items()])
self.thingsToCheck['temperatures']['data'] = data
@inlineCallbacks
def update_timers(self):
"""Helper function to read timers.
Sets self.currentData["temperatures"] to a dict mapping timer names ->
remaining time. Remaining times are Values with time units.
"""
now = datetime.datetime.now()
p = self.reg.packet()
for timer_name in self.timerSettings:
p.get('%s_reset' % timer_name, True, now, key=timer_name)
p.get('%s_count' % timer_name, False, -1, key=timer_name+"-count")
ans = yield p.send()
self.timers = {}
self.counters = {}
for timer_name in self.timerSettings:
self.timers[timer_name] = \
(self.timerSettings[timer_name].inUnitsOf('s'),
ans[timer_name])
self.counters[timer_name] = ans[timer_name+"-count"]
remaining_time = [(name, (x[0] - td_to_seconds(now-x[1])*s )) \
for name, x in self.timers.iteritems()]
self.thingsToCheck['timers']['data'] = remaining_time
rt_sleepy = [(name + ' Overnight', x) for name, x in remaining_time]
self.thingsToCheck['sleepyTimers']['data'] = rt_sleepy
@inlineCallbacks
def checkForAndSendAlerts(self):
'''
Timed callback to check timers and send notifications.
'''
yield self.loadRegistryInfo()
yield self.update_timers()
yield self.update_temperatures()
if not (self.enabled and self.cold):
return
alerts = []
for thingToCheck in self.thingsToCheck:
data = dict(self.thingsToCheck[thingToCheck]['data'])
thereIsProblem = self.thingsToCheck[thingToCheck]['func']
for t in data:
if thereIsProblem(t, data[t]):
if t not in self.sent_notifications:
self.sent_notifications.add(t)
alerts.append(t)
else:
pass
else:
self.sent_notifications.discard(t)
if alerts:
print "Alerts exist on: ", alerts
print "Notifying the following users: ", self.users
#SMS notifications
yield self.sendSMSNotifications("Cryo Alert", "%s cryos need to be filled." % (alerts,))
yield self.sendEmailNotifications("Cryo Alert", "%s cryos need to be filled." % (alerts,))
@inlineCallbacks
def sendEmailNotifications(self, subj, msg):
"""Notify all users via email"""
try:
print "sending email notifications to: ", self.email
if not DEBUG:
p = self.client.telecomm_server.packet()
p.send_mail(self.email, subj, msg)
yield p.send()
except Exception:
print "Sending mail to users %s failed" % self.email
@inlineCallbacks
def sendSMSNotifications(self, subj, msg):
"""Notify all users via SMS"""
#Try each user separately in case one fails
print("Sending SMS notifications to %s"%self.users)
for u in self.users:
try:
if not DEBUG:
p = self.client.telecomm_server.packet()
p.send_sms(subj, msg, u)
yield p.send()
except Exception:
print("SMS attempt for user %s failed" % u)
__server__ = CryoNotifier()
if __name__ == '__main__':
from labrad import util
util.runServer(__server__)