The requested changes are too big and content was truncated. Show full diff
@@ -1,940 +1,940 | |||
|
1 | 1 | from django.db import models |
|
2 | 2 | from apps.main.models import Configuration |
|
3 | 3 | from django.core.urlresolvers import reverse |
|
4 | 4 | # Create your models here. |
|
5 | 5 | |
|
6 | 6 | import ast |
|
7 | 7 | import socket |
|
8 | 8 | import json |
|
9 | 9 | import requests |
|
10 | 10 | import struct |
|
11 | 11 | import sys, time |
|
12 | 12 | |
|
13 | 13 | import multiprocessing |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | antenna_default = json.dumps({ |
|
17 | 17 | "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
18 | 18 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
19 | 19 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
20 | 20 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
21 | 21 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
22 | 22 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
23 | 23 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
24 | 24 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0] |
|
25 | 25 | ] |
|
26 | 26 | , |
|
27 | 27 | "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
28 | 28 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
29 | 29 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
30 | 30 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
31 | 31 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
32 | 32 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
33 | 33 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
34 | 34 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]], |
|
35 | 35 | }) |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | tx_default = json.dumps({ |
|
39 | 39 | "up": [[1,1,1,1,0,0,0,0], |
|
40 | 40 | [1,1,1,1,0,0,0,0], |
|
41 | 41 | [1,1,1,1,0,0,0,0], |
|
42 | 42 | [1,1,1,1,0,0,0,0], |
|
43 | 43 | [0,0,0,0,1,1,1,1], |
|
44 | 44 | [0,0,0,0,1,1,1,1], |
|
45 | 45 | [0,0,0,0,1,1,1,1], |
|
46 | 46 | [0,0,0,0,1,1,1,1]], |
|
47 | 47 | |
|
48 | 48 | "down": [[1,1,1,1,0,0,0,0], |
|
49 | 49 | [1,1,1,1,0,0,0,0], |
|
50 | 50 | [1,1,1,1,0,0,0,0], |
|
51 | 51 | [1,1,1,1,0,0,0,0], |
|
52 | 52 | [0,0,0,0,1,1,1,1], |
|
53 | 53 | [0,0,0,0,1,1,1,1], |
|
54 | 54 | [0,0,0,0,1,1,1,1], |
|
55 | 55 | [0,0,0,0,1,1,1,1]], |
|
56 | 56 | }) |
|
57 | 57 | |
|
58 | 58 | rx_default = json.dumps({ |
|
59 | 59 | "up": [[1,1,1,1,0,0,0,0], |
|
60 | 60 | [1,1,1,1,0,0,0,0], |
|
61 | 61 | [1,1,1,1,0,0,0,0], |
|
62 | 62 | [1,1,1,1,0,0,0,0], |
|
63 | 63 | [0,0,0,0,1,1,1,1], |
|
64 | 64 | [0,0,0,0,1,1,1,1], |
|
65 | 65 | [0,0,0,0,1,1,1,1], |
|
66 | 66 | [0,0,0,0,1,1,1,1]], |
|
67 | 67 | |
|
68 | 68 | "down": [[1,1,1,1,0,0,0,0], |
|
69 | 69 | [1,1,1,1,0,0,0,0], |
|
70 | 70 | [1,1,1,1,0,0,0,0], |
|
71 | 71 | [1,1,1,1,0,0,0,0], |
|
72 | 72 | [0,0,0,0,1,1,1,1], |
|
73 | 73 | [0,0,0,0,1,1,1,1], |
|
74 | 74 | [0,0,0,0,1,1,1,1], |
|
75 | 75 | [0,0,0,0,1,1,1,1]], |
|
76 | 76 | }) |
|
77 | 77 | |
|
78 | 78 | conf_default = {} |
|
79 | 79 | status_default = {} |
|
80 | 80 | for i in range(1,65): |
|
81 | 81 | conf_default[str(i)] = "" |
|
82 | 82 | status_default[str(i)] = 0 |
|
83 | 83 | |
|
84 | 84 | ues_default = json.dumps({ |
|
85 | 85 | "up": [0.533333,0.00000,1.06667,0.00000], |
|
86 | 86 | "down": [0.533333,0.00000,1.06667,0.00000] |
|
87 | 87 | }) |
|
88 | 88 | |
|
89 | 89 | onlyrx_default = json.dumps({ |
|
90 | 90 | "up": False, |
|
91 | 91 | "down": False |
|
92 | 92 | }) |
|
93 | 93 | |
|
94 | 94 | def up_convertion(cadena): |
|
95 | 95 | valores = [] |
|
96 | 96 | for c in cadena: |
|
97 | 97 | if c == 1.0: valores=valores+['000'] |
|
98 | 98 | if c == 2.0: valores=valores+['001'] |
|
99 | 99 | if c == 3.0: valores=valores+['010'] |
|
100 | 100 | if c == 0.0: valores=valores+['011'] |
|
101 | 101 | if c == 0.5: valores=valores+['100'] |
|
102 | 102 | if c == 1.5: valores=valores+['101'] |
|
103 | 103 | if c == 2.5: valores=valores+['110'] |
|
104 | 104 | if c == 3.5: valores=valores+['111'] |
|
105 | 105 | |
|
106 | 106 | return valores |
|
107 | 107 | |
|
108 | 108 | def up_conv_bits(value): |
|
109 | 109 | |
|
110 | 110 | if value == 1.0: bits="000" |
|
111 | 111 | if value == 2.0: bits="001" |
|
112 | 112 | if value == 3.0: bits="010" |
|
113 | 113 | if value == 0.0: bits="011" |
|
114 | 114 | if value == 0.5: bits="100" |
|
115 | 115 | if value == 1.5: bits="101" |
|
116 | 116 | if value == 2.5: bits="110" |
|
117 | 117 | if value == 3.5: bits="111" |
|
118 | 118 | |
|
119 | 119 | return bits |
|
120 | 120 | |
|
121 | 121 | def down_convertion(cadena): |
|
122 | 122 | valores = [] |
|
123 | 123 | for c in cadena: |
|
124 | 124 | if c == 1.0: valores=valores+['000'] |
|
125 | 125 | if c == 2.0: valores=valores+['001'] |
|
126 | 126 | if c == 3.0: valores=valores+['010'] |
|
127 | 127 | if c == 0.0: valores=valores+['011'] |
|
128 | 128 | if c == 0.5: valores=valores+['100'] |
|
129 | 129 | if c == 1.5: valores=valores+['101'] |
|
130 | 130 | if c == 2.5: valores=valores+['110'] |
|
131 | 131 | if c == 3.5: valores=valores+['111'] |
|
132 | 132 | |
|
133 | 133 | return valores |
|
134 | 134 | |
|
135 | 135 | def down_conv_bits(value): |
|
136 | 136 | |
|
137 | 137 | if value == 1.0: bits="000" |
|
138 | 138 | if value == 2.0: bits="001" |
|
139 | 139 | if value == 3.0: bits="010" |
|
140 | 140 | if value == 0.0: bits="011" |
|
141 | 141 | if value == 0.5: bits="100" |
|
142 | 142 | if value == 1.5: bits="101" |
|
143 | 143 | if value == 2.5: bits="110" |
|
144 | 144 | if value == 3.5: bits="111" |
|
145 | 145 | |
|
146 | 146 | return bits |
|
147 | 147 | |
|
148 | 148 | def ip2position(module_number): |
|
149 | 149 | j=0 |
|
150 | 150 | i=0 |
|
151 | 151 | for x in range(0,module_number-1): |
|
152 | 152 | j=j+1 |
|
153 | 153 | if j==8: |
|
154 | 154 | i=i+1 |
|
155 | 155 | j=0 |
|
156 | 156 | |
|
157 | 157 | pos = [i,j] |
|
158 | 158 | return pos |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | def change_beam_for_multiprocessing(module): |
|
162 | 162 | |
|
163 | 163 | for i in range (1,50): |
|
164 | 164 | beam_pos = 0 |
|
165 | 165 | module_address = ('192.168.1.'+str(module), 5500) |
|
166 | 166 | header = 'JROABSCeCnModCnMod0100000' |
|
167 | 167 | numbers = len(str(beam_pos)) |
|
168 | 168 | function = 'CHGB' |
|
169 | 169 | |
|
170 | 170 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
171 | 171 | |
|
172 | 172 | # Create the datagram socket |
|
173 | 173 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
174 | 174 | sock.connect(module_address) |
|
175 | 175 | |
|
176 | 176 | sock.send(message_tx) |
|
177 | 177 | #t = sock.recv(1024) |
|
178 | 178 | sock.close() |
|
179 | 179 | sock = None |
|
180 | 180 | |
|
181 | 181 | |
|
182 | 182 | time.sleep(0.2) |
|
183 | 183 | |
|
184 | 184 | |
|
185 | 185 | beam_pos = 1 |
|
186 | 186 | numbers = len(str(beam_pos)) |
|
187 | 187 | |
|
188 | 188 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
189 | 189 | |
|
190 | 190 | # Create the datagram socket |
|
191 | 191 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
192 | 192 | sock.connect(module_address) |
|
193 | 193 | sock.send(message_tx) |
|
194 | 194 | sock.close() |
|
195 | 195 | sock = None |
|
196 | 196 | |
|
197 | 197 | time.sleep(0.2) |
|
198 | 198 | |
|
199 | 199 | |
|
200 | 200 | |
|
201 | 201 | class ABSConfiguration(Configuration): |
|
202 | 202 | beams = models.CharField(verbose_name='Beams', max_length=20000, default="{}") |
|
203 | 203 | module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default)) |
|
204 | 204 | |
|
205 | 205 | class Meta: |
|
206 | 206 | db_table = 'abs_configurations' |
|
207 | 207 | |
|
208 | 208 | def get_absolute_url_plot(self): |
|
209 | 209 | return reverse('url_plot_abs_patterns', args=[str(self.id)]) |
|
210 | 210 | |
|
211 | 211 | |
|
212 | 212 | def parms_to_dict(self): |
|
213 | 213 | |
|
214 | 214 | parameters = {} |
|
215 | 215 | |
|
216 | 216 | parameters['device_id'] = self.device.id |
|
217 | 217 | parameters['name'] = self.name |
|
218 | 218 | parameters['beams'] = {} |
|
219 | 219 | |
|
220 | 220 | beams = ast.literal_eval(self.beams) |
|
221 | 221 | b=1 |
|
222 | 222 | for beam in beams: |
|
223 | 223 | absbeam = ABSBeam.objects.get(pk=beams[beam]) |
|
224 | 224 | parameters['beams']['beam'+str(b)] = absbeam.parms_to_dict() |
|
225 | 225 | b+=1 |
|
226 | 226 | |
|
227 | 227 | return parameters |
|
228 | 228 | |
|
229 | 229 | |
|
230 | 230 | def module_conf(self, module_num, beams): |
|
231 | 231 | """ |
|
232 | 232 | This function creates beam configurations for one abs module. |
|
233 | 233 | """ |
|
234 | 234 | ip_address = self.device.ip_address |
|
235 | 235 | ip_address = ip_address.split('.') |
|
236 | 236 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
237 | 237 | dot = '.' |
|
238 | 238 | module_ip = dot.join(module_seq)+'.'+str(module_num) |
|
239 | 239 | module_port = self.device.port_address |
|
240 | 240 | write_route = 'http://'+module_ip+':'+str(module_port)+'/configure' |
|
241 | 241 | |
|
242 | 242 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
243 | 243 | module = 'ABS_'+str(module_num) |
|
244 | 244 | bs = {} |
|
245 | 245 | i=1 |
|
246 | 246 | #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
247 | 247 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
248 | 248 | for beam in beams: |
|
249 | 249 | bs[i] = beam.module_6bits(module_num) |
|
250 | 250 | i=i+1 |
|
251 | 251 | |
|
252 | 252 | beams = bs |
|
253 | 253 | |
|
254 | 254 | parameters = {} |
|
255 | 255 | parameters['header'] = header |
|
256 | 256 | parameters['module'] = module |
|
257 | 257 | parameters['beams'] = json.dumps(beams) |
|
258 | 258 | |
|
259 | 259 | answer = '' |
|
260 | 260 | |
|
261 | 261 | try: |
|
262 | 262 | r_write = requests.post(write_route, parameters, timeout=0.5) |
|
263 | 263 | answer = r_write.json() |
|
264 | 264 | self.message = answer['message'] |
|
265 | 265 | except: |
|
266 | 266 | self.message = "Could not write ABS parameters" |
|
267 | 267 | return 0 |
|
268 | 268 | return 1 |
|
269 | 269 | |
|
270 | 270 | |
|
271 | 271 | def read_module(self, module): |
|
272 | 272 | |
|
273 | 273 | """ |
|
274 | 274 | Read out-bits (up-down) of 1 abs module NOT for Configuration |
|
275 | 275 | """ |
|
276 | 276 | |
|
277 | 277 | parameters = {} |
|
278 | 278 | ip_address = self.device.ip_address |
|
279 | 279 | ip_address = ip_address.split('.') |
|
280 | 280 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
281 | 281 | dot = '.' |
|
282 | 282 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
283 | 283 | module_port = self.device.port_address |
|
284 | 284 | read_route = 'http://'+module_ip+':'+str(module_port)+'/read' |
|
285 | 285 | |
|
286 |
print |
|
|
286 | print(read_route) | |
|
287 | 287 | |
|
288 | 288 | answer = '' |
|
289 | 289 | module_bits = '' |
|
290 | 290 | |
|
291 | 291 | try: |
|
292 | 292 | r_write = requests.get(read_route, timeout=0.7) |
|
293 | 293 | answer = r_write.json() |
|
294 | 294 | message = answer['message'] |
|
295 | 295 | module_bits = answer['allbits'] |
|
296 | 296 | except: |
|
297 | 297 | message = "Could not read ABS parameters" |
|
298 | 298 | return 0 |
|
299 | 299 | |
|
300 | 300 | return module_bits |
|
301 | 301 | |
|
302 | 302 | |
|
303 | 303 | def write_device(self): |
|
304 | 304 | """ |
|
305 | 305 | This function sends the beams list to every abs module. |
|
306 | 306 | """ |
|
307 | 307 | |
|
308 | 308 | beams_list = ast.literal_eval(self.beams) |
|
309 | 309 | beams = [] |
|
310 | 310 | |
|
311 | 311 | for bl in range(1,len(beams_list)+1): |
|
312 | 312 | b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) |
|
313 | 313 | beams.append(b) |
|
314 | 314 | |
|
315 | 315 | #---Write each abs module--- |
|
316 | 316 | beams_status = ast.literal_eval(self.module_status) |
|
317 | 317 | for i in range(62,65): |
|
318 | 318 | try: |
|
319 | 319 | self.module_conf(i, beams) |
|
320 | 320 | beams_status[str(i)] = 1 |
|
321 | 321 | self.module_status = json.dumps(beams_status) |
|
322 | 322 | self.save() |
|
323 | 323 | #self.module_conf(63,beams) |
|
324 | 324 | #beams_status[str(63)] = 1 |
|
325 | 325 | #self.module_status = json.dumps(beams_status) |
|
326 | 326 | except: |
|
327 | 327 | beams_status[str(i)] = 0 |
|
328 | 328 | self.module_status = json.dumps(beams_status) |
|
329 | 329 | self.save() |
|
330 | 330 | #return 0 |
|
331 | 331 | |
|
332 | 332 | #self.device.status = 1 |
|
333 | 333 | self.save() |
|
334 | 334 | return 1 |
|
335 | 335 | |
|
336 | 336 | |
|
337 | 337 | def write_module(self, module): |
|
338 | 338 | |
|
339 | 339 | """ |
|
340 | 340 | Send configuration to one abs module |
|
341 | 341 | """ |
|
342 | 342 | |
|
343 | 343 | parameters = {} |
|
344 | 344 | ip_address = self.abs_conf.device.ip_address |
|
345 | 345 | ip_address = ip_address.split('.') |
|
346 | 346 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
347 | 347 | dot = '.' |
|
348 | 348 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
349 | 349 | module_port = self.abs_conf.device.port_address |
|
350 | 350 | write_route = 'http://'+module_ip+':'+str(module_port)+'/configure' |
|
351 | 351 | |
|
352 | 352 | #print write_route |
|
353 | 353 | |
|
354 | 354 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
355 | 355 | module = 'ABS_'+str(module) |
|
356 | 356 | beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
357 | 357 | 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
358 | 358 | |
|
359 | 359 | parameters['header'] = header |
|
360 | 360 | parameters['module'] = module |
|
361 | 361 | parameters['beams'] = json.dumps(beams) |
|
362 | 362 | |
|
363 | 363 | answer = '' |
|
364 | 364 | |
|
365 | 365 | try: |
|
366 | 366 | r_write = requests.post(write_route, parameters, timeout=0.5) |
|
367 | 367 | answer = r_write.json() |
|
368 | 368 | self.message = answer['message'] |
|
369 | 369 | except: |
|
370 | 370 | self.message = "Could not write ABS parameters" |
|
371 | 371 | return 0 |
|
372 | 372 | |
|
373 | 373 | |
|
374 | 374 | #self.device.status = int(answer['status']) |
|
375 | 375 | |
|
376 | 376 | return 1 |
|
377 | 377 | |
|
378 | 378 | |
|
379 | 379 | def beam_selector(self, module, beam_pos): |
|
380 | 380 | """ |
|
381 | 381 | This function selects the beam number for one absmodule. |
|
382 | 382 | """ |
|
383 | 383 | |
|
384 | 384 | if beam_pos > 0: |
|
385 | 385 | beam_pos = beam_pos - 1 |
|
386 | 386 | else: |
|
387 | 387 | beam_pos = 0 |
|
388 | 388 | |
|
389 | 389 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
390 | 390 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
391 | 391 | beams_list = ast.literal_eval(self.beams) |
|
392 | 392 | if len(beams_list) < beam_pos: |
|
393 | 393 | return 0 |
|
394 | 394 | |
|
395 | 395 | flag = 1 |
|
396 | 396 | if beam_pos>9: |
|
397 | 397 | flag = 2 |
|
398 | 398 | |
|
399 | 399 | module_address = ('192.168.1.'+str(module), 5500) |
|
400 | 400 | header = 'JROABSCeCnModCnMod0100000' |
|
401 | 401 | numbers = len(str(beam_pos)) |
|
402 | 402 | function = 'CHGB' |
|
403 | 403 | |
|
404 | 404 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
405 | 405 | |
|
406 | 406 | # Create the datagram socket |
|
407 | 407 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
408 | 408 | #sock.connect(module_address) |
|
409 | 409 | try: |
|
410 | 410 | sock.connect(module_address) |
|
411 | 411 | sock.send(message_tx) |
|
412 | 412 | sock.close() |
|
413 |
print |
|
|
413 | print("Writing abs module:"+module_address[0]+"...") | |
|
414 | 414 | except: |
|
415 | 415 | sock = None |
|
416 |
print |
|
|
416 | print("Problem writing abs module:"+module_address[0]) | |
|
417 | 417 | return 0 |
|
418 | 418 | |
|
419 | 419 | return 1 |
|
420 | 420 | |
|
421 | 421 | |
|
422 | 422 | def change_beam(self, beam_pos): |
|
423 | 423 | """ |
|
424 | 424 | This function selects the beam number for all absmodules. |
|
425 | 425 | """ |
|
426 | 426 | for i in range(1,65): |
|
427 | 427 | try: |
|
428 | 428 | self.beam_selector(i,beam_pos) |
|
429 | 429 | except: |
|
430 |
print |
|
|
430 | print("Problem with module: 192.168.1."+str(i)) | |
|
431 | 431 | self.message = "Problem with module: 192.168.1."+str(i) |
|
432 | 432 | #return 0 |
|
433 | 433 | return 1 |
|
434 | 434 | |
|
435 | 435 | |
|
436 | 436 | def send_beam_num(self, beam_pos): |
|
437 | 437 | """ |
|
438 | 438 | This function connects to a multicast group and sends the beam number |
|
439 | 439 | to all abs modules. |
|
440 | 440 | """ |
|
441 | 441 | |
|
442 | 442 | if beam_pos > 0: |
|
443 | 443 | beam_pos = beam_pos - 1 |
|
444 | 444 | else: |
|
445 | 445 | beam_pos = 0 |
|
446 | 446 | |
|
447 | 447 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
448 | 448 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
449 | 449 | beams_list = ast.literal_eval(self.beams) |
|
450 | 450 | if len(beams_list) < beam_pos: |
|
451 | 451 | return 0 |
|
452 | 452 | |
|
453 | 453 | flag = 1 |
|
454 | 454 | if beam_pos>9: |
|
455 | 455 | flag = 2 |
|
456 | 456 | |
|
457 | 457 | header = 'JROABSCeCnModCnMod0100000' |
|
458 | 458 | flag = str(flag) |
|
459 | 459 | function = 'CHGB' |
|
460 | 460 | message_tx = header+flag+function+str(beam_pos)+'0' |
|
461 | 461 | |
|
462 | 462 | multicast_group = '224.3.29.71' |
|
463 | 463 | server_address = ('',10000) |
|
464 | 464 | |
|
465 | 465 | # Create the socket |
|
466 | 466 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
467 | 467 | # Bind to the server address |
|
468 | 468 | sock.bind(server_address) |
|
469 | 469 | # Telling the OS add the socket to the multicast on all interfaces |
|
470 | 470 | group = socket.inet_aton(multicast_group) |
|
471 | 471 | mreq = struct.pack('4sL', group, socket.INADDR_ANY) |
|
472 | 472 | sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) |
|
473 | 473 | |
|
474 | 474 | #print 'sending acknowledgement to all: \n' + message_tx |
|
475 | 475 | sock.sendto(message_tx, (multicast_group, 10000)) |
|
476 | 476 | sock.close() |
|
477 | 477 | sock = None |
|
478 | 478 | |
|
479 | 479 | return 1 |
|
480 | 480 | |
|
481 | 481 | def test1(self): |
|
482 | 482 | t1 = time.time() |
|
483 | 483 | t2 = 0 |
|
484 | 484 | while (t2-t1)<300: |
|
485 | 485 | t2 = time.time() |
|
486 | 486 | self.send_beam_num(1) |
|
487 | 487 | time.sleep(0.04) |
|
488 | 488 | self.send_beam_num(2) |
|
489 | 489 | time.sleep(0.04) |
|
490 | 490 | return |
|
491 | 491 | |
|
492 | 492 | def change_procs_test1(self, module): |
|
493 | 493 | |
|
494 | 494 | for i in range (1,300): |
|
495 | 495 | beam_pos = 0 |
|
496 | 496 | module_address = ('192.168.1.'+str(module), 5500) |
|
497 | 497 | header = 'JROABSCeCnModCnMod0100000' |
|
498 | 498 | numbers = len(str(beam_pos)) |
|
499 | 499 | function = 'CHGB' |
|
500 | 500 | |
|
501 | 501 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
502 | 502 | |
|
503 | 503 | # Create the datagram socket |
|
504 | 504 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
505 | 505 | sock.connect(module_address) |
|
506 | 506 | |
|
507 | 507 | sock.send(message_tx) |
|
508 | 508 | #t = sock.recv(1024) |
|
509 | 509 | sock.close() |
|
510 | 510 | sock = None |
|
511 | 511 | |
|
512 | 512 | |
|
513 | 513 | time.sleep(0.2) |
|
514 | 514 | |
|
515 | 515 | |
|
516 | 516 | beam_pos = 1 |
|
517 | 517 | numbers = len(str(beam_pos)) |
|
518 | 518 | |
|
519 | 519 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
520 | 520 | |
|
521 | 521 | # Create the datagram socket |
|
522 | 522 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
523 | 523 | sock.connect(module_address) |
|
524 | 524 | sock.send(message_tx) |
|
525 | 525 | sock.close() |
|
526 | 526 | sock = None |
|
527 | 527 | |
|
528 | 528 | time.sleep(0.2) |
|
529 | 529 | |
|
530 | 530 | |
|
531 | 531 | def multi_procs_test1(self): |
|
532 | 532 | |
|
533 | 533 | #if __name__ == "__main__": |
|
534 | 534 | size = 10000000 # Number of random numbers to add |
|
535 | 535 | procs = 65 # (Number-1) of processes to create |
|
536 | 536 | |
|
537 | 537 | # Create a list of jobs and then iterate through |
|
538 | 538 | # the number of processes appending each process to |
|
539 | 539 | # the job list |
|
540 | 540 | jobs = [] |
|
541 | 541 | for i in range(62, procs): |
|
542 | 542 | |
|
543 | 543 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) |
|
544 | 544 | jobs.append(process) |
|
545 | 545 | #print jobs |
|
546 | 546 | |
|
547 | 547 | # Start the processes (i.e. calculate the random number lists) |
|
548 | 548 | for j in jobs: |
|
549 | 549 | #time.sleep(0.4) |
|
550 | 550 | #print j |
|
551 | 551 | j.start() |
|
552 | 552 | |
|
553 | 553 | # Ensure all of the processes have finished |
|
554 | 554 | for j in jobs: |
|
555 | 555 | j.join() |
|
556 | 556 | |
|
557 |
print |
|
|
557 | print("List processing complete.") | |
|
558 | 558 | return 1 |
|
559 | 559 | |
|
560 | 560 | |
|
561 | 561 | def status_device(self): |
|
562 | 562 | |
|
563 | 563 | return 1 |
|
564 | 564 | |
|
565 | 565 | |
|
566 | 566 | |
|
567 | 567 | class ABSBeam(models.Model): |
|
568 | 568 | |
|
569 | 569 | name = models.CharField(max_length=60, default='Beam') |
|
570 | 570 | antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default) |
|
571 | 571 | abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration') |
|
572 | 572 | tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default) |
|
573 | 573 | rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default) |
|
574 | 574 | s_time = models.TimeField(verbose_name='Star Time', default='00:00:00') |
|
575 | 575 | e_time = models.TimeField(verbose_name='End Time', default='23:59:59') |
|
576 | 576 | modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default)) |
|
577 | 577 | ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default) |
|
578 | 578 | only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default) |
|
579 | 579 | |
|
580 | 580 | class Meta: |
|
581 | 581 | db_table = 'abs_beams' |
|
582 | 582 | |
|
583 | 583 | def __unicode__(self): |
|
584 | 584 | return u'%s' % (self.name) |
|
585 | 585 | |
|
586 | 586 | def parms_to_dict(self): |
|
587 | 587 | |
|
588 | 588 | #Update data |
|
589 | 589 | self.modules_6bits() |
|
590 | 590 | |
|
591 | 591 | parameters = {} |
|
592 | 592 | |
|
593 | 593 | parameters['name'] = self.name |
|
594 | 594 | parameters['antenna'] = ast.literal_eval(self.antenna) |
|
595 | 595 | parameters['abs_conf'] = self.abs_conf.name |
|
596 | 596 | parameters['tx'] = ast.literal_eval(self.tx) |
|
597 | 597 | parameters['rx'] = ast.literal_eval(self.rx) |
|
598 | 598 | parameters['s_time'] = self.s_time.strftime("%H:%M:%S") |
|
599 | 599 | parameters['e_time'] = self.e_time.strftime("%H:%M:%S") |
|
600 | 600 | parameters['configuration'] = ast.literal_eval(self.modules_conf) |
|
601 | 601 | parameters['ues'] = ast.literal_eval(self.ues) |
|
602 | 602 | parameters['only_rx'] = json.loads(self.only_rx) |
|
603 | 603 | |
|
604 | 604 | return parameters |
|
605 | 605 | |
|
606 | 606 | def dict_to_parms(self, parameters): |
|
607 | 607 | |
|
608 | 608 | self.name = parameters['name'] |
|
609 | 609 | self.antenna = json.dumps(parameters['antenna']) |
|
610 | 610 | #self.abs_conf = parameters['abs_conf'] |
|
611 | 611 | self.tx = json.dumps(parameters['tx']) |
|
612 | 612 | self.rx = json.dumps(parameters['rx']) |
|
613 | 613 | #parameters['s_time'] |
|
614 | 614 | #parameters['e_time'] |
|
615 | 615 | self.ues = json.dumps(parameters['ues']) |
|
616 | 616 | self.only_rx = json.dumps(parameters['only_rx']) |
|
617 | 617 | |
|
618 | 618 | self.modules_6bits() |
|
619 | 619 | self.save() |
|
620 | 620 | |
|
621 | 621 | return parameters |
|
622 | 622 | |
|
623 | 623 | |
|
624 | 624 | def change_beam(self, beam_pos=0): |
|
625 | 625 | |
|
626 | 626 | module_63 = ('192.168.1.63', 5500) |
|
627 | 627 | header = 'JROABSCeCnModCnMod0100000' |
|
628 | 628 | numbers = len(str(beam_pos)) |
|
629 | 629 | function = 'CHGB' |
|
630 | 630 | |
|
631 | 631 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
632 | 632 | |
|
633 | 633 | # Create the datagram socket |
|
634 | 634 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
635 | 635 | sock.connect(module_63) |
|
636 | 636 | sock.send(message_tx) |
|
637 | 637 | sock.close() |
|
638 | 638 | return message_tx |
|
639 | 639 | |
|
640 | 640 | def change_module_beam(self, module=61,beam_pos=0): |
|
641 | 641 | |
|
642 | 642 | module_address = ('192.168.1.'+str(module), 5500) |
|
643 | 643 | header = 'JROABSCeCnModCnMod0100000' |
|
644 | 644 | numbers = len(str(beam_pos)) |
|
645 | 645 | function = 'CHGB' |
|
646 | 646 | |
|
647 | 647 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
648 | 648 | |
|
649 | 649 | # Create the datagram socket |
|
650 | 650 | try: |
|
651 | 651 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
652 | 652 | sock.connect(module_address) |
|
653 | 653 | sock.send(message_tx) |
|
654 | 654 | sock.close() |
|
655 | 655 | except: |
|
656 | 656 | return 0 |
|
657 | 657 | return message_tx |
|
658 | 658 | |
|
659 | 659 | def write_device(self): |
|
660 | 660 | |
|
661 | 661 | parameters = {} |
|
662 | 662 | |
|
663 | 663 | module_ip = '192.168.1.63' |
|
664 | 664 | write_route = 'http://192.168.1.63:8080/configure' |
|
665 | 665 | |
|
666 | 666 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
667 | 667 | module = 'ABS_63' |
|
668 | 668 | beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
669 | 669 | 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
670 | 670 | |
|
671 | 671 | parameters['header'] = header |
|
672 | 672 | parameters['module'] = module |
|
673 | 673 | parameters['beams'] = json.dumps(beams) |
|
674 | 674 | |
|
675 | 675 | answer = '' |
|
676 | 676 | |
|
677 | 677 | try: |
|
678 | 678 | r_write = requests.post(write_route, parameters, timeout=0.5) |
|
679 | 679 | answer = r_write.json() |
|
680 | 680 | self.message = answer['message'] |
|
681 | 681 | except: |
|
682 | 682 | self.message = "Could not write ABS parameters" |
|
683 | 683 | return 0 |
|
684 | 684 | |
|
685 | 685 | |
|
686 | 686 | #self.device.status = int(answer['status']) |
|
687 | 687 | |
|
688 | 688 | return 1 |
|
689 | 689 | |
|
690 | 690 | |
|
691 | 691 | |
|
692 | 692 | def module_6bits(self, module): |
|
693 | 693 | """ |
|
694 | 694 | This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module |
|
695 | 695 | """ |
|
696 | 696 | if module > 64: |
|
697 | 697 | beam_bits = "" |
|
698 | 698 | return beam_bits |
|
699 | 699 | |
|
700 | 700 | data = ast.literal_eval(self.antenna) |
|
701 | 701 | up_data = data['antenna_up'] |
|
702 | 702 | down_data = data['antenna_down'] |
|
703 | 703 | |
|
704 | 704 | pos = ip2position(module) |
|
705 | 705 | up_value = up_data[pos[0]][pos[1]] |
|
706 | 706 | down_value = down_data[pos[0]][pos[1]] |
|
707 | 707 | |
|
708 | 708 | up_bits = up_conv_bits(up_value) |
|
709 | 709 | down_bits = down_conv_bits(down_value) |
|
710 | 710 | beam_bits = up_bits+down_bits |
|
711 | 711 | |
|
712 | 712 | return beam_bits |
|
713 | 713 | |
|
714 | 714 | def modules_6bits(self): |
|
715 | 715 | """ |
|
716 | 716 | This function returns 6bits from every abs module (1-64) in a dict |
|
717 | 717 | """ |
|
718 | 718 | modules_configuration = ast.literal_eval(self.modules_conf) |
|
719 | 719 | |
|
720 | 720 | for i in range(1,65): |
|
721 | 721 | modules_configuration[str(i)] = self.module_6bits(i) |
|
722 | 722 | |
|
723 | 723 | self.modules_conf = json.dumps(modules_configuration) |
|
724 | 724 | self.save() |
|
725 | 725 | |
|
726 | 726 | return self.modules_conf |
|
727 | 727 | |
|
728 | 728 | def add_beam2list(self): |
|
729 | 729 | """ |
|
730 | 730 | This function adds a beam to the beams list of ABS Configuration. |
|
731 | 731 | """ |
|
732 | 732 | beams = ast.literal_eval(self.abs_conf.beams) |
|
733 | 733 | if any(beams): |
|
734 | 734 | for beam in beams: |
|
735 | 735 | if beams[beam] == self.id: |
|
736 | 736 | return |
|
737 | 737 | i = len(beams)+1 |
|
738 | 738 | beams['beam'+str(i)] = self.id |
|
739 | 739 | else: |
|
740 | 740 | beams['beam1'] = self.id |
|
741 | 741 | |
|
742 | 742 | self.abs_conf.beams = json.dumps(beams) |
|
743 | 743 | self.abs_conf.save() |
|
744 | 744 | |
|
745 | 745 | return |
|
746 | 746 | |
|
747 | 747 | def remove_beamfromlist(self): |
|
748 | 748 | """ |
|
749 | 749 | This function removes current beam from the beams list of ABS Configuration. |
|
750 | 750 | """ |
|
751 | 751 | beams = ast.literal_eval(self.abs_conf.beams) |
|
752 | 752 | dict_position = '' |
|
753 | 753 | |
|
754 | 754 | if any(beams): |
|
755 | 755 | for beam in beams: |
|
756 | 756 | if beams[beam] == self.id: |
|
757 | 757 | dict_position = beam |
|
758 | 758 | if dict_position != '': |
|
759 | 759 | beams.pop(str(dict_position),None) |
|
760 | 760 | else: |
|
761 | 761 | return |
|
762 | 762 | |
|
763 | 763 | self.abs_conf.beams = json.dumps(beams) |
|
764 | 764 | self.abs_conf.save() |
|
765 | 765 | |
|
766 | 766 | return |
|
767 | 767 | |
|
768 | 768 | @property |
|
769 | 769 | def get_upvalues(self): |
|
770 | 770 | """ |
|
771 | 771 | This function reads antenna pattern and show the up-value of one abs module |
|
772 | 772 | """ |
|
773 | 773 | |
|
774 | 774 | data = ast.literal_eval(self.antenna) |
|
775 | 775 | up_data = data['antenna_up'] |
|
776 | 776 | |
|
777 | 777 | up_values = [] |
|
778 | 778 | for data in up_data: |
|
779 | 779 | for i in range(0,8): |
|
780 | 780 | up_values.append(data[i]) |
|
781 | 781 | |
|
782 | 782 | return up_values |
|
783 | 783 | |
|
784 | 784 | @property |
|
785 | 785 | def antenna_upvalues(self): |
|
786 | 786 | """ |
|
787 | 787 | This function reads antenna pattern and show the up - values of one abs beam |
|
788 | 788 | in a particular order |
|
789 | 789 | """ |
|
790 | 790 | data = ast.literal_eval(self.antenna) |
|
791 | 791 | up_data = data['antenna_up'] |
|
792 | 792 | |
|
793 | 793 | return up_data |
|
794 | 794 | |
|
795 | 795 | @property |
|
796 | 796 | def antenna_downvalues(self): |
|
797 | 797 | """ |
|
798 | 798 | This function reads antenna pattern and show the down - values of one abs beam |
|
799 | 799 | in a particular order |
|
800 | 800 | """ |
|
801 | 801 | data = ast.literal_eval(self.antenna) |
|
802 | 802 | down_data = data['antenna_down'] |
|
803 | 803 | |
|
804 | 804 | return down_data |
|
805 | 805 | |
|
806 | 806 | @property |
|
807 | 807 | def get_downvalues(self): |
|
808 | 808 | """ |
|
809 | 809 | This function reads antenna pattern and show the down-value of one abs module |
|
810 | 810 | """ |
|
811 | 811 | |
|
812 | 812 | data = ast.literal_eval(self.antenna) |
|
813 | 813 | down_data = data['antenna_down'] |
|
814 | 814 | |
|
815 | 815 | down_values = [] |
|
816 | 816 | for data in down_data: |
|
817 | 817 | for i in range(0,8): |
|
818 | 818 | down_values.append(data[i]) |
|
819 | 819 | |
|
820 | 820 | return down_values |
|
821 | 821 | |
|
822 | 822 | @property |
|
823 | 823 | def get_up_ues(self): |
|
824 | 824 | """ |
|
825 | 825 | This function shows the up-ues-value of one beam |
|
826 | 826 | """ |
|
827 | 827 | data = ast.literal_eval(self.ues) |
|
828 | 828 | up_ues = data['up'] |
|
829 | 829 | |
|
830 | 830 | return up_ues |
|
831 | 831 | |
|
832 | 832 | @property |
|
833 | 833 | def get_down_ues(self): |
|
834 | 834 | """ |
|
835 | 835 | This function shows the down-ues-value of one beam |
|
836 | 836 | """ |
|
837 | 837 | data = ast.literal_eval(self.ues) |
|
838 | 838 | down_ues = data['down'] |
|
839 | 839 | |
|
840 | 840 | return down_ues |
|
841 | 841 | |
|
842 | 842 | @property |
|
843 | 843 | def get_up_onlyrx(self): |
|
844 | 844 | """ |
|
845 | 845 | This function shows the up-onlyrx-value of one beam |
|
846 | 846 | """ |
|
847 | 847 | data = json.loads(self.only_rx) |
|
848 | 848 | up_onlyrx = data['up'] |
|
849 | 849 | |
|
850 | 850 | return up_onlyrx |
|
851 | 851 | |
|
852 | 852 | @property |
|
853 | 853 | def get_down_onlyrx(self): |
|
854 | 854 | """ |
|
855 | 855 | This function shows the down-onlyrx-value of one beam |
|
856 | 856 | """ |
|
857 | 857 | data = json.loads(self.only_rx) |
|
858 | 858 | down_onlyrx = data['down'] |
|
859 | 859 | |
|
860 | 860 | return down_onlyrx |
|
861 | 861 | |
|
862 | 862 | @property |
|
863 | 863 | def get_tx(self): |
|
864 | 864 | """ |
|
865 | 865 | This function shows the tx-values of one beam |
|
866 | 866 | """ |
|
867 | 867 | data = json.loads(self.tx) |
|
868 | 868 | |
|
869 | 869 | return data |
|
870 | 870 | |
|
871 | 871 | @property |
|
872 | 872 | def get_uptx(self): |
|
873 | 873 | """ |
|
874 | 874 | This function shows the up-tx-values of one beam |
|
875 | 875 | """ |
|
876 | 876 | data = json.loads(self.tx) |
|
877 | 877 | up_data = data['up'] |
|
878 | 878 | |
|
879 | 879 | up_values = [] |
|
880 | 880 | for data in up_data: |
|
881 | 881 | for i in range(0,8): |
|
882 | 882 | up_values.append(data[i]) |
|
883 | 883 | |
|
884 | 884 | return up_values |
|
885 | 885 | |
|
886 | 886 | @property |
|
887 | 887 | def get_downtx(self): |
|
888 | 888 | """ |
|
889 | 889 | This function shows the down-tx-values of one beam |
|
890 | 890 | """ |
|
891 | 891 | data = json.loads(self.tx) |
|
892 | 892 | down_data = data['down'] |
|
893 | 893 | |
|
894 | 894 | down_values = [] |
|
895 | 895 | for data in down_data: |
|
896 | 896 | for i in range(0,8): |
|
897 | 897 | down_values.append(data[i]) |
|
898 | 898 | |
|
899 | 899 | return down_values |
|
900 | 900 | |
|
901 | 901 | |
|
902 | 902 | |
|
903 | 903 | @property |
|
904 | 904 | def get_rx(self): |
|
905 | 905 | """ |
|
906 | 906 | This function shows the rx-values of one beam |
|
907 | 907 | """ |
|
908 | 908 | data = json.loads(self.rx) |
|
909 | 909 | |
|
910 | 910 | return data |
|
911 | 911 | |
|
912 | 912 | @property |
|
913 | 913 | def get_uprx(self): |
|
914 | 914 | """ |
|
915 | 915 | This function shows the up-rx-values of one beam |
|
916 | 916 | """ |
|
917 | 917 | data = json.loads(self.rx) |
|
918 | 918 | up_data = data['up'] |
|
919 | 919 | |
|
920 | 920 | up_values = [] |
|
921 | 921 | for data in up_data: |
|
922 | 922 | for i in range(0,8): |
|
923 | 923 | up_values.append(data[i]) |
|
924 | 924 | |
|
925 | 925 | return up_values |
|
926 | 926 | |
|
927 | 927 | @property |
|
928 | 928 | def get_downrx(self): |
|
929 | 929 | """ |
|
930 | 930 | This function shows the down-rx-values of one beam |
|
931 | 931 | """ |
|
932 | 932 | data = json.loads(self.rx) |
|
933 | 933 | down_data = data['down'] |
|
934 | 934 | |
|
935 | 935 | down_values = [] |
|
936 | 936 | for data in down_data: |
|
937 | 937 | for i in range(0,8): |
|
938 | 938 | down_values.append(data[i]) |
|
939 | 939 | |
|
940 | 940 | return down_values |
@@ -1,15 +1,15 | |||
|
1 | 1 | from django.conf.urls import url |
|
2 | 2 | |
|
3 | import views | |
|
3 | from apps.abs import views | |
|
4 | 4 | |
|
5 | 5 | urlpatterns = ( |
|
6 | 6 | url(r'^(?P<id_conf>-?\d+)/$', views.abs_conf, name='url_abs_conf'), |
|
7 | 7 | url(r'^(?P<id_conf>-?\d+)/edit/$', views.abs_conf_edit, name='url_edit_abs_conf'), |
|
8 | 8 | #url(r'^(?P<id_conf>-?\d+)/read/$', views.dev_conf_read, name='url_read_abs_conf'), |
|
9 | 9 | #url(r'^(?P<id_conf>-?\d+)/import/$', views.dev_conf_import, name='url_import_abs_conf'), |
|
10 | 10 | #url(r'^(?P<id_conf>-?\d+)/export/$', views.dev_conf_export, name='url_export_abs_conf'), |
|
11 | 11 | url(r'^(?P<id_conf>-?\d+)/plot/$', views.plot_patterns, name='url_plot_abs_patterns'), |
|
12 | 12 | url(r'^(?P<id_conf>-?\d+)/add_beam/$', views.add_beam, name='url_add_abs_beam'), |
|
13 | 13 | url(r'^(?P<id_conf>-?\d+)/beam/(?P<id_beam>-?\d+)/delete/$', views.remove_beam, name='url_remove_abs_beam'), |
|
14 | 14 | url(r'^(?P<id_conf>-?\d+)/beam/(?P<id_beam>-?\d+)/edit/$', views.edit_beam, name='url_edit_abs_beam'), |
|
15 | 15 | ) |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,1589 +1,1587 | |||
|
1 | 1 | from django.shortcuts import render, redirect, get_object_or_404, HttpResponse |
|
2 | 2 | from django.utils.safestring import mark_safe |
|
3 | 3 | from django.http import HttpResponseRedirect |
|
4 | 4 | from django.core.urlresolvers import reverse |
|
5 | 5 | from django.db.models import Q |
|
6 | 6 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
|
7 | 7 | from django.contrib import messages |
|
8 | 8 | from django.http.request import QueryDict |
|
9 | 9 | from datetime import datetime |
|
10 | 10 | |
|
11 | 11 | try: |
|
12 | 12 | from urllib.parse import urlencode |
|
13 | 13 | except ImportError: |
|
14 | 14 | from urllib import urlencode |
|
15 | 15 | |
|
16 | 16 | from .forms import CampaignForm, ExperimentForm, DeviceForm, ConfigurationForm, LocationForm, UploadFileForm, DownloadFileForm, OperationForm, NewForm |
|
17 | 17 | from .forms import OperationSearchForm, FilterForm |
|
18 | 18 | |
|
19 | 19 | from apps.rc.forms import RCConfigurationForm |
|
20 | 20 | from apps.dds.forms import DDSConfigurationForm |
|
21 | 21 | from apps.jars.forms import JARSConfigurationForm |
|
22 | 22 | from apps.cgs.forms import CGSConfigurationForm |
|
23 | 23 | from apps.abs.forms import ABSConfigurationForm |
|
24 | 24 | from apps.usrp.forms import USRPConfigurationForm |
|
25 | 25 | |
|
26 | 26 | from .models import Campaign, Experiment, Device, Configuration, Location, RunningExperiment |
|
27 | 27 | from apps.cgs.models import CGSConfiguration |
|
28 | 28 | from apps.jars.models import JARSConfiguration, EXPERIMENT_TYPE |
|
29 | 29 | from apps.usrp.models import USRPConfiguration |
|
30 | 30 | from apps.abs.models import ABSConfiguration |
|
31 | 31 | from apps.rc.models import RCConfiguration, RCLine, RCLineType |
|
32 | 32 | from apps.dds.models import DDSConfiguration |
|
33 | 33 | from django.http.request import QueryDict |
|
34 | 34 | #from __builtin__ import False |
|
35 | 35 | |
|
36 | 36 | # Create your views here. |
|
37 | 37 | |
|
38 | 38 | CONF_FORMS = { |
|
39 | 39 | 'rc': RCConfigurationForm, |
|
40 | 40 | 'dds': DDSConfigurationForm, |
|
41 | 41 | 'jars': JARSConfigurationForm, |
|
42 | 42 | 'cgs': CGSConfigurationForm, |
|
43 | 43 | 'abs': ABSConfigurationForm, |
|
44 | 44 | 'usrp': USRPConfigurationForm, |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | CONF_MODELS = { |
|
48 | 48 | 'rc': RCConfiguration, |
|
49 | 49 | 'dds': DDSConfiguration, |
|
50 | 50 | 'jars': JARSConfiguration, |
|
51 | 51 | 'cgs': CGSConfiguration, |
|
52 | 52 | 'abs': ABSConfiguration, |
|
53 | 53 | 'usrp': USRPConfiguration, |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | MIX_MODES = { |
|
57 | 57 | '0': 'P', |
|
58 | 58 | '1': 'S', |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | MIX_OPERATIONS = { |
|
62 | 62 | '0': 'OR', |
|
63 | 63 | '1': 'XOR', |
|
64 | 64 | '2': 'AND', |
|
65 | 65 | '3': 'NAND', |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def index(request): |
|
70 | 70 | kwargs = {} |
|
71 | 71 | |
|
72 | 72 | return render(request, 'index.html', kwargs) |
|
73 | 73 | |
|
74 | 74 | |
|
75 | 75 | def locations(request): |
|
76 | 76 | |
|
77 | 77 | page = request.GET.get('page') |
|
78 | 78 | order = ('name',) |
|
79 | 79 | |
|
80 | 80 | kwargs = get_paginator(Location, page, order) |
|
81 | 81 | |
|
82 | 82 | kwargs['keys'] = ['name', 'description'] |
|
83 | 83 | kwargs['title'] = 'Radar System' |
|
84 | 84 | kwargs['suptitle'] = 'List' |
|
85 | 85 | |
|
86 | 86 | return render(request, 'base_list.html', kwargs) |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | def location(request, id_loc): |
|
90 | 90 | |
|
91 | 91 | location = get_object_or_404(Location, pk=id_loc) |
|
92 | 92 | |
|
93 | 93 | kwargs = {} |
|
94 | 94 | kwargs['location'] = location |
|
95 | 95 | kwargs['location_keys'] = ['name', 'description'] |
|
96 | 96 | |
|
97 | 97 | kwargs['title'] = 'Location' |
|
98 | 98 | kwargs['suptitle'] = 'Details' |
|
99 | 99 | |
|
100 | 100 | return render(request, 'location.html', kwargs) |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | def location_new(request): |
|
104 | 104 | |
|
105 | 105 | if request.method == 'GET': |
|
106 | 106 | form = LocationForm() |
|
107 | 107 | |
|
108 | 108 | if request.method == 'POST': |
|
109 | 109 | form = LocationForm(request.POST) |
|
110 | 110 | |
|
111 | 111 | if form.is_valid(): |
|
112 | 112 | form.save() |
|
113 | 113 | return redirect('url_locations') |
|
114 | 114 | |
|
115 | 115 | kwargs = {} |
|
116 | 116 | kwargs['form'] = form |
|
117 | 117 | kwargs['title'] = 'Radar System' |
|
118 | 118 | kwargs['suptitle'] = 'New' |
|
119 | 119 | kwargs['button'] = 'Create' |
|
120 | 120 | |
|
121 | 121 | return render(request, 'base_edit.html', kwargs) |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def location_edit(request, id_loc): |
|
125 | 125 | |
|
126 | 126 | location = get_object_or_404(Location, pk=id_loc) |
|
127 | 127 | |
|
128 | 128 | if request.method=='GET': |
|
129 | 129 | form = LocationForm(instance=location) |
|
130 | 130 | |
|
131 | 131 | if request.method=='POST': |
|
132 | 132 | form = LocationForm(request.POST, instance=location) |
|
133 | 133 | |
|
134 | 134 | if form.is_valid(): |
|
135 | 135 | form.save() |
|
136 | 136 | return redirect('url_locations') |
|
137 | 137 | |
|
138 | 138 | kwargs = {} |
|
139 | 139 | kwargs['form'] = form |
|
140 | 140 | kwargs['title'] = 'Location' |
|
141 | 141 | kwargs['suptitle'] = 'Edit' |
|
142 | 142 | kwargs['button'] = 'Update' |
|
143 | 143 | |
|
144 | 144 | return render(request, 'base_edit.html', kwargs) |
|
145 | 145 | |
|
146 | 146 | |
|
147 | 147 | def location_delete(request, id_loc): |
|
148 | 148 | |
|
149 | 149 | location = get_object_or_404(Location, pk=id_loc) |
|
150 | 150 | |
|
151 | 151 | if request.method=='POST': |
|
152 | 152 | |
|
153 | 153 | if request.user.is_staff: |
|
154 | 154 | location.delete() |
|
155 | 155 | return redirect('url_locations') |
|
156 | 156 | |
|
157 | 157 | messages.error(request, 'Not enough permission to delete this object') |
|
158 | 158 | return redirect(location.get_absolute_url()) |
|
159 | 159 | |
|
160 | 160 | kwargs = { |
|
161 | 161 | 'title': 'Delete', |
|
162 | 162 | 'suptitle': 'Location', |
|
163 | 163 | 'object': location, |
|
164 | 164 | 'previous': location.get_absolute_url(), |
|
165 | 165 | 'delete': True |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | return render(request, 'confirm.html', kwargs) |
|
169 | 169 | |
|
170 | 170 | |
|
171 | 171 | def devices(request): |
|
172 | 172 | |
|
173 | 173 | page = request.GET.get('page') |
|
174 | 174 | order = ('device_type', 'name') |
|
175 | 175 | |
|
176 | 176 | kwargs = get_paginator(Device, page, order) |
|
177 | 177 | kwargs['keys'] = ['name', 'ip_address', 'port_address', 'device_type'] |
|
178 | 178 | kwargs['title'] = 'Device' |
|
179 | 179 | kwargs['suptitle'] = 'List' |
|
180 | 180 | |
|
181 | 181 | return render(request, 'base_list.html', kwargs) |
|
182 | 182 | |
|
183 | 183 | |
|
184 | 184 | def device(request, id_dev): |
|
185 | 185 | |
|
186 | 186 | device = get_object_or_404(Device, pk=id_dev) |
|
187 | 187 | |
|
188 | 188 | kwargs = {} |
|
189 | 189 | kwargs['device'] = device |
|
190 | 190 | kwargs['device_keys'] = ['device_type', 'name', 'ip_address', 'port_address', 'description'] |
|
191 | 191 | |
|
192 | 192 | kwargs['title'] = 'Device' |
|
193 | 193 | kwargs['suptitle'] = 'Details' |
|
194 | 194 | |
|
195 | 195 | return render(request, 'device.html', kwargs) |
|
196 | 196 | |
|
197 | 197 | |
|
198 | 198 | def device_new(request): |
|
199 | 199 | |
|
200 | 200 | if request.method == 'GET': |
|
201 | 201 | form = DeviceForm() |
|
202 | 202 | |
|
203 | 203 | if request.method == 'POST': |
|
204 | 204 | form = DeviceForm(request.POST) |
|
205 | 205 | |
|
206 | 206 | if form.is_valid(): |
|
207 | 207 | form.save() |
|
208 | 208 | return redirect('url_devices') |
|
209 | 209 | |
|
210 | 210 | kwargs = {} |
|
211 | 211 | kwargs['form'] = form |
|
212 | 212 | kwargs['title'] = 'Device' |
|
213 | 213 | kwargs['suptitle'] = 'New' |
|
214 | 214 | kwargs['button'] = 'Create' |
|
215 | 215 | |
|
216 | 216 | return render(request, 'base_edit.html', kwargs) |
|
217 | 217 | |
|
218 | 218 | |
|
219 | 219 | def device_edit(request, id_dev): |
|
220 | 220 | |
|
221 | 221 | device = get_object_or_404(Device, pk=id_dev) |
|
222 | 222 | |
|
223 | 223 | if request.method=='GET': |
|
224 | 224 | form = DeviceForm(instance=device) |
|
225 | 225 | |
|
226 | 226 | if request.method=='POST': |
|
227 | 227 | form = DeviceForm(request.POST, instance=device) |
|
228 | 228 | |
|
229 | 229 | if form.is_valid(): |
|
230 | 230 | form.save() |
|
231 | 231 | return redirect(device.get_absolute_url()) |
|
232 | 232 | |
|
233 | 233 | kwargs = {} |
|
234 | 234 | kwargs['form'] = form |
|
235 | 235 | kwargs['title'] = 'Device' |
|
236 | 236 | kwargs['suptitle'] = 'Edit' |
|
237 | 237 | kwargs['button'] = 'Update' |
|
238 | 238 | |
|
239 | 239 | return render(request, 'base_edit.html', kwargs) |
|
240 | 240 | |
|
241 | 241 | |
|
242 | 242 | def device_delete(request, id_dev): |
|
243 | 243 | |
|
244 | 244 | device = get_object_or_404(Device, pk=id_dev) |
|
245 | 245 | |
|
246 | 246 | if request.method=='POST': |
|
247 | 247 | |
|
248 | 248 | if request.user.is_staff: |
|
249 | 249 | device.delete() |
|
250 | 250 | return redirect('url_devices') |
|
251 | 251 | |
|
252 | 252 | messages.error(request, 'Not enough permission to delete this object') |
|
253 | 253 | return redirect(device.get_absolute_url()) |
|
254 | 254 | |
|
255 | 255 | kwargs = { |
|
256 | 256 | 'title': 'Delete', |
|
257 | 257 | 'suptitle': 'Device', |
|
258 | 258 | 'object': device, |
|
259 | 259 | 'previous': device.get_absolute_url(), |
|
260 | 260 | 'delete': True |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | return render(request, 'confirm.html', kwargs) |
|
264 | 264 | |
|
265 | 265 | |
|
266 | 266 | def campaigns(request): |
|
267 | 267 | |
|
268 | 268 | page = request.GET.get('page') |
|
269 | 269 | order = ('start_date',) |
|
270 | 270 | filters = request.GET.copy() |
|
271 | 271 | |
|
272 | 272 | kwargs = get_paginator(Campaign, page, order, filters) |
|
273 | 273 | |
|
274 | 274 | form = FilterForm(initial=request.GET, extra_fields=['range_date', 'tags','template']) |
|
275 | 275 | kwargs['keys'] = ['name', 'start_date', 'end_date'] |
|
276 | 276 | kwargs['title'] = 'Campaign' |
|
277 | 277 | kwargs['suptitle'] = 'List' |
|
278 | 278 | kwargs['form'] = form |
|
279 | 279 | filters.pop('page', None) |
|
280 | 280 | kwargs['q'] = urlencode(filters) |
|
281 | 281 | |
|
282 | 282 | return render(request, 'base_list.html', kwargs) |
|
283 | 283 | |
|
284 | 284 | |
|
285 | 285 | def campaign(request, id_camp): |
|
286 | 286 | |
|
287 | 287 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
288 | 288 | experiments = Experiment.objects.filter(campaign=campaign) |
|
289 | 289 | |
|
290 | 290 | form = CampaignForm(instance=campaign) |
|
291 | 291 | |
|
292 | 292 | kwargs = {} |
|
293 | 293 | kwargs['campaign'] = campaign |
|
294 | 294 | kwargs['campaign_keys'] = ['template', 'name', 'start_date', 'end_date', 'tags', 'description'] |
|
295 | 295 | |
|
296 | 296 | kwargs['experiments'] = experiments |
|
297 | 297 | kwargs['experiment_keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
298 | 298 | |
|
299 | 299 | kwargs['title'] = 'Campaign' |
|
300 | 300 | kwargs['suptitle'] = 'Details' |
|
301 | 301 | |
|
302 | 302 | kwargs['form'] = form |
|
303 | 303 | kwargs['button'] = 'Add Experiment' |
|
304 | 304 | |
|
305 | 305 | return render(request, 'campaign.html', kwargs) |
|
306 | 306 | |
|
307 | 307 | |
|
308 | 308 | def campaign_new(request): |
|
309 | 309 | |
|
310 | 310 | kwargs = {} |
|
311 | 311 | |
|
312 | 312 | if request.method == 'GET': |
|
313 | 313 | |
|
314 | 314 | if 'template' in request.GET: |
|
315 | 315 | if request.GET['template']=='0': |
|
316 | 316 | form = NewForm(initial={'create_from':2}, |
|
317 | 317 | template_choices=Campaign.objects.filter(template=True).values_list('id', 'name')) |
|
318 | 318 | else: |
|
319 | 319 | kwargs['button'] = 'Create' |
|
320 | 320 | kwargs['experiments'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
321 | 321 | kwargs['experiment_keys'] = ['name', 'start_time', 'end_time'] |
|
322 | 322 | camp = Campaign.objects.get(pk=request.GET['template']) |
|
323 | 323 | form = CampaignForm(instance=camp, |
|
324 | 324 | initial={'name':'{} [{:%Y/%m/%d}]'.format(camp.name, datetime.now()), |
|
325 | 325 | 'template':False}) |
|
326 | 326 | elif 'blank' in request.GET: |
|
327 | 327 | kwargs['button'] = 'Create' |
|
328 | 328 | form = CampaignForm() |
|
329 | 329 | else: |
|
330 | 330 | form = NewForm() |
|
331 | 331 | |
|
332 | 332 | if request.method == 'POST': |
|
333 | 333 | kwargs['button'] = 'Create' |
|
334 | 334 | post = request.POST.copy() |
|
335 | 335 | experiments = [] |
|
336 | 336 | |
|
337 | 337 | for id_exp in post.getlist('experiments'): |
|
338 | 338 | exp = Experiment.objects.get(pk=id_exp) |
|
339 | 339 | new_exp = exp.clone(template=False) |
|
340 | 340 | experiments.append(new_exp) |
|
341 | 341 | |
|
342 | 342 | post.setlist('experiments', []) |
|
343 | 343 | |
|
344 | 344 | form = CampaignForm(post) |
|
345 | 345 | |
|
346 | 346 | if form.is_valid(): |
|
347 | 347 | campaign = form.save() |
|
348 | 348 | for exp in experiments: |
|
349 | 349 | campaign.experiments.add(exp) |
|
350 | 350 | campaign.save() |
|
351 | 351 | return redirect('url_campaign', id_camp=campaign.id) |
|
352 | 352 | |
|
353 | 353 | kwargs['form'] = form |
|
354 | 354 | kwargs['title'] = 'Campaign' |
|
355 | 355 | kwargs['suptitle'] = 'New' |
|
356 | 356 | |
|
357 | 357 | return render(request, 'campaign_edit.html', kwargs) |
|
358 | 358 | |
|
359 | 359 | |
|
360 | 360 | def campaign_edit(request, id_camp): |
|
361 | 361 | |
|
362 | 362 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
363 | 363 | |
|
364 | 364 | if request.method=='GET': |
|
365 | 365 | form = CampaignForm(instance=campaign) |
|
366 | 366 | |
|
367 | 367 | if request.method=='POST': |
|
368 | 368 | exps = campaign.experiments.all().values_list('pk', flat=True) |
|
369 | 369 | post = request.POST.copy() |
|
370 | 370 | new_exps = post.getlist('experiments') |
|
371 | 371 | post.setlist('experiments', []) |
|
372 | 372 | form = CampaignForm(post, instance=campaign) |
|
373 | 373 | |
|
374 | 374 | if form.is_valid(): |
|
375 | 375 | camp = form.save() |
|
376 | 376 | for id_exp in new_exps: |
|
377 | 377 | if int(id_exp) in exps: |
|
378 | 378 | exps.pop(id_exp) |
|
379 | 379 | else: |
|
380 | 380 | exp = Experiment.objects.get(pk=id_exp) |
|
381 | 381 | if exp.template: |
|
382 | 382 | camp.experiments.add(exp.clone(template=False)) |
|
383 | 383 | else: |
|
384 | 384 | camp.experiments.add(exp) |
|
385 | 385 | |
|
386 | 386 | for id_exp in exps: |
|
387 | 387 | camp.experiments.remove(Experiment.objects.get(pk=id_exp)) |
|
388 | 388 | |
|
389 | 389 | return redirect('url_campaign', id_camp=id_camp) |
|
390 | 390 | |
|
391 | 391 | kwargs = {} |
|
392 | 392 | kwargs['form'] = form |
|
393 | 393 | kwargs['title'] = 'Campaign' |
|
394 | 394 | kwargs['suptitle'] = 'Edit' |
|
395 | 395 | kwargs['button'] = 'Update' |
|
396 | 396 | |
|
397 | 397 | return render(request, 'campaign_edit.html', kwargs) |
|
398 | 398 | |
|
399 | 399 | |
|
400 | 400 | def campaign_delete(request, id_camp): |
|
401 | 401 | |
|
402 | 402 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
403 | 403 | |
|
404 | 404 | if request.method=='POST': |
|
405 | 405 | if request.user.is_staff: |
|
406 | 406 | |
|
407 | 407 | for exp in campaign.experiments.all(): |
|
408 | 408 | for conf in Configuration.objects.filter(experiment=exp): |
|
409 | 409 | conf.delete() |
|
410 | 410 | exp.delete() |
|
411 | 411 | campaign.delete() |
|
412 | 412 | |
|
413 | 413 | return redirect('url_campaigns') |
|
414 | 414 | |
|
415 | 415 | messages.error(request, 'Not enough permission to delete this object') |
|
416 | 416 | return redirect(campaign.get_absolute_url()) |
|
417 | 417 | |
|
418 | 418 | kwargs = { |
|
419 | 419 | 'title': 'Delete', |
|
420 | 420 | 'suptitle': 'Campaign', |
|
421 | 421 | 'object': campaign, |
|
422 | 422 | 'previous': campaign.get_absolute_url(), |
|
423 | 423 | 'delete': True |
|
424 | 424 | } |
|
425 | 425 | |
|
426 | 426 | return render(request, 'confirm.html', kwargs) |
|
427 | 427 | |
|
428 | 428 | def campaign_export(request, id_camp): |
|
429 | 429 | |
|
430 | 430 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
431 | 431 | content = campaign.parms_to_dict() |
|
432 | 432 | content_type = 'application/json' |
|
433 | 433 | filename = '%s_%s.json' %(campaign.name, campaign.id) |
|
434 | 434 | |
|
435 | 435 | response = HttpResponse(content_type=content_type) |
|
436 | 436 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
437 | 437 | response.write(content) |
|
438 | 438 | |
|
439 | 439 | return response |
|
440 | 440 | |
|
441 | 441 | |
|
442 | 442 | def campaign_import(request, id_camp): |
|
443 | 443 | |
|
444 | 444 | campaign = get_object_or_404(Campaign, pk=id_camp) |
|
445 | 445 | |
|
446 | 446 | if request.method == 'GET': |
|
447 | 447 | file_form = UploadFileForm() |
|
448 | 448 | |
|
449 | 449 | if request.method == 'POST': |
|
450 | 450 | file_form = UploadFileForm(request.POST, request.FILES) |
|
451 | 451 | |
|
452 | 452 | if file_form.is_valid(): |
|
453 | 453 | |
|
454 | 454 | parms = campaign.import_from_file(request.FILES['file']) |
|
455 | 455 | |
|
456 | 456 | if parms: |
|
457 | 457 | parms['name'] = parms['campaign'] |
|
458 | 458 | |
|
459 | 459 | new_camp = campaign.dict_to_parms(parms, CONF_MODELS) |
|
460 | 460 | |
|
461 | 461 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
462 | 462 | |
|
463 | 463 | return redirect(new_camp.get_absolute_url_edit()) |
|
464 | 464 | |
|
465 | 465 | messages.error(request, "Could not import parameters from file") |
|
466 | 466 | |
|
467 | 467 | kwargs = {} |
|
468 | 468 | kwargs['title'] = 'Campaign' |
|
469 | 469 | kwargs['form'] = file_form |
|
470 | 470 | kwargs['suptitle'] = 'Importing file' |
|
471 | 471 | kwargs['button'] = 'Import' |
|
472 | 472 | |
|
473 | 473 | return render(request, 'campaign_import.html', kwargs) |
|
474 | 474 | |
|
475 | 475 | |
|
476 | 476 | def experiments(request): |
|
477 | 477 | |
|
478 | 478 | page = request.GET.get('page') |
|
479 | 479 | order = ('location',) |
|
480 | 480 | filters = request.GET.copy() |
|
481 | 481 | |
|
482 | 482 | kwargs = get_paginator(Experiment, page, order, filters) |
|
483 | 483 | |
|
484 | 484 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
485 | 485 | |
|
486 | 486 | kwargs['keys'] = ['name', 'radar_system', 'start_time', 'end_time'] |
|
487 | 487 | kwargs['title'] = 'Experiment' |
|
488 | 488 | kwargs['suptitle'] = 'List' |
|
489 | 489 | kwargs['form'] = form |
|
490 | 490 | filters.pop('page', None) |
|
491 | 491 | kwargs['q'] = urlencode(filters) |
|
492 | 492 | |
|
493 | 493 | return render(request, 'base_list.html', kwargs) |
|
494 | 494 | |
|
495 | 495 | |
|
496 | 496 | def experiment(request, id_exp): |
|
497 | 497 | |
|
498 | 498 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
499 | 499 | |
|
500 | 500 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
501 | 501 | |
|
502 | 502 | kwargs = {} |
|
503 | 503 | |
|
504 | 504 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] |
|
505 | 505 | kwargs['experiment'] = experiment |
|
506 | 506 | |
|
507 | 507 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
508 | 508 | kwargs['configurations'] = configurations |
|
509 | 509 | |
|
510 | 510 | kwargs['title'] = 'Experiment' |
|
511 | 511 | kwargs['suptitle'] = 'Details' |
|
512 | 512 | |
|
513 | 513 | kwargs['button'] = 'Add Configuration' |
|
514 | 514 | |
|
515 | 515 | ###### SIDEBAR ###### |
|
516 | 516 | kwargs.update(sidebar(experiment=experiment)) |
|
517 | 517 | |
|
518 | 518 | return render(request, 'experiment.html', kwargs) |
|
519 | 519 | |
|
520 | 520 | |
|
521 | 521 | def experiment_new(request, id_camp=None): |
|
522 | 522 | |
|
523 | 523 | kwargs = {} |
|
524 | 524 | |
|
525 | 525 | if request.method == 'GET': |
|
526 | 526 | if 'template' in request.GET: |
|
527 | 527 | if request.GET['template']=='0': |
|
528 | 528 | form = NewForm(initial={'create_from':2}, |
|
529 | 529 | template_choices=Experiment.objects.filter(template=True).values_list('id', 'name')) |
|
530 | 530 | else: |
|
531 | 531 | kwargs['button'] = 'Create' |
|
532 | 532 | kwargs['configurations'] = Configuration.objects.filter(experiment=request.GET['template']) |
|
533 | 533 | kwargs['configuration_keys'] = ['name', 'device__name', 'device__ip_address', 'device__port_address'] |
|
534 | 534 | exp=Experiment.objects.get(pk=request.GET['template']) |
|
535 | 535 | form = ExperimentForm(instance=exp, |
|
536 | 536 | initial={'name': '{} [{:%Y/%m/%d}]'.format(exp.name, datetime.now()), |
|
537 | 537 | 'template': False}) |
|
538 | 538 | elif 'blank' in request.GET: |
|
539 | 539 | kwargs['button'] = 'Create' |
|
540 | 540 | form = ExperimentForm() |
|
541 | 541 | else: |
|
542 | 542 | form = NewForm() |
|
543 | 543 | |
|
544 | 544 | if request.method == 'POST': |
|
545 | 545 | form = ExperimentForm(request.POST) |
|
546 | 546 | if form.is_valid(): |
|
547 | 547 | experiment = form.save() |
|
548 | 548 | |
|
549 | 549 | if 'template' in request.GET: |
|
550 | 550 | configurations = Configuration.objects.filter(experiment=request.GET['template'], type=0) |
|
551 | 551 | for conf in configurations: |
|
552 | 552 | conf.clone(experiment=experiment, template=False) |
|
553 | 553 | |
|
554 | 554 | return redirect('url_experiment', id_exp=experiment.id) |
|
555 | 555 | |
|
556 | 556 | kwargs['form'] = form |
|
557 | 557 | kwargs['title'] = 'Experiment' |
|
558 | 558 | kwargs['suptitle'] = 'New' |
|
559 | 559 | |
|
560 | 560 | return render(request, 'experiment_edit.html', kwargs) |
|
561 | 561 | |
|
562 | 562 | |
|
563 | 563 | def experiment_edit(request, id_exp): |
|
564 | 564 | |
|
565 | 565 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
566 | 566 | |
|
567 | 567 | if request.method == 'GET': |
|
568 | 568 | form = ExperimentForm(instance=experiment) |
|
569 | 569 | |
|
570 | 570 | if request.method=='POST': |
|
571 | 571 | form = ExperimentForm(request.POST, instance=experiment) |
|
572 | 572 | |
|
573 | 573 | if form.is_valid(): |
|
574 | 574 | experiment = form.save() |
|
575 | 575 | return redirect('url_experiment', id_exp=experiment.id) |
|
576 | 576 | |
|
577 | 577 | kwargs = {} |
|
578 | 578 | kwargs['form'] = form |
|
579 | 579 | kwargs['title'] = 'Experiment' |
|
580 | 580 | kwargs['suptitle'] = 'Edit' |
|
581 | 581 | kwargs['button'] = 'Update' |
|
582 | 582 | |
|
583 | 583 | return render(request, 'experiment_edit.html', kwargs) |
|
584 | 584 | |
|
585 | 585 | |
|
586 | 586 | def experiment_delete(request, id_exp): |
|
587 | 587 | |
|
588 | 588 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
589 | 589 | |
|
590 | 590 | if request.method=='POST': |
|
591 | 591 | if request.user.is_staff: |
|
592 | 592 | for conf in Configuration.objects.filter(experiment=experiment): |
|
593 | 593 | conf.delete() |
|
594 | 594 | experiment.delete() |
|
595 | 595 | return redirect('url_experiments') |
|
596 | 596 | |
|
597 | 597 | messages.error(request, 'Not enough permission to delete this object') |
|
598 | 598 | return redirect(experiment.get_absolute_url()) |
|
599 | 599 | |
|
600 | 600 | kwargs = { |
|
601 | 601 | 'title': 'Delete', |
|
602 | 602 | 'suptitle': 'Experiment', |
|
603 | 603 | 'object': experiment, |
|
604 | 604 | 'previous': experiment.get_absolute_url(), |
|
605 | 605 | 'delete': True |
|
606 | 606 | } |
|
607 | 607 | |
|
608 | 608 | return render(request, 'confirm.html', kwargs) |
|
609 | 609 | |
|
610 | 610 | |
|
611 | 611 | def experiment_export(request, id_exp): |
|
612 | 612 | |
|
613 | 613 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
614 | 614 | content = experiment.parms_to_dict() |
|
615 | 615 | content_type = 'application/json' |
|
616 | 616 | filename = '%s_%s.json' %(experiment.name, experiment.id) |
|
617 | 617 | |
|
618 | 618 | response = HttpResponse(content_type=content_type) |
|
619 | 619 | response['Content-Disposition'] = 'attachment; filename="%s"' %filename |
|
620 | 620 | response.write(content) |
|
621 | 621 | |
|
622 | 622 | return response |
|
623 | 623 | |
|
624 | 624 | def experiment_import(request, id_exp): |
|
625 | 625 | |
|
626 | 626 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
627 | 627 | configurations = Configuration.objects.filter(experiment=experiment) |
|
628 | 628 | |
|
629 | 629 | if request.method == 'GET': |
|
630 | 630 | file_form = UploadFileForm() |
|
631 | 631 | |
|
632 | 632 | if request.method == 'POST': |
|
633 | 633 | file_form = UploadFileForm(request.POST, request.FILES) |
|
634 | 634 | |
|
635 | 635 | if file_form.is_valid(): |
|
636 | 636 | |
|
637 | 637 | parms = experiment.import_from_file(request.FILES['file']) |
|
638 | 638 | |
|
639 | 639 | if parms: |
|
640 | 640 | |
|
641 | 641 | new_exp = experiment.dict_to_parms(parms, CONF_MODELS) |
|
642 | 642 | |
|
643 | 643 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
644 | 644 | |
|
645 | 645 | return redirect(new_exp.get_absolute_url_edit()) |
|
646 | 646 | |
|
647 | 647 | messages.error(request, "Could not import parameters from file") |
|
648 | 648 | |
|
649 | 649 | kwargs = {} |
|
650 | 650 | kwargs['title'] = 'Experiment' |
|
651 | 651 | kwargs['form'] = file_form |
|
652 | 652 | kwargs['suptitle'] = 'Importing file' |
|
653 | 653 | kwargs['button'] = 'Import' |
|
654 | 654 | |
|
655 | 655 | kwargs.update(sidebar(experiment=experiment)) |
|
656 | 656 | |
|
657 | 657 | return render(request, 'experiment_import.html', kwargs) |
|
658 | 658 | |
|
659 | 659 | def experiment_mix(request, id_exp): |
|
660 | 660 | |
|
661 | 661 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
662 | 662 | rc_confs = [conf for conf in RCConfiguration.objects.filter(experiment=id_exp, |
|
663 | 663 | mix=False)] |
|
664 | 664 | |
|
665 | 665 | if len(rc_confs)<2: |
|
666 | 666 | messages.warning(request, 'You need at least two RC Configurations to make a mix') |
|
667 | 667 | return redirect(experiment.get_absolute_url()) |
|
668 | 668 | |
|
669 | 669 | mix_confs = RCConfiguration.objects.filter(experiment=id_exp, mix=True) |
|
670 | 670 | |
|
671 | 671 | if mix_confs: |
|
672 | 672 | mix = mix_confs[0] |
|
673 | 673 | else: |
|
674 | 674 | mix = RCConfiguration(experiment=experiment, |
|
675 | 675 | device=rc_confs[0].device, |
|
676 | 676 | ipp=rc_confs[0].ipp, |
|
677 | 677 | clock_in=rc_confs[0].clock_in, |
|
678 | 678 | clock_divider=rc_confs[0].clock_divider, |
|
679 | 679 | mix=True, |
|
680 | 680 | parameters='') |
|
681 | 681 | mix.save() |
|
682 | 682 | |
|
683 | 683 | line_type = RCLineType.objects.get(name='mix') |
|
684 | 684 | for i in range(len(rc_confs[0].get_lines())): |
|
685 | 685 | line = RCLine(rc_configuration=mix, line_type=line_type, channel=i) |
|
686 | 686 | line.save() |
|
687 | 687 | |
|
688 | 688 | initial = {'name': mix.name, |
|
689 | 689 | 'result': parse_mix_result(mix.parameters), |
|
690 | 690 | 'delay': 0, |
|
691 | 691 | 'mask': [0,1,2,3,4,5,6,7] |
|
692 | 692 | } |
|
693 | 693 | |
|
694 | 694 | if request.method=='GET': |
|
695 | 695 | form = RCMixConfigurationForm(confs=rc_confs, initial=initial) |
|
696 | 696 | |
|
697 | 697 | if request.method=='POST': |
|
698 | 698 | result = mix.parameters |
|
699 | 699 | |
|
700 | 700 | if '{}|'.format(request.POST['experiment']) in result: |
|
701 | 701 | messages.error(request, 'Configuration already added') |
|
702 | 702 | else: |
|
703 | 703 | if 'operation' in request.POST: |
|
704 | 704 | operation = MIX_OPERATIONS[request.POST['operation']] |
|
705 | 705 | else: |
|
706 | 706 | operation = ' ' |
|
707 | 707 | |
|
708 | 708 | mode = MIX_MODES[request.POST['mode']] |
|
709 | 709 | |
|
710 | 710 | if result: |
|
711 | 711 | result = '{}-{}|{}|{}|{}|{}'.format(mix.parameters, |
|
712 | 712 | request.POST['experiment'], |
|
713 | 713 | mode, |
|
714 | 714 | operation, |
|
715 | 715 | float(request.POST['delay']), |
|
716 | 716 | parse_mask(request.POST.getlist('mask')) |
|
717 | 717 | ) |
|
718 | 718 | else: |
|
719 | 719 | result = '{}|{}|{}|{}|{}'.format(request.POST['experiment'], |
|
720 | 720 | mode, |
|
721 | 721 | operation, |
|
722 | 722 | float(request.POST['delay']), |
|
723 | 723 | parse_mask(request.POST.getlist('mask')) |
|
724 | 724 | ) |
|
725 | 725 | |
|
726 | 726 | mix.parameters = result |
|
727 | 727 | mix.name = request.POST['name'] |
|
728 | 728 | mix.save() |
|
729 | 729 | mix.update_pulses() |
|
730 | 730 | |
|
731 | 731 | initial['result'] = parse_mix_result(result) |
|
732 | 732 | initial['name'] = mix.name |
|
733 | 733 | |
|
734 | 734 | form = RCMixConfigurationForm(initial=initial, confs=rc_confs) |
|
735 | 735 | |
|
736 | 736 | |
|
737 | 737 | kwargs = { |
|
738 | 738 | 'title': 'Experiment', |
|
739 | 739 | 'suptitle': 'Mix Configurations', |
|
740 | 740 | 'form' : form, |
|
741 | 741 | 'extra_button': 'Delete', |
|
742 | 742 | 'button': 'Add', |
|
743 | 743 | 'cancel': 'Back', |
|
744 | 744 | 'previous': experiment.get_absolute_url(), |
|
745 | 745 | 'id_exp':id_exp, |
|
746 | 746 | |
|
747 | 747 | } |
|
748 | 748 | |
|
749 | 749 | return render(request, 'experiment_mix.html', kwargs) |
|
750 | 750 | |
|
751 | 751 | |
|
752 | 752 | def experiment_mix_delete(request, id_exp): |
|
753 | 753 | |
|
754 | 754 | conf = RCConfiguration.objects.get(experiment=id_exp, mix=True) |
|
755 | 755 | values = conf.parameters.split('-') |
|
756 | 756 | conf.parameters = '-'.join(values[:-1]) |
|
757 | 757 | conf.save() |
|
758 | 758 | |
|
759 | 759 | return redirect('url_mix_experiment', id_exp=id_exp) |
|
760 | 760 | |
|
761 | 761 | |
|
762 | 762 | def experiment_summary(request, id_exp): |
|
763 | 763 | |
|
764 | 764 | import json |
|
765 | 765 | import ast |
|
766 | 766 | |
|
767 | 767 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
768 | 768 | experiment_data = json.loads(experiment.parms_to_dict()) |
|
769 | 769 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
770 | 770 | |
|
771 | 771 | kwargs = {} |
|
772 | 772 | |
|
773 | 773 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] |
|
774 | 774 | kwargs['experiment'] = experiment |
|
775 | 775 | |
|
776 | 776 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
777 | 777 | kwargs['configurations'] = configurations |
|
778 | 778 | kwargs['experiment_data'] = experiment_data |
|
779 | 779 | |
|
780 | 780 | kwargs['title'] = 'Experiment Summary' |
|
781 | 781 | kwargs['suptitle'] = 'Details' |
|
782 | 782 | |
|
783 | 783 | kwargs['button'] = 'Verify Parameters' |
|
784 | 784 | |
|
785 | 785 | jars_conf = False |
|
786 | 786 | rc_conf = False |
|
787 | 787 | |
|
788 | 788 | for configuration in configurations: |
|
789 | 789 | #-------------------- JARS -----------------------: |
|
790 | 790 | if configuration.device.device_type.name == 'jars': |
|
791 | 791 | jars_conf = True |
|
792 | 792 | kwargs['jars_conf'] = jars_conf |
|
793 | 793 | kwargs['exp_type'] = EXPERIMENT_TYPE[configuration.exp_type][1] |
|
794 | 794 | channels_number = configuration.channels_number |
|
795 | 795 | exp_type = configuration.exp_type |
|
796 | 796 | fftpoints = configuration.fftpoints |
|
797 | 797 | filter_parms = configuration.filter_parms |
|
798 | 798 | filter_parms = ast.literal_eval(filter_parms) |
|
799 | 799 | spectral_number = configuration.spectral_number |
|
800 | 800 | |
|
801 | 801 | #--------------------- RC ----------------------: |
|
802 | 802 | if configuration.device.device_type.name == 'rc': |
|
803 | 803 | rc_conf = True |
|
804 | 804 | kwargs['rc_conf'] = rc_conf |
|
805 | 805 | rc_lines = experiment_data['configurations']['rc']['lines'] |
|
806 | 806 | ipp = configuration.ipp |
|
807 | 807 | if experiment_data['configurations']['rc']['mix'] == 'True': |
|
808 | 808 | tx = '' |
|
809 | 809 | code = '' |
|
810 | 810 | window = '' |
|
811 | 811 | else: |
|
812 | 812 | code = rc_lines[3]['code'] |
|
813 | 813 | |
|
814 | 814 | window_data = rc_lines[6]['params'][0] |
|
815 | 815 | h0 = str(window_data['first_height']) |
|
816 | 816 | dh = str(window_data['resolution']) |
|
817 | 817 | nsa = str(window_data['number_of_samples']) |
|
818 | 818 | window = 'Ho='+h0+'km\nDH='+dh+'km\nNSA='+nsa |
|
819 | 819 | |
|
820 | 820 | tx = '' |
|
821 | 821 | if float(rc_lines[1]['delays']) == 0: |
|
822 | 822 | tx = rc_lines[2]['pulse_width'] |
|
823 | 823 | elif float(rc_lines[2]['delays']) == 0: |
|
824 | 824 | tx = rc_lines[1]['pulse_width'] |
|
825 | 825 | else: |
|
826 | 826 | tx = rc_lines[1]['pulse_width']+' | '+rc_lines[2]['pulse_width'] |
|
827 | 827 | |
|
828 | 828 | kwargs['tx'] = tx |
|
829 | 829 | kwargs['code'] = code |
|
830 | 830 | kwargs['window'] = window |
|
831 | 831 | |
|
832 | 832 | #-------------------- DDS -----------------------: |
|
833 | 833 | if configuration.device.device_type.name == 'dds': |
|
834 | 834 | dds_conf = True |
|
835 | 835 | kwargs['dds_conf'] = dds_conf |
|
836 | 836 | |
|
837 | 837 | #------ RC & JARS ------: |
|
838 | 838 | ipp = 937.5 # |
|
839 | 839 | nsa = 200# |
|
840 | 840 | dh = 1.5 # |
|
841 | 841 | channels_number = 5 # |
|
842 | 842 | |
|
843 | 843 | if rc_conf and jars_conf: |
|
844 | 844 | if exp_type == 0: #Short |
|
845 | 845 | bytes = 2 |
|
846 | 846 | b = nsa*2*bytes*channels_number |
|
847 | 847 | else: #Float |
|
848 | 848 | bytes = 4 |
|
849 | 849 | channels = channels_number + spectral_number |
|
850 | 850 | b = nsa*2*bytes*fftpoints*channels |
|
851 | 851 | |
|
852 | 852 | ipps = (ipp*pow(10,-6))/0.15 |
|
853 | 853 | GB = 1048576.0*1024.0 |
|
854 | 854 | Hour = 3600 |
|
855 | 855 | rate = b/ipps |
|
856 | 856 | rate = rate *(1/GB)*(Hour) |
|
857 | 857 | kwargs['rate'] = str(rate)+" GB/h" |
|
858 | 858 | else: |
|
859 | 859 | kwargs['rate'] = '' |
|
860 | 860 | |
|
861 | 861 | ###### SIDEBAR ###### |
|
862 | 862 | kwargs.update(sidebar(experiment=experiment)) |
|
863 | 863 | |
|
864 | 864 | return render(request, 'experiment_summary.html', kwargs) |
|
865 | 865 | |
|
866 | 866 | def experiment_verify(request, id_exp): |
|
867 | 867 | |
|
868 | 868 | import json |
|
869 | 869 | import ast |
|
870 | 870 | |
|
871 | 871 | experiment = get_object_or_404(Experiment, pk=id_exp) |
|
872 | 872 | experiment_data = json.loads(experiment.parms_to_dict()) |
|
873 | 873 | configurations = Configuration.objects.filter(experiment=experiment, type=0) |
|
874 | 874 | |
|
875 | 875 | kwargs = {} |
|
876 | 876 | |
|
877 | 877 | kwargs['experiment_keys'] = ['template', 'radar_system', 'name', 'start_time', 'end_time'] |
|
878 | 878 | kwargs['experiment'] = experiment |
|
879 | 879 | |
|
880 | 880 | kwargs['configuration_keys'] = ['name', 'device__ip_address', 'device__port_address', 'device__status'] |
|
881 | 881 | kwargs['configurations'] = configurations |
|
882 | 882 | kwargs['experiment_data'] = experiment_data |
|
883 | 883 | |
|
884 | 884 | kwargs['title'] = 'Verify Experiment' |
|
885 | 885 | kwargs['suptitle'] = 'Parameters' |
|
886 | 886 | |
|
887 | 887 | kwargs['button'] = 'Update' |
|
888 | 888 | |
|
889 | 889 | jars_conf = False |
|
890 | 890 | rc_conf = False |
|
891 | 891 | dds_conf = False |
|
892 | 892 | |
|
893 | 893 | for configuration in configurations: |
|
894 | 894 | #-------------------- JARS -----------------------: |
|
895 | 895 | if configuration.device.device_type.name == 'jars': |
|
896 | 896 | jars_conf = True |
|
897 | 897 | kwargs['jars_conf'] = jars_conf |
|
898 | 898 | filter_parms = configuration.filter_parms |
|
899 | 899 | filter_parms = ast.literal_eval(filter_parms) |
|
900 | 900 | kwargs['filter_parms'] = filter_parms |
|
901 | 901 | #--Sampling Frequency |
|
902 | 902 | clock = filter_parms['clock'] |
|
903 | 903 | filter_2 = filter_parms['filter_2'] |
|
904 | 904 | filter_5 = filter_parms['filter_5'] |
|
905 | 905 | filter_fir = filter_parms['filter_fir'] |
|
906 | 906 | samp_freq_jars = clock/filter_2/filter_5/filter_fir |
|
907 | 907 | |
|
908 | 908 | kwargs['samp_freq_jars'] = samp_freq_jars |
|
909 | 909 | kwargs['jars'] = configuration |
|
910 | 910 | |
|
911 | 911 | #--------------------- RC ----------------------: |
|
912 | 912 | if configuration.device.device_type.name == 'rc': |
|
913 | 913 | rc_conf = True |
|
914 | 914 | rc_parms = configuration.parms_to_dict() |
|
915 | 915 | if rc_parms['mix'] == 'True': |
|
916 | 916 | pass |
|
917 | 917 | else: |
|
918 | 918 | rc_lines = rc_parms['lines'] |
|
919 | 919 | dh = rc_lines[6]['params'][0]['resolution'] |
|
920 | 920 | #--Sampling Frequency |
|
921 | 921 | samp_freq_rc = 0.15/dh |
|
922 | 922 | kwargs['samp_freq_rc'] = samp_freq_rc |
|
923 | 923 | |
|
924 | 924 | kwargs['rc_conf'] = rc_conf |
|
925 | 925 | kwargs['rc'] = configuration |
|
926 | 926 | |
|
927 | 927 | #-------------------- DDS ----------------------: |
|
928 | 928 | if configuration.device.device_type.name == 'dds': |
|
929 | 929 | dds_conf = True |
|
930 | 930 | dds_parms = configuration.parms_to_dict() |
|
931 | 931 | |
|
932 | 932 | kwargs['dds_conf'] = dds_conf |
|
933 | 933 | kwargs['dds'] = configuration |
|
934 | 934 | |
|
935 | 935 | |
|
936 | 936 | #------------Validation------------: |
|
937 | 937 | #Clock |
|
938 | 938 | if dds_conf and rc_conf and jars_conf: |
|
939 | 939 | if filter_parms['clock'] != rc_parms['clock_in'] and rc_parms['clock_in'] != dds_parms['clock']: |
|
940 | 940 | messages.warning(request, "Devices don't have the same clock.") |
|
941 | 941 | elif rc_conf and jars_conf: |
|
942 | 942 | if filter_parms['clock'] != rc_parms['clock_in']: |
|
943 | 943 | messages.warning(request, "Devices don't have the same clock.") |
|
944 | 944 | elif rc_conf and dds_conf: |
|
945 | 945 | if rc_parms['clock_in'] != dds_parms['clock']: |
|
946 | 946 | messages.warning(request, "Devices don't have the same clock.") |
|
947 | 947 | if float(samp_freq_rc) != float(dds_parms['frequencyA']): |
|
948 | 948 | messages.warning(request, "Devices don't have the same Frequency A.") |
|
949 | 949 | |
|
950 | 950 | |
|
951 | 951 | |
|
952 | 952 | ###### SIDEBAR ###### |
|
953 | 953 | kwargs.update(sidebar(experiment=experiment)) |
|
954 | 954 | |
|
955 | 955 | |
|
956 | 956 | |
|
957 | 957 | |
|
958 | 958 | |
|
959 | 959 | return render(request, 'experiment_verify.html', kwargs) |
|
960 | 960 | |
|
961 | 961 | |
|
962 | 962 | def parse_mix_result(s): |
|
963 | 963 | |
|
964 | 964 | values = s.split('-') |
|
965 | 965 | html = 'EXP MOD OPE DELAY MASK\r\n' |
|
966 | 966 | |
|
967 | 967 | if not values or values[0] in ('', ' '): |
|
968 | 968 | return mark_safe(html) |
|
969 | 969 | |
|
970 | 970 | for i, value in enumerate(values): |
|
971 | 971 | if not value: |
|
972 | 972 | continue |
|
973 | 973 | pk, mode, operation, delay, mask = value.split('|') |
|
974 | 974 | conf = RCConfiguration.objects.get(pk=pk) |
|
975 | 975 | if i==0: |
|
976 | 976 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
977 | 977 | conf.name, |
|
978 | 978 | mode, |
|
979 | 979 | ' ', |
|
980 | 980 | delay, |
|
981 | 981 | mask) |
|
982 | 982 | else: |
|
983 | 983 | html += '{:20.18}{:3}{:4}{:9}km{:>6}\r\n'.format( |
|
984 | 984 | conf.name, |
|
985 | 985 | mode, |
|
986 | 986 | operation, |
|
987 | 987 | delay, |
|
988 | 988 | mask) |
|
989 | 989 | |
|
990 | 990 | return mark_safe(html) |
|
991 | 991 | |
|
992 | 992 | def parse_mask(l): |
|
993 | 993 | |
|
994 | 994 | values = [] |
|
995 | 995 | |
|
996 | 996 | for x in range(8): |
|
997 | 997 | if '{}'.format(x) in l: |
|
998 | 998 | values.append(1) |
|
999 | 999 | else: |
|
1000 | 1000 | values.append(0) |
|
1001 | 1001 | |
|
1002 | 1002 | values.reverse() |
|
1003 | 1003 | |
|
1004 | 1004 | return int(''.join([str(x) for x in values]), 2) |
|
1005 | 1005 | |
|
1006 | 1006 | |
|
1007 | 1007 | def dev_confs(request): |
|
1008 | 1008 | |
|
1009 | 1009 | |
|
1010 | 1010 | page = request.GET.get('page') |
|
1011 | 1011 | order = ('type', 'device__device_type', 'experiment') |
|
1012 | 1012 | filters = request.GET.copy() |
|
1013 | 1013 | |
|
1014 | 1014 | kwargs = get_paginator(Configuration, page, order, filters) |
|
1015 | 1015 | |
|
1016 | 1016 | form = FilterForm(initial=request.GET, extra_fields=['tags','template']) |
|
1017 | 1017 | kwargs['keys'] = ['name', 'experiment', 'type', 'programmed_date'] |
|
1018 | 1018 | kwargs['title'] = 'Configuration' |
|
1019 | 1019 | kwargs['suptitle'] = 'List' |
|
1020 | 1020 | kwargs['form'] = form |
|
1021 | 1021 | filters.pop('page', None) |
|
1022 | 1022 | kwargs['q'] = urlencode(filters) |
|
1023 | 1023 | |
|
1024 | 1024 | return render(request, 'base_list.html', kwargs) |
|
1025 | 1025 | |
|
1026 | 1026 | |
|
1027 | 1027 | def dev_conf(request, id_conf): |
|
1028 | 1028 | |
|
1029 | 1029 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1030 | 1030 | |
|
1031 | 1031 | return redirect(conf.get_absolute_url()) |
|
1032 | 1032 | |
|
1033 | 1033 | |
|
1034 | 1034 | def dev_conf_new(request, id_exp=0, id_dev=0): |
|
1035 | 1035 | |
|
1036 | 1036 | initial = {} |
|
1037 | 1037 | kwargs = {} |
|
1038 | 1038 | |
|
1039 | 1039 | if id_exp!=0: |
|
1040 | 1040 | initial['experiment'] = id_exp |
|
1041 | 1041 | |
|
1042 | 1042 | if id_dev!=0: |
|
1043 | 1043 | initial['device'] = id_dev |
|
1044 | 1044 | |
|
1045 | 1045 | if request.method == 'GET': |
|
1046 | 1046 | |
|
1047 | 1047 | if id_dev: |
|
1048 | 1048 | kwargs['button'] = 'Create' |
|
1049 | 1049 | device = Device.objects.get(pk=id_dev) |
|
1050 | 1050 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1051 | 1051 | initial['name'] = request.GET['name'] |
|
1052 | 1052 | form = DevConfForm(initial=initial) |
|
1053 | 1053 | else: |
|
1054 | 1054 | if 'template' in request.GET: |
|
1055 | 1055 | if request.GET['template']=='0': |
|
1056 | 1056 | choices = [(conf.pk, '{}'.format(conf)) for conf in Configuration.objects.filter(template=True)] |
|
1057 | 1057 | form = NewForm(initial={'create_from':2}, |
|
1058 | 1058 | template_choices=choices) |
|
1059 | 1059 | else: |
|
1060 | 1060 | kwargs['button'] = 'Create' |
|
1061 | 1061 | conf = Configuration.objects.get(pk=request.GET['template']) |
|
1062 | 1062 | id_dev = conf.device.pk |
|
1063 | 1063 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1064 | 1064 | form = DevConfForm(instance=conf, |
|
1065 | 1065 | initial={'name': '{} [{:%Y/%m/%d}]'.format(conf.name, datetime.now()), |
|
1066 | 1066 | 'template': False, |
|
1067 | 1067 | 'experiment':id_exp}) |
|
1068 | 1068 | elif 'blank' in request.GET: |
|
1069 | 1069 | kwargs['button'] = 'Create' |
|
1070 | 1070 | form = ConfigurationForm(initial=initial) |
|
1071 | 1071 | else: |
|
1072 | 1072 | form = NewForm() |
|
1073 | 1073 | |
|
1074 | 1074 | if request.method == 'POST': |
|
1075 | 1075 | |
|
1076 | 1076 | device = Device.objects.get(pk=request.POST['device']) |
|
1077 | 1077 | DevConfForm = CONF_FORMS[device.device_type.name] |
|
1078 | 1078 | |
|
1079 | 1079 | form = DevConfForm(request.POST) |
|
1080 | 1080 | kwargs['button'] = 'Create' |
|
1081 | 1081 | if form.is_valid(): |
|
1082 | 1082 | conf = form.save() |
|
1083 | 1083 | |
|
1084 | 1084 | if 'template' in request.GET and conf.device.device_type.name=='rc': |
|
1085 | 1085 | lines = RCLine.objects.filter(rc_configuration=request.GET['template']) |
|
1086 | 1086 | for line in lines: |
|
1087 | 1087 | line.clone(rc_configuration=conf) |
|
1088 | 1088 | |
|
1089 | 1089 | if conf.device.device_type.name=='jars': |
|
1090 | 1090 | conf.add_parms_to_filter() |
|
1091 | 1091 | |
|
1092 | 1092 | return redirect('url_dev_conf', id_conf=conf.pk) |
|
1093 | 1093 | |
|
1094 | 1094 | kwargs['id_exp'] = id_exp |
|
1095 | 1095 | kwargs['form'] = form |
|
1096 | 1096 | kwargs['title'] = 'Configuration' |
|
1097 | 1097 | kwargs['suptitle'] = 'New' |
|
1098 | 1098 | |
|
1099 | 1099 | |
|
1100 | 1100 | if id_dev != 0: |
|
1101 | 1101 | device = Device.objects.get(pk=id_dev) |
|
1102 | 1102 | kwargs['device'] = device.device_type.name |
|
1103 | 1103 | |
|
1104 | 1104 | return render(request, 'dev_conf_edit.html', kwargs) |
|
1105 | 1105 | |
|
1106 | 1106 | |
|
1107 | 1107 | def dev_conf_edit(request, id_conf): |
|
1108 | 1108 | |
|
1109 | 1109 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1110 | 1110 | |
|
1111 | 1111 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1112 | 1112 | |
|
1113 | 1113 | if request.method=='GET': |
|
1114 | 1114 | form = DevConfForm(instance=conf) |
|
1115 | 1115 | |
|
1116 | 1116 | if request.method=='POST': |
|
1117 | 1117 | form = DevConfForm(request.POST, instance=conf) |
|
1118 | 1118 | |
|
1119 | 1119 | if form.is_valid(): |
|
1120 | 1120 | form.save() |
|
1121 | 1121 | return redirect('url_dev_conf', id_conf=id_conf) |
|
1122 | 1122 | |
|
1123 | 1123 | kwargs = {} |
|
1124 | 1124 | kwargs['form'] = form |
|
1125 | 1125 | kwargs['title'] = 'Device Configuration' |
|
1126 | 1126 | kwargs['suptitle'] = 'Edit' |
|
1127 | 1127 | kwargs['button'] = 'Update' |
|
1128 | 1128 | |
|
1129 | 1129 | ###### SIDEBAR ###### |
|
1130 | 1130 | kwargs.update(sidebar(conf=conf)) |
|
1131 | 1131 | |
|
1132 | 1132 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1133 | 1133 | |
|
1134 | 1134 | |
|
1135 | 1135 | def dev_conf_start(request, id_conf): |
|
1136 | 1136 | |
|
1137 | 1137 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1138 | 1138 | |
|
1139 | 1139 | if conf.start_device(): |
|
1140 | 1140 | messages.success(request, conf.message) |
|
1141 | 1141 | else: |
|
1142 | 1142 | messages.error(request, conf.message) |
|
1143 | 1143 | |
|
1144 | 1144 | conf.status_device() |
|
1145 | 1145 | |
|
1146 | 1146 | return redirect(conf.get_absolute_url()) |
|
1147 | 1147 | |
|
1148 | 1148 | |
|
1149 | 1149 | def dev_conf_stop(request, id_conf): |
|
1150 | 1150 | |
|
1151 | 1151 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1152 | 1152 | |
|
1153 | 1153 | if conf.stop_device(): |
|
1154 | 1154 | messages.success(request, conf.message) |
|
1155 | 1155 | else: |
|
1156 | 1156 | messages.error(request, conf.message) |
|
1157 | 1157 | |
|
1158 | 1158 | conf.status_device() |
|
1159 | 1159 | |
|
1160 | 1160 | return redirect(conf.get_absolute_url()) |
|
1161 | 1161 | |
|
1162 | 1162 | |
|
1163 | 1163 | def dev_conf_status(request, id_conf): |
|
1164 | 1164 | |
|
1165 | 1165 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1166 | 1166 | |
|
1167 | 1167 | if conf.status_device(): |
|
1168 | 1168 | messages.success(request, conf.message) |
|
1169 | 1169 | else: |
|
1170 | 1170 | messages.error(request, conf.message) |
|
1171 | 1171 | |
|
1172 | 1172 | return redirect(conf.get_absolute_url()) |
|
1173 | 1173 | |
|
1174 | 1174 | |
|
1175 | 1175 | def dev_conf_write(request, id_conf): |
|
1176 | 1176 | |
|
1177 | 1177 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1178 | 1178 | |
|
1179 | 1179 | answer = conf.write_device() |
|
1180 | 1180 | conf.status_device() |
|
1181 | 1181 | |
|
1182 | 1182 | if answer: |
|
1183 | 1183 | messages.success(request, conf.message) |
|
1184 | 1184 | |
|
1185 | 1185 | #Creating a historical configuration |
|
1186 | 1186 | conf.clone(type=1, template=False) |
|
1187 | 1187 | |
|
1188 | 1188 | #Original configuration |
|
1189 | 1189 | conf = DevConfModel.objects.get(pk=id_conf) |
|
1190 | 1190 | else: |
|
1191 | 1191 | messages.error(request, conf.message) |
|
1192 | 1192 | |
|
1193 | 1193 | return redirect(conf.get_absolute_url()) |
|
1194 | 1194 | |
|
1195 | 1195 | |
|
1196 | 1196 | def dev_conf_read(request, id_conf): |
|
1197 | 1197 | |
|
1198 | 1198 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1199 | 1199 | |
|
1200 | 1200 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1201 | 1201 | |
|
1202 | 1202 | if request.method=='GET': |
|
1203 | 1203 | |
|
1204 | 1204 | parms = conf.read_device() |
|
1205 | 1205 | conf.status_device() |
|
1206 | 1206 | |
|
1207 | 1207 | if not parms: |
|
1208 | 1208 | messages.error(request, conf.message) |
|
1209 | 1209 | return redirect(conf.get_absolute_url()) |
|
1210 | 1210 | |
|
1211 | 1211 | form = DevConfForm(initial=parms, instance=conf) |
|
1212 | 1212 | |
|
1213 | 1213 | if request.method=='POST': |
|
1214 | 1214 | form = DevConfForm(request.POST, instance=conf) |
|
1215 | 1215 | |
|
1216 | 1216 | if form.is_valid(): |
|
1217 | 1217 | form.save() |
|
1218 | 1218 | return redirect(conf.get_absolute_url()) |
|
1219 | 1219 | |
|
1220 | 1220 | messages.error(request, "Parameters could not be saved") |
|
1221 | 1221 | |
|
1222 | 1222 | kwargs = {} |
|
1223 | 1223 | kwargs['id_dev'] = conf.id |
|
1224 | 1224 | kwargs['form'] = form |
|
1225 | 1225 | kwargs['title'] = 'Device Configuration' |
|
1226 | 1226 | kwargs['suptitle'] = 'Parameters read from device' |
|
1227 | 1227 | kwargs['button'] = 'Save' |
|
1228 | 1228 | |
|
1229 | 1229 | ###### SIDEBAR ###### |
|
1230 | 1230 | kwargs.update(sidebar(conf=conf)) |
|
1231 | 1231 | |
|
1232 | 1232 | return render(request, '%s_conf_edit.html' %conf.device.device_type.name, kwargs) |
|
1233 | 1233 | |
|
1234 | 1234 | |
|
1235 | 1235 | def dev_conf_import(request, id_conf): |
|
1236 | 1236 | |
|
1237 | 1237 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1238 | 1238 | DevConfForm = CONF_FORMS[conf.device.device_type.name] |
|
1239 | 1239 | |
|
1240 | 1240 | if request.method == 'GET': |
|
1241 | 1241 | file_form = UploadFileForm() |
|
1242 | 1242 | |
|
1243 | 1243 | if request.method == 'POST': |
|
1244 | 1244 | file_form = UploadFileForm(request.POST, request.FILES) |
|
1245 | 1245 | |
|
1246 | 1246 | if file_form.is_valid(): |
|
1247 | 1247 | |
|
1248 | 1248 | parms = conf.import_from_file(request.FILES['file']) |
|
1249 | 1249 | |
|
1250 | 1250 | if parms: |
|
1251 | 1251 | messages.success(request, "Parameters imported from: '%s'." %request.FILES['file'].name) |
|
1252 | 1252 | form = DevConfForm(initial=parms, instance=conf) |
|
1253 | 1253 | |
|
1254 | 1254 | kwargs = {} |
|
1255 | 1255 | kwargs['id_dev'] = conf.id |
|
1256 | 1256 | kwargs['form'] = form |
|
1257 | 1257 | kwargs['title'] = 'Device Configuration' |
|
1258 | 1258 | kwargs['suptitle'] = 'Parameters imported' |
|
1259 | 1259 | kwargs['button'] = 'Save' |
|
1260 | 1260 | kwargs['action'] = conf.get_absolute_url_edit() |
|
1261 | 1261 | kwargs['previous'] = conf.get_absolute_url() |
|
1262 | 1262 | |
|
1263 | 1263 | ###### SIDEBAR ###### |
|
1264 | 1264 | kwargs.update(sidebar(conf=conf)) |
|
1265 | 1265 | |
|
1266 | 1266 | return render(request, '%s_conf_edit.html' % conf.device.device_type.name, kwargs) |
|
1267 | 1267 | |
|
1268 | 1268 | messages.error(request, "Could not import parameters from file") |
|
1269 | 1269 | |
|
1270 | 1270 | kwargs = {} |
|
1271 | 1271 | kwargs['id_dev'] = conf.id |
|
1272 | 1272 | kwargs['title'] = 'Device Configuration' |
|
1273 | 1273 | kwargs['form'] = file_form |
|
1274 | 1274 | kwargs['suptitle'] = 'Importing file' |
|
1275 | 1275 | kwargs['button'] = 'Import' |
|
1276 | 1276 | |
|
1277 | 1277 | kwargs.update(sidebar(conf=conf)) |
|
1278 | 1278 | |
|
1279 | 1279 | return render(request, 'dev_conf_import.html', kwargs) |
|
1280 | 1280 | |
|
1281 | 1281 | |
|
1282 | 1282 | def dev_conf_export(request, id_conf): |
|
1283 | 1283 | |
|
1284 | 1284 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1285 | 1285 | |
|
1286 | 1286 | if request.method == 'GET': |
|
1287 | 1287 | file_form = DownloadFileForm(conf.device.device_type.name) |
|
1288 | 1288 | |
|
1289 | 1289 | if request.method == 'POST': |
|
1290 | 1290 | file_form = DownloadFileForm(conf.device.device_type.name, request.POST) |
|
1291 | 1291 | |
|
1292 | 1292 | if file_form.is_valid(): |
|
1293 | 1293 | fields = conf.export_to_file(format = file_form.cleaned_data['format']) |
|
1294 | 1294 | |
|
1295 | 1295 | response = HttpResponse(content_type=fields['content_type']) |
|
1296 | 1296 | response['Content-Disposition'] = 'attachment; filename="%s"' %fields['filename'] |
|
1297 | 1297 | response.write(fields['content']) |
|
1298 | 1298 | |
|
1299 | 1299 | return response |
|
1300 | 1300 | |
|
1301 | 1301 | messages.error(request, "Could not export parameters") |
|
1302 | 1302 | |
|
1303 | 1303 | kwargs = {} |
|
1304 | 1304 | kwargs['id_dev'] = conf.id |
|
1305 | 1305 | kwargs['title'] = 'Device Configuration' |
|
1306 | 1306 | kwargs['form'] = file_form |
|
1307 | 1307 | kwargs['suptitle'] = 'Exporting file' |
|
1308 | 1308 | kwargs['button'] = 'Export' |
|
1309 | 1309 | |
|
1310 | 1310 | return render(request, 'dev_conf_export.html', kwargs) |
|
1311 | 1311 | |
|
1312 | 1312 | |
|
1313 | 1313 | def dev_conf_delete(request, id_conf): |
|
1314 | 1314 | |
|
1315 | 1315 | conf = get_object_or_404(Configuration, pk=id_conf) |
|
1316 | 1316 | |
|
1317 | 1317 | if request.method=='POST': |
|
1318 | 1318 | if request.user.is_staff: |
|
1319 | 1319 | conf.delete() |
|
1320 | 1320 | return redirect('url_dev_confs') |
|
1321 | 1321 | |
|
1322 | 1322 | messages.error(request, 'Not enough permission to delete this object') |
|
1323 | 1323 | return redirect(conf.get_absolute_url()) |
|
1324 | 1324 | |
|
1325 | 1325 | kwargs = { |
|
1326 | 1326 | 'title': 'Delete', |
|
1327 | 1327 | 'suptitle': 'Experiment', |
|
1328 | 1328 | 'object': conf, |
|
1329 | 1329 | 'previous': conf.get_absolute_url(), |
|
1330 | 1330 | 'delete': True |
|
1331 | 1331 | } |
|
1332 | 1332 | |
|
1333 | 1333 | return render(request, 'confirm.html', kwargs) |
|
1334 | 1334 | |
|
1335 | 1335 | |
|
1336 | 1336 | def sidebar(**kwargs): |
|
1337 | 1337 | |
|
1338 | 1338 | side_data = {} |
|
1339 | 1339 | |
|
1340 | 1340 | conf = kwargs.get('conf', None) |
|
1341 | 1341 | experiment = kwargs.get('experiment', None) |
|
1342 | 1342 | |
|
1343 | 1343 | if not experiment: |
|
1344 | 1344 | experiment = conf.experiment |
|
1345 | 1345 | |
|
1346 | 1346 | if experiment: |
|
1347 | 1347 | side_data['experiment'] = experiment |
|
1348 | 1348 | campaign = experiment.campaign_set.all() |
|
1349 | 1349 | if campaign: |
|
1350 | 1350 | side_data['campaign'] = campaign[0] |
|
1351 | 1351 | experiments = campaign[0].experiments.all() |
|
1352 | 1352 | else: |
|
1353 | 1353 | experiments = [experiment] |
|
1354 | 1354 | configurations = experiment.configuration_set.filter(type=0) |
|
1355 | 1355 | side_data['side_experiments'] = experiments |
|
1356 | 1356 | side_data['side_configurations'] = configurations |
|
1357 | 1357 | |
|
1358 | 1358 | return side_data |
|
1359 | 1359 | |
|
1360 | 1360 | def get_paginator(model, page, order, filters={}, n=10): |
|
1361 | 1361 | |
|
1362 | 1362 | kwargs = {} |
|
1363 | 1363 | query = Q() |
|
1364 | 1364 | if isinstance(filters, QueryDict): |
|
1365 | 1365 | filters = filters.dict() |
|
1366 | 1366 | [filters.pop(key) for key in filters.keys() if filters[key] in ('', ' ')] |
|
1367 | 1367 | filters.pop('page', None) |
|
1368 | 1368 | |
|
1369 | 1369 | if 'start_date' in filters: |
|
1370 | 1370 | filters['start_date__gte'] = filters.pop('start_date') |
|
1371 | 1371 | if 'end_date' in filters: |
|
1372 | 1372 | filters['start_date__lte'] = filters.pop('end_date') |
|
1373 | 1373 | if 'tags' in filters: |
|
1374 | 1374 | tags = filters.pop('tags') |
|
1375 | 1375 | if 'tags' in model._meta.get_all_field_names(): |
|
1376 | 1376 | query = query | Q(tags__icontains=tags) |
|
1377 | 1377 | if 'name' in model._meta.get_all_field_names(): |
|
1378 | 1378 | query = query | Q(name__icontains=tags) |
|
1379 | 1379 | if 'location' in model._meta.get_all_field_names(): |
|
1380 | 1380 | query = query | Q(location__name__icontains=tags) |
|
1381 | 1381 | if 'device' in model._meta.get_all_field_names(): |
|
1382 | 1382 | query = query | Q(device__name__icontains=tags) |
|
1383 | 1383 | |
|
1384 | 1384 | object_list = model.objects.filter(query, **filters).order_by(*order) |
|
1385 | 1385 | paginator = Paginator(object_list, n) |
|
1386 | 1386 | |
|
1387 | 1387 | try: |
|
1388 | 1388 | objects = paginator.page(page) |
|
1389 | 1389 | except PageNotAnInteger: |
|
1390 | 1390 | objects = paginator.page(1) |
|
1391 | 1391 | except EmptyPage: |
|
1392 | 1392 | objects = paginator.page(paginator.num_pages) |
|
1393 | 1393 | |
|
1394 | 1394 | kwargs['objects'] = objects |
|
1395 | 1395 | kwargs['offset'] = (int(page)-1)*n if page else 0 |
|
1396 | 1396 | |
|
1397 | 1397 | return kwargs |
|
1398 | 1398 | |
|
1399 | 1399 | def operation(request, id_camp=None): |
|
1400 | 1400 | |
|
1401 | 1401 | kwargs = {} |
|
1402 | 1402 | campaigns = Campaign.objects.filter(start_date__lte=datetime.now(), |
|
1403 | 1403 | end_date__gte=datetime.now()).order_by('-start_date') |
|
1404 | 1404 | |
|
1405 | 1405 | |
|
1406 | 1406 | if id_camp: |
|
1407 | 1407 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1408 | 1408 | form = OperationForm(initial={'campaign': campaign.id}, campaigns=campaigns) |
|
1409 | 1409 | kwargs['campaign'] = campaign |
|
1410 | 1410 | else: |
|
1411 | 1411 | form = OperationForm(campaigns=campaigns) |
|
1412 | 1412 | kwargs['form'] = form |
|
1413 | 1413 | return render(request, 'operation.html', kwargs) |
|
1414 | 1414 | |
|
1415 | 1415 | |
|
1416 | 1416 | |
|
1417 | 1417 | #---Experiment |
|
1418 | 1418 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1419 | 1419 | kwargs['experiment_keys'] = keys[1:] |
|
1420 | 1420 | kwargs['experiments'] = experiments |
|
1421 | 1421 | #---Radar |
|
1422 | 1422 | kwargs['locations'] = campaign.get_experiments_by_location() |
|
1423 | print kwargs['locations'] | |
|
1424 | 1423 | #---Else |
|
1425 | 1424 | kwargs['title'] = 'Campaign' |
|
1426 | 1425 | kwargs['suptitle'] = campaign.name |
|
1427 | 1426 |
kwargs['form'] = form |
|
1428 | 1427 | |
|
1429 | 1428 | return render(request, 'operation.html', kwargs) |
|
1430 | 1429 | |
|
1431 | 1430 | |
|
1432 | 1431 | def operation_search(request, id_camp=None): |
|
1433 | 1432 | |
|
1434 | 1433 | |
|
1435 | 1434 | if not id_camp: |
|
1436 | 1435 | campaigns = Campaign.objects.all().order_by('-start_date') |
|
1437 | 1436 | |
|
1438 | 1437 | if not campaigns: |
|
1439 | 1438 | return render(request, 'operation.html', {}) |
|
1440 | 1439 | |
|
1441 | 1440 | id_camp = campaigns[0].id |
|
1442 | 1441 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1443 | 1442 | |
|
1444 | 1443 | if request.method=='GET': |
|
1445 | 1444 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
1446 | 1445 | |
|
1447 | 1446 | if request.method=='POST': |
|
1448 | 1447 | form = OperationSearchForm(request.POST, initial={'campaign':campaign.id}) |
|
1449 | 1448 | |
|
1450 | 1449 | if form.is_valid(): |
|
1451 | 1450 | return redirect('url_operation', id_camp=campaign.id) |
|
1452 | 1451 | |
|
1453 | 1452 | #locations = Location.objects.filter(experiment__campaign__pk = campaign.id).distinct() |
|
1454 | 1453 | experiments = Experiment.objects.filter(campaign__pk=campaign.id) |
|
1455 | 1454 | #for exs in experiments: |
|
1456 | 1455 | # exs.get_status() |
|
1457 | 1456 | locations= Location.objects.filter(experiment=experiments).distinct() |
|
1458 | 1457 | form = OperationSearchForm(initial={'campaign': campaign.id}) |
|
1459 | 1458 | |
|
1460 | 1459 | kwargs = {} |
|
1461 | 1460 | #---Campaign |
|
1462 | 1461 | kwargs['campaign'] = campaign |
|
1463 | 1462 | kwargs['campaign_keys'] = ['name', 'start_date', 'end_date', 'tags', 'description'] |
|
1464 | 1463 | #---Experiment |
|
1465 | 1464 | keys = ['id', 'name', 'start_time', 'end_time', 'status'] |
|
1466 | 1465 | kwargs['experiment_keys'] = keys[1:] |
|
1467 | 1466 | kwargs['experiments'] = experiments |
|
1468 | 1467 | #---Radar |
|
1469 | 1468 | kwargs['locations'] = locations |
|
1470 | 1469 | #---Else |
|
1471 | 1470 | kwargs['title'] = 'Campaign' |
|
1472 | 1471 | kwargs['suptitle'] = campaign.name |
|
1473 | 1472 | kwargs['form'] = form |
|
1474 | 1473 | kwargs['button'] = 'Select' |
|
1475 | 1474 | kwargs['details'] = True |
|
1476 | 1475 | kwargs['search_button'] = False |
|
1477 | 1476 | |
|
1478 | 1477 | return render(request, 'operation.html', kwargs) |
|
1479 | 1478 | |
|
1480 | 1479 | |
|
1481 | 1480 | def radar_play(request, id_camp, id_radar): |
|
1482 | 1481 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1483 | 1482 | radar = get_object_or_404(Location, pk = id_radar) |
|
1484 | 1483 | today = datetime.today() |
|
1485 | 1484 | now = today.time() |
|
1486 | 1485 | |
|
1487 | 1486 | #--Clear Old Experiments From RunningExperiment Object |
|
1488 | 1487 | running_experiment = RunningExperiment.objects.filter(radar=radar) |
|
1489 | 1488 | if running_experiment: |
|
1490 | 1489 | running_experiment = running_experiment[0] |
|
1491 | 1490 | running_experiment.running_experiment.clear() |
|
1492 | 1491 | running_experiment.save() |
|
1493 | 1492 | |
|
1494 | 1493 | #--If campaign datetime is ok: |
|
1495 | 1494 | if today >= campaign.start_date and today <= campaign.end_date: |
|
1496 | 1495 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1497 | 1496 | for exp in experiments: |
|
1498 | 1497 | #--If experiment time is ok: |
|
1499 | 1498 | if now >= exp.start_time and now <= exp.end_time: |
|
1500 | 1499 | configurations = Configuration.objects.filter(experiment = exp) |
|
1501 | 1500 | for conf in configurations: |
|
1502 | 1501 | if 'cgs' in conf.device.device_type.name: |
|
1503 | 1502 | conf.status_device() |
|
1504 | 1503 | else: |
|
1505 | 1504 | answer = conf.start_device() |
|
1506 | 1505 | conf.status_device() |
|
1507 | 1506 | #--Running Experiment |
|
1508 | 1507 | old_running_experiment = RunningExperiment.objects.filter(radar=radar) |
|
1509 | 1508 | #--If RunningExperiment element exists |
|
1510 | 1509 | if old_running_experiment: |
|
1511 | 1510 | old_running_experiment = old_running_experiment[0] |
|
1512 | 1511 | old_running_experiment.running_experiment.add(exp) |
|
1513 | 1512 | old_running_experiment.status = 3 |
|
1514 | 1513 | old_running_experiment.save() |
|
1515 | 1514 | #--Create a new Running_Experiment Object |
|
1516 | 1515 | else: |
|
1517 | 1516 | new_running_experiment = RunningExperiment( |
|
1518 | 1517 | radar = radar, |
|
1519 | 1518 | status = 3, |
|
1520 | 1519 | ) |
|
1521 | 1520 | new_running_experiment.save() |
|
1522 | 1521 | new_running_experiment.running_experiment.add(exp) |
|
1523 | 1522 | new_running_experiment.save() |
|
1524 | 1523 | |
|
1525 | 1524 | if answer: |
|
1526 | 1525 | messages.success(request, conf.message) |
|
1527 | 1526 | exp.status=2 |
|
1528 | 1527 | exp.save() |
|
1529 | 1528 | else: |
|
1530 | 1529 | messages.error(request, conf.message) |
|
1531 | 1530 | else: |
|
1532 | 1531 | if exp.status == 1 or exp.status == 3: |
|
1533 | 1532 | exp.status=3 |
|
1534 | 1533 | exp.save() |
|
1535 | 1534 | |
|
1536 | 1535 | |
|
1537 | 1536 | route = request.META['HTTP_REFERER'] |
|
1538 | 1537 | route = str(route) |
|
1539 | 1538 | if 'search' in route: |
|
1540 | 1539 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1541 | 1540 | else: |
|
1542 | 1541 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1543 | 1542 | |
|
1544 | 1543 | |
|
1545 | 1544 | def radar_stop(request, id_camp, id_radar): |
|
1546 | 1545 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1547 | 1546 | radar = get_object_or_404(Location, pk = id_radar) |
|
1548 | 1547 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1549 | 1548 | |
|
1550 | 1549 | for exp in experiments: |
|
1551 | 1550 | configurations = Configuration.objects.filter(experiment = exp) |
|
1552 | 1551 | for conf in configurations: |
|
1553 | 1552 | if 'cgs' in conf.device.device_type.name: |
|
1554 | 1553 | conf.status_device() |
|
1555 | 1554 | else: |
|
1556 | 1555 | answer = conf.stop_device() |
|
1557 | 1556 | conf.status_device() |
|
1558 | 1557 | |
|
1559 | 1558 | if answer: |
|
1560 | 1559 | messages.success(request, conf.message) |
|
1561 | 1560 | exp.status=1 |
|
1562 | 1561 | exp.save() |
|
1563 | 1562 | else: |
|
1564 | 1563 | messages.error(request, conf.message) |
|
1565 | 1564 | |
|
1566 | 1565 | |
|
1567 | 1566 | route = request.META['HTTP_REFERER'] |
|
1568 | 1567 | route = str(route) |
|
1569 | 1568 | if 'search' in route: |
|
1570 | 1569 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1571 | 1570 | else: |
|
1572 | 1571 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1573 | 1572 | |
|
1574 | 1573 | |
|
1575 | 1574 | def radar_refresh(request, id_camp, id_radar): |
|
1576 | 1575 | |
|
1577 | 1576 | campaign = get_object_or_404(Campaign, pk = id_camp) |
|
1578 | 1577 | radar = get_object_or_404(Location, pk = id_radar) |
|
1579 | 1578 | experiments = Experiment.objects.filter(campaign=campaign).filter(location=radar) |
|
1580 | 1579 | for exs in experiments: |
|
1581 | 1580 | exs.get_status() |
|
1582 | 1581 | |
|
1583 | 1582 | route = request.META['HTTP_REFERER'] |
|
1584 | 1583 | route = str(route) |
|
1585 | 1584 | if 'search' in route: |
|
1586 | 1585 | return HttpResponseRedirect(reverse('url_operation_search', args=[id_camp])) |
|
1587 | 1586 | else: |
|
1588 | 1587 | return HttpResponseRedirect(reverse('url_operation', args=[id_camp])) |
|
1589 |
General Comments 0
You need to be logged in to leave comments.
Login now