-
Notifications
You must be signed in to change notification settings - Fork 1
/
instrument.py
487 lines (401 loc) · 14.8 KB
/
instrument.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
from fnmatch import fnmatch
import iso8601
from pubsub import Channel, publisher, subscriber, restful
class Facility(object):
def __init__(self):
self.instruments = {}
def add(self, instrument):
self.instruments[name.name] = instrument
def remove(self, name):
del self.instrument[name]
class Instrument(object):
def __init__(self, name):
self.name = name
CHANNELS = [
ConsoleChannel,
ControlChannel,
DataChannel,
DeviceChannel,
EventChannel,
ExperimentChannel,
QueueChannel,
]
self.channel = dict((chan.name,chan()) for chan in CHANNELS)
class ControlChannel(Channel):
"""
NICE controller.
Publisher-subscriber relationship is reversed.
"""
name = 'control'
def __init__(self):
Channel.__init__(self, fan_in=True, fan_out=False)
def channel_state(self):
return {}
# No channel_reset since publisher should not send it in this case.
@publisher
def console(self, command):
return self.emit('console', command)
@publisher
def complete(self, command):
return self.emit('complete', command)
@publisher
def move(self, move_list, relative):
return self.emit('move', move_list, relative)
@publisher
def read(self, nodeID):
"""
Poll dependent device drivers for the value of a device node.
Note: probably want to do a console read command instead so that
you can poll multiple devices and use regular expressions to do so.
The new values will come through the device channel, so no need to
wait for the callback to return.
"""
return self.emit('read', nodeID)
@publisher
def echo(self, message):
return self.emit('echo', message)
@publisher
def setUserBreak(self, commandID, flag):
return self.emit('setUserBreak', commandID, flag)
@publisher
def setSystemBreak(self, commandID, flag):
return self.emit('setSystemBreak', commandID, flag)
class EventChannel(Channel):
"""
NICE logger interface.
"""
name = 'events'
def __init__(self):
Channel.__init__(self, fan_in=False, fan_out=True)
self.channel_reset([])
def channel_reset(self, state):
self.events = state[:10]
def channel_state(self):
return { 'events': self.events }
@publisher
def created(self, event):
self.events.append(event)
self.events = self.events[:10]
#print "event channel is", self.channel
self.emit('created', event)
@publisher
def acknowledged(self, event_id):
pass
@publisher
def resolved(self, event_id, resolution):
pass
class ConsoleChannel(Channel):
name = 'console'
def __init__(self):
Channel.__init__(self, fan_in=False, fan_out=True)
self.channel_reset([])
def channel_reset(self, state):
self.events = state
def channel_state(self):
return { 'events': self.events }
@publisher
def report(self, event):
self.events.append(event)
self.emit('report', event)
class DataChannel(Channel):
name = 'data'
def __init__(self):
Channel.__init__(self, fan_in=False, fan_out=True)
self.records = []
def channel_state(self):
return {'records':self.records}
def channel_reset(self, state):
self.records = state
@publisher
def data(self, record):
# Note: may want instrument specific handling of the data so that we only send
# a summary of the detector image to the server, not the whole detector. We
# will still want to store the individual detector frames so that we can
# return them to the client on request..
#print "data command",record['command']
if record['command'] == "Configure":
self.records = []
self.records.append(record)
#print "emitting data",record['command']
self.emit('record', record)
class DeviceChannel(Channel):
"""
NICE device model.
"""
name = 'device'
def __init__(self):
Channel.__init__(self, fan_in=False, fan_out=True)
self.devices = None
self.nodes = None
self.view = {}
def channel_reset(self, state):
#print "receiving device state"
self.devices, self.nodes, self.view = state
_fixup_devices(self.devices,self.nodes)
def channel_state(self):
#print "getting device state"
return {'devices':self.devices, 'view':self.view}
@restful
def device(self, response):
pattern = response.get_argument('id','*')
return dict((deviceID, device)
for deviceID,device in self.devices.items()
if fnmatch(deviceID,pattern))
@restful
def node(self, response):
pattern = response.get_argument('id','*.*')
return dict((".".join((deviceID,nodeID)),node)
for deviceID,device in self.devices.items()
for nodeID,node in device['nodes'].items()
if fnmatch(".".join((deviceID,nodeID)),pattern))
@subscriber
def device_hierarchy(self):
return self.view # take the "structure" part
@subscriber
def filled_device_hierarchy(self):
from copy import deepcopy
devices = self.devices
nodes = self.nodes
filled_structure = deepcopy(self.view)
def get_value(dottedname):
keys = dottedname.split('.')
def fill_children(item):
if len(item['children']['elements']) == 0:
value = -999
new_id = "unknown"
if item['nodeID'] in nodes.keys():
node = nodes[item['nodeID']]
# then we're a node
device = devices[node.deviceID]
value = node.currentValue.userVal.val
new_id = node['id']
elif item['nodeID'] in devices:
device = devices[item['nodeID']]
primaryNodeID = device['primaryNodeID']
if primaryNodeID == '': primaryNodeID = device['visibleNodeIDs'][0]
primaryNode = nodes[item['nodeID'] +'.'+ primaryNodeID]
value = primaryNode['currentValue']['userVal']
new_id = primaryNode['id']
item['value'] = value
item['id'] = new_id
else:
for i in item['children']['elements']:
fill_children(i)
fill_children(filled_structure)
return filled_structure
@publisher
def added(self, devices, nodes):
"""
Nodes added to the instrument. Forward their details to the
clients.
"""
_fixup_devices(devices, nodes)
self.devices.update(devices)
self.emit('added', devices)
@publisher
def removed(self, devices, nodes):
"""
Nodes removed from the instrument. Forward their names to the clients.
"""
deviceIDs = devices.keys()
for device in deviceIDs:
del self.devices[device]
self.emit('removed', deviceIDs)
@publisher
def changed(self, nodes):
"""
Node value or properties changed. Forward the details to the clients.
"""
for node in nodes.values():
#print node
self.devices[node['deviceID']]['nodes'][node['nodeID']] = node
# May want to do bandwidth limiting, an only send updates to big nodes
# such as 2-D detector and ROI mask every minute rather than every
# time they are updated.
self.emit('changed', nodes)
def _fixup_devices(devices, nodes):
for v in devices.values():
v['nodes'] = {}
#print v['primaryNodeID'],v['stateNodeID'],v['visibleNodeIDs']
v['primaryNodeID'] = v['primaryNodeID'].split('.')[1] if v['primaryNodeID'] else ''
v['stateNodeID'] = v['stateNodeID'].split('.')[1] if v['stateNodeID'] else ''
v['visibleNodeIDs'] = [id.split('.')[1] for id in v['visibleNodeIDs']]
for v in nodes.values():
devices[v['deviceID']]['nodes'][v['nodeID']] = v
class ExperimentChannel(Channel):
"""
NICE experiment model.
Subscribers emit 'subscribe' when first connected, which returns the
details of the currently active experiment.
Subscribers should expect the following events::
exp.on('changed', function (data) {})
update the details of the current experiment, or switch to a
new experiment
exp.on('reset', function (data) {})
get details of the current experiment
Clients can also use direct HTTP requests to retrieve data:
experiment/state
get details of the current experiment
experiment/list
get list of [id, title, date] for all experiments sorted by id
The creationDateTimestamp in the experiment data record is stored in
milliseconds since Jan 1, 1970. The date returned in the list of
experiment titles and dates is a string such as 2007-01-25T07:00:00-05:00.
"""
name = 'experiment'
def __init__(self):
Channel.__init__(self, fan_in=False, fan_out=True)
self.experiments = {}
self.current = {}
def channel_state(self):
return self.current
def channel_reset(self, state):
self.experiments, self.current = state
@restful
def list(self, response):
"""
Returns { experiments: [[id, title, date], ...] }.
Experiment id, title and date are strings, with date formatted
like 2007-01-25T07:00:00-05:00. The experiments are sorted by
experiment id.
"""
items = ((exp['id'], exp['title'], iso8601.format_date(exp['creationTimeStamp']*0.001))
for exp in self.experiments.values())
return { 'experiments': list(sorted(items)) }
@publisher
def added(self, data):
"""
Experiment added, so record new id and title.
"""
self.experiments[data['id']] = data
## Not maintaining a list of experiments on browser, so don't need to
## signal that a new experiment was added.
#self.emit('added', data['id'], data['title'])
@publisher
def changed(self, data):
"""
Current experiment updated.
"""
self.experiments[data['id']] = data
self.current = data
self.emit('changed', data)
@publisher
def selected(self, data):
"""
Changed active experiment.
"""
self.current = data
self.emit('changed', data)
class QueueChannel(Channel):
"""
NICE queue model.
Publisher follows NICE queue API, but with linked list of children
replaced by a normal list, and with parentID/prevID removed from the
node details for ease in maintaining a consistent queue state on the
client.
Subscribers emit 'subscribe' when first connected, which returns the
complete queue. A queue node looks like::
node = {
"id": int,
"parentID": int,
"children": [node, ...],
"status": {
"commandStr": string,
"errors": [string, ...],
"isBreakPoint": boolean,
"metaState": string,
"state": "QUEUED|RUNNING|CHILDREN|FINISHED|SKIPPED",
}
},
"origin": int,
Queue path is a list of integers.
Queue status is "IDLE|STOPPING|BUSY|SUSPENDED|SUSPENDING|SHUTDOWN".
Subscribers should expect the following events::
queue.on('added', function (path, node) {})
add the nodes and all its children as a child of the parent node
after the sibling node, or at the beginning if sibling is 0.
queue.on('removed', function (path) {})
remove the node
queue.on('moved', function (oldpath, newpath, node) {})
remove the node and add it to parent after sibling
queue.on('changed', function (path, status) {})
update the status of the node
queue.on('status', function (queue_status) {})
update the status of the queue
queue.on('reset', function (node) {})
replace the queue with the given queue
Rather than maintaining the state on the client, subscribers can
simply resubmit the subscribe message to get an up-to-date version
of the queue.
"""
name = 'queue'
def __init__(self):
Channel.__init__(self, fan_in=False, fan_out=True)
self.queue_root = { 'children': [] }
self.queue_status = 'IDLE'
def channel_state(self):
return { 'queue': self.queue_root, 'status': self.queue_status }
def channel_reset(self, state):
self.queue_root = state[0]
self.queue_status = state[1]
#print self.queue_root
#print "queue status",self.queue_status
@publisher
def added(self, path, node):
"""
Node added to the queue. Forward the details to the
clients.
"""
queue_add(self.queue_root, path, node)
#print "queue add",[n['id'] for n in nodes]
self.emit('added', path, node)
@publisher
def removed(self, path, node):
"""
Nodes removed from the queue. Forward them to the clients.
"""
#print "queue remove",nodeIDs[0]
queue_remove(self.queue_root, path)
self.emit('removed', path)
@publisher
def moved(self, old, new, node):
"""
Nodes moved from the instrument. Forward their names to the clients.
"""
#print "queue move",nodeID,parentID,prevID
queue_remove(self.queue_root, old)
queue_add(self.queue_root, new, node)
self.emit('moved', old, new, node)
@publisher
def changed(self, path, nodeID, status):
"""
Node state changed. Forward the details to the clients.
"""
queue_update(self.queue_root, path, status)
#print "queue change",nodeID
self.emit('changed', path, status)
@publisher
def status(self, status):
"""
Queue state changed.
"""
self.queue_status = status
self.emit('status', status)
def queue_update(root, path, status):
"""
Update the status on a node.
"""
for idx in path: root = root['children'][idx]
root['status'] = status
def queue_remove(root, path):
"""
Remove a set of nodes from the queue.
"""
for idx in path[:-1]: root = root['children'][idx]
del root['children'][path[-1]]
def queue_add(root, path, node):
"""
Add a node to the queue in a given position
"""
for idx in path[:-1]: root = root['children'][idx]
root['children'].insert(path[-1], node)