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