-
Notifications
You must be signed in to change notification settings - Fork 11
/
BeamlineActionsMockup.py
78 lines (62 loc) · 2.67 KB
/
BeamlineActionsMockup.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
from HardwareRepository.BaseHardwareObjects import HardwareObject
from HardwareRepository.TaskUtils import *
from HardwareRepository.CommandContainer import CommandObject
import gevent
import logging
class SimulatedAction:
def __call__(self, *args, **kw):
gevent.sleep(3)
return args
class SimulatedActionError:
def __call__(self, *args, **kw):
raise RuntimeError("Simulated error")
class LongSimulatedAction:
def __call__(self, *args, **kw):
for i in range(10):
gevent.sleep(1)
logging.getLogger("user_level_log").info("%d, sleeping for 1 second", i+1)
return args
class ControllerCommand(CommandObject):
def __init__(self, name, cmd, username=None, klass=SimulatedAction):
CommandObject.__init__(self, name, username)
self._cmd = klass()
self._cmd_execution = None
if self.name() == 'Anneal':
self.addArgument("Time [s]", "float")
if self.name() == 'Test':
self.addArgument("combo test", "combo", [ {"value1": 0, "value2": 1 } ])
def isConnected(self):
return True
@task
def __call__(self, *args, **kwargs):
self.emit('commandBeginWaitReply', (str(self.name()), ))
self._cmd_execution = gevent.spawn(self._cmd, *args, **kwargs)
self._cmd_execution.link(self._cmd_done)
def _cmd_done(self, cmd_execution):
try:
try:
res = cmd_execution.get()
except Exception:
logging.getLogger('HWR').exception("%s: execution failed", str(self.userName()))
self.emit('commandFailed', (str(self.name()), ))
else:
if isinstance(res, gevent.GreenletExit):
# command aborted
self.emit('commandFailed', (str(self.name()), ))
else:
self.emit('commandReplyArrived', (str(self.name()), res))
finally:
self.emit('commandReady')
def abort(self):
if self._cmd_execution and not self._cmd_execution.ready():
self._cmd_execution.kill()
class BeamlineActionsMockup(HardwareObject):
def __init__(self, *args):
HardwareObject.__init__(self, *args)
def init(self):
self.centrebeam = ControllerCommand("centrebeam", None, "Centre beam")
self.quick_realign = ControllerCommand("realign", None, "Quick realign", klass=LongSimulatedAction)
self.anneal = ControllerCommand("Anneal", None, klass=SimulatedActionError)
self.combotest = ControllerCommand("Test", None, "Test with combo box")
def getCommands(self):
return [self.centrebeam, self.quick_realign, self.anneal, self.combotest]