-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComServer_tango_client.py
More file actions
200 lines (174 loc) · 7.68 KB
/
Copy pathComServer_tango_client.py
File metadata and controls
200 lines (174 loc) · 7.68 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 14 14:39:56 2020
@author: Michael Schneider <mschneid@mbi-berlin.de>, Max Born Institut Berlin
"""
import socket
from threading import Lock, Thread
from time import time
import tango
from tango import AttrQuality, DevState
from tango.server import Device, attribute, command
from tango.server import device_property
from tango import READ, READ_WRITE
class P04_beamline(Device):
DYN_ATTRS = [
dict(name='photonenergy', label='photon energy', dtype=tango.DevFloat,
access=READ_WRITE, unit='eV', format='%6.2f', min_value=240,
max_value=2000),
dict(name='exitslit', label="exit slit", dtype=tango.DevFloat,
access=READ_WRITE, unit="um", format="%4.0f"),
dict(name='helicity', label='helicity', dtype=tango.DevLong,
access=READ_WRITE, min_value=-1, max_value=1),
dict(name='mono', label="monochromator", dtype=tango.DevFloat,
access=READ, unit="eV", format="%6.2f"),
dict(name='undugap', label='undulator gap', dtype=tango.DevFloat,
access=READ, unit='mm'),
# dict(name='undufactor', label='undulator scale factor',
# access=READ, format='%3.2f', dtype=tango.DevFloat),
dict(name='undushift', label='undulator shift', dtype=tango.DevFloat,
access=READ, unit='mm'),
dict(name='ringcurrent', label='ring current', dtype=tango.DevFloat,
access=READ, unit='mA'),
# dict(name='keithley1', label='beamline keithley', dtype=tango.DevFloat,
# access=READ),
# dict(name='keithley2', label='user keithley', dtype=tango.DevFloat,
# access=READ),
dict(name='slt2hleft', label='slit hor left', dtype=tango.DevFloat,
access=READ),
dict(name='slt2hright', label='slit hor right', dtype=tango.DevFloat,
access=READ),
dict(name='slt2vgap', label='slit ver gap', dtype=tango.DevFloat,
access=READ),
dict(name='slt2voffset', label='slit ver offset', dtype=tango.DevFloat,
access=READ),
# dict(name='exsu2bpm', label='exsu2bpm', dtype=tango.DevFloat,
# access=READ),
# dict(name='exsu2baffle', label='exsu2baffle', dtype=tango.DevFloat,
# access=READ),
# dict(name='pressure', label='experiment pressure', access=READ,
# dtype=tango.DevFloat, unit='mbar', format='%.2E'),
dict(name='screen', label='beamline screen', dtype=tango.DevLong,
access=READ_WRITE, min_value=0, max_value=2,
enum_labels=['closed', 'mesh', 'open'])
]
ready_to_move = attribute(
name='ready_to_move', label='in position', access=READ,
dtype=tango.DevBoolean, polling_period=1000, fread="is_movable")
host = device_property(dtype=str, mandatory=True, update_db=True)
port = device_property(dtype=int, default_value=3002)
def init_device(self):
Device.init_device(self)
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((self.host, self.port))
self.s.setblocking(True)
self.lock = Lock()
self.set_state(DevState.ON)
energy = self.read_attr('photonenergy')[0]
self._setpoint_E = [energy, energy]
self._setpoint_helicity = self.read_attr('helicity')[0]
def initialize_dynamic_attributes(self):
# TODO: setup polling and event filter
for d in self.DYN_ATTRS:
new_attr = attribute(fget=self.read_general,
fset=self.write_general, **d)
self.add_attribute(new_attr)
@command(dtype_in=str)
def query(self, msg):
'''Send a query and wait for its reply.'''
if self.lock.acquire(timeout=0.5):
if not msg.endswith(' eoc'):
msg += ' eoc'
# print('sent:', msg, file=self.log_debug)
self.s.sendall(msg.encode())
ans = self.s.recv(1024).decode()
# print('received:', ans, file=self.log_debug)
assert ans.endswith('eoa')
self.lock.release()
return ans[:-4]
else:
print(f"can't send '{msg}': socket is locked", file=self.log_error)
return 'busy'
def read_general(self, attr):
key = attr.get_name()
# print('reading', key, file=self.log_debug)
val, time, quality = self.read_attr(key)
attr.set_value(val)
# def write_general(self, attr):
# key = attr.get_name()
# val = attr.get_write_value()
# send_attrs = ['photonenergy', 'exitslit', 'helicity', 'screen']
# cmd = 'send' if key in send_attrs else 'set'
# ntries = 0
# while ntries < 10:
# ntries += 1
# if self.is_movable():
# ans = self.query(f'{cmd} {key} {val}')
# print(f'setting {key}: {val} ({ntries}/10)', file=self.log_debug)
# if ans == 'started':
# self.set_state(DevState.MOVING)
# print(f'[key] moving to {val}', file=self.log_debug)
# return
# time.sleep(1)
# print(f'could not send {key} to {val}', file=self.log_error)
def write_general(self, attr):
key = attr.get_name()
val = attr.get_write_value()
send_attrs = ['photonenergy', 'exitslit', 'helicity', 'screen']
cmd = 'send' if key in send_attrs else 'set'
if key == 'photonenergy':
self._setpoint_E[0] = val
if key == 'helicity':
self._setpoint_helicity = val
ans = self.query(f'{cmd} {key} {val}')
print(f'setting {key}: {val}', file=self.log_debug)
if ans == 'started':
self._setpoint_E[1] = val
self.set_state(DevState.MOVING)
print(f'[key] moving to {val}', file=self.log_debug)
return
print(f'could not send {key} to {val}', file=self.log_error)
def is_movable(self):
'''Check whether undulator and monochromator are in position.'''
in_pos = self.query('check photonenergy')
in_pos= True if in_pos == '1' else False
if (self._setpoint_E[0] != self._setpoint_E[1]) and in_pos:
ans_set = self.query(f'send photonenergy {self._setpoint_E[0]}')
if ans_set == 'started':
self._setpoint_E[1] = self._setpoint_E[0]
helicity = self.read_attr('helicity')[0]
state = (helicity == self._setpoint_helicity) and in_pos
self.set_state(DevState.ON if state else DevState.MOVING)
return in_pos
@command(dtype_in=str, dtype_out=str)
def cmd_async(self, msg, test):
'''Send a command without waiting for it to finish.
The socket will still be blocked!
'''
t = Thread(target=self.query, args=(msg,))
t.daemon = True
t.start()
@command
def closeconnection(self):
ans = self.query('closeconnection')
if 'bye!' in ans:
self.s.close()
self.set_state(DevState.OFF)
def read_attr(self, attr):
'''Queries the position of given attribute name.
Returns
-------
val : float
tstamp : time stamp
quality : AttrQuality instance (ATTR_VALID, ATTR_CHANGING, ...)
'''
ans = self.query(f'read {attr}')
if 'Current value' in ans:
val = float(ans.split(':')[1])
return val, time(), AttrQuality.ATTR_VALID
else:
self.error_stream('Socket busy or unexpected/incomplete answer')
return None, time(), AttrQuality.ATTR_INVALID
if __name__ == "__main__":
P04_beamline.run_server()