-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_script.py
319 lines (268 loc) · 9.63 KB
/
test_script.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
import requests
import datetime
from time import sleep
from flask import Response
from urllib3 import disable_warnings
import ssl
disable_warnings()
'''
DONT FORGET TO CLEAR LOG BEFORE TESTING
'''
def initialize_experiment():
'''
Initializes an experiment consisting of 2 nodes (PBR+GAS, PBR+GMS)
'''
my_dict = {
1 : {
'experiment_details' : {'sleep_time' : 15},
'devices' : [{
'device_type' : 'PBR',
'device_class' : 'PSI_test',
'address' : None,
'setup' : {
'initial_commands' : [{'time': (datetime.datetime.utcnow().strftime("%Y-%m-%d, %H:%M:%S")),
'cmd_id': 8,
'args': '[1, True]'}],
'lower_outlier_tol' : 2,
'upper_outlier_tol' : 3,
'max_outliers' : 6,
'min_OD' : 0.1,
'max_OD' : 0.9,
'pump_id' : 1
}
},
{
'device_type' : 'GAS',
'device_class' : 'PSI_test',
'address' : None,
'setup' : {
'initial_commands' : []
}
}
]
},
2 : {
'experiment_details' : {'sleep_time' : 10},
'devices' : [{
'device_type' : 'PBR',
'device_class' : 'PSI_test',
'address' : None,
'setup' : {
'initial_commands' : [],
'lower_outlier_tol' : 2,
'upper_outlier_tol' : 3,
'max_outliers' : 6,
'min_OD' : 0.1,
'max_OD' : 0.9,
'pump_id' : 1,
}
},
{
'device_type': 'GMS',
'device_class': 'PSI_test',
'address': None,
'setup': {
'initial_commands': []
}
}
]
}
}
requests.post('https://localhost:5000/initiate', str(my_dict), verify=False, auth=('BioArInEO', 'sybila'))
def cmds_PBR():
'''
:return: dict of commands for PBR
'''
cmds_dict = {
1: [],
2: [],
3: [31],
4: [],
5: [1],
6: [3],
7: [3, 1, 0.3],
8: [1, False],
9: [5],
10: [1, 25],
11: [1, False],
12: [],
13: [12, True],
14: [],
15: [],
16: [1],
17: [3],
18: [],
19: [],
20: [7],
21: [False, 16],
22: [False, 4]
}
return cmds_dict
def cmds_GAS():
'''
:return: dict of commands for GAS
'''
cmds_dict = {
23: [3],
24: [],
25: [0.2],
26: [],
27: [],
28: [],
29: [],
30: [],
31: [2]
}
return cmds_dict
def cmds_GMS():
'''
:return: list of commands for GMS
'''
cmds_dict = {
32: [1],
33: [0],
34: [1, 0.7]
}
return cmds_dict
def test_all():
'''
Tests all manual commands excluding ending nodes/devices/program and changing time periods
:return: None
'''
initialize_experiment()
id = 1
pbr = cmds_PBR()
gas = cmds_GAS()
gms = cmds_GMS()
while id < 23:
t = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
r = requests.post('https://localhost:5000/command?node_id=1&device_type=PBR', str([{'time': t, 'cmd_id': id, 'args': str(pbr[id]), 'source' : 'external'}]), verify=False, auth=('BioArInEO', 'sybila'))
print(r.status_code, r.text)
id+=1
sleep(1)
while id < 32:
t = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
r=requests.post('https://localhost:5000/command?node_id=1&device_type=GAS', str([{'time': t, 'cmd_id': id, 'args': str(gas[id]), 'source' : 'external'}]), verify=False, auth=('BioArInEO', 'sybila'))
print(r.status_code, r.text)
sleep(1)
id+=1
while id < 35:
t = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
r = requests.post('https://localhost:5000/command?node_id=2&device_type=GMS', str([{'time': t, 'cmd_id': id, 'args': str(gms[id]), 'source' : 'external'}]), verify=False, auth=('BioArInEO', 'sybila'))
print(r.status_code, r.text)
sleep(1)
id+=1
def change_time(node, time_period):
'''
node: string
time_period: float
change time period of periodical measurement on node to a specific time_period
:return: None
'''
t = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
requests.post('https://localhost:5000/command?node_id='+str(node), str({'time': t, 'cmd_id': 35, 'args': str([time_period]), 'source': 'external'}), verify=False, auth=('BioArInEO', 'sybila'))
def get_log():
e = requests.get('https://localhost:5000/log?node_id=19&time=20191107150000', verify=False, auth=('BioArInEO', 'sybila'))
x = eval(e.text)
for i in x:
print(i)
def add_node(node_number):
'''
node_number: int
adds node with node_number with a PBR and a GAS device
:return:
'''
str_dict = str({node_number: {'experiment_details': {'sleep_time': 5}, 'devices': [{'device_type': 'PBR', 'device_class': 'PSI_test', 'address': None, 'setup': {'initial_commands': [{'cmd_id': 16, 'args': '[1]'}, {'cmd_id': 13, 'args': '[50, True]'}, {'cmd_id': 8, 'args': '[5, False]'}], 'lower_outlier_tol': 2, 'upper_outlier_tol': 3, 'max_outliers': 6, 'min_OD': 0.45, 'max_OD': 0.5, 'pump_id': 5}}, {'device_type': 'GAS', 'device_class': 'PSI_test', 'address': None, 'setup': {'initial_commands': []}}]}})
x = requests.post("https://localhost:5000/initiate", str_dict, verify=False, auth=('BioArInEO', 'sybila'))
print(x.text)
def post_cmd(node, device_type, cmd_id, args):
'''
post command to specific device
:param node: int/str
:param device_id: str
:param cmd_id: int
:param args: list
'''
t = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
x = requests.post('https://localhost:5000/command?node_id=1&device_type=PBR',
str([{'time': t, 'cmd_id': cmd_id, 'args': str(args), 'source': 'external'}]), verify=False,
auth=('BioArInEO', 'sybila'))
print(x.text)
def end_node(node):
requests.get('https://localhost:5000/end?node_id='+str(node), verify=False, auth=('BioArInEO', 'sybila'))
def end_device(node, device):
requests.get('https://localhost:5000/end?node_id='+str(node)+'&device_type='+device, verify=False, auth=('BioArInEO', 'sybila'))
def end_program():
requests.get('https://localhost:5000/end', verify=False, auth=('BioArInEO', 'sybila'))
def get_node_endpoints(node):
'''
:param node: int
:return: list of active devices on given node
'''
r = requests.get('https://localhost:5000/' + str(node), verify=False, auth=('BioArInEO', 'sybila'))
return r.text
def add_device(node, device):
data = {
'device_type' : device,
'device_class' : 'PSI_test',
'address' : None,
'setup' : {
'initial_commands' : [{'time': (datetime.datetime.utcnow().strftime("%Y-%m-%d, %H:%M:%S")),'cmd_id': 8, 'args': '[1, True]'}],
'lower_outlier_tol' : 2,
'upper_outlier_tol' : 3,
'max_outliers' : 6,
'min_OD' : 0.1,
'max_OD' : 0.9,
'pump_id' : 1
}
}
t = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
requests.post('https://localhost:5000/add_device?node_id='+str(node),
str(data), verify=False,
auth=('BioArInEO', 'sybila'))
def repeat_log():
while True:
get_log()
sleep(3)
#repeat_log()
#test_all()
#add_node(2)
#get_log()
#sleep(2)
#change_time(2, 30)
#post_cmd(2, 'PBR', 8, [5, True])
#end_device(2, 'PBR')
#end_node(1)
#end_program()
#print(get_node_endpoints(2))
#add_device(2, 'PBR')
#initialize_experiment()
def real_test():
my_dict = {
19: {
'experiment_details': {'sleep_time': 60},
'devices': [{
'device_type': 'PBR',
'device_class': 'PSI_java',
'device_id': 'PBR19',
'device_port': 9000,
'address': '/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0',
'setup': {
'initial_commands': [{'time': (datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")),'cmd_id': 8, 'args': '[5, False]'}],
'lower_outlier_tol': 5,
'max_outliers': 5,
'min_OD': 0.48,
'max_OD': 0.52,
'pump_id': 5,
'ft_channel': 5,
}
}]},
}
x = requests.post('https://localhost:5000/initiate', str(my_dict), verify=False, auth=('BioArInEO', 'sybila'))
print(x.text)
real_test()
#r = requests.get('https://localhost:5000/ping', verify=False)
#print(r.status_code, r.text)
#t = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%S")
#r = requests.get('https://localhost:5000/log?node_id=1&time=' + t, verify='MyCertificate.crt', auth=('BioArInEO', 'sybila'))
#print(r.status_code, r.text)