This diff has been collapsed as it changes many lines, (870 lines changed) Show them Hide them | |||||
@@ -1,10 +1,878 | |||||
1 | from django.db import models |
|
1 | from django.db import models | |
2 | from apps.main.models import Configuration |
|
2 | from apps.main.models import Configuration | |
|
3 | from django.core.urlresolvers import reverse | |||
3 | # Create your models here. |
|
4 | # Create your models here. | |
4 |
|
5 | |||
5 | class ABSConfiguration(Configuration): |
|
6 | import ast | |
|
7 | import socket | |||
|
8 | import json | |||
|
9 | import requests | |||
|
10 | import struct | |||
|
11 | import sys, time | |||
|
12 | ||||
|
13 | import multiprocessing | |||
|
14 | ||||
|
15 | ||||
|
16 | antenna_default = json.dumps({ | |||
|
17 | "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
18 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
19 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
20 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
21 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], | |||
|
22 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], | |||
|
23 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], | |||
|
24 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0] | |||
|
25 | ] | |||
|
26 | , | |||
|
27 | "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
28 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
29 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
30 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], | |||
|
31 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], | |||
|
32 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], | |||
|
33 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], | |||
|
34 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]], | |||
|
35 | }) | |||
|
36 | ||||
|
37 | ||||
|
38 | tx_default = json.dumps({ | |||
|
39 | "up": [[1,1,1,1,0,0,0,0], | |||
|
40 | [1,1,1,1,0,0,0,0], | |||
|
41 | [1,1,1,1,0,0,0,0], | |||
|
42 | [1,1,1,1,0,0,0,0], | |||
|
43 | [0,0,0,0,1,1,1,1], | |||
|
44 | [0,0,0,0,1,1,1,1], | |||
|
45 | [0,0,0,0,1,1,1,1], | |||
|
46 | [0,0,0,0,1,1,1,1]], | |||
|
47 | ||||
|
48 | "down": [[1,1,1,1,0,0,0,0], | |||
|
49 | [1,1,1,1,0,0,0,0], | |||
|
50 | [1,1,1,1,0,0,0,0], | |||
|
51 | [1,1,1,1,0,0,0,0], | |||
|
52 | [0,0,0,0,1,1,1,1], | |||
|
53 | [0,0,0,0,1,1,1,1], | |||
|
54 | [0,0,0,0,1,1,1,1], | |||
|
55 | [0,0,0,0,1,1,1,1]], | |||
|
56 | }) | |||
|
57 | ||||
|
58 | rx_default = json.dumps({ | |||
|
59 | "up": [[1,1,1,1,0,0,0,0], | |||
|
60 | [1,1,1,1,0,0,0,0], | |||
|
61 | [1,1,1,1,0,0,0,0], | |||
|
62 | [1,1,1,1,0,0,0,0], | |||
|
63 | [0,0,0,0,1,1,1,1], | |||
|
64 | [0,0,0,0,1,1,1,1], | |||
|
65 | [0,0,0,0,1,1,1,1], | |||
|
66 | [0,0,0,0,1,1,1,1]], | |||
|
67 | ||||
|
68 | "down": [[1,1,1,1,0,0,0,0], | |||
|
69 | [1,1,1,1,0,0,0,0], | |||
|
70 | [1,1,1,1,0,0,0,0], | |||
|
71 | [1,1,1,1,0,0,0,0], | |||
|
72 | [0,0,0,0,1,1,1,1], | |||
|
73 | [0,0,0,0,1,1,1,1], | |||
|
74 | [0,0,0,0,1,1,1,1], | |||
|
75 | [0,0,0,0,1,1,1,1]], | |||
|
76 | }) | |||
|
77 | ||||
|
78 | conf_default = {} | |||
|
79 | status_default = {} | |||
|
80 | for i in range(1,65): | |||
|
81 | conf_default[str(i)] = "" | |||
|
82 | status_default[str(i)] = 0 | |||
|
83 | ||||
|
84 | ues_default = json.dumps({ | |||
|
85 | "up": [0.533333,0.00000,1.06667,0.00000], | |||
|
86 | "down": [0.533333,0.00000,1.06667,0.00000] | |||
|
87 | }) | |||
|
88 | ||||
|
89 | onlyrx_default = json.dumps({ | |||
|
90 | "up": False, | |||
|
91 | "down": False | |||
|
92 | }) | |||
|
93 | ||||
|
94 | def up_convertion(cadena): | |||
|
95 | valores = [] | |||
|
96 | for c in cadena: | |||
|
97 | if c == 1.0: valores=valores+['000'] | |||
|
98 | if c == 2.0: valores=valores+['001'] | |||
|
99 | if c == 3.0: valores=valores+['010'] | |||
|
100 | if c == 0.0: valores=valores+['011'] | |||
|
101 | if c == 0.5: valores=valores+['100'] | |||
|
102 | if c == 1.5: valores=valores+['101'] | |||
|
103 | if c == 2.5: valores=valores+['110'] | |||
|
104 | if c == 3.5: valores=valores+['111'] | |||
|
105 | ||||
|
106 | return valores | |||
|
107 | ||||
|
108 | def up_conv_bits(value): | |||
|
109 | ||||
|
110 | if value == 1.0: bits="000" | |||
|
111 | if value == 2.0: bits="001" | |||
|
112 | if value == 3.0: bits="010" | |||
|
113 | if value == 0.0: bits="011" | |||
|
114 | if value == 0.5: bits="100" | |||
|
115 | if value == 1.5: bits="101" | |||
|
116 | if value == 2.5: bits="110" | |||
|
117 | if value == 3.5: bits="111" | |||
|
118 | ||||
|
119 | return bits | |||
|
120 | ||||
|
121 | def down_convertion(cadena): | |||
|
122 | valores = [] | |||
|
123 | for c in cadena: | |||
|
124 | if c == 1.0: valores=valores+['000'] | |||
|
125 | if c == 2.0: valores=valores+['001'] | |||
|
126 | if c == 3.0: valores=valores+['010'] | |||
|
127 | if c == 0.0: valores=valores+['011'] | |||
|
128 | if c == 0.5: valores=valores+['100'] | |||
|
129 | if c == 1.5: valores=valores+['101'] | |||
|
130 | if c == 2.5: valores=valores+['110'] | |||
|
131 | if c == 3.5: valores=valores+['111'] | |||
|
132 | ||||
|
133 | return valores | |||
|
134 | ||||
|
135 | def down_conv_bits(value): | |||
|
136 | ||||
|
137 | if value == 1.0: bits="000" | |||
|
138 | if value == 2.0: bits="001" | |||
|
139 | if value == 3.0: bits="010" | |||
|
140 | if value == 0.0: bits="011" | |||
|
141 | if value == 0.5: bits="100" | |||
|
142 | if value == 1.5: bits="101" | |||
|
143 | if value == 2.5: bits="110" | |||
|
144 | if value == 3.5: bits="111" | |||
|
145 | ||||
|
146 | return bits | |||
|
147 | ||||
|
148 | def ip2position(module_number): | |||
|
149 | j=0 | |||
|
150 | i=0 | |||
|
151 | for x in range(0,module_number-1): | |||
|
152 | j=j+1 | |||
|
153 | if j==8: | |||
|
154 | i=i+1 | |||
|
155 | j=0 | |||
|
156 | ||||
|
157 | pos = [i,j] | |||
|
158 | return pos | |||
|
159 | ||||
|
160 | ||||
|
161 | def change_beam_for_multiprocessing(module): | |||
6 |
|
162 | |||
|
163 | for i in range (1,50): | |||
|
164 | beam_pos = 0 | |||
|
165 | module_address = ('192.168.1.'+str(module), 5500) | |||
|
166 | header = 'JROABSCeCnModCnMod0100000' | |||
|
167 | numbers = len(str(beam_pos)) | |||
|
168 | function = 'CHGB' | |||
|
169 | ||||
|
170 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
171 | ||||
|
172 | # Create the datagram socket | |||
|
173 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
174 | sock.connect(module_address) | |||
|
175 | ||||
|
176 | sock.send(message_tx) | |||
|
177 | #t = sock.recv(1024) | |||
|
178 | sock.close() | |||
|
179 | sock = None | |||
|
180 | ||||
|
181 | ||||
|
182 | time.sleep(0.2) | |||
|
183 | ||||
|
184 | ||||
|
185 | beam_pos = 1 | |||
|
186 | numbers = len(str(beam_pos)) | |||
|
187 | ||||
|
188 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
189 | ||||
|
190 | # Create the datagram socket | |||
|
191 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
192 | sock.connect(module_address) | |||
|
193 | sock.send(message_tx) | |||
|
194 | sock.close() | |||
|
195 | sock = None | |||
|
196 | ||||
|
197 | time.sleep(0.2) | |||
|
198 | ||||
|
199 | ||||
|
200 | ||||
|
201 | class ABSConfiguration(Configuration): | |||
|
202 | beams = models.CharField(verbose_name='Beams', max_length=20000, default="{}") | |||
|
203 | module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default)) | |||
7 |
|
204 | |||
8 | class Meta: |
|
205 | class Meta: | |
9 | db_table = 'abs_configurations' |
|
206 | db_table = 'abs_configurations' | |
10 |
|
207 | |||
|
208 | def get_absolute_url_plot(self): | |||
|
209 | return reverse('url_plot_abs_patterns', args=[str(self.id)]) | |||
|
210 | ||||
|
211 | ||||
|
212 | def parms_to_dict(self): | |||
|
213 | ||||
|
214 | parameters = {} | |||
|
215 | ||||
|
216 | parameters['device_id'] = self.device.id | |||
|
217 | parameters['name'] = self.name | |||
|
218 | parameters['beams'] = {} | |||
|
219 | ||||
|
220 | beams = ast.literal_eval(self.beams) | |||
|
221 | b=1 | |||
|
222 | for beam in beams: | |||
|
223 | absbeam = ABSBeam.objects.get(pk=beams[beam]) | |||
|
224 | parameters['beams']['beam'+str(b)] = absbeam.parms_to_dict() | |||
|
225 | b+=1 | |||
|
226 | ||||
|
227 | return parameters | |||
|
228 | ||||
|
229 | ||||
|
230 | def module_conf(self, module_num, beams): | |||
|
231 | """ | |||
|
232 | This function creates beam configurations for one abs module. | |||
|
233 | """ | |||
|
234 | ip_address = self.device.ip_address | |||
|
235 | ip_address = ip_address.split('.') | |||
|
236 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) | |||
|
237 | dot = '.' | |||
|
238 | module_ip = dot.join(module_seq)+'.'+str(module_num) | |||
|
239 | module_port = self.device.port_address | |||
|
240 | write_route = 'http://'+module_ip+':'+str(module_port)+'/configure' | |||
|
241 | ||||
|
242 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' | |||
|
243 | module = 'ABS_'+str(module_num) | |||
|
244 | bs = {} | |||
|
245 | i=1 | |||
|
246 | #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', | |||
|
247 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} | |||
|
248 | for beam in beams: | |||
|
249 | bs[i] = beam.module_6bits(module_num) | |||
|
250 | i=i+1 | |||
|
251 | ||||
|
252 | beams = bs | |||
|
253 | ||||
|
254 | parameters = {} | |||
|
255 | parameters['header'] = header | |||
|
256 | parameters['module'] = module | |||
|
257 | parameters['beams'] = json.dumps(beams) | |||
|
258 | ||||
|
259 | answer = '' | |||
|
260 | ||||
|
261 | try: | |||
|
262 | r_write = requests.post(write_route, parameters, timeout=0.5) | |||
|
263 | answer = r_write.json() | |||
|
264 | self.message = answer['message'] | |||
|
265 | except: | |||
|
266 | self.message = "Could not write ABS parameters" | |||
|
267 | return 0 | |||
|
268 | return 1 | |||
|
269 | ||||
|
270 | ||||
|
271 | def read_module(self, module): | |||
|
272 | ||||
|
273 | """ | |||
|
274 | Read out-bits (up-down) of 1 abs module NOT for Configuration | |||
|
275 | """ | |||
|
276 | ||||
|
277 | parameters = {} | |||
|
278 | ip_address = self.device.ip_address | |||
|
279 | ip_address = ip_address.split('.') | |||
|
280 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) | |||
|
281 | dot = '.' | |||
|
282 | module_ip = dot.join(module_seq)+'.'+str(module) | |||
|
283 | module_port = self.device.port_address | |||
|
284 | read_route = 'http://'+module_ip+':'+str(module_port)+'/read' | |||
|
285 | ||||
|
286 | print read_route | |||
|
287 | ||||
|
288 | answer = '' | |||
|
289 | module_bits = '' | |||
|
290 | ||||
|
291 | try: | |||
|
292 | r_write = requests.get(read_route, timeout=0.7) | |||
|
293 | answer = r_write.json() | |||
|
294 | message = answer['message'] | |||
|
295 | module_bits = answer['allbits'] | |||
|
296 | except: | |||
|
297 | message = "Could not read ABS parameters" | |||
|
298 | return 0 | |||
|
299 | ||||
|
300 | return module_bits | |||
|
301 | ||||
|
302 | ||||
|
303 | def write_device(self): | |||
|
304 | """ | |||
|
305 | This function sends the beams list to every abs module. | |||
|
306 | """ | |||
|
307 | ||||
|
308 | beams_list = ast.literal_eval(self.beams) | |||
|
309 | beams = [] | |||
|
310 | ||||
|
311 | for bl in range(1,len(beams_list)+1): | |||
|
312 | b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) | |||
|
313 | beams.append(b) | |||
|
314 | ||||
|
315 | #---Write each abs module--- | |||
|
316 | beams_status = ast.literal_eval(self.module_status) | |||
|
317 | for i in range(62,65): | |||
|
318 | try: | |||
|
319 | self.module_conf(i, beams) | |||
|
320 | beams_status[str(i)] = 1 | |||
|
321 | self.module_status = json.dumps(beams_status) | |||
|
322 | self.save() | |||
|
323 | #self.module_conf(63,beams) | |||
|
324 | #beams_status[str(63)] = 1 | |||
|
325 | #self.module_status = json.dumps(beams_status) | |||
|
326 | except: | |||
|
327 | beams_status[str(i)] = 0 | |||
|
328 | self.module_status = json.dumps(beams_status) | |||
|
329 | self.save() | |||
|
330 | #return 0 | |||
|
331 | ||||
|
332 | #self.device.status = 1 | |||
|
333 | self.save() | |||
|
334 | return 1 | |||
|
335 | ||||
|
336 | ||||
|
337 | def write_module(self, module): | |||
|
338 | ||||
|
339 | """ | |||
|
340 | Send configuration to one abs module | |||
|
341 | """ | |||
|
342 | ||||
|
343 | parameters = {} | |||
|
344 | ip_address = self.abs_conf.device.ip_address | |||
|
345 | ip_address = ip_address.split('.') | |||
|
346 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) | |||
|
347 | dot = '.' | |||
|
348 | module_ip = dot.join(module_seq)+'.'+str(module) | |||
|
349 | module_port = self.abs_conf.device.port_address | |||
|
350 | write_route = 'http://'+module_ip+':'+str(module_port)+'/configure' | |||
|
351 | ||||
|
352 | #print write_route | |||
|
353 | ||||
|
354 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' | |||
|
355 | module = 'ABS_'+str(module) | |||
|
356 | beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', | |||
|
357 | 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} | |||
|
358 | ||||
|
359 | parameters['header'] = header | |||
|
360 | parameters['module'] = module | |||
|
361 | parameters['beams'] = json.dumps(beams) | |||
|
362 | ||||
|
363 | answer = '' | |||
|
364 | ||||
|
365 | try: | |||
|
366 | r_write = requests.post(write_route, parameters, timeout=0.5) | |||
|
367 | answer = r_write.json() | |||
|
368 | self.message = answer['message'] | |||
|
369 | except: | |||
|
370 | self.message = "Could not write ABS parameters" | |||
|
371 | return 0 | |||
|
372 | ||||
|
373 | ||||
|
374 | #self.device.status = int(answer['status']) | |||
|
375 | ||||
|
376 | return 1 | |||
|
377 | ||||
|
378 | ||||
|
379 | def beam_selector(self, module, beam_pos): | |||
|
380 | """ | |||
|
381 | This function selects the beam number for one absmodule. | |||
|
382 | """ | |||
|
383 | ||||
|
384 | if beam_pos > 0: | |||
|
385 | beam_pos = beam_pos - 1 | |||
|
386 | else: | |||
|
387 | beam_pos = 0 | |||
|
388 | ||||
|
389 | #El indice del apunte debe ser menor que el numero total de apuntes | |||
|
390 | #El servidor tcp en el embebido comienza a contar desde 0 | |||
|
391 | beams_list = ast.literal_eval(self.beams) | |||
|
392 | if len(beams_list) < beam_pos: | |||
|
393 | return 0 | |||
|
394 | ||||
|
395 | flag = 1 | |||
|
396 | if beam_pos>9: | |||
|
397 | flag = 2 | |||
|
398 | ||||
|
399 | module_address = ('192.168.1.'+str(module), 5500) | |||
|
400 | header = 'JROABSCeCnModCnMod0100000' | |||
|
401 | numbers = len(str(beam_pos)) | |||
|
402 | function = 'CHGB' | |||
|
403 | ||||
|
404 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
405 | ||||
|
406 | # Create the datagram socket | |||
|
407 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
408 | #sock.connect(module_address) | |||
|
409 | try: | |||
|
410 | sock.connect(module_address) | |||
|
411 | sock.send(message_tx) | |||
|
412 | sock.close() | |||
|
413 | print "Writing abs module:"+module_address[0]+"..." | |||
|
414 | except: | |||
|
415 | sock = None | |||
|
416 | print "Problem writing abs module:"+module_address[0] | |||
|
417 | return 0 | |||
|
418 | ||||
|
419 | return 1 | |||
|
420 | ||||
|
421 | ||||
|
422 | def change_beam(self, beam_pos): | |||
|
423 | """ | |||
|
424 | This function selects the beam number for all absmodules. | |||
|
425 | """ | |||
|
426 | for i in range(1,65): | |||
|
427 | try: | |||
|
428 | self.beam_selector(i,beam_pos) | |||
|
429 | except: | |||
|
430 | print "Problem with module: 192.168.1."+str(i) | |||
|
431 | self.message = "Problem with module: 192.168.1."+str(i) | |||
|
432 | #return 0 | |||
|
433 | return 1 | |||
|
434 | ||||
|
435 | ||||
|
436 | def send_beam_num(self, beam_pos): | |||
|
437 | """ | |||
|
438 | This function connects to a multicast group and sends the beam number | |||
|
439 | to all abs modules. | |||
|
440 | """ | |||
|
441 | ||||
|
442 | if beam_pos > 0: | |||
|
443 | beam_pos = beam_pos - 1 | |||
|
444 | else: | |||
|
445 | beam_pos = 0 | |||
|
446 | ||||
|
447 | #El indice del apunte debe ser menor que el numero total de apuntes | |||
|
448 | #El servidor tcp en el embebido comienza a contar desde 0 | |||
|
449 | beams_list = ast.literal_eval(self.beams) | |||
|
450 | if len(beams_list) < beam_pos: | |||
|
451 | return 0 | |||
|
452 | ||||
|
453 | flag = 1 | |||
|
454 | if beam_pos>9: | |||
|
455 | flag = 2 | |||
|
456 | ||||
|
457 | header = 'JROABSCeCnModCnMod0100000' | |||
|
458 | flag = str(flag) | |||
|
459 | function = 'CHGB' | |||
|
460 | message_tx = header+flag+function+str(beam_pos)+'0' | |||
|
461 | ||||
|
462 | multicast_group = '224.3.29.71' | |||
|
463 | server_address = ('',10000) | |||
|
464 | ||||
|
465 | # Create the socket | |||
|
466 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |||
|
467 | # Bind to the server address | |||
|
468 | sock.bind(server_address) | |||
|
469 | # Telling the OS add the socket to the multicast on all interfaces | |||
|
470 | group = socket.inet_aton(multicast_group) | |||
|
471 | mreq = struct.pack('4sL', group, socket.INADDR_ANY) | |||
|
472 | sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) | |||
|
473 | ||||
|
474 | #print 'sending acknowledgement to all: \n' + message_tx | |||
|
475 | sock.sendto(message_tx, (multicast_group, 10000)) | |||
|
476 | sock.close() | |||
|
477 | sock = None | |||
|
478 | ||||
|
479 | return 1 | |||
|
480 | ||||
|
481 | def test1(self): | |||
|
482 | t1 = time.time() | |||
|
483 | t2 = 0 | |||
|
484 | while (t2-t1)<300: | |||
|
485 | t2 = time.time() | |||
|
486 | self.send_beam_num(1) | |||
|
487 | time.sleep(0.04) | |||
|
488 | self.send_beam_num(2) | |||
|
489 | time.sleep(0.04) | |||
|
490 | return | |||
|
491 | ||||
|
492 | def change_procs_test1(self, module): | |||
|
493 | ||||
|
494 | for i in range (1,300): | |||
|
495 | beam_pos = 0 | |||
|
496 | module_address = ('192.168.1.'+str(module), 5500) | |||
|
497 | header = 'JROABSCeCnModCnMod0100000' | |||
|
498 | numbers = len(str(beam_pos)) | |||
|
499 | function = 'CHGB' | |||
|
500 | ||||
|
501 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
502 | ||||
|
503 | # Create the datagram socket | |||
|
504 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
505 | sock.connect(module_address) | |||
|
506 | ||||
|
507 | sock.send(message_tx) | |||
|
508 | #t = sock.recv(1024) | |||
|
509 | sock.close() | |||
|
510 | sock = None | |||
|
511 | ||||
|
512 | ||||
|
513 | time.sleep(0.2) | |||
|
514 | ||||
|
515 | ||||
|
516 | beam_pos = 1 | |||
|
517 | numbers = len(str(beam_pos)) | |||
|
518 | ||||
|
519 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
520 | ||||
|
521 | # Create the datagram socket | |||
|
522 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
523 | sock.connect(module_address) | |||
|
524 | sock.send(message_tx) | |||
|
525 | sock.close() | |||
|
526 | sock = None | |||
|
527 | ||||
|
528 | time.sleep(0.2) | |||
|
529 | ||||
|
530 | ||||
|
531 | def multi_procs_test1(self): | |||
|
532 | ||||
|
533 | #if __name__ == "__main__": | |||
|
534 | size = 10000000 # Number of random numbers to add | |||
|
535 | procs = 65 # (Number-1) of processes to create | |||
|
536 | ||||
|
537 | # Create a list of jobs and then iterate through | |||
|
538 | # the number of processes appending each process to | |||
|
539 | # the job list | |||
|
540 | jobs = [] | |||
|
541 | for i in range(62, procs): | |||
|
542 | ||||
|
543 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) | |||
|
544 | jobs.append(process) | |||
|
545 | #print jobs | |||
|
546 | ||||
|
547 | # Start the processes (i.e. calculate the random number lists) | |||
|
548 | for j in jobs: | |||
|
549 | #time.sleep(0.4) | |||
|
550 | #print j | |||
|
551 | j.start() | |||
|
552 | ||||
|
553 | # Ensure all of the processes have finished | |||
|
554 | for j in jobs: | |||
|
555 | j.join() | |||
|
556 | ||||
|
557 | print "List processing complete." | |||
|
558 | return 1 | |||
|
559 | ||||
|
560 | ||||
|
561 | def status_device(self): | |||
|
562 | ||||
|
563 | return 1 | |||
|
564 | ||||
|
565 | ||||
|
566 | ||||
|
567 | class ABSBeam(models.Model): | |||
|
568 | ||||
|
569 | name = models.CharField(max_length=60, default='Beam') | |||
|
570 | antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default) | |||
|
571 | abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration') | |||
|
572 | tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default) | |||
|
573 | rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default) | |||
|
574 | s_time = models.TimeField(verbose_name='Star Time', default='00:00:00') | |||
|
575 | e_time = models.TimeField(verbose_name='End Time', default='23:59:59') | |||
|
576 | modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default)) | |||
|
577 | ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default) | |||
|
578 | only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default) | |||
|
579 | ||||
|
580 | class Meta: | |||
|
581 | db_table = 'abs_beams' | |||
|
582 | ||||
|
583 | def __unicode__(self): | |||
|
584 | return u'%s' % (self.name) | |||
|
585 | ||||
|
586 | def parms_to_dict(self): | |||
|
587 | ||||
|
588 | #Update data | |||
|
589 | self.modules_6bits() | |||
|
590 | ||||
|
591 | parameters = {} | |||
|
592 | ||||
|
593 | parameters['name'] = self.name | |||
|
594 | parameters['antenna'] = ast.literal_eval(self.antenna) | |||
|
595 | parameters['abs_conf'] = self.abs_conf.name | |||
|
596 | parameters['tx'] = ast.literal_eval(self.tx) | |||
|
597 | parameters['rx'] = ast.literal_eval(self.rx) | |||
|
598 | parameters['s_time'] = self.s_time.strftime("%H:%M:%S") | |||
|
599 | parameters['e_time'] = self.e_time.strftime("%H:%M:%S") | |||
|
600 | parameters['configuration'] = ast.literal_eval(self.modules_conf) | |||
|
601 | parameters['ues'] = ast.literal_eval(self.ues) | |||
|
602 | parameters['only_rx'] = json.loads(self.only_rx) | |||
|
603 | ||||
|
604 | return parameters | |||
|
605 | ||||
|
606 | def dict_to_parms(self, parameters): | |||
|
607 | ||||
|
608 | self.name = parameters['name'] | |||
|
609 | self.antenna = json.dumps(parameters['antenna']) | |||
|
610 | #self.abs_conf = parameters['abs_conf'] | |||
|
611 | self.tx = json.dumps(parameters['tx']) | |||
|
612 | self.rx = json.dumps(parameters['rx']) | |||
|
613 | #parameters['s_time'] | |||
|
614 | #parameters['e_time'] | |||
|
615 | self.ues = json.dumps(parameters['ues']) | |||
|
616 | self.only_rx = json.dumps(parameters['only_rx']) | |||
|
617 | ||||
|
618 | self.modules_6bits() | |||
|
619 | self.save() | |||
|
620 | ||||
|
621 | return parameters | |||
|
622 | ||||
|
623 | ||||
|
624 | def change_beam(self, beam_pos=0): | |||
|
625 | ||||
|
626 | module_63 = ('192.168.1.63', 5500) | |||
|
627 | header = 'JROABSCeCnModCnMod0100000' | |||
|
628 | numbers = len(str(beam_pos)) | |||
|
629 | function = 'CHGB' | |||
|
630 | ||||
|
631 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
632 | ||||
|
633 | # Create the datagram socket | |||
|
634 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
635 | sock.connect(module_63) | |||
|
636 | sock.send(message_tx) | |||
|
637 | sock.close() | |||
|
638 | return message_tx | |||
|
639 | ||||
|
640 | def change_module_beam(self, module=61,beam_pos=0): | |||
|
641 | ||||
|
642 | module_address = ('192.168.1.'+str(module), 5500) | |||
|
643 | header = 'JROABSCeCnModCnMod0100000' | |||
|
644 | numbers = len(str(beam_pos)) | |||
|
645 | function = 'CHGB' | |||
|
646 | ||||
|
647 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' | |||
|
648 | ||||
|
649 | # Create the datagram socket | |||
|
650 | try: | |||
|
651 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |||
|
652 | sock.connect(module_address) | |||
|
653 | sock.send(message_tx) | |||
|
654 | sock.close() | |||
|
655 | except: | |||
|
656 | return 0 | |||
|
657 | return message_tx | |||
|
658 | ||||
|
659 | def write_device(self): | |||
|
660 | ||||
|
661 | parameters = {} | |||
|
662 | ||||
|
663 | module_ip = '192.168.1.63' | |||
|
664 | write_route = 'http://192.168.1.63:8080/configure' | |||
|
665 | ||||
|
666 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' | |||
|
667 | module = 'ABS_63' | |||
|
668 | beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', | |||
|
669 | 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} | |||
|
670 | ||||
|
671 | parameters['header'] = header | |||
|
672 | parameters['module'] = module | |||
|
673 | parameters['beams'] = json.dumps(beams) | |||
|
674 | ||||
|
675 | answer = '' | |||
|
676 | ||||
|
677 | try: | |||
|
678 | r_write = requests.post(write_route, parameters, timeout=0.5) | |||
|
679 | answer = r_write.json() | |||
|
680 | self.message = answer['message'] | |||
|
681 | except: | |||
|
682 | self.message = "Could not write ABS parameters" | |||
|
683 | return 0 | |||
|
684 | ||||
|
685 | ||||
|
686 | #self.device.status = int(answer['status']) | |||
|
687 | ||||
|
688 | return 1 | |||
|
689 | ||||
|
690 | ||||
|
691 | ||||
|
692 | def module_6bits(self, module): | |||
|
693 | """ | |||
|
694 | This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module | |||
|
695 | """ | |||
|
696 | if module > 64: | |||
|
697 | beam_bits = "" | |||
|
698 | return beam_bits | |||
|
699 | ||||
|
700 | data = ast.literal_eval(self.antenna) | |||
|
701 | up_data = data['antenna_up'] | |||
|
702 | down_data = data['antenna_down'] | |||
|
703 | ||||
|
704 | pos = ip2position(module) | |||
|
705 | up_value = up_data[pos[0]][pos[1]] | |||
|
706 | down_value = down_data[pos[0]][pos[1]] | |||
|
707 | ||||
|
708 | up_bits = up_conv_bits(up_value) | |||
|
709 | down_bits = down_conv_bits(down_value) | |||
|
710 | beam_bits = up_bits+down_bits | |||
|
711 | ||||
|
712 | return beam_bits | |||
|
713 | ||||
|
714 | def modules_6bits(self): | |||
|
715 | """ | |||
|
716 | This function returns 6bits from every abs module (1-64) in a dict | |||
|
717 | """ | |||
|
718 | modules_configuration = ast.literal_eval(self.modules_conf) | |||
|
719 | ||||
|
720 | for i in range(1,65): | |||
|
721 | modules_configuration[str(i)] = self.module_6bits(i) | |||
|
722 | ||||
|
723 | self.modules_conf = json.dumps(modules_configuration) | |||
|
724 | self.save() | |||
|
725 | ||||
|
726 | return self.modules_conf | |||
|
727 | ||||
|
728 | def add_beam2list(self): | |||
|
729 | """ | |||
|
730 | This function adds a beam to the beams list of ABS Configuration. | |||
|
731 | """ | |||
|
732 | beams = ast.literal_eval(self.abs_conf.beams) | |||
|
733 | if any(beams): | |||
|
734 | for beam in beams: | |||
|
735 | if beams[beam] == self.id: | |||
|
736 | return | |||
|
737 | i = len(beams)+1 | |||
|
738 | beams['beam'+str(i)] = self.id | |||
|
739 | else: | |||
|
740 | beams['beam1'] = self.id | |||
|
741 | ||||
|
742 | self.abs_conf.beams = json.dumps(beams) | |||
|
743 | self.abs_conf.save() | |||
|
744 | ||||
|
745 | return | |||
|
746 | ||||
|
747 | def remove_beamfromlist(self): | |||
|
748 | """ | |||
|
749 | This function removes current beam from the beams list of ABS Configuration. | |||
|
750 | """ | |||
|
751 | beams = ast.literal_eval(self.abs_conf.beams) | |||
|
752 | dict_position = '' | |||
|
753 | ||||
|
754 | if any(beams): | |||
|
755 | for beam in beams: | |||
|
756 | if beams[beam] == self.id: | |||
|
757 | dict_position = beam | |||
|
758 | if dict_position != '': | |||
|
759 | beams.pop(str(dict_position),None) | |||
|
760 | else: | |||
|
761 | return | |||
|
762 | ||||
|
763 | self.abs_conf.beams = json.dumps(beams) | |||
|
764 | self.abs_conf.save() | |||
|
765 | ||||
|
766 | return | |||
|
767 | ||||
|
768 | @property | |||
|
769 | def get_upvalues(self): | |||
|
770 | """ | |||
|
771 | This function reads antenna pattern and show the up-value of one abs module | |||
|
772 | """ | |||
|
773 | ||||
|
774 | data = ast.literal_eval(self.antenna) | |||
|
775 | up_data = data['antenna_up'] | |||
|
776 | ||||
|
777 | up_values = [] | |||
|
778 | for data in up_data: | |||
|
779 | for i in range(0,8): | |||
|
780 | up_values.append(data[i]) | |||
|
781 | ||||
|
782 | return up_values | |||
|
783 | ||||
|
784 | @property | |||
|
785 | def antenna_upvalues(self): | |||
|
786 | """ | |||
|
787 | This function reads antenna pattern and show the up - values of one abs beam | |||
|
788 | in a particular order | |||
|
789 | """ | |||
|
790 | data = ast.literal_eval(self.antenna) | |||
|
791 | up_data = data['antenna_up'] | |||
|
792 | ||||
|
793 | return up_data | |||
|
794 | ||||
|
795 | @property | |||
|
796 | def antenna_downvalues(self): | |||
|
797 | """ | |||
|
798 | This function reads antenna pattern and show the down - values of one abs beam | |||
|
799 | in a particular order | |||
|
800 | """ | |||
|
801 | data = ast.literal_eval(self.antenna) | |||
|
802 | down_data = data['antenna_down'] | |||
|
803 | ||||
|
804 | return down_data | |||
|
805 | ||||
|
806 | @property | |||
|
807 | def get_downvalues(self): | |||
|
808 | """ | |||
|
809 | This function reads antenna pattern and show the down-value of one abs module | |||
|
810 | """ | |||
|
811 | ||||
|
812 | data = ast.literal_eval(self.antenna) | |||
|
813 | down_data = data['antenna_down'] | |||
|
814 | ||||
|
815 | down_values = [] | |||
|
816 | for data in down_data: | |||
|
817 | for i in range(0,8): | |||
|
818 | down_values.append(data[i]) | |||
|
819 | ||||
|
820 | return down_values | |||
|
821 | ||||
|
822 | @property | |||
|
823 | def get_up_ues(self): | |||
|
824 | """ | |||
|
825 | This function shows the up-ues-value of one beam | |||
|
826 | """ | |||
|
827 | data = ast.literal_eval(self.ues) | |||
|
828 | up_ues = data['up'] | |||
|
829 | ||||
|
830 | return up_ues | |||
|
831 | ||||
|
832 | @property | |||
|
833 | def get_down_ues(self): | |||
|
834 | """ | |||
|
835 | This function shows the down-ues-value of one beam | |||
|
836 | """ | |||
|
837 | data = ast.literal_eval(self.ues) | |||
|
838 | down_ues = data['down'] | |||
|
839 | ||||
|
840 | return down_ues | |||
|
841 | ||||
|
842 | @property | |||
|
843 | def get_up_onlyrx(self): | |||
|
844 | """ | |||
|
845 | This function shows the up-onlyrx-value of one beam | |||
|
846 | """ | |||
|
847 | data = json.loads(self.only_rx) | |||
|
848 | up_onlyrx = data['up'] | |||
|
849 | ||||
|
850 | return up_onlyrx | |||
|
851 | ||||
|
852 | @property | |||
|
853 | def get_down_onlyrx(self): | |||
|
854 | """ | |||
|
855 | This function shows the down-onlyrx-value of one beam | |||
|
856 | """ | |||
|
857 | data = json.loads(self.only_rx) | |||
|
858 | down_onlyrx = data['down'] | |||
|
859 | ||||
|
860 | return down_onlyrx | |||
|
861 | ||||
|
862 | @property | |||
|
863 | def get_tx(self): | |||
|
864 | """ | |||
|
865 | This function shows the tx-values of one beam | |||
|
866 | """ | |||
|
867 | data = json.loads(self.tx) | |||
|
868 | ||||
|
869 | return data | |||
|
870 | ||||
|
871 | @property | |||
|
872 | def get_rx(self): | |||
|
873 | """ | |||
|
874 | This function shows the rx-values of one beam | |||
|
875 | """ | |||
|
876 | data = json.loads(self.rx) | |||
|
877 | ||||
|
878 | return data |
General Comments 0
You need to be logged in to leave comments.
Login now