-
Notifications
You must be signed in to change notification settings - Fork 1
/
PNA-X.py
267 lines (214 loc) · 8.95 KB
/
PNA-X.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
import time, json, signal, logging, sys
from web_api.simpleapi import SimpleApi
from routines.SharedLib.PNA import PNA
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s:%(funcName)s:%(lineno)d - %(levelname)s - %(message)s")
logging.root.name="PNA-X.py"
def signal_handler(signum, frame):
print(f"Requested exit")
SimpleApi.shutdown_requested = True
PNA.notSoGracefulExit()
logging.debug("Exiting code")
sys.exit()
signal.signal(signal.SIGINT, signal_handler)
def style_css(payload:str):
print(f'Recebido payload: {payload}')
try:
with open('web-pages/style.css', 'r', encoding='utf-8') as file:
return SimpleApi.http_resource['css'] + file.read().encode('utf-8')
except:
print('INDEX file not found, sending 404')
return SimpleApi.http_header['404']
def start_html(payload:str):
print(f'Recebido payload: {payload}')
try:
with open('web-pages/index.html', 'r', encoding='utf-8') as file:
return SimpleApi.http_resource['html'] + file.read().encode('utf-8')
except:
print('INDEX file not found, sending 404')
return SimpleApi.http_header['404']
def SParamCal_html(payload:str):
print(f'Recebido payload: {payload}')
try:
with open('web-pages/SParamCal.html', 'r', encoding='utf-8') as file:
return SimpleApi.http_resource['html'] + file.read().encode('utf-8')
except:
print('INDEX file not found, sending 404')
return SimpleApi.http_header['404']
def ComPt_html(payload:str):
print(f'Recebido payload: {payload}')
try:
with open('web-pages/ComPt.html', 'r', encoding='utf-8') as file:
return SimpleApi.http_resource['html'] + file.read().encode('utf-8')
except:
print('INDEX file not found, sending 404')
return SimpleApi.http_header['404']
def getConnectorOpt(payload:str):
print(f'Recebido payload: {payload}')
# conectores = ["opt1","opt2","opt3"]
conectores = PNA.queryConnector()
ans = ""
for con in conectores:
ans = ans + f',{{"name":"{con}"}}'
ans = ans[1:]
print (ans)
return SimpleApi.http_resource['html'] + f"[{ans}]".encode('utf-8')
def getCalkitOpt(payload:str):
print(f'Recebido payload: {payload}')
# calkit = ["opt1","opt2","opt3"]
calkit = PNA.queryCalkit(payload)
ans = ""
for con in calkit:
ans = ans + f',{{"name":"{con}"}}'
ans = ans[1:]
print (ans)
return SimpleApi.http_resource['html'] + f"[{ans}]".encode('utf-8')
def getVisaAvailable(payload: str):
print(f'Recebido payload: {payload}')
ans:str = ''
for visa in PNA.visaAvailable():
ans = ans + f',{{"name":"{visa}"}}'
ans = ans[1:]
if (len(ans) == 0):
ans = f'{{"name": "Error loading visa devices"}}'
# print(ans)
return SimpleApi.http_resource['json'] + f'[{ans}]'.encode('utf-8') + b'\r\n\r\n'
def postVisaConnect(payload: str):
print(f'Recebido payload: {payload}')
PNA.initiate(payload)
error = PNA.queryError()
if error == None:
return SimpleApi.http_header['200']
else:
return SimpleApi.http_header['404'] + error.encode('utf-8')
def start_sparam(payload: str):
print(f'Recebido payload: {payload}')
data = json.loads(payload)
try:
# visa = data['visa']
init_freq = float(data['init_freq'])
init_freq_unit = data['init_freq_unit']
end_freq = float(data['end_freq'])
end_freq_unit = data['end_freq_unit']
sweep_pt = int(data['sweep_pt'])
power = int(data['power'])
average = int(data['average'])
# ports_number = data['ports_number']
conn_1 = data['conn_1']
ckit_1 = data['ckit_1']
conn_2 = data['conn_2']
ckit_2 = data['ckit_2']
conn_3 = data['conn_3']
ckit_3 = data['ckit_3']
conn_4 = data['conn_4']
ckit_4 = data['ckit_4']
calibrate = data['calibrate']
save = data['save']
PNA.SParam.save = save
if not (init_freq_unit in ['Hz', 'kHz', 'MHz', 'GHz'] and end_freq_unit in ['Hz', 'kHz', 'MHz', 'GHz']):
return SimpleApi.http_header['400']
except:
return SimpleApi.http_header['400']
if (init_freq_unit == 'Hz'): pass
elif (init_freq_unit == 'kHz'): init_freq *= 1000
elif (init_freq_unit == 'MHz'): init_freq *= 1000000
elif (init_freq_unit == 'GHz'): init_freq *= 1000000000
PNA.SParam.freq_start = init_freq
if (end_freq_unit == 'Hz'): pass
elif (end_freq_unit == 'kHz'): end_freq *= 1000
elif (end_freq_unit == 'MHz'): end_freq *= 1000000
elif (end_freq_unit == 'GHz'): end_freq *= 1000000000
PNA.SParam.freq_stop = end_freq
PNA.SParam.sweep_points = sweep_pt
PNA.SParam.amplitude_dB = power
PNA.SParam.average = average
try:
# TODO: this congigure function could be simplified but testing is needed
PNA.SParam.configure(num=1, name='gain', meas='S21')
PNA.SParam.configure(num=2, name='InputRL', meas='S11')
PNA.SParam.configure(num=3, name='loss', meas='S12')
PNA.SParam.configure(num=4, name='OutputRL', meas='S22')
if calibrate:
PNA.SParam.guided_calibration(
connectors=[conn_1, conn_2, conn_3, conn_4],
calkits=[ckit_1, ckit_2, ckit_3, ckit_4])
# TODO: this function still requires waaaay much work to interact with the webpage - > code 100?
except Exception as e:
return SimpleApi.http_header['417'] + str(e).encode('utf-8')
if PNA.error == None:
return SimpleApi.http_header['200']
else:
return SimpleApi.http_header['404'] + PNA.error.encode('utf-8')
def last_img (payload:str):
print(f'Recebido payload: {payload}')
try:
# PNA.ComPt.last_image = 'web-pages/imagem.webp' # TODO: debug line
with open(PNA.ComPt.last_image, 'rb') as file:
return SimpleApi.http_resource['png'] + file.read()
except: return SimpleApi.http_header['404']
def getCalStep (payload:str):
print(f'Recebido payload: {payload}')
try:
ans = PNA.SParam.cal_step()
print(PNA.SParam.total_steps)
print(PNA.SParam.current_step)
if PNA.error:
return SimpleApi.http_header['404'] + PNA.error.encode('utf-8')
return SimpleApi.http_resource['json'] + ans.encode('utf-8')
except Exception as e: return SimpleApi.http_header['404'] + str(e).encode('utf-8')
def start_compt(payload: str):
print(f'Recebido payload: {payload}')
data = json.loads(payload)
try:
# visa = data['visa'] # TODO: check if a sesssion to the instrument is active, if not, create one. For now, reload the page
freq = float(data['freq'])
freq_unit = data['freq_unit']
average = int(data['average'])
start_pow = int(data['start_pow'])
stop_pow = int(data['stop_pow'])
offset = float(data['offset'])
if not (freq_unit in ['Hz', 'kHz', 'MHz', 'GHz']):
return SimpleApi.http_header['400']
except Exception as e:
return SimpleApi.http_header['400']
if (freq_unit == 'Hz'): pass
elif (freq_unit == 'kHz'): freq *= 1000
elif (freq_unit == 'MHz'): freq *= 1000000
elif (freq_unit == 'GHz'): freq *= 1000000000
PNA.ComPt.frequency = freq
PNA.ComPt.average = average
PNA.ComPt.start_power = start_pow
PNA.ComPt.stop_power = stop_pow
PNA.ComPt.offset = offset
PNA.ComPt.frequency = freq
try:
PNA.ComPt.sweep_power()
PNA.ComPt.parameter_config()
PNA.ComPt.plot_data()
except Exception as e:
return SimpleApi.http_header['417'] + str(e).encode('utf-8')
if (PNA.error == None):
return SimpleApi.http_header['200']
else:
return SimpleApi.http_header['400'] + str(PNA.error).encode('utf-8')
if __name__ == '__main__':
server = SimpleApi()
# GUI resources
server.configure_endpoints('GET', '/', start_html)
server.configure_endpoints('GET', '/style.css', style_css)
server.configure_endpoints('GET', '/sparam', SParamCal_html)
server.configure_endpoints('GET', '/compt', ComPt_html)
server.configure_endpoints('GET', '/last_img', last_img)
# VISA endpoints
server.configure_endpoints('GET', '/visaAvailable', getVisaAvailable)
server.configure_endpoints('POST', '/connectTo', postVisaConnect)
server.configure_endpoints('GET', '/connector', getConnectorOpt)
server.configure_endpoints('POST', '/calkit', getCalkitOpt)
# Start function
server.configure_endpoints('POST', '/start_sparam', start_sparam)
server.configure_endpoints('GET', '/cal_step', getCalStep)
server.configure_endpoints('POST', '/start_compt', start_compt)
server.begin_workers()
while not SimpleApi.shutdown_requested:
time.sleep(1)
# TODO: error handling from visa to UI
# TODO: test development flag to ease testing