-
Notifications
You must be signed in to change notification settings - Fork 0
/
scifygui.py
254 lines (197 loc) · 8.9 KB
/
scifygui.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
from PyQt5.QtWidgets import QMainWindow, QWidget
from PyQt5.QtCore import QTimer, pyqtSignal
from PyQt5.uic import loadUi
from opcua import OPCUAConnection
from asyncua import ua
from datetime import datetime
from redisclient import RedisClient
from camera.scify import MainWindow as camera_ui
from configparser import ConfigParser
from components.motor import Motor
from shutters_window import ShutterWindow
from tiptilt_window import TipTiltWindow
# async def call_method_async(opcua_client, node_id, method_name, args):
# method_node = opcua_client.get_node(node_id)
# input_args = [ua.Variant(arg, ua.VariantType.Variant) for arg in args]
# result = await method_node.call_method(method_name, *input_args)
# return result
async def call_method_async(opcua_conn, node_id, method_name, *args):
try:
# get the node and method objects from the server
node = await opcua_conn.get_node(node_id)
method = await node.get_child([ua.QualifiedName(4, method_name)])
# call the method on the server
result = await method.call(*args)
return result
except Exception as e:
print(f"Error calling RPC method: {e}")
class MainWindow(QMainWindow):
def __init__(self, opcua_conn):
super(MainWindow, self).__init__()
# save the OPC UA connection
self.opcua_conn = opcua_conn
self.camera_window = None
self.delayline_window = None
self.shutter_window = None
self.tiptilt_window = None
config = ConfigParser()
config.read('config.ini')
url = config['DEFAULT']['databaseurl']
self.redis_client = RedisClient(url)
# set up the main window
self.ui = loadUi('main_window.ui', self)
# print("self.opcua_conn in MainWindow", self.opcua_conn)
# Show Delay line window
self.ui.main_pb_delay_lines.clicked.connect(self.open_delay_lines)
self.ui.pushButton_shutters.clicked.connect(self.open_shutter_window)
self.ui.pushButton_tiptilt.clicked.connect(self.open_tiptilt_window)
self.ui.pushButton_camera.clicked.connect(self.open_camera_interface)
# Dl status on main window
self.load_dl1_status()
# update the temp values
self.update_cryo_temps()
self.t = QTimer()
self.t.timeout.connect(self.refresh_status)
self.t.start(10000)
def open_camera_interface(self):
try:
if self.camera_window is None:
self.camera_window = camera_ui()
self.camera_window.show()
self.camera_window.closing.connect(self.clear_camera_window)
else:
self.camera_window.activateWindow()
except Exception as e:
print(f"Error opening camera window: {e}")
def clear_camera_window(self):
self.camera_window = None
def closeEvent(self, *args):
self.t.stop()
self.opcua_conn.disconnect()
super().closeEvent(*args)
def refresh_status(self):
try:
self.load_dl1_status()
self.update_cryo_temps()
now = datetime.utcnow()
# fileName = r'C:\Users\fys-lab-ivs\Documents\Python Scripts\Log\Temperatures_' \
# + now.strftime(r'%Y-%m-%d') + '.csv'
# f = open(fileName, 'a')
# f.write(f'{str(now)}, {self.temp1}, {self.temp2}, {self.temp3}, {self.temp4} \n')
self.redis_client.add_temperature_1(now, self.temp1)
self.redis_client.add_temperature_2(now, self.temp2)
self.redis_client.add_temperature_3(now, self.temp3)
self.redis_client.add_temperature_4(now, self.temp4)
self.ui.label_error.clear()
except Exception as e:
print(e)
self.ui.label_error.setText(str(e))
def open_delay_lines(self):
try:
if self.delayline_window is None:
self.delayline_window = DelayLinesWindow(self, self.opcua_conn, self.redis_client)
self.delayline_window.closing.connect(self.clear_dl_window)
self.delayline_window.show()
print("Dl window is opening fine")
else:
self.delayline_window.activateWindow()
except Exception as e:
print(f"Error opening delay lines window: {e}")
def open_shutter_window(self):
try:
if self.shutter_window is None:
self.shutter_window = ShutterWindow(self, self.opcua_conn, self.redis_client)
self.shutter_window.closing.connect(self.clear_shutter_window)
self.shutter_window.show()
print("Shutter window is opening fine")
else:
self.shutter_window.activateWindow()
except Exception as e:
print(f"Error opening shutter window: {e}")
def open_tiptilt_window(self):
try:
if self.tiptilt_window is None:
self.tiptilt_window = TipTiltWindow(self, self.opcua_conn, self.redis_client)
self.tiptilt_window.closing.connect(self.clear_tiptilt_window)
self.tiptilt_window.show()
print("Tiptilt window is opening fine")
else:
self.tiptilt_window.activateWindow()
except Exception as e:
print(f"Error opening tiptilt window: {e}")
def clear_shutter_window(self):
self.shutter_window = None
def clear_dl_window(self):
self.delayline_window = None
def clear_tiptilt_window(self):
self.tiptilt_window = None
def load_dl1_status(self):
self.ui.label_dl_status.setText(str(self.opcua_conn.read_node("ns=4;s=MAIN.nott_ics.Delay_Lines.NDL1.stat.sStatus")))
self.ui.label_dl_state.setText(str(self.opcua_conn.read_node("ns=4;s=MAIN.nott_ics.Delay_Lines.NDL1.stat.sState")))
def update_cryo_temps(self):
nodes = ["ns=4;s=GVL_Cryo_Temperatures.Temp_1",
"ns=4;s=GVL_Cryo_Temperatures.Temp_2",
"ns=4;s=GVL_Cryo_Temperatures.Temp_3",
"ns=4;s=GVL_Cryo_Temperatures.Temp_4" ]
values = self.opcua_conn.read_nodes(nodes)
# update the value in the delay lines window
self.temp1 = str(values[0])
self.ui.main_label_temp1.setText(self.temp1)
self.temp2 = str(values[1])
self.ui.main_label_temp2.setText(self.temp2)
self.temp3 = str(values[2])
self.ui.main_label_temp3.setText(self.temp3)
self.temp4 = str(values[3])
self.ui.main_label_temp4.setText(self.temp4)
class DelayLinesWindow(QWidget):
closing = pyqtSignal()
def __init__(self, parent, opcua_conn, redis_client):
super(DelayLinesWindow, self).__init__()
self.parent = parent
config = ConfigParser()
config.read('config.ini')
url = config['DEFAULT']['opcuaaddress']
# save the OPC UA connection
self.opcua_conn = OPCUAConnection(url)
self.opcua_conn.connect()
self._motor1 = Motor(self.opcua_conn, "ns=4;s=MAIN.nott_ics.Delay_Lines.NDL1", 'DL_1')
self._motor2 = Motor(self.opcua_conn, "ns=4;s=MAIN.nott_ics.Delay_Lines.NDL2", 'DL_2')
self._motor3 = Motor(self.opcua_conn, "ns=4;s=MAIN.nott_ics.Delay_Lines.NDL3", 'DL_3')
self._motor4 = Motor(self.opcua_conn, "ns=4;s=MAIN.nott_ics.Delay_Lines.NDL4", 'DL_4')
self.redis_client = redis_client
# set up the delay lines window
self.ui = loadUi('delay_lines.ui', self)
# Dl statuses
#self.dl1_status()
self.ui.motor_widget_1.setup(self.opcua_conn, self.redis_client, self._motor1)
self.ui.motor_widget_2.setup(self.opcua_conn, self.redis_client, self._motor2)
self.ui.motor_widget_3.setup(self.opcua_conn, self.redis_client, self._motor3)
self.ui.motor_widget_4.setup(self.opcua_conn, self.redis_client, self._motor4)
self._activeCommand = None
self.timestamp = None
self.t_pos = QTimer()
self.t_pos.timeout.connect(self.load_positions)
self.t_pos.start(10)
self.t = QTimer()
self.t.timeout.connect(self.refresh_status)
self.t.start(500)
def closeEvent(self, *args):
self.t.stop()
self.t_pos.stop()
self.opcua_conn.disconnect()
self.closing.emit()
super().closeEvent(*args)
def startCameraRecording(self):
self.parent.camera_window.start_recording()
def stopCameraRecording(self):
self.parent.camera_window.stop_recording()
def refresh_status(self):
self.ui.motor_widget_1.refresh_status()
self.ui.motor_widget_2.refresh_status()
self.ui.motor_widget_3.refresh_status()
self.ui.motor_widget_4.refresh_status()
def load_positions(self):
self.ui.motor_widget_1.load_position()
self.ui.motor_widget_2.load_position()
self.ui.motor_widget_3.load_position()
self.ui.motor_widget_4.load_position()