@@ -1,1102 +1,1140 | |||
|
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 | from celery.execute import send_task | |
|
6 | from datetime import datetime | |
|
6 | 7 | import ast |
|
7 | 8 | import socket |
|
8 | 9 | import json |
|
9 | 10 | import requests |
|
10 | 11 | import struct |
|
11 | 12 | import sys, time |
|
12 | 13 | |
|
13 | 14 | import multiprocessing |
|
14 | 15 | |
|
15 | 16 | |
|
16 | 17 | antenna_default = json.dumps({ |
|
17 | 18 | "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
18 | 19 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
19 | 20 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
20 | 21 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
21 | 22 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
22 | 23 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
23 | 24 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
24 | 25 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0] |
|
25 | 26 | ] |
|
26 | 27 | , |
|
27 | 28 | "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
28 | 29 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
29 | 30 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
30 | 31 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
31 | 32 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
32 | 33 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
33 | 34 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
34 | 35 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]], |
|
35 | 36 | }) |
|
36 | 37 | |
|
37 | 38 | |
|
38 | 39 | tx_default = json.dumps({ |
|
39 | 40 | "up": [[1,1,1,1,0,0,0,0], |
|
40 | 41 | [1,1,1,1,0,0,0,0], |
|
41 | 42 | [1,1,1,1,0,0,0,0], |
|
42 | 43 | [1,1,1,1,0,0,0,0], |
|
43 | 44 | [0,0,0,0,1,1,1,1], |
|
44 | 45 | [0,0,0,0,1,1,1,1], |
|
45 | 46 | [0,0,0,0,1,1,1,1], |
|
46 | 47 | [0,0,0,0,1,1,1,1]], |
|
47 | 48 | |
|
48 | 49 | "down": [[1,1,1,1,0,0,0,0], |
|
49 | 50 | [1,1,1,1,0,0,0,0], |
|
50 | 51 | [1,1,1,1,0,0,0,0], |
|
51 | 52 | [1,1,1,1,0,0,0,0], |
|
52 | 53 | [0,0,0,0,1,1,1,1], |
|
53 | 54 | [0,0,0,0,1,1,1,1], |
|
54 | 55 | [0,0,0,0,1,1,1,1], |
|
55 | 56 | [0,0,0,0,1,1,1,1]], |
|
56 | 57 | }) |
|
57 | 58 | |
|
58 | 59 | rx_default = json.dumps({ |
|
59 | 60 | "up": [[1,1,1,1,0,0,0,0], |
|
60 | 61 | [1,1,1,1,0,0,0,0], |
|
61 | 62 | [1,1,1,1,0,0,0,0], |
|
62 | 63 | [1,1,1,1,0,0,0,0], |
|
63 | 64 | [0,0,0,0,1,1,1,1], |
|
64 | 65 | [0,0,0,0,1,1,1,1], |
|
65 | 66 | [0,0,0,0,1,1,1,1], |
|
66 | 67 | [0,0,0,0,1,1,1,1]], |
|
67 | 68 | |
|
68 | 69 | "down": [[1,1,1,1,0,0,0,0], |
|
69 | 70 | [1,1,1,1,0,0,0,0], |
|
70 | 71 | [1,1,1,1,0,0,0,0], |
|
71 | 72 | [1,1,1,1,0,0,0,0], |
|
72 | 73 | [0,0,0,0,1,1,1,1], |
|
73 | 74 | [0,0,0,0,1,1,1,1], |
|
74 | 75 | [0,0,0,0,1,1,1,1], |
|
75 | 76 | [0,0,0,0,1,1,1,1]], |
|
76 | 77 | }) |
|
77 | 78 | |
|
78 | 79 | conf_default = {} |
|
79 | 80 | status_default = {} |
|
80 | 81 | default_messages = {} |
|
81 | 82 | for i in range(1,65): |
|
82 | 83 | conf_default[str(i)] = "" |
|
83 | 84 | status_default[str(i)] = 0 |
|
84 | 85 | default_messages[str(i)] = "Module "+str(i) |
|
85 | 86 | |
|
86 | 87 | |
|
87 | 88 | ues_default = json.dumps({ |
|
88 | 89 | "up": [0.533333,0.00000,1.06667,0.00000], |
|
89 | 90 | "down": [0.533333,0.00000,1.06667,0.00000] |
|
90 | 91 | }) |
|
91 | 92 | |
|
92 | 93 | onlyrx_default = json.dumps({ |
|
93 | 94 | "up": False, |
|
94 | 95 | "down": False |
|
95 | 96 | }) |
|
96 | 97 | |
|
97 | 98 | def up_convertion(cadena): |
|
98 | 99 | valores = [] |
|
99 | 100 | for c in cadena: |
|
100 | 101 | if c == 1.0: valores=valores+['000'] |
|
101 | 102 | if c == 2.0: valores=valores+['001'] |
|
102 | 103 | if c == 3.0: valores=valores+['010'] |
|
103 | 104 | if c == 0.0: valores=valores+['011'] |
|
104 | 105 | if c == 0.5: valores=valores+['100'] |
|
105 | 106 | if c == 1.5: valores=valores+['101'] |
|
106 | 107 | if c == 2.5: valores=valores+['110'] |
|
107 | 108 | if c == 3.5: valores=valores+['111'] |
|
108 | 109 | |
|
109 | 110 | return valores |
|
110 | 111 | |
|
111 | 112 | def up_conv_bits(value): |
|
112 | 113 | |
|
113 | 114 | if value == 1.0: bits="000" |
|
114 | 115 | if value == 2.0: bits="001" |
|
115 | 116 | if value == 3.0: bits="010" |
|
116 | 117 | if value == 0.0: bits="011" |
|
117 | 118 | if value == 0.5: bits="100" |
|
118 | 119 | if value == 1.5: bits="101" |
|
119 | 120 | if value == 2.5: bits="110" |
|
120 | 121 | if value == 3.5: bits="111" |
|
121 | 122 | |
|
122 | 123 | return bits |
|
123 | 124 | |
|
124 | 125 | def down_convertion(cadena): |
|
125 | 126 | valores = [] |
|
126 | 127 | for c in cadena: |
|
127 | 128 | if c == 1.0: valores=valores+['000'] |
|
128 | 129 | if c == 2.0: valores=valores+['001'] |
|
129 | 130 | if c == 3.0: valores=valores+['010'] |
|
130 | 131 | if c == 0.0: valores=valores+['011'] |
|
131 | 132 | if c == 0.5: valores=valores+['100'] |
|
132 | 133 | if c == 1.5: valores=valores+['101'] |
|
133 | 134 | if c == 2.5: valores=valores+['110'] |
|
134 | 135 | if c == 3.5: valores=valores+['111'] |
|
135 | 136 | |
|
136 | 137 | return valores |
|
137 | 138 | |
|
138 | 139 | def down_conv_bits(value): |
|
139 | 140 | |
|
140 | 141 | if value == 1.0: bits="000" |
|
141 | 142 | if value == 2.0: bits="001" |
|
142 | 143 | if value == 3.0: bits="010" |
|
143 | 144 | if value == 0.0: bits="011" |
|
144 | 145 | if value == 0.5: bits="100" |
|
145 | 146 | if value == 1.5: bits="101" |
|
146 | 147 | if value == 2.5: bits="110" |
|
147 | 148 | if value == 3.5: bits="111" |
|
148 | 149 | |
|
149 | 150 | return bits |
|
150 | 151 | |
|
151 | 152 | def ip2position(module_number): |
|
152 | 153 | j=0 |
|
153 | 154 | i=0 |
|
154 | 155 | for x in range(0,module_number-1): |
|
155 | 156 | j=j+1 |
|
156 | 157 | if j==8: |
|
157 | 158 | i=i+1 |
|
158 | 159 | j=0 |
|
159 | 160 | |
|
160 | 161 | pos = [i,j] |
|
161 | 162 | return pos |
|
162 | 163 | |
|
163 | 164 | |
|
164 | 165 | def fromBinary2Char(binary_string): |
|
165 | 166 | number = int(binary_string, 2) |
|
166 | 167 | #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f' |
|
167 | 168 | number = number + 33 |
|
168 | 169 | char = chr(number) |
|
169 | 170 | return char |
|
170 | 171 | |
|
171 | 172 | def fromChar2Binary(char): |
|
172 | 173 | number = ord(char) - 33 |
|
173 | 174 | #Minus 33 to get the real value |
|
174 | 175 | bits = bin(number)[2:] |
|
175 | 176 | #To ensure we have a string with 6bits |
|
176 | 177 | if len(bits) < 6: |
|
177 | 178 | bits = bits.zfill(6) |
|
178 | 179 | return bits |
|
179 | 180 | |
|
180 | 181 | OPERATION_MODES = ( |
|
181 | 182 | (0, 'Manual'), |
|
182 | 183 | (1, 'Automatic'), |
|
183 | 184 | ) |
|
184 | 185 | |
|
185 | 186 | |
|
186 | 187 | class ABSConfiguration(Configuration): |
|
187 | 188 | active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}") |
|
188 | 189 | module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default)) |
|
189 | 190 | operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0) |
|
190 | 191 | operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True) |
|
191 | 192 | module_messages = models.CharField(verbose_name='Modules Messages', max_length=10000, default=json.dumps(default_messages)) |
|
192 | 193 | |
|
193 | 194 | class Meta: |
|
194 | 195 | db_table = 'abs_configurations' |
|
195 | 196 | |
|
196 | 197 | def get_absolute_url_plot(self): |
|
197 | 198 | return reverse('url_plot_abs_patterns', args=[str(self.id)]) |
|
198 | 199 | |
|
199 | 200 | |
|
200 | 201 | def parms_to_dict(self): |
|
201 | 202 | |
|
202 | 203 | parameters = {} |
|
203 | 204 | |
|
204 | 205 | parameters['device_id'] = self.device.id |
|
205 | 206 | parameters['name'] = self.name |
|
206 | 207 | parameters['beams'] = {} |
|
207 | 208 | |
|
208 | 209 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
209 | 210 | b=1 |
|
210 | 211 | for beam in beams: |
|
211 | 212 | #absbeam = ABSBeam.objects.get(pk=beams[beam]) |
|
212 | 213 | parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict() |
|
213 | 214 | b+=1 |
|
214 | 215 | |
|
215 | 216 | return parameters |
|
216 | 217 | |
|
217 | 218 | def dict_to_parms(self, parameters): |
|
218 | 219 | |
|
219 | 220 | self.name = parameters['name'] |
|
220 | 221 | |
|
221 | 222 | absbeams = ABSBeam.objects.filter(abs_conf=self) |
|
222 | 223 | beams = parameters['beams'] |
|
223 | 224 | |
|
224 | 225 | if absbeams: |
|
225 | 226 | beams_number = len(beams) |
|
226 | 227 | absbeams_number = len(absbeams) |
|
227 | 228 | if beams_number==absbeams_number: |
|
228 | 229 | i = 1 |
|
229 | 230 | for absbeam in absbeams: |
|
230 | 231 | absbeam.dict_to_parms(beams['beam'+str(i)]) |
|
231 | 232 | i = i+1 |
|
232 | 233 | elif beams_number > absbeams_number: |
|
233 | 234 | i = 1 |
|
234 | 235 | for absbeam in absbeams: |
|
235 | 236 | absbeam.dict_to_parms(beams['beam'+str(i)]) |
|
236 | 237 | i=i+1 |
|
237 | 238 | for x in range(i,beams_number+1): |
|
238 | 239 | new_beam = ABSBeam( |
|
239 | 240 | name =beams['beam'+str(i)]['name'], |
|
240 | 241 | antenna =json.dumps(beams['beam'+str(i)]['antenna']), |
|
241 | 242 | abs_conf = self, |
|
242 | 243 | tx =json.dumps(beams['beam'+str(i)]['tx']), |
|
243 | 244 | rx =json.dumps(beams['beam'+str(i)]['rx']), |
|
244 | 245 | ues =json.dumps(beams['beam'+str(i)]['ues']), |
|
245 | 246 | only_rx =json.dumps(beams['beam'+str(i)]['only_rx']) |
|
246 | 247 | ) |
|
247 | 248 | new_beam.save() |
|
248 | 249 | i=i+1 |
|
249 | 250 | else: #beams_number < absbeams_number: |
|
250 | 251 | i = 1 |
|
251 | 252 | for absbeam in absbeams: |
|
252 | 253 | if i <= beams_number: |
|
253 | 254 | absbeam.dict_to_parms(beams['beam'+str(i)]) |
|
254 | 255 | i=i+1 |
|
255 | 256 | else: |
|
256 | 257 | absbeam.delete() |
|
257 | 258 | else: |
|
258 | 259 | for beam in beams: |
|
259 | 260 | new_beam = ABSBeam( |
|
260 | 261 | name =beams[beam]['name'], |
|
261 | 262 | antenna =json.dumps(beams[beam]['antenna']), |
|
262 | 263 | abs_conf = self, |
|
263 | 264 | tx =json.dumps(beams[beam]['tx']), |
|
264 | 265 | rx =json.dumps(beams[beam]['rx']), |
|
265 | 266 | ues =json.dumps(beams[beam]['ues']), |
|
266 | 267 | only_rx =json.dumps(beams[beam]['only_rx']) |
|
267 | 268 | ) |
|
268 | 269 | new_beam.save() |
|
269 | 270 | |
|
270 | 271 | |
|
271 | 272 | |
|
272 | 273 | def update_from_file(self, parameters): |
|
273 | 274 | |
|
274 | 275 | self.dict_to_parms(parameters) |
|
275 | 276 | self.save() |
|
276 | 277 | |
|
277 | 278 | |
|
278 | 279 | def get_beams(self, **kwargs): |
|
279 | 280 | ''' |
|
280 | 281 | This function returns ABS Configuration beams |
|
281 | 282 | ''' |
|
282 | 283 | return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs) |
|
283 | 284 | |
|
284 | 285 | def clone(self, **kwargs): |
|
285 | 286 | |
|
286 | 287 | beams = self.get_beams() |
|
287 | 288 | self.pk = None |
|
288 | 289 | self.id = None |
|
289 | 290 | for attr, value in kwargs.items(): |
|
290 | 291 | setattr(self, attr, value) |
|
291 | 292 | self.save() |
|
292 | 293 | |
|
293 | 294 | for beam in beams: |
|
294 | 295 | beam.clone(abs_conf=self) |
|
295 | 296 | |
|
296 | 297 | #-----For Active Beam----- |
|
297 | 298 | active_beam = json.loads(self.active_beam) |
|
298 | 299 | new_beams = ABSBeam.objects.filter(abs_conf=self) |
|
299 | 300 | active_beam['active_beam'] = new_beams[0].id |
|
301 | ||
|
300 | 302 | self.active_beam = json.dumps(active_beam) |
|
301 | 303 | self.save() |
|
302 | 304 | #-----For Active Beam----- |
|
305 | #-----For Device Status--- | |
|
306 | self.device.status = 3 | |
|
307 | self.device.save() | |
|
308 | #-----For Device Status--- | |
|
303 | 309 | |
|
304 | 310 | return self |
|
305 | 311 | |
|
306 | 312 | |
|
313 | def start_device(self): | |
|
314 | ||
|
315 | if self.device.status == 3: | |
|
316 | ||
|
317 | try: | |
|
318 | #self.write_device() | |
|
319 | send_task('task_change_beam', [self.id],) | |
|
320 | self.message = 'ABS running' | |
|
321 | ||
|
322 | except Exception as e: | |
|
323 | self.message = str(e) | |
|
324 | return False | |
|
325 | ||
|
326 | return True | |
|
327 | ||
|
328 | else: | |
|
329 | self.message = 'Please, select Write ABS Device first.' | |
|
330 | return False | |
|
331 | ||
|
332 | ||
|
333 | def stop_device(self): | |
|
334 | ||
|
335 | self.device.status = 2 | |
|
336 | self.device.save() | |
|
337 | self.message = 'ABS has been stopped.' | |
|
338 | self.save() | |
|
339 | ||
|
340 | return True | |
|
341 | ||
|
342 | ||
|
307 | 343 | def module_conf(self, module_num, beams): |
|
308 | 344 | """ |
|
309 | 345 | This function creates beam configurations for one abs module. |
|
310 | 346 | """ |
|
311 | 347 | ip_address = self.device.ip_address |
|
312 | 348 | ip_address = ip_address.split('.') |
|
313 | 349 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
314 | 350 | dot = '.' |
|
315 | 351 | module_ip = dot.join(module_seq)+'.'+str(module_num) |
|
316 | 352 | module_port = self.device.port_address |
|
317 | 353 | write_route = 'http://'+module_ip+':'+str(module_port)+'/write' |
|
318 | 354 | |
|
319 | 355 | #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
320 | 356 | #module = 'ABS_'+str(module_num) |
|
321 | 357 | bs = '' #{} |
|
322 | 358 | i=1 |
|
323 | 359 | |
|
324 | 360 | for beam in beams: |
|
325 | 361 | #bs[i] = fromBinary2Char(beam.module_6bits(module_num)) |
|
326 | 362 | bs = bs + fromBinary2Char(beam.module_6bits(module_num)) |
|
327 | 363 | i=i+1 |
|
328 | 364 | |
|
329 | 365 | beams = bs |
|
330 | 366 | |
|
331 | 367 | parameters = {} |
|
332 | 368 | parameters['beams'] = beams #json.dumps(beams) |
|
333 | 369 | print parameters |
|
334 | 370 | answer = '' |
|
335 | 371 | |
|
336 | 372 | try: |
|
337 | 373 | #r_write = requests.post(write_route, parameters, timeout=0.5) |
|
338 | 374 | r_write = requests.post(write_route, json = parameters, timeout=0.5) |
|
339 | 375 | answer = r_write.json() |
|
340 | 376 | #self.message = answer['message'] |
|
341 | 377 | except: |
|
342 | 378 | #self.message = "Could not write ABS parameters" |
|
343 | 379 | return False |
|
344 | 380 | |
|
345 | 381 | return answer |
|
346 | 382 | |
|
347 | 383 | |
|
348 | 384 | |
|
349 | 385 | def write_device(self): |
|
350 | 386 | |
|
351 | 387 | """ |
|
352 | 388 | This function sends the beams list to every abs module. |
|
353 | 389 | It needs 'module_conf' function |
|
354 | 390 | """ |
|
355 | 391 | |
|
356 | 392 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
357 | 393 | connected_modules = ast.literal_eval(self.module_status) |
|
358 | 394 | suma_connected_modules = 0 |
|
359 | 395 | |
|
360 | 396 | for c in connected_modules: |
|
361 | 397 | suma_connected_modules = suma_connected_modules+connected_modules[c] |
|
362 | 398 | if not suma_connected_modules > 0 : |
|
363 | 399 | self.message = "No ABS Module detected." |
|
364 | 400 | return False |
|
365 | 401 | |
|
366 | 402 | #-------------Write each abs module----------- |
|
367 | 403 | if beams: |
|
368 | 404 | beams_status = ast.literal_eval(self.module_status) |
|
369 | 405 | disconnected_modules = 0 |
|
370 | 406 | for i in range(1,65): #(62,65) |
|
371 | 407 | #--------------JUEVES------------- |
|
372 | 408 | if beams_status[str(i)] != 0: |
|
373 | 409 | try: |
|
374 | 410 | answer = self.module_conf(i,beams) |
|
375 | 411 | if answer: |
|
376 | 412 | if answer['status']: |
|
377 | 413 | beams_status[str(i)] = 3 |
|
378 | 414 | |
|
379 | 415 | except: |
|
380 | 416 | beams_status[str(i)] = 1 |
|
381 | 417 | |
|
382 | 418 | pass |
|
383 | 419 | else: |
|
384 | 420 | disconnected_modules += 1 |
|
385 | 421 | |
|
386 | 422 | else: |
|
387 | 423 | self.message = "ABS Configuration does not have beams" |
|
388 | 424 | return False |
|
389 | 425 | |
|
390 | 426 | #self.device.status = 1 |
|
391 | 427 | ## |
|
392 | 428 | #-------------Jueves------------- |
|
393 | 429 | if disconnected_modules == 64: |
|
394 | 430 | self.message = "Could not write ABS Modules" |
|
431 | self.device.status = 0 | |
|
395 | 432 | return False |
|
396 | 433 | else: |
|
397 | 434 | self.message = "ABS Beams List have been sent to ABS Modules" |
|
398 | 435 | beams[0].set_as_activebeam() |
|
399 | 436 | |
|
437 | self.device.status = 3 | |
|
400 | 438 | self.module_status = json.dumps(beams_status) |
|
401 | self.save() | |
|
439 | ||
|
402 | 440 | |
|
403 | 441 | |
|
404 | 442 | self.save() |
|
405 | 443 | return True |
|
406 | 444 | |
|
407 | 445 | |
|
408 | 446 | |
|
409 | 447 | |
|
410 | 448 | def read_module(self, module): |
|
411 | 449 | |
|
412 | 450 | """ |
|
413 | 451 | Read out-bits (up-down) of 1 abs module NOT for Configuration |
|
414 | 452 | """ |
|
415 | 453 | |
|
416 | 454 | parameters = {} |
|
417 | 455 | ip_address = self.device.ip_address |
|
418 | 456 | ip_address = ip_address.split('.') |
|
419 | 457 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
420 | 458 | dot = '.' |
|
421 | 459 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
422 | 460 | module_port = self.device.port_address |
|
423 | 461 | read_route = 'http://'+module_ip+':'+str(module_port)+'/read' |
|
424 | 462 | |
|
425 | 463 | print(read_route) |
|
426 | 464 | |
|
427 | 465 | answer = '' |
|
428 | 466 | module_bits = '' |
|
429 | 467 | |
|
430 | 468 | try: |
|
431 | 469 | r_write = requests.get(read_route, timeout=0.7) |
|
432 | 470 | answer = r_write.json() |
|
433 | 471 | self.message = answer['message'] |
|
434 | 472 | module_bits = answer['allbits'] |
|
435 | 473 | except: |
|
436 | 474 | #message = "Could not read ABS parameters" |
|
437 | 475 | answer = r_write.json() |
|
438 | 476 | return 0 |
|
439 | 477 | |
|
440 | 478 | return module_bits |
|
441 | 479 | |
|
442 | 480 | |
|
443 | 481 | def absmodule_status(self): |
|
444 | 482 | """ |
|
445 | 483 | This function gets the status of each abs module. It sends GET method to Web Application |
|
446 | 484 | in Python Bottle. |
|
447 | 485 | This function updates "module_status" field from ABSconfiguration. |
|
448 | 486 | """ |
|
449 | 487 | ip_address = self.device.ip_address |
|
450 | 488 | ip_address = ip_address.split('.') |
|
451 | 489 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
452 | 490 | dot = '.' |
|
453 | 491 | module_port = self.device.port_address |
|
454 | 492 | |
|
455 | 493 | modules_status = json.loads(self.module_status) |
|
456 | 494 | module_messages = json.loads(self.module_messages) |
|
457 | 495 | |
|
458 | 496 | for i in range(1,65): |
|
459 | 497 | module_ip = dot.join(module_seq)+'.'+str(i) |
|
460 | 498 | print module_ip |
|
461 | 499 | |
|
462 | 500 | route = 'http://'+module_ip+':'+str(module_port)+'/status' |
|
463 | 501 | |
|
464 | 502 | try: |
|
465 | 503 | r = requests.get(route, timeout=0.5)#, timeout=0.7) |
|
466 | 504 | answer = r.json() |
|
467 | 505 | modules_status[str(i)] = answer['status'] |
|
468 | 506 | module_messages[str(i)] = answer['message'] |
|
469 | 507 | except: |
|
470 | 508 | modules_status[str(i)] = 0 |
|
471 | 509 | pass |
|
472 | 510 | |
|
473 | 511 | |
|
474 | 512 | self.module_status = json.dumps(modules_status) |
|
475 | 513 | self.module_messages = json.dumps(module_messages) |
|
476 | 514 | self.save() |
|
477 | 515 | |
|
478 | 516 | return |
|
479 | 517 | |
|
480 | 518 | |
|
481 | 519 | def connected_modules(self): |
|
482 | 520 | """ |
|
483 | 521 | This function returns the number of connected abs-modules without updating. |
|
484 | 522 | """ |
|
485 | 523 | modules_status = json.loads(self.module_status) |
|
486 | 524 | num = 0 |
|
487 | 525 | for status in modules_status: |
|
488 | 526 | if modules_status[status] != 0: |
|
489 | 527 | num += 1 |
|
490 | 528 | |
|
491 | 529 | return num |
|
492 | 530 | |
|
493 | 531 | |
|
494 | 532 | def status_device(self): |
|
495 | 533 | """ |
|
496 | 534 | This function returns the status of all abs-modules as one. |
|
497 | 535 | If at least one module is connected, its answer is "1" |
|
498 | 536 | """ |
|
499 | 537 | self.absmodule_status() |
|
500 | 538 | connected_modules = self.connected_modules() |
|
501 | 539 | if connected_modules>0: |
|
502 | 540 | self.message = 'ABS modules Status have been updated.' |
|
503 | 541 | return 1 |
|
504 | 542 | self.message = 'No ABS module is connected.' |
|
505 | 543 | return 0 |
|
506 | 544 | |
|
507 | 545 | |
|
508 | 546 | |
|
509 | 547 | def write_module(self, module): |
|
510 | 548 | |
|
511 | 549 | """ |
|
512 | 550 | Send configuration to one abs module |
|
513 | 551 | """ |
|
514 | 552 | |
|
515 | 553 | parameters = {} |
|
516 | 554 | ip_address = self.abs_conf.device.ip_address |
|
517 | 555 | ip_address = ip_address.split('.') |
|
518 | 556 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
519 | 557 | dot = '.' |
|
520 | 558 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
521 | 559 | module_port = self.abs_conf.device.port_address |
|
522 | 560 | write_route = 'http://'+module_ip+':'+str(module_port)+'/write' |
|
523 | 561 | |
|
524 | 562 | #print write_route |
|
525 | 563 | |
|
526 | 564 | #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
527 | 565 | #module = 'ABS_'+str(module) |
|
528 | 566 | beams = '!`*3<ENW'#{1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
529 | 567 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
530 | 568 | |
|
531 | 569 | #parameters['header'] = header |
|
532 | 570 | parameters['module'] = module |
|
533 | 571 | parameters['beams'] = json.dumps(beams) |
|
534 | 572 | |
|
535 | 573 | answer = '' |
|
536 | 574 | |
|
537 | 575 | try: |
|
538 | 576 | r_write = requests.post(write_route, parameters, timeout=0.5) |
|
539 | 577 | answer = r_write.json() |
|
540 | 578 | self.message = answer['message'] |
|
541 | 579 | except: |
|
542 | 580 | self.message = "Could not write ABS parameters" |
|
543 | 581 | return 0 |
|
544 | 582 | |
|
545 | 583 | |
|
546 | 584 | #self.device.status = int(answer['status']) |
|
547 | 585 | |
|
548 | 586 | return 1 |
|
549 | 587 | |
|
550 | 588 | |
|
551 | 589 | def beam_selector(self, module, beam_pos): |
|
552 | 590 | """ |
|
553 | 591 | This function selects the beam number for one absmodule. |
|
554 | 592 | """ |
|
555 | 593 | |
|
556 | 594 | if beam_pos > 0: |
|
557 | 595 | beam_pos = beam_pos - 1 |
|
558 | 596 | else: |
|
559 | 597 | beam_pos = 0 |
|
560 | 598 | |
|
561 | 599 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
562 | 600 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
563 | 601 | beams_list = ABSBeam.objects.filter(abs_conf=self) |
|
564 | 602 | if len(beams_list) < beam_pos: |
|
565 | 603 | return 0 |
|
566 | 604 | |
|
567 | 605 | flag = 1 |
|
568 | 606 | if beam_pos>9: |
|
569 | 607 | flag = 2 |
|
570 | 608 | |
|
571 | 609 | module_address = ('192.168.1.'+str(module), 5500) |
|
572 | 610 | header = 'JROABSCeCnModCnMod0100000' |
|
573 | 611 | numbers = len(str(beam_pos)) |
|
574 | 612 | function = 'CHGB' |
|
575 | 613 | |
|
576 | 614 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
577 | 615 | |
|
578 | 616 | # Create the datagram socket |
|
579 | 617 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
580 | 618 | #sock.connect(module_address) |
|
581 | 619 | try: |
|
582 | 620 | sock.connect(module_address) |
|
583 | 621 | sock.send(message_tx) |
|
584 | 622 | sock.close() |
|
585 | 623 | print("Writing abs module:"+module_address[0]+"...") |
|
586 | 624 | except: |
|
587 | 625 | sock = None |
|
588 | 626 | print("Problem writing abs module:"+module_address[0]) |
|
589 | 627 | return 0 |
|
590 | 628 | |
|
591 | 629 | return 1 |
|
592 | 630 | |
|
593 | 631 | |
|
594 | 632 | def change_beam(self, beam_pos): |
|
595 | 633 | """ |
|
596 | 634 | This function selects the beam number for all absmodules. |
|
597 | 635 | """ |
|
598 | 636 | for i in range(1,65): |
|
599 | 637 | try: |
|
600 | 638 | self.beam_selector(i,beam_pos) |
|
601 | 639 | except: |
|
602 | 640 | print("Problem with module: 192.168.1."+str(i)) |
|
603 | 641 | self.message = "Problem with module: 192.168.1."+str(i) |
|
604 | 642 | #return 0 |
|
605 | 643 | return 1 |
|
606 | 644 | |
|
607 | 645 | |
|
608 | 646 | def send_beam_num(self, beam_pos): |
|
609 | 647 | """ |
|
610 | 648 | This function connects to a multicast group and sends the beam number |
|
611 | 649 | to all abs modules. |
|
612 | 650 | """ |
|
613 | 651 | |
|
614 | 652 | if beam_pos > 0: |
|
615 | 653 | beam_pos = beam_pos - 1 |
|
616 | 654 | else: |
|
617 | 655 | beam_pos = 0 |
|
618 | 656 | |
|
619 | 657 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
620 | 658 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
621 | 659 | beams_list = ABSBeam.objects.filter(abs_conf=self) |
|
622 | 660 | if len(beams_list) < beam_pos: |
|
623 | 661 | return 0 |
|
624 | 662 | |
|
625 | 663 | flag = 1 |
|
626 | 664 | if beam_pos>9: |
|
627 | 665 | flag = 2 |
|
628 | 666 | |
|
629 | 667 | header = 'JROABSCeCnModCnMod0100000' |
|
630 | 668 | flag = str(flag) |
|
631 | 669 | function = 'CHGB' |
|
632 | 670 | message_tx = header+flag+function+str(beam_pos)+'0' |
|
633 | 671 | |
|
634 | 672 | multicast_group = '224.3.29.71' |
|
635 | 673 | server_address = ('',10000) |
|
636 | 674 | |
|
637 | 675 | # Create the socket |
|
638 | 676 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
639 | 677 | # Bind to the server address |
|
640 | 678 | sock.bind(server_address) |
|
641 | 679 | # Telling the OS add the socket to the multicast on all interfaces |
|
642 | 680 | group = socket.inet_aton(multicast_group) |
|
643 | 681 | mreq = struct.pack('4sL', group, socket.INADDR_ANY) |
|
644 | 682 | sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) |
|
645 | 683 | |
|
646 | 684 | #print 'sending acknowledgement to all: \n' + message_tx |
|
647 | 685 | sock.sendto(message_tx, (multicast_group, 10000)) |
|
648 | 686 | sock.close() |
|
649 | 687 | sock = None |
|
650 | 688 | |
|
651 | 689 | return 1 |
|
652 | 690 | |
|
653 | 691 | def test1(self): |
|
654 | 692 | t1 = time.time() |
|
655 | 693 | t2 = 0 |
|
656 | 694 | while (t2-t1)<100:#300 |
|
657 | 695 | t2 = time.time() |
|
658 | 696 | self.send_beam_num(2) |
|
659 | 697 | time.sleep(0.04) |
|
660 | 698 | self.send_beam_num(1) |
|
661 | 699 | time.sleep(0.04) |
|
662 | 700 | return |
|
663 | 701 | |
|
664 | 702 | def change_procs_test1(self, module): |
|
665 | 703 | |
|
666 | 704 | for i in range (1,300):#300 |
|
667 | 705 | beam_pos = 1 |
|
668 | 706 | module_address = ('192.168.1.'+str(module), 5500) |
|
669 | 707 | header = 'JROABSCeCnModCnMod0100000' |
|
670 | 708 | numbers = len(str(beam_pos)) |
|
671 | 709 | function = 'CHGB' |
|
672 | 710 | |
|
673 | 711 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
674 | 712 | |
|
675 | 713 | # Create the datagram socket |
|
676 | 714 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
677 | 715 | sock.connect(module_address) |
|
678 | 716 | |
|
679 | 717 | sock.send(message_tx) |
|
680 | 718 | #t = sock.recv(1024) |
|
681 | 719 | sock.close() |
|
682 | 720 | sock = None |
|
683 | 721 | |
|
684 | 722 | |
|
685 | 723 | time.sleep(0.04) |
|
686 | 724 | |
|
687 | 725 | |
|
688 | 726 | beam_pos = 0 |
|
689 | 727 | numbers = len(str(beam_pos)) |
|
690 | 728 | |
|
691 | 729 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
692 | 730 | |
|
693 | 731 | # Create the datagram socket |
|
694 | 732 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
695 | 733 | sock.connect(module_address) |
|
696 | 734 | sock.send(message_tx) |
|
697 | 735 | sock.close() |
|
698 | 736 | sock = None |
|
699 | 737 | |
|
700 | 738 | time.sleep(0.04) |
|
701 | 739 | |
|
702 | 740 | |
|
703 | 741 | def multi_procs_test1(self): |
|
704 | 742 | |
|
705 | 743 | """ |
|
706 | 744 | This function sends the beam number to all abs modules using multiprocessing. |
|
707 | 745 | """ |
|
708 | 746 | |
|
709 | 747 | #if __name__ == "__main__": |
|
710 | 748 | size = 10000000 # Number of random numbers to add |
|
711 | 749 | procs = 65 # (Number-1) of processes to create (absmodule) |
|
712 | 750 | |
|
713 | 751 | # Create a list of jobs and then iterate through |
|
714 | 752 | # the number of processes appending each process to |
|
715 | 753 | # the job list |
|
716 | 754 | jobs = [] |
|
717 | 755 | for i in range(1, procs): |
|
718 | 756 | |
|
719 | 757 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) |
|
720 | 758 | jobs.append(process) |
|
721 | 759 | #print jobs |
|
722 | 760 | |
|
723 | 761 | # Start the processes (i.e. calculate the random number lists) |
|
724 | 762 | for j in jobs: |
|
725 | 763 | #time.sleep(0.4) |
|
726 | 764 | #print j |
|
727 | 765 | j.start() |
|
728 | 766 | |
|
729 | 767 | # Ensure all of the processes have finished |
|
730 | 768 | for j in jobs: |
|
731 | 769 | j.join() |
|
732 | 770 | |
|
733 | 771 | print("List processing complete.") |
|
734 | 772 | return 1 |
|
735 | 773 | |
|
736 | 774 | |
|
737 | 775 | |
|
738 | 776 | def multi_procs_test2(self): |
|
739 | 777 | """ |
|
740 | 778 | This function use multiprocessing python library. Importing Pool we can select |
|
741 | 779 | the number of cores we want to use |
|
742 | 780 | After 'nproc' linux command, we know how many cores computer has. |
|
743 | 781 | NOT WORKING |
|
744 | 782 | """ |
|
745 | 783 | import multiprocessing |
|
746 | 784 | pool = multiprocessing.Pool(3) # cores |
|
747 | 785 | |
|
748 | 786 | tasks = [] |
|
749 | 787 | procs = 65 |
|
750 | 788 | for i in range(62, procs): |
|
751 | 789 | tasks.append( (i,)) |
|
752 | 790 | #print tasks |
|
753 | 791 | #pool.apply_async(self.change_procs_test1, 62) |
|
754 | 792 | results = [pool.apply( self.change_procs_test1, t ) for t in tasks] |
|
755 | 793 | #for result in results: |
|
756 | 794 | #result.get() |
|
757 | 795 | #(plotNum, plotFilename) = result.get() |
|
758 | 796 | #print("Result: plot %d written to %s" % (plotNum, plotFilename) ) |
|
759 | 797 | |
|
760 | 798 | return 1 |
|
761 | 799 | |
|
762 | 800 | |
|
763 | 801 | def multi_procs_test3(self): |
|
764 | 802 | """ |
|
765 | 803 | This function use multiprocessing python library. Importing Pool we can select |
|
766 | 804 | the number of cores we want to use |
|
767 | 805 | After 'nproc' linux command, we know how many cores computer has. |
|
768 | 806 | """ |
|
769 | 807 | from multiprocessing import Pool |
|
770 | 808 | import time |
|
771 | 809 | |
|
772 | 810 | def f(x): |
|
773 | 811 | return x*x |
|
774 | 812 | |
|
775 | 813 | tasks = [] |
|
776 | 814 | procs = 65 |
|
777 | 815 | pool = Pool(processes=4) |
|
778 | 816 | |
|
779 | 817 | #for i in range(62, procs): |
|
780 | 818 | #result = pool.map(f, range(62,65)) |
|
781 | 819 | it = pool.imap(self.change_procs_test1, range(62,65)) |
|
782 | 820 | #result.get(timeout=5) |
|
783 | 821 | |
|
784 | 822 | return 1 |
|
785 | 823 | |
|
786 | 824 | |
|
787 | 825 | def f(x): |
|
788 | 826 | print x |
|
789 | 827 | return |
|
790 | 828 | |
|
791 | 829 | def multi_procs_test4(self): |
|
792 | 830 | import multiprocessing as mp |
|
793 | 831 | |
|
794 | 832 | |
|
795 | 833 | num_workers = mp.cpu_count() |
|
796 | 834 | |
|
797 | 835 | pool = mp.Pool(num_workers) |
|
798 | 836 | procs = 65 |
|
799 | 837 | for i in range(62, procs): |
|
800 | 838 | #for task in tasks: |
|
801 | 839 | pool.apply_async(self.f, args = (i,)) |
|
802 | 840 | |
|
803 | 841 | pool.close() |
|
804 | 842 | pool.join() |
|
805 | 843 | |
|
806 | 844 | return 1 |
|
807 | 845 | |
|
808 | 846 | |
|
809 | 847 | def get_absolute_url_import(self): |
|
810 | 848 | return reverse('url_import_abs_conf', args=[str(self.id)]) |
|
811 | 849 | |
|
812 | 850 | |
|
813 | 851 | |
|
814 | 852 | |
|
815 | 853 | class ABSBeam(models.Model): |
|
816 | 854 | |
|
817 | 855 | name = models.CharField(max_length=60, default='Beam') |
|
818 | 856 | antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default) |
|
819 | 857 | abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration') |
|
820 | 858 | tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default) |
|
821 | 859 | rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default) |
|
822 | 860 | s_time = models.TimeField(verbose_name='Star Time', default='00:00:00') |
|
823 | 861 | e_time = models.TimeField(verbose_name='End Time', default='23:59:59') |
|
824 | 862 | modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default)) |
|
825 | 863 | ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default) |
|
826 | 864 | only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default) |
|
827 | 865 | |
|
828 | 866 | class Meta: |
|
829 | 867 | db_table = 'abs_beams' |
|
830 | 868 | |
|
831 | 869 | def __unicode__(self): |
|
832 | 870 | return u'%s' % (self.name) |
|
833 | 871 | |
|
834 | 872 | def parms_to_dict(self): |
|
835 | 873 | |
|
836 | 874 | #Update data |
|
837 | 875 | self.modules_6bits() |
|
838 | 876 | |
|
839 | 877 | parameters = {} |
|
840 | 878 | |
|
841 | 879 | parameters['name'] = self.name |
|
842 | 880 | parameters['antenna'] = ast.literal_eval(self.antenna) |
|
843 | 881 | parameters['abs_conf'] = self.abs_conf.name |
|
844 | 882 | parameters['tx'] = ast.literal_eval(self.tx) |
|
845 | 883 | parameters['rx'] = ast.literal_eval(self.rx) |
|
846 | 884 | parameters['s_time'] = self.s_time.strftime("%H:%M:%S") |
|
847 | 885 | parameters['e_time'] = self.e_time.strftime("%H:%M:%S") |
|
848 | 886 | parameters['configuration'] = ast.literal_eval(self.modules_conf) |
|
849 | 887 | parameters['ues'] = ast.literal_eval(self.ues) |
|
850 | 888 | parameters['only_rx'] = json.loads(self.only_rx) |
|
851 | 889 | |
|
852 | 890 | return parameters |
|
853 | 891 | |
|
854 | 892 | def dict_to_parms(self, parameters): |
|
855 | 893 | |
|
856 | 894 | self.name = parameters['name'] |
|
857 | 895 | self.antenna = json.dumps(parameters['antenna']) |
|
858 | 896 | #self.abs_conf = parameters['abs_conf'] |
|
859 | 897 | self.tx = json.dumps(parameters['tx']) |
|
860 | 898 | self.rx = json.dumps(parameters['rx']) |
|
861 | 899 | #self.s_time = parameters['s_time'] |
|
862 | 900 | #self.e_time = parameters['e_time'] |
|
863 | 901 | self.ues = json.dumps(parameters['ues']) |
|
864 | 902 | self.only_rx = json.dumps(parameters['only_rx']) |
|
865 | 903 | |
|
866 | 904 | self.modules_6bits() |
|
867 | 905 | self.save() |
|
868 | 906 | |
|
869 | 907 | |
|
870 | 908 | def clone(self, **kwargs): |
|
871 | 909 | |
|
872 | 910 | self.pk = None |
|
873 | 911 | self.id = None |
|
874 | 912 | for attr, value in kwargs.items(): |
|
875 | 913 | setattr(self, attr, value) |
|
876 | 914 | |
|
877 | 915 | self.save() |
|
878 | 916 | |
|
879 | 917 | return self |
|
880 | 918 | |
|
881 | 919 | |
|
882 | 920 | |
|
883 | 921 | def module_6bits(self, module): |
|
884 | 922 | """ |
|
885 | 923 | This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module |
|
886 | 924 | """ |
|
887 | 925 | if module > 64: |
|
888 | 926 | beam_bits = "" |
|
889 | 927 | return beam_bits |
|
890 | 928 | |
|
891 | 929 | data = ast.literal_eval(self.antenna) |
|
892 | 930 | up_data = data['antenna_up'] |
|
893 | 931 | down_data = data['antenna_down'] |
|
894 | 932 | |
|
895 | 933 | pos = ip2position(module) |
|
896 | 934 | up_value = up_data[pos[0]][pos[1]] |
|
897 | 935 | down_value = down_data[pos[0]][pos[1]] |
|
898 | 936 | |
|
899 | 937 | up_bits = up_conv_bits(up_value) |
|
900 | 938 | down_bits = down_conv_bits(down_value) |
|
901 | 939 | beam_bits = up_bits+down_bits |
|
902 | 940 | |
|
903 | 941 | return beam_bits |
|
904 | 942 | |
|
905 | 943 | def modules_6bits(self): |
|
906 | 944 | """ |
|
907 | 945 | This function returns 6bits from every abs module (1-64) in a dict |
|
908 | 946 | """ |
|
909 | 947 | modules_configuration = ast.literal_eval(self.modules_conf) |
|
910 | 948 | |
|
911 | 949 | for i in range(1,65): |
|
912 | 950 | modules_configuration[str(i)] = self.module_6bits(i) |
|
913 | 951 | |
|
914 | 952 | self.modules_conf = json.dumps(modules_configuration) |
|
915 | 953 | self.save() |
|
916 | 954 | |
|
917 | 955 | return self.modules_conf |
|
918 | 956 | |
|
919 | 957 | |
|
920 | 958 | def set_as_activebeam(self): |
|
921 | 959 | """ |
|
922 | 960 | This function set this beam as the active beam of its ABS Configuration. |
|
923 | 961 | """ |
|
924 | 962 | self.abs_conf.active_beam = json.dumps({'active_beam': self.id}) |
|
925 | 963 | self.abs_conf.save() |
|
926 | 964 | |
|
927 | 965 | return |
|
928 | 966 | |
|
929 | 967 | |
|
930 | 968 | @property |
|
931 | 969 | def get_upvalues(self): |
|
932 | 970 | """ |
|
933 | 971 | This function reads antenna pattern and show the up-value of one abs module |
|
934 | 972 | """ |
|
935 | 973 | |
|
936 | 974 | data = ast.literal_eval(self.antenna) |
|
937 | 975 | up_data = data['antenna_up'] |
|
938 | 976 | |
|
939 | 977 | up_values = [] |
|
940 | 978 | for data in up_data: |
|
941 | 979 | for i in range(0,8): |
|
942 | 980 | up_values.append(data[i]) |
|
943 | 981 | |
|
944 | 982 | return up_values |
|
945 | 983 | |
|
946 | 984 | @property |
|
947 | 985 | def antenna_upvalues(self): |
|
948 | 986 | """ |
|
949 | 987 | This function reads antenna pattern and show the up - values of one abs beam |
|
950 | 988 | in a particular order |
|
951 | 989 | """ |
|
952 | 990 | data = ast.literal_eval(self.antenna) |
|
953 | 991 | up_data = data['antenna_up'] |
|
954 | 992 | |
|
955 | 993 | return up_data |
|
956 | 994 | |
|
957 | 995 | @property |
|
958 | 996 | def antenna_downvalues(self): |
|
959 | 997 | """ |
|
960 | 998 | This function reads antenna pattern and show the down - values of one abs beam |
|
961 | 999 | in a particular order |
|
962 | 1000 | """ |
|
963 | 1001 | data = ast.literal_eval(self.antenna) |
|
964 | 1002 | down_data = data['antenna_down'] |
|
965 | 1003 | |
|
966 | 1004 | return down_data |
|
967 | 1005 | |
|
968 | 1006 | @property |
|
969 | 1007 | def get_downvalues(self): |
|
970 | 1008 | """ |
|
971 | 1009 | This function reads antenna pattern and show the down-value of one abs module |
|
972 | 1010 | """ |
|
973 | 1011 | |
|
974 | 1012 | data = ast.literal_eval(self.antenna) |
|
975 | 1013 | down_data = data['antenna_down'] |
|
976 | 1014 | |
|
977 | 1015 | down_values = [] |
|
978 | 1016 | for data in down_data: |
|
979 | 1017 | for i in range(0,8): |
|
980 | 1018 | down_values.append(data[i]) |
|
981 | 1019 | |
|
982 | 1020 | return down_values |
|
983 | 1021 | |
|
984 | 1022 | @property |
|
985 | 1023 | def get_up_ues(self): |
|
986 | 1024 | """ |
|
987 | 1025 | This function shows the up-ues-value of one beam |
|
988 | 1026 | """ |
|
989 | 1027 | data = ast.literal_eval(self.ues) |
|
990 | 1028 | up_ues = data['up'] |
|
991 | 1029 | |
|
992 | 1030 | return up_ues |
|
993 | 1031 | |
|
994 | 1032 | @property |
|
995 | 1033 | def get_down_ues(self): |
|
996 | 1034 | """ |
|
997 | 1035 | This function shows the down-ues-value of one beam |
|
998 | 1036 | """ |
|
999 | 1037 | data = ast.literal_eval(self.ues) |
|
1000 | 1038 | down_ues = data['down'] |
|
1001 | 1039 | |
|
1002 | 1040 | return down_ues |
|
1003 | 1041 | |
|
1004 | 1042 | @property |
|
1005 | 1043 | def get_up_onlyrx(self): |
|
1006 | 1044 | """ |
|
1007 | 1045 | This function shows the up-onlyrx-value of one beam |
|
1008 | 1046 | """ |
|
1009 | 1047 | data = json.loads(self.only_rx) |
|
1010 | 1048 | up_onlyrx = data['up'] |
|
1011 | 1049 | |
|
1012 | 1050 | return up_onlyrx |
|
1013 | 1051 | |
|
1014 | 1052 | @property |
|
1015 | 1053 | def get_down_onlyrx(self): |
|
1016 | 1054 | """ |
|
1017 | 1055 | This function shows the down-onlyrx-value of one beam |
|
1018 | 1056 | """ |
|
1019 | 1057 | data = json.loads(self.only_rx) |
|
1020 | 1058 | down_onlyrx = data['down'] |
|
1021 | 1059 | |
|
1022 | 1060 | return down_onlyrx |
|
1023 | 1061 | |
|
1024 | 1062 | @property |
|
1025 | 1063 | def get_tx(self): |
|
1026 | 1064 | """ |
|
1027 | 1065 | This function shows the tx-values of one beam |
|
1028 | 1066 | """ |
|
1029 | 1067 | data = json.loads(self.tx) |
|
1030 | 1068 | |
|
1031 | 1069 | return data |
|
1032 | 1070 | |
|
1033 | 1071 | @property |
|
1034 | 1072 | def get_uptx(self): |
|
1035 | 1073 | """ |
|
1036 | 1074 | This function shows the up-tx-values of one beam |
|
1037 | 1075 | """ |
|
1038 | 1076 | data = json.loads(self.tx) |
|
1039 | 1077 | up_data = data['up'] |
|
1040 | 1078 | |
|
1041 | 1079 | up_values = [] |
|
1042 | 1080 | for data in up_data: |
|
1043 | 1081 | for i in range(0,8): |
|
1044 | 1082 | up_values.append(data[i]) |
|
1045 | 1083 | |
|
1046 | 1084 | return up_values |
|
1047 | 1085 | |
|
1048 | 1086 | @property |
|
1049 | 1087 | def get_downtx(self): |
|
1050 | 1088 | """ |
|
1051 | 1089 | This function shows the down-tx-values of one beam |
|
1052 | 1090 | """ |
|
1053 | 1091 | data = json.loads(self.tx) |
|
1054 | 1092 | down_data = data['down'] |
|
1055 | 1093 | |
|
1056 | 1094 | down_values = [] |
|
1057 | 1095 | for data in down_data: |
|
1058 | 1096 | for i in range(0,8): |
|
1059 | 1097 | down_values.append(data[i]) |
|
1060 | 1098 | |
|
1061 | 1099 | return down_values |
|
1062 | 1100 | |
|
1063 | 1101 | |
|
1064 | 1102 | |
|
1065 | 1103 | @property |
|
1066 | 1104 | def get_rx(self): |
|
1067 | 1105 | """ |
|
1068 | 1106 | This function shows the rx-values of one beam |
|
1069 | 1107 | """ |
|
1070 | 1108 | data = json.loads(self.rx) |
|
1071 | 1109 | |
|
1072 | 1110 | return data |
|
1073 | 1111 | |
|
1074 | 1112 | @property |
|
1075 | 1113 | def get_uprx(self): |
|
1076 | 1114 | """ |
|
1077 | 1115 | This function shows the up-rx-values of one beam |
|
1078 | 1116 | """ |
|
1079 | 1117 | data = json.loads(self.rx) |
|
1080 | 1118 | up_data = data['up'] |
|
1081 | 1119 | |
|
1082 | 1120 | up_values = [] |
|
1083 | 1121 | for data in up_data: |
|
1084 | 1122 | for i in range(0,8): |
|
1085 | 1123 | up_values.append(data[i]) |
|
1086 | 1124 | |
|
1087 | 1125 | return up_values |
|
1088 | 1126 | |
|
1089 | 1127 | @property |
|
1090 | 1128 | def get_downrx(self): |
|
1091 | 1129 | """ |
|
1092 | 1130 | This function shows the down-rx-values of one beam |
|
1093 | 1131 | """ |
|
1094 | 1132 | data = json.loads(self.rx) |
|
1095 | 1133 | down_data = data['down'] |
|
1096 | 1134 | |
|
1097 | 1135 | down_values = [] |
|
1098 | 1136 | for data in down_data: |
|
1099 | 1137 | for i in range(0,8): |
|
1100 | 1138 | down_values.append(data[i]) |
|
1101 | 1139 | |
|
1102 | 1140 | return down_values |
@@ -1,63 +1,54 | |||
|
1 | 1 | from __future__ import absolute_import |
|
2 | 2 | |
|
3 | 3 | from apps.main.models import Configuration |
|
4 | 4 | from .models import ABSBeam |
|
5 | 5 | import json |
|
6 | 6 | from datetime import timedelta, datetime |
|
7 | 7 | from celery.task import task |
|
8 | 8 | |
|
9 | """ | |
|
10 | @task | |
|
11 | def beam_task(id_conf): | |
|
12 | ||
|
13 | abs_conf = Configuration.objects.get(pk=id_conf) | |
|
14 | ||
|
15 | task_change_beam(abs_conf.pk) | |
|
16 | ||
|
17 | return task_change_beam(abs_conf.pk) | |
|
18 | ||
|
19 | """ | |
|
20 | ||
|
21 | @task | |
|
9 | @task(name='task_change_beam') | |
|
22 | 10 | def task_change_beam(id_conf): |
|
23 | 11 | |
|
24 | 12 | abs_conf = Configuration.objects.get(pk=id_conf) |
|
25 | 13 | beams_list = ABSBeam.objects.filter(abs_conf=abs_conf) |
|
26 | 14 | active_beam = json.loads(abs_conf.active_beam) |
|
27 | 15 | |
|
28 | 16 | run_every = timedelta(seconds=abs_conf.operation_value) |
|
29 | 17 | now = datetime.utcnow() |
|
30 | 18 | date = now + run_every |
|
31 | 19 | |
|
20 | if abs_conf.device.status != 3: | |
|
21 | return abs_conf.device.status | |
|
22 | ||
|
32 | 23 | if abs_conf.operation_mode == 0: #Manual Mode |
|
33 | 24 | return 1 |
|
34 | 25 | |
|
35 | 26 | if active_beam: |
|
36 | 27 | current_beam = ABSBeam.objects.get(pk=active_beam['active_beam']) |
|
37 | 28 | i=0 |
|
38 | 29 | for beam in beams_list: |
|
39 | 30 | if beam == current_beam: |
|
40 | 31 | i+=1 |
|
41 | 32 | break |
|
42 | 33 | i+=1 |
|
43 | 34 | |
|
44 | 35 | if i < len(beams_list): |
|
45 | 36 | next_beam = beams_list[i] |
|
46 | 37 | abs_conf.send_beam_num(i+1) |
|
47 | 38 | next_beam.set_as_activebeam() |
|
48 | 39 | task = task_change_beam.apply_async((abs_conf.pk,), eta=date) |
|
49 | 40 | print next_beam |
|
50 | 41 | else: |
|
51 | 42 | abs_conf.send_beam_num(1) |
|
52 | 43 | beams_list[0].set_as_activebeam() |
|
53 | 44 | task = task_change_beam.apply_async((abs_conf.pk,), eta=date) |
|
54 |
print |
|
|
45 | print beams_list[0] | |
|
55 | 46 | i=0 |
|
56 | 47 | |
|
57 | 48 | else: |
|
58 | 49 | abs_conf.send_beam_num(1) |
|
59 | 50 | beams_list[0].set_as_activebeam() |
|
60 | 51 | task = task_change_beam.apply_async((abs_conf.pk,), eta=date) |
|
61 | 52 | |
|
62 | 53 | |
|
63 | 54 | return 2 |
@@ -1,427 +1,427 | |||
|
1 | 1 | from django.shortcuts import render_to_response |
|
2 | 2 | from django.template import RequestContext |
|
3 | 3 | from django.shortcuts import redirect, render, get_object_or_404 |
|
4 | 4 | from django.contrib import messages |
|
5 | 5 | from django.conf import settings |
|
6 | 6 | from django.http import HttpResponse |
|
7 | 7 | from django.core.urlresolvers import reverse |
|
8 | 8 | |
|
9 | 9 | from datetime import datetime |
|
10 | 10 | from time import sleep |
|
11 | 11 | import os |
|
12 | 12 | |
|
13 | from apps.main.models import Device, Configuration | |
|
13 | from apps.main.models import Device, Configuration, Experiment | |
|
14 | 14 | from apps.main.views import sidebar |
|
15 | 15 | |
|
16 | 16 | from .models import ABSConfiguration, ABSBeam |
|
17 | 17 | from .forms import ABSConfigurationForm, ABSBeamEditForm, ABSBeamAddForm, ABSImportForm |
|
18 | 18 | |
|
19 | 19 | from .utils.overJroShow import overJroShow |
|
20 | 20 | from .utils.OverJRO import OverJRO |
|
21 | 21 | # Create your views here. |
|
22 | 22 | import json, ast |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | def get_values_from_form(form_data): |
|
26 | 26 | |
|
27 | 27 | sublistup = [] |
|
28 | 28 | sublistdown = [] |
|
29 | 29 | subtxlistup = [] |
|
30 | 30 | subtxlistdown = [] |
|
31 | 31 | subrxlistup = [] |
|
32 | 32 | subrxlistdown = [] |
|
33 | 33 | |
|
34 | 34 | up_values_list = [] |
|
35 | 35 | down_values_list = [] |
|
36 | 36 | up_txvalues_list = [] |
|
37 | 37 | down_txvalues_list = [] |
|
38 | 38 | up_rxvalues_list = [] |
|
39 | 39 | down_rxvalues_list = [] |
|
40 | 40 | |
|
41 | 41 | values_list = {} |
|
42 | 42 | cont = 1 |
|
43 | 43 | |
|
44 | 44 | for i in range(1,65): |
|
45 | 45 | x = float(form_data['abs_up'+str(i)]) |
|
46 | 46 | y = float(form_data['abs_down'+str(i)]) |
|
47 | 47 | sublistup.append(x) |
|
48 | 48 | sublistdown.append(y) |
|
49 | 49 | |
|
50 | 50 | if str(i) in form_data.getlist('uptx_checks'): |
|
51 | 51 | subtxlistup.append(1) |
|
52 | 52 | else: |
|
53 | 53 | subtxlistup.append(0) |
|
54 | 54 | if str(i) in form_data.getlist('downtx_checks'): |
|
55 | 55 | subtxlistdown.append(1) |
|
56 | 56 | else: |
|
57 | 57 | subtxlistdown.append(0) |
|
58 | 58 | |
|
59 | 59 | if str(i) in form_data.getlist('uprx_checks'): |
|
60 | 60 | subrxlistup.append(1) |
|
61 | 61 | else: |
|
62 | 62 | subrxlistup.append(0) |
|
63 | 63 | if str(i) in form_data.getlist('downrx_checks'): |
|
64 | 64 | subrxlistdown.append(1) |
|
65 | 65 | else: |
|
66 | 66 | subrxlistdown.append(0) |
|
67 | 67 | |
|
68 | 68 | cont = cont+1 |
|
69 | 69 | |
|
70 | 70 | if cont == 9: |
|
71 | 71 | up_values_list.append(sublistup) |
|
72 | 72 | down_values_list.append(sublistdown) |
|
73 | 73 | sublistup = [] |
|
74 | 74 | sublistdown = [] |
|
75 | 75 | |
|
76 | 76 | up_txvalues_list.append(subtxlistup) |
|
77 | 77 | down_txvalues_list.append(subtxlistdown) |
|
78 | 78 | subtxlistup = [] |
|
79 | 79 | subtxlistdown = [] |
|
80 | 80 | up_rxvalues_list.append(subrxlistup) |
|
81 | 81 | down_rxvalues_list.append(subrxlistdown) |
|
82 | 82 | subrxlistup = [] |
|
83 | 83 | subrxlistdown = [] |
|
84 | 84 | cont = 1 |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | list_uesup = [] |
|
88 | 88 | list_uesdown = [] |
|
89 | 89 | for i in range(1,5): |
|
90 | 90 | if form_data['ues_up'+str(i)] == '': |
|
91 | 91 | list_uesup.append(0.0) |
|
92 | 92 | else: |
|
93 | 93 | list_uesup.append(float(form_data['ues_up'+str(i)])) |
|
94 | 94 | |
|
95 | 95 | if form_data['ues_down'+str(i)] == '': |
|
96 | 96 | list_uesdown.append(0.0) |
|
97 | 97 | else: |
|
98 | 98 | list_uesdown.append(float(form_data['ues_down'+str(i)])) |
|
99 | 99 | |
|
100 | 100 | onlyrx_list = form_data.getlist('onlyrx') |
|
101 | 101 | only_rx = {} |
|
102 | 102 | if '1' in onlyrx_list: |
|
103 | 103 | only_rx['up'] = True |
|
104 | 104 | else: |
|
105 | 105 | only_rx['up'] = False |
|
106 | 106 | if '2' in onlyrx_list: |
|
107 | 107 | only_rx['down'] = True |
|
108 | 108 | else: |
|
109 | 109 | only_rx['down'] = False |
|
110 | 110 | |
|
111 | 111 | antenna = {'antenna_up': up_values_list, 'antenna_down': down_values_list} |
|
112 | 112 | tx = {'up': up_txvalues_list, 'down': down_txvalues_list} |
|
113 | 113 | rx = {'up': up_rxvalues_list, 'down': down_rxvalues_list} |
|
114 | 114 | ues = {'up': list_uesup, 'down': list_uesdown} |
|
115 | 115 | name = str(form_data['beam_name']) |
|
116 | 116 | |
|
117 | 117 | beam_data = {'name': name, 'antenna': antenna, 'tx': tx, 'rx': rx, 'ues': ues, 'only_rx': only_rx} |
|
118 | 118 | |
|
119 | 119 | return beam_data |
|
120 | 120 | |
|
121 | 121 | |
|
122 | 122 | |
|
123 | 123 | def abs_conf(request, id_conf): |
|
124 | 124 | |
|
125 | 125 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
126 | 126 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
127 | 127 | active_beam_id = json.loads(conf.active_beam) |
|
128 | 128 | |
|
129 | 129 | #------------Colors for Active Beam:------------- |
|
130 | 130 | modules_status = json.loads(conf.module_status) |
|
131 | 131 | module_messages = json.loads(conf.module_messages) |
|
132 | 132 | |
|
133 | 133 | color_status = {} |
|
134 | 134 | for status in modules_status: |
|
135 | 135 | if modules_status[status] == 3: #Running background-color: #00cc00; |
|
136 | 136 | color_status[status] = 'class=text-success'#'bgcolor=#00cc00' |
|
137 | 137 | elif modules_status[status] == 1: #Connected background-color: #ee902c; |
|
138 | 138 | color_status[status] = 'class=text-warning'#'bgcolor=#ee902c' |
|
139 | 139 | else: #Disconnected background-color: #ff0000; |
|
140 | 140 | color_status[status] = 'class=text-danger'#'bgcolor=#FF0000' |
|
141 | 141 | #------------------------------------------------ |
|
142 | 142 | |
|
143 | 143 | kwargs = {} |
|
144 | 144 | #kwargs['status'] = conf.device.get_status_display() |
|
145 | 145 | kwargs['connected_modules'] = str(conf.connected_modules())+'/64' |
|
146 | 146 | |
|
147 | 147 | kwargs['dev_conf'] = conf |
|
148 | 148 | kwargs['dev_conf_keys'] = ['name',] |
|
149 | 149 | |
|
150 | 150 | kwargs['title'] = 'ABS Configuration' |
|
151 | 151 | kwargs['suptitle'] = 'Details' |
|
152 | kwargs['no_play'] = True | |
|
152 | #kwargs['no_play'] = True | |
|
153 | 153 | |
|
154 | 154 | kwargs['button'] = 'Edit Configuration' |
|
155 | 155 | #------------------Active Beam----------------------- |
|
156 | 156 | try: |
|
157 | 157 | active_beam_id = active_beam_id['active_beam'] |
|
158 | 158 | active_beam = ABSBeam.objects.get(pk=active_beam_id) |
|
159 | 159 | kwargs['active_beam'] = active_beam |
|
160 | 160 | for beam in beams: |
|
161 | 161 | if beam.id == active_beam.id: |
|
162 | 162 | beam.color_status = color_status |
|
163 | 163 | beam.module_messages = module_messages |
|
164 | 164 | except: |
|
165 | 165 | active_beam = '' |
|
166 | 166 | #---------------------------------------------------- |
|
167 | 167 | kwargs['beams'] = beams |
|
168 | 168 | kwargs['modules_status'] = modules_status |
|
169 | 169 | kwargs['color_status'] = color_status |
|
170 | 170 | kwargs['module_messages'] = module_messages |
|
171 | 171 | |
|
172 | kwargs['only_stop'] = True | |
|
172 | #kwargs['only_stop'] = True | |
|
173 | 173 | |
|
174 | 174 | ###### SIDEBAR ###### |
|
175 | 175 | kwargs.update(sidebar(conf=conf)) |
|
176 | 176 | |
|
177 | 177 | return render(request, 'abs_conf.html', kwargs) |
|
178 | 178 | |
|
179 | 179 | def abs_conf_edit(request, id_conf): |
|
180 | 180 | |
|
181 | 181 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
182 | 182 | |
|
183 | 183 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
184 | 184 | print beams |
|
185 | 185 | |
|
186 | 186 | if request.method=='GET': |
|
187 | 187 | form = ABSConfigurationForm(instance=conf) |
|
188 | 188 | |
|
189 | 189 | if request.method=='POST': |
|
190 | 190 | form = ABSConfigurationForm(request.POST, instance=conf) |
|
191 | 191 | |
|
192 | 192 | if form.is_valid(): |
|
193 | 193 | conf = form.save(commit=False) |
|
194 | 194 | conf.save() |
|
195 | 195 | return redirect('url_abs_conf', id_conf=conf.id) |
|
196 | 196 | |
|
197 | 197 | ###### SIDEBAR ###### |
|
198 | 198 | kwargs = {} |
|
199 | 199 | |
|
200 | 200 | kwargs['dev_conf'] = conf |
|
201 | 201 | #kwargs['id_dev'] = conf.id |
|
202 | 202 | kwargs['id_conf'] = conf.id |
|
203 | 203 | kwargs['form'] = form |
|
204 | 204 | kwargs['abs_beams'] = beams |
|
205 | 205 | kwargs['title'] = 'Device Configuration' |
|
206 | 206 | kwargs['suptitle'] = 'Edit' |
|
207 | 207 | kwargs['button'] = 'Save' |
|
208 | 208 | |
|
209 | 209 | kwargs['edit'] = True |
|
210 | 210 | |
|
211 | 211 | return render(request, 'abs_conf_edit.html', kwargs) |
|
212 | 212 | |
|
213 | 213 | |
|
214 | 214 | def import_file(request, id_conf): |
|
215 | 215 | |
|
216 | 216 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
217 | 217 | if request.method=='POST': |
|
218 | 218 | form = ABSImportForm(request.POST, request.FILES) |
|
219 | 219 | if form.is_valid(): |
|
220 | 220 | try: |
|
221 | 221 | parms = conf.import_from_file(request.FILES['file_name']) |
|
222 | 222 | |
|
223 | 223 | if parms: |
|
224 | 224 | conf.update_from_file(parms) |
|
225 | 225 | messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) |
|
226 | 226 | return redirect(conf.get_absolute_url_edit()) |
|
227 | 227 | |
|
228 | 228 | except Exception as e: |
|
229 | 229 | messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e)) |
|
230 | 230 | |
|
231 | 231 | else: |
|
232 | 232 | messages.warning(request, 'Your current configuration will be replaced') |
|
233 | 233 | form = ABSImportForm() |
|
234 | 234 | |
|
235 | 235 | kwargs = {} |
|
236 | 236 | kwargs['form'] = form |
|
237 | 237 | kwargs['title'] = 'ABS Configuration' |
|
238 | 238 | kwargs['suptitle'] = 'Import file' |
|
239 | 239 | kwargs['button'] = 'Upload' |
|
240 | 240 | kwargs['previous'] = conf.get_absolute_url() |
|
241 | 241 | |
|
242 | 242 | return render(request, 'abs_import.html', kwargs) |
|
243 | 243 | |
|
244 | 244 | |
|
245 | 245 | def send_beam(request, id_conf, id_beam): |
|
246 | 246 | |
|
247 | 247 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
248 | 248 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
249 | 249 | beams_list = ABSBeam.objects.filter(abs_conf=conf) |
|
250 | 250 | #To set this beam as an Active Beam |
|
251 | 251 | beam.set_as_activebeam() |
|
252 | 252 | #To send beam position to abs-modules |
|
253 | 253 | i = 0 |
|
254 | 254 | for b in beams_list: |
|
255 | 255 | if b.id == int(id_beam): |
|
256 | 256 | break |
|
257 | 257 | else: |
|
258 | 258 | i += 1 |
|
259 | 259 | beam_pos = i + 1 #Estandarizar |
|
260 | 260 | print 'Position: ',beam_pos |
|
261 | 261 | conf.send_beam_num(beam_pos) |
|
262 | 262 | |
|
263 | 263 | return redirect('url_abs_conf', conf.id) |
|
264 | 264 | |
|
265 | 265 | |
|
266 | 266 | def add_beam(request, id_conf): |
|
267 | 267 | |
|
268 | 268 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
269 | 269 | confs = Configuration.objects.all() |
|
270 | 270 | |
|
271 | 271 | if request.method=='GET': |
|
272 | 272 | #form = ABSBeamEditForm() |
|
273 | 273 | form = ABSBeamAddForm() |
|
274 | 274 | |
|
275 | 275 | if request.method=='POST': |
|
276 | 276 | form = ABSBeamAddForm(request.POST) |
|
277 | 277 | |
|
278 | 278 | beam_data = get_values_from_form(request.POST) |
|
279 | 279 | |
|
280 | 280 | new_beam = ABSBeam( |
|
281 | 281 | name =beam_data['name'], |
|
282 | 282 | antenna =json.dumps(beam_data['antenna']), |
|
283 | 283 | abs_conf=conf, |
|
284 | 284 | tx =json.dumps(beam_data['tx']), |
|
285 | 285 | rx =json.dumps(beam_data['rx']), |
|
286 | 286 | ues =json.dumps(beam_data['ues']), |
|
287 | 287 | only_rx =json.dumps(beam_data['only_rx']) |
|
288 | 288 | ) |
|
289 | 289 | new_beam.save() |
|
290 | 290 | #---Update 6bits configuration and add beam to abs configuration beams list. |
|
291 | 291 | new_beam.modules_6bits() |
|
292 | 292 | #new_beam.add_beam2list() |
|
293 | 293 | messages.success(request, 'Beam: "%s" has been added.' % new_beam.name) |
|
294 | 294 | |
|
295 | 295 | return redirect('url_edit_abs_conf', conf.id) |
|
296 | 296 | |
|
297 | 297 | ###### SIDEBAR ###### |
|
298 | 298 | kwargs = {} |
|
299 | 299 | |
|
300 | 300 | #kwargs['dev_conf'] = conf.device |
|
301 | 301 | #kwargs['id_dev'] = conf.device |
|
302 | 302 | kwargs['id_conf'] = conf.id |
|
303 | 303 | kwargs['form'] = form |
|
304 | 304 | kwargs['title'] = 'ABS Beams' |
|
305 | 305 | kwargs['suptitle'] = 'Add Beam' |
|
306 | 306 | kwargs['button'] = 'Add' |
|
307 | 307 | kwargs['no_sidebar'] = True |
|
308 | 308 | |
|
309 | 309 | #kwargs['previous'] = conf.get_absolute_url_edit() |
|
310 | 310 | kwargs['edit'] = True |
|
311 | 311 | |
|
312 | 312 | return render(request, 'abs_add_beam.html', kwargs) |
|
313 | 313 | |
|
314 | 314 | |
|
315 | 315 | def edit_beam(request, id_conf, id_beam): |
|
316 | 316 | |
|
317 | 317 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
318 | 318 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
319 | 319 | |
|
320 | 320 | if request.method=='GET': |
|
321 | 321 | form = ABSBeamEditForm(initial={'beam': beam}) |
|
322 | 322 | |
|
323 | 323 | if request.method=='POST': |
|
324 | 324 | form = ABSBeamEditForm(request.POST) |
|
325 | 325 | |
|
326 | 326 | beam_data = get_values_from_form(request.POST) |
|
327 | 327 | |
|
328 | 328 | beam.dict_to_parms(beam_data) |
|
329 | 329 | beam.save() |
|
330 | 330 | |
|
331 | 331 | messages.success(request, 'Beam: "%s" has been updated.' % beam.name) |
|
332 | 332 | |
|
333 | 333 | return redirect('url_edit_abs_conf', conf.id) |
|
334 | 334 | |
|
335 | 335 | ###### SIDEBAR ###### |
|
336 | 336 | kwargs = {} |
|
337 | 337 | |
|
338 | 338 | kwargs['id_conf'] = conf.id |
|
339 | 339 | kwargs['form'] = form |
|
340 | 340 | kwargs['title'] = 'ABS Beams' |
|
341 | 341 | kwargs['suptitle'] = 'Edit Beam' |
|
342 | 342 | kwargs['button'] = 'Save' |
|
343 | 343 | kwargs['no_sidebar'] = True |
|
344 | 344 | |
|
345 | 345 | #kwargs['previous'] = conf.get_absolute_url_edit() |
|
346 | 346 | kwargs['edit'] = True |
|
347 | 347 | |
|
348 | 348 | return render(request, 'abs_edit_beam.html', kwargs) |
|
349 | 349 | |
|
350 | 350 | |
|
351 | 351 | |
|
352 | 352 | def remove_beam(request, id_conf, id_beam): |
|
353 | 353 | |
|
354 | 354 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
355 | 355 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
356 | 356 | |
|
357 | 357 | if request.method=='POST': |
|
358 | 358 | if beam: |
|
359 | 359 | try: |
|
360 | 360 | beam.remove_beamfromlist() |
|
361 | 361 | beam.delete() |
|
362 | 362 | messages.success(request, 'Beam: "%s" has been deleted.' % beam) |
|
363 | 363 | except: |
|
364 | 364 | messages.error(request, 'Unable to delete beam: "%s".' % beam) |
|
365 | 365 | |
|
366 | 366 | return redirect('url_edit_abs_conf', conf.id) |
|
367 | 367 | |
|
368 | 368 | ###### SIDEBAR ###### |
|
369 | 369 | kwargs = {} |
|
370 | 370 | |
|
371 | 371 | kwargs['object'] = beam |
|
372 | 372 | kwargs['delete'] = True |
|
373 | 373 | kwargs['title'] = 'Delete' |
|
374 | 374 | kwargs['suptitle'] = 'Beam' |
|
375 | 375 | kwargs['previous'] = conf.get_absolute_url_edit() |
|
376 | 376 | return render(request, 'confirm.html', kwargs) |
|
377 | 377 | |
|
378 | 378 | |
|
379 | 379 | |
|
380 | 380 | def plot_patterns(request, id_conf, id_beam=None): |
|
381 | 381 | |
|
382 | 382 | kwargs = {} |
|
383 | 383 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
384 | 384 | beams = ABSBeam.objects.filter(abs_conf=conf) |
|
385 | 385 | |
|
386 | 386 | if id_beam: |
|
387 | 387 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
388 | 388 | kwargs['beam'] = beam |
|
389 | 389 | |
|
390 | 390 | |
|
391 | 391 | ###### SIDEBAR ###### |
|
392 | 392 | |
|
393 | 393 | kwargs['dev_conf'] = conf.device |
|
394 | 394 | kwargs['id_dev'] = conf.device |
|
395 | 395 | kwargs['id_conf'] = conf.id |
|
396 | 396 | kwargs['abs_beams'] = beams |
|
397 | 397 | kwargs['title'] = 'ABS Patterns' |
|
398 | 398 | kwargs['suptitle'] = conf.name |
|
399 | 399 | kwargs['no_sidebar'] = True |
|
400 | 400 | |
|
401 | 401 | return render(request, 'abs_patterns.html', kwargs) |
|
402 | 402 | |
|
403 | 403 | |
|
404 | 404 | def plot_pattern(request, id_conf, id_beam, antenna): |
|
405 | 405 | |
|
406 | 406 | if antenna=='down': |
|
407 | 407 | sleep(3) |
|
408 | 408 | |
|
409 | 409 | conf = get_object_or_404(ABSConfiguration, pk=id_conf) |
|
410 | 410 | beam = get_object_or_404(ABSBeam, pk=id_beam) |
|
411 | 411 | |
|
412 | 412 | name = conf.experiment.name |
|
413 | 413 | |
|
414 | 414 | just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0 |
|
415 | 415 | phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)] |
|
416 | 416 | gain_tx = json.loads(beam.tx)[antenna] |
|
417 | 417 | gain_rx = json.loads(beam.rx)[antenna] |
|
418 | 418 | ues = json.loads(beam.ues)[antenna] |
|
419 | 419 | |
|
420 | 420 | newOverJro = overJroShow(name) |
|
421 | 421 | fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx) |
|
422 | 422 | |
|
423 | 423 | response=HttpResponse(content_type='image/png') |
|
424 | 424 | |
|
425 | 425 | fig.canvas.print_png(response) |
|
426 | 426 | |
|
427 | 427 | return response |
General Comments 0
You need to be logged in to leave comments.
Login now