@@ -1,1043 +1,1043 | |||
|
1 | 1 | from django.db import models |
|
2 | 2 | from apps.main.models import Configuration |
|
3 | 3 | from django.core.urlresolvers import reverse |
|
4 | 4 | # Create your models here. |
|
5 | 5 | |
|
6 | 6 | import ast |
|
7 | 7 | import socket |
|
8 | 8 | import json |
|
9 | 9 | import requests |
|
10 | 10 | import struct |
|
11 | 11 | import sys, time |
|
12 | 12 | |
|
13 | 13 | import multiprocessing |
|
14 | 14 | |
|
15 | 15 | |
|
16 | 16 | antenna_default = json.dumps({ |
|
17 | 17 | "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
18 | 18 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
19 | 19 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
20 | 20 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
21 | 21 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
22 | 22 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
23 | 23 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0], |
|
24 | 24 | [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0] |
|
25 | 25 | ] |
|
26 | 26 | , |
|
27 | 27 | "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
28 | 28 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
29 | 29 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
30 | 30 | [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5], |
|
31 | 31 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
32 | 32 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
33 | 33 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0], |
|
34 | 34 | [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]], |
|
35 | 35 | }) |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | tx_default = json.dumps({ |
|
39 | 39 | "up": [[1,1,1,1,0,0,0,0], |
|
40 | 40 | [1,1,1,1,0,0,0,0], |
|
41 | 41 | [1,1,1,1,0,0,0,0], |
|
42 | 42 | [1,1,1,1,0,0,0,0], |
|
43 | 43 | [0,0,0,0,1,1,1,1], |
|
44 | 44 | [0,0,0,0,1,1,1,1], |
|
45 | 45 | [0,0,0,0,1,1,1,1], |
|
46 | 46 | [0,0,0,0,1,1,1,1]], |
|
47 | 47 | |
|
48 | 48 | "down": [[1,1,1,1,0,0,0,0], |
|
49 | 49 | [1,1,1,1,0,0,0,0], |
|
50 | 50 | [1,1,1,1,0,0,0,0], |
|
51 | 51 | [1,1,1,1,0,0,0,0], |
|
52 | 52 | [0,0,0,0,1,1,1,1], |
|
53 | 53 | [0,0,0,0,1,1,1,1], |
|
54 | 54 | [0,0,0,0,1,1,1,1], |
|
55 | 55 | [0,0,0,0,1,1,1,1]], |
|
56 | 56 | }) |
|
57 | 57 | |
|
58 | 58 | rx_default = json.dumps({ |
|
59 | 59 | "up": [[1,1,1,1,0,0,0,0], |
|
60 | 60 | [1,1,1,1,0,0,0,0], |
|
61 | 61 | [1,1,1,1,0,0,0,0], |
|
62 | 62 | [1,1,1,1,0,0,0,0], |
|
63 | 63 | [0,0,0,0,1,1,1,1], |
|
64 | 64 | [0,0,0,0,1,1,1,1], |
|
65 | 65 | [0,0,0,0,1,1,1,1], |
|
66 | 66 | [0,0,0,0,1,1,1,1]], |
|
67 | 67 | |
|
68 | 68 | "down": [[1,1,1,1,0,0,0,0], |
|
69 | 69 | [1,1,1,1,0,0,0,0], |
|
70 | 70 | [1,1,1,1,0,0,0,0], |
|
71 | 71 | [1,1,1,1,0,0,0,0], |
|
72 | 72 | [0,0,0,0,1,1,1,1], |
|
73 | 73 | [0,0,0,0,1,1,1,1], |
|
74 | 74 | [0,0,0,0,1,1,1,1], |
|
75 | 75 | [0,0,0,0,1,1,1,1]], |
|
76 | 76 | }) |
|
77 | 77 | |
|
78 | 78 | conf_default = {} |
|
79 | 79 | status_default = {} |
|
80 | 80 | for i in range(1,65): |
|
81 | 81 | conf_default[str(i)] = "" |
|
82 | 82 | status_default[str(i)] = 0 |
|
83 | 83 | |
|
84 | 84 | ues_default = json.dumps({ |
|
85 | 85 | "up": [0.533333,0.00000,1.06667,0.00000], |
|
86 | 86 | "down": [0.533333,0.00000,1.06667,0.00000] |
|
87 | 87 | }) |
|
88 | 88 | |
|
89 | 89 | onlyrx_default = json.dumps({ |
|
90 | 90 | "up": False, |
|
91 | 91 | "down": False |
|
92 | 92 | }) |
|
93 | 93 | |
|
94 | 94 | def up_convertion(cadena): |
|
95 | 95 | valores = [] |
|
96 | 96 | for c in cadena: |
|
97 | 97 | if c == 1.0: valores=valores+['000'] |
|
98 | 98 | if c == 2.0: valores=valores+['001'] |
|
99 | 99 | if c == 3.0: valores=valores+['010'] |
|
100 | 100 | if c == 0.0: valores=valores+['011'] |
|
101 | 101 | if c == 0.5: valores=valores+['100'] |
|
102 | 102 | if c == 1.5: valores=valores+['101'] |
|
103 | 103 | if c == 2.5: valores=valores+['110'] |
|
104 | 104 | if c == 3.5: valores=valores+['111'] |
|
105 | 105 | |
|
106 | 106 | return valores |
|
107 | 107 | |
|
108 | 108 | def up_conv_bits(value): |
|
109 | 109 | |
|
110 | 110 | if value == 1.0: bits="000" |
|
111 | 111 | if value == 2.0: bits="001" |
|
112 | 112 | if value == 3.0: bits="010" |
|
113 | 113 | if value == 0.0: bits="011" |
|
114 | 114 | if value == 0.5: bits="100" |
|
115 | 115 | if value == 1.5: bits="101" |
|
116 | 116 | if value == 2.5: bits="110" |
|
117 | 117 | if value == 3.5: bits="111" |
|
118 | 118 | |
|
119 | 119 | return bits |
|
120 | 120 | |
|
121 | 121 | def down_convertion(cadena): |
|
122 | 122 | valores = [] |
|
123 | 123 | for c in cadena: |
|
124 | 124 | if c == 1.0: valores=valores+['000'] |
|
125 | 125 | if c == 2.0: valores=valores+['001'] |
|
126 | 126 | if c == 3.0: valores=valores+['010'] |
|
127 | 127 | if c == 0.0: valores=valores+['011'] |
|
128 | 128 | if c == 0.5: valores=valores+['100'] |
|
129 | 129 | if c == 1.5: valores=valores+['101'] |
|
130 | 130 | if c == 2.5: valores=valores+['110'] |
|
131 | 131 | if c == 3.5: valores=valores+['111'] |
|
132 | 132 | |
|
133 | 133 | return valores |
|
134 | 134 | |
|
135 | 135 | def down_conv_bits(value): |
|
136 | 136 | |
|
137 | 137 | if value == 1.0: bits="000" |
|
138 | 138 | if value == 2.0: bits="001" |
|
139 | 139 | if value == 3.0: bits="010" |
|
140 | 140 | if value == 0.0: bits="011" |
|
141 | 141 | if value == 0.5: bits="100" |
|
142 | 142 | if value == 1.5: bits="101" |
|
143 | 143 | if value == 2.5: bits="110" |
|
144 | 144 | if value == 3.5: bits="111" |
|
145 | 145 | |
|
146 | 146 | return bits |
|
147 | 147 | |
|
148 | 148 | def ip2position(module_number): |
|
149 | 149 | j=0 |
|
150 | 150 | i=0 |
|
151 | 151 | for x in range(0,module_number-1): |
|
152 | 152 | j=j+1 |
|
153 | 153 | if j==8: |
|
154 | 154 | i=i+1 |
|
155 | 155 | j=0 |
|
156 | 156 | |
|
157 | 157 | pos = [i,j] |
|
158 | 158 | return pos |
|
159 | 159 | |
|
160 | 160 | |
|
161 | 161 | def fromBinary2Char(binary_string): |
|
162 | 162 | number = int(binary_string, 2) |
|
163 | 163 | #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f' |
|
164 | 164 | number = number + 33 |
|
165 | 165 | char = chr(number) |
|
166 | 166 | return char |
|
167 | 167 | |
|
168 | 168 | def fromChar2Binary(char): |
|
169 | 169 | number = ord(char) - 33 |
|
170 | 170 | #Minus 33 to get the real value |
|
171 | 171 | bits = bin(number)[2:] |
|
172 | 172 | #To ensure we have a string with 6bits |
|
173 | 173 | if len(bits) < 6: |
|
174 | 174 | bits = bits.zfill(6) |
|
175 | 175 | return bits |
|
176 | 176 | |
|
177 | 177 | |
|
178 | 178 | class ABSConfiguration(Configuration): |
|
179 | 179 | active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}") |
|
180 | 180 | module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default)) |
|
181 | 181 | |
|
182 | 182 | class Meta: |
|
183 | 183 | db_table = 'abs_configurations' |
|
184 | 184 | |
|
185 | 185 | def get_absolute_url_plot(self): |
|
186 | 186 | return reverse('url_plot_abs_patterns', args=[str(self.id)]) |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | def parms_to_dict(self): |
|
190 | 190 | |
|
191 | 191 | parameters = {} |
|
192 | 192 | |
|
193 | 193 | parameters['device_id'] = self.device.id |
|
194 | 194 | parameters['name'] = self.name |
|
195 | 195 | parameters['beams'] = {} |
|
196 | 196 | |
|
197 | 197 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
198 | 198 | b=1 |
|
199 | 199 | for beam in beams: |
|
200 | 200 | #absbeam = ABSBeam.objects.get(pk=beams[beam]) |
|
201 | 201 | parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict() |
|
202 | 202 | b+=1 |
|
203 | 203 | |
|
204 | 204 | return parameters |
|
205 | 205 | |
|
206 | 206 | def dict_to_parms(self, parameters): |
|
207 | 207 | |
|
208 | 208 | self.name = parameters['name'] |
|
209 | 209 | |
|
210 | 210 | absbeams = ABSBeam.objects.filter(abs_conf=self) |
|
211 | 211 | beams = parameters['beams'] |
|
212 | 212 | |
|
213 | 213 | if absbeams: |
|
214 | 214 | beams_number = len(beams) |
|
215 | 215 | absbeams_number = len(absbeams) |
|
216 | 216 | if beams_number==absbeams_number: |
|
217 | 217 | i = 1 |
|
218 | 218 | for absbeam in absbeams: |
|
219 | 219 | absbeam.dict_to_parms(beams['beam'+str(i)]) |
|
220 | 220 | i = i+1 |
|
221 | 221 | elif beams_number > absbeams_number: |
|
222 | 222 | i = 1 |
|
223 | 223 | for absbeam in absbeams: |
|
224 | 224 | absbeam.dict_to_parms(beams['beam'+str(i)]) |
|
225 | 225 | i=i+1 |
|
226 | 226 | for x in range(i,beams_number+1): |
|
227 | 227 | new_beam = ABSBeam( |
|
228 | 228 | name =beams['beam'+str(i)]['name'], |
|
229 | 229 | antenna =json.dumps(beams['beam'+str(i)]['antenna']), |
|
230 | 230 | abs_conf = self, |
|
231 | 231 | tx =json.dumps(beams['beam'+str(i)]['tx']), |
|
232 | 232 | rx =json.dumps(beams['beam'+str(i)]['rx']), |
|
233 | 233 | ues =json.dumps(beams['beam'+str(i)]['ues']), |
|
234 | 234 | only_rx =json.dumps(beams['beam'+str(i)]['only_rx']) |
|
235 | 235 | ) |
|
236 | 236 | new_beam.save() |
|
237 | 237 | i=i+1 |
|
238 | 238 | else: #beams_number < absbeams_number: |
|
239 | 239 | i = 1 |
|
240 | 240 | for absbeam in absbeams: |
|
241 | 241 | if i <= beams_number: |
|
242 | 242 | absbeam.dict_to_parms(beams['beam'+str(i)]) |
|
243 | 243 | i=i+1 |
|
244 | 244 | else: |
|
245 | 245 | absbeam.delete() |
|
246 | 246 | else: |
|
247 | 247 | for beam in beams: |
|
248 | 248 | new_beam = ABSBeam( |
|
249 | 249 | name =beams[beam]['name'], |
|
250 | 250 | antenna =json.dumps(beams[beam]['antenna']), |
|
251 | 251 | abs_conf = self, |
|
252 | 252 | tx =json.dumps(beams[beam]['tx']), |
|
253 | 253 | rx =json.dumps(beams[beam]['rx']), |
|
254 | 254 | ues =json.dumps(beams[beam]['ues']), |
|
255 | 255 | only_rx =json.dumps(beams[beam]['only_rx']) |
|
256 | 256 | ) |
|
257 | 257 | new_beam.save() |
|
258 | 258 | |
|
259 | 259 | |
|
260 | 260 | |
|
261 | 261 | def update_from_file(self, parameters): |
|
262 | 262 | |
|
263 | 263 | self.dict_to_parms(parameters) |
|
264 | 264 | self.save() |
|
265 | 265 | |
|
266 | 266 | |
|
267 | 267 | def get_beams(self, **kwargs): |
|
268 | 268 | ''' |
|
269 | 269 | This function returns ABS Configuration beams |
|
270 | 270 | ''' |
|
271 | 271 | return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs) |
|
272 | 272 | |
|
273 | 273 | def clone(self, **kwargs): |
|
274 | 274 | |
|
275 | 275 | beams = self.get_beams() |
|
276 | 276 | self.pk = None |
|
277 | 277 | self.id = None |
|
278 | 278 | for attr, value in kwargs.items(): |
|
279 | 279 | setattr(self, attr, value) |
|
280 | 280 | self.save() |
|
281 | 281 | |
|
282 | 282 | for beam in beams: |
|
283 | 283 | beam.clone(abs_conf=self) |
|
284 | 284 | |
|
285 | 285 | return self |
|
286 | 286 | |
|
287 | 287 | |
|
288 | 288 | def module_conf(self, module_num, beams): |
|
289 | 289 | """ |
|
290 | 290 | This function creates beam configurations for one abs module. |
|
291 | 291 | """ |
|
292 | 292 | ip_address = self.device.ip_address |
|
293 | 293 | ip_address = ip_address.split('.') |
|
294 | 294 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
295 | 295 | dot = '.' |
|
296 | 296 | module_ip = dot.join(module_seq)+'.'+str(module_num) |
|
297 | 297 | module_port = self.device.port_address |
|
298 |
write_route = 'http://'+module_ip+':'+str(module_port)+'/ |
|
|
298 | write_route = 'http://'+module_ip+':'+str(module_port)+'/write' | |
|
299 | 299 | |
|
300 | 300 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
301 | 301 | module = 'ABS_'+str(module_num) |
|
302 | 302 | bs = '' #{} |
|
303 | 303 | i=1 |
|
304 | 304 | #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
305 | 305 | # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
306 | 306 | for beam in beams: |
|
307 | 307 | #bs[i] = fromBinary2Char(beam.module_6bits(module_num)) |
|
308 | 308 | bs = bs + fromBinary2Char(beam.module_6bits(module_num)) |
|
309 | 309 | i=i+1 |
|
310 | 310 | |
|
311 | 311 | beams = bs |
|
312 | 312 | |
|
313 | 313 | parameters = {} |
|
314 | 314 | parameters['header'] = header |
|
315 | 315 | parameters['module'] = module |
|
316 | 316 | parameters['beams'] = beams #json.dumps(beams) |
|
317 | 317 | print parameters['beams'] |
|
318 | 318 | answer = '' |
|
319 | 319 | |
|
320 | 320 | try: |
|
321 | 321 | r_write = requests.post(write_route, parameters, timeout=0.5) |
|
322 | 322 | answer = r_write.json() |
|
323 | 323 | self.message = answer['message'] |
|
324 | 324 | except: |
|
325 | 325 | self.message = "Could not write ABS parameters" |
|
326 | 326 | return 0 |
|
327 | 327 | return 1 |
|
328 | 328 | |
|
329 | 329 | def read_module(self, module): |
|
330 | 330 | |
|
331 | 331 | """ |
|
332 | 332 | Read out-bits (up-down) of 1 abs module NOT for Configuration |
|
333 | 333 | """ |
|
334 | 334 | |
|
335 | 335 | parameters = {} |
|
336 | 336 | ip_address = self.device.ip_address |
|
337 | 337 | ip_address = ip_address.split('.') |
|
338 | 338 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
339 | 339 | dot = '.' |
|
340 | 340 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
341 | 341 | module_port = self.device.port_address |
|
342 | 342 | read_route = 'http://'+module_ip+':'+str(module_port)+'/read' |
|
343 | 343 | |
|
344 | 344 | print(read_route) |
|
345 | 345 | |
|
346 | 346 | answer = '' |
|
347 | 347 | module_bits = '' |
|
348 | 348 | |
|
349 | 349 | try: |
|
350 | 350 | r_write = requests.get(read_route, timeout=0.7) |
|
351 | 351 | answer = r_write.json() |
|
352 | 352 | self.message = answer['message'] |
|
353 | 353 | module_bits = answer['allbits'] |
|
354 | 354 | except: |
|
355 | 355 | #message = "Could not read ABS parameters" |
|
356 | 356 | return 0 |
|
357 | 357 | |
|
358 | 358 | return module_bits |
|
359 | 359 | |
|
360 | 360 | def status_device(self): |
|
361 | 361 | """ |
|
362 | 362 | This function gets the status of each abs module. It sends GET method to Web Application |
|
363 | 363 | in Python Bottle. |
|
364 | 364 | """ |
|
365 | 365 | ip_address = self.device.ip_address |
|
366 | 366 | ip_address = ip_address.split('.') |
|
367 | 367 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
368 | 368 | dot = '.' |
|
369 | 369 | module_port = self.device.port_address |
|
370 | 370 | |
|
371 | 371 | modules_status = json.loads(self.module_status) |
|
372 | 372 | |
|
373 | 373 | for i in range(1,65): |
|
374 | 374 | module_ip = dot.join(module_seq)+'.'+str(i) |
|
375 | 375 | print module_ip |
|
376 | 376 | |
|
377 | 377 | route = 'http://'+module_ip+':'+str(module_port)+'/hello' |
|
378 | 378 | |
|
379 | 379 | try: |
|
380 | 380 | r = requests.get(route, timeout=0.7) |
|
381 | 381 | modules_status[str(i)] = 1 |
|
382 | 382 | except: |
|
383 | 383 | modules_status[str(i)] = 0 |
|
384 | 384 | pass |
|
385 | 385 | |
|
386 | 386 | self.message = 'ABS modules Status have been updated.' |
|
387 | 387 | self.module_status=json.dumps(modules_status) |
|
388 | 388 | self.save() |
|
389 | 389 | |
|
390 | 390 | return |
|
391 | 391 | |
|
392 | 392 | |
|
393 | 393 | def write_device(self): |
|
394 | 394 | """ |
|
395 | 395 | This function sends the beams list to every abs module. |
|
396 | 396 | It needs 'module_conf' function |
|
397 | 397 | """ |
|
398 | 398 | |
|
399 | 399 | ###beams_list = ast.literal_eval(self.beams) |
|
400 | 400 | ###beams = [] |
|
401 | 401 | beams = ABSBeam.objects.filter(abs_conf=self) |
|
402 | 402 | ###for bl in range(1,len(beams_list)+1): |
|
403 | 403 | ### b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)]) |
|
404 | 404 | ### beams.append(b) |
|
405 | 405 | |
|
406 | 406 | #---Write each abs module--- |
|
407 | 407 | if beams: |
|
408 | 408 | beams_status = ast.literal_eval(self.module_status) |
|
409 | 409 | for i in range(62,65): #(62,65) |
|
410 | 410 | try: |
|
411 | 411 | answer = self.module_conf(i, beams) |
|
412 | 412 | beams_status[str(i)] = 1 |
|
413 | 413 | self.module_status = json.dumps(beams_status) |
|
414 | 414 | self.save() |
|
415 | 415 | #self.module_conf(63,beams) |
|
416 | 416 | #beams_status[str(63)] = 1 |
|
417 | 417 | #self.module_status = json.dumps(beams_status) |
|
418 | 418 | except: |
|
419 | 419 | beams_status[str(i)] = 0 |
|
420 | 420 | self.module_status = json.dumps(beams_status) |
|
421 | 421 | self.save() |
|
422 | 422 | answer = 0 |
|
423 | 423 | return 0 |
|
424 | 424 | else: |
|
425 | 425 | self.message = "ABS Configuration does not have beams" |
|
426 | 426 | return 0 |
|
427 | 427 | |
|
428 | 428 | #self.device.status = 1 |
|
429 | 429 | ## |
|
430 | 430 | if answer==1: |
|
431 | 431 | self.message = "ABS Beams List have been sent to ABS Modules" |
|
432 | 432 | else: |
|
433 | 433 | self.message = "Could not read ABS parameters" |
|
434 | 434 | |
|
435 | 435 | ## |
|
436 | 436 | self.save() |
|
437 | 437 | return 1 |
|
438 | 438 | |
|
439 | 439 | |
|
440 | 440 | def write_module(self, module): |
|
441 | 441 | |
|
442 | 442 | """ |
|
443 | 443 | Send configuration to one abs module |
|
444 | 444 | """ |
|
445 | 445 | |
|
446 | 446 | parameters = {} |
|
447 | 447 | ip_address = self.abs_conf.device.ip_address |
|
448 | 448 | ip_address = ip_address.split('.') |
|
449 | 449 | module_seq = (ip_address[0],ip_address[1],ip_address[2]) |
|
450 | 450 | dot = '.' |
|
451 | 451 | module_ip = dot.join(module_seq)+'.'+str(module) |
|
452 | 452 | module_port = self.abs_conf.device.port_address |
|
453 |
write_route = 'http://'+module_ip+':'+str(module_port)+'/ |
|
|
453 | write_route = 'http://'+module_ip+':'+str(module_port)+'/write' | |
|
454 | 454 | |
|
455 | 455 | #print write_route |
|
456 | 456 | |
|
457 | 457 | header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1' |
|
458 | 458 | module = 'ABS_'+str(module) |
|
459 | 459 | beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101', |
|
460 | 460 | 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'} |
|
461 | 461 | |
|
462 | 462 | parameters['header'] = header |
|
463 | 463 | parameters['module'] = module |
|
464 | 464 | parameters['beams'] = json.dumps(beams) |
|
465 | 465 | |
|
466 | 466 | answer = '' |
|
467 | 467 | |
|
468 | 468 | try: |
|
469 | 469 | r_write = requests.post(write_route, parameters, timeout=0.5) |
|
470 | 470 | answer = r_write.json() |
|
471 | 471 | self.message = answer['message'] |
|
472 | 472 | except: |
|
473 | 473 | self.message = "Could not write ABS parameters" |
|
474 | 474 | return 0 |
|
475 | 475 | |
|
476 | 476 | |
|
477 | 477 | #self.device.status = int(answer['status']) |
|
478 | 478 | |
|
479 | 479 | return 1 |
|
480 | 480 | |
|
481 | 481 | |
|
482 | 482 | def beam_selector(self, module, beam_pos): |
|
483 | 483 | """ |
|
484 | 484 | This function selects the beam number for one absmodule. |
|
485 | 485 | """ |
|
486 | 486 | |
|
487 | 487 | if beam_pos > 0: |
|
488 | 488 | beam_pos = beam_pos - 1 |
|
489 | 489 | else: |
|
490 | 490 | beam_pos = 0 |
|
491 | 491 | |
|
492 | 492 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
493 | 493 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
494 | 494 | beams_list = ABSBeam.objects.filter(abs_conf=self) |
|
495 | 495 | if len(beams_list) < beam_pos: |
|
496 | 496 | return 0 |
|
497 | 497 | |
|
498 | 498 | flag = 1 |
|
499 | 499 | if beam_pos>9: |
|
500 | 500 | flag = 2 |
|
501 | 501 | |
|
502 | 502 | module_address = ('192.168.1.'+str(module), 5500) |
|
503 | 503 | header = 'JROABSCeCnModCnMod0100000' |
|
504 | 504 | numbers = len(str(beam_pos)) |
|
505 | 505 | function = 'CHGB' |
|
506 | 506 | |
|
507 | 507 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
508 | 508 | |
|
509 | 509 | # Create the datagram socket |
|
510 | 510 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
511 | 511 | #sock.connect(module_address) |
|
512 | 512 | try: |
|
513 | 513 | sock.connect(module_address) |
|
514 | 514 | sock.send(message_tx) |
|
515 | 515 | sock.close() |
|
516 | 516 | print("Writing abs module:"+module_address[0]+"...") |
|
517 | 517 | except: |
|
518 | 518 | sock = None |
|
519 | 519 | print("Problem writing abs module:"+module_address[0]) |
|
520 | 520 | return 0 |
|
521 | 521 | |
|
522 | 522 | return 1 |
|
523 | 523 | |
|
524 | 524 | |
|
525 | 525 | def change_beam(self, beam_pos): |
|
526 | 526 | """ |
|
527 | 527 | This function selects the beam number for all absmodules. |
|
528 | 528 | """ |
|
529 | 529 | for i in range(1,65): |
|
530 | 530 | try: |
|
531 | 531 | self.beam_selector(i,beam_pos) |
|
532 | 532 | except: |
|
533 | 533 | print("Problem with module: 192.168.1."+str(i)) |
|
534 | 534 | self.message = "Problem with module: 192.168.1."+str(i) |
|
535 | 535 | #return 0 |
|
536 | 536 | return 1 |
|
537 | 537 | |
|
538 | 538 | |
|
539 | 539 | def send_beam_num(self, beam_pos): |
|
540 | 540 | """ |
|
541 | 541 | This function connects to a multicast group and sends the beam number |
|
542 | 542 | to all abs modules. |
|
543 | 543 | """ |
|
544 | 544 | |
|
545 | 545 | if beam_pos > 0: |
|
546 | 546 | beam_pos = beam_pos - 1 |
|
547 | 547 | else: |
|
548 | 548 | beam_pos = 0 |
|
549 | 549 | |
|
550 | 550 | #El indice del apunte debe ser menor que el numero total de apuntes |
|
551 | 551 | #El servidor tcp en el embebido comienza a contar desde 0 |
|
552 | 552 | beams_list = ABSBeam.objects.filter(abs_conf=self) |
|
553 | 553 | if len(beams_list) < beam_pos: |
|
554 | 554 | return 0 |
|
555 | 555 | |
|
556 | 556 | flag = 1 |
|
557 | 557 | if beam_pos>9: |
|
558 | 558 | flag = 2 |
|
559 | 559 | |
|
560 | 560 | header = 'JROABSCeCnModCnMod0100000' |
|
561 | 561 | flag = str(flag) |
|
562 | 562 | function = 'CHGB' |
|
563 | 563 | message_tx = header+flag+function+str(beam_pos)+'0' |
|
564 | 564 | |
|
565 | 565 | multicast_group = '224.3.29.71' |
|
566 | 566 | server_address = ('',10000) |
|
567 | 567 | |
|
568 | 568 | # Create the socket |
|
569 | 569 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
570 | 570 | # Bind to the server address |
|
571 | 571 | sock.bind(server_address) |
|
572 | 572 | # Telling the OS add the socket to the multicast on all interfaces |
|
573 | 573 | group = socket.inet_aton(multicast_group) |
|
574 | 574 | mreq = struct.pack('4sL', group, socket.INADDR_ANY) |
|
575 | 575 | sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) |
|
576 | 576 | |
|
577 | 577 | #print 'sending acknowledgement to all: \n' + message_tx |
|
578 | 578 | sock.sendto(message_tx, (multicast_group, 10000)) |
|
579 | 579 | sock.close() |
|
580 | 580 | sock = None |
|
581 | 581 | |
|
582 | 582 | return 1 |
|
583 | 583 | |
|
584 | 584 | def test1(self): |
|
585 | 585 | t1 = time.time() |
|
586 | 586 | t2 = 0 |
|
587 | 587 | while (t2-t1)<100:#300 |
|
588 | 588 | t2 = time.time() |
|
589 | 589 | self.send_beam_num(2) |
|
590 | 590 | time.sleep(0.04) |
|
591 | 591 | self.send_beam_num(1) |
|
592 | 592 | time.sleep(0.04) |
|
593 | 593 | return |
|
594 | 594 | |
|
595 | 595 | def change_procs_test1(self, module): |
|
596 | 596 | |
|
597 | 597 | for i in range (1,300):#300 |
|
598 | 598 | beam_pos = 1 |
|
599 | 599 | module_address = ('192.168.1.'+str(module), 5500) |
|
600 | 600 | header = 'JROABSCeCnModCnMod0100000' |
|
601 | 601 | numbers = len(str(beam_pos)) |
|
602 | 602 | function = 'CHGB' |
|
603 | 603 | |
|
604 | 604 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
605 | 605 | |
|
606 | 606 | # Create the datagram socket |
|
607 | 607 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
608 | 608 | sock.connect(module_address) |
|
609 | 609 | |
|
610 | 610 | sock.send(message_tx) |
|
611 | 611 | #t = sock.recv(1024) |
|
612 | 612 | sock.close() |
|
613 | 613 | sock = None |
|
614 | 614 | |
|
615 | 615 | |
|
616 | 616 | time.sleep(0.04) |
|
617 | 617 | |
|
618 | 618 | |
|
619 | 619 | beam_pos = 0 |
|
620 | 620 | numbers = len(str(beam_pos)) |
|
621 | 621 | |
|
622 | 622 | message_tx = header+str(numbers)+function+str(beam_pos)+'0' |
|
623 | 623 | |
|
624 | 624 | # Create the datagram socket |
|
625 | 625 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
626 | 626 | sock.connect(module_address) |
|
627 | 627 | sock.send(message_tx) |
|
628 | 628 | sock.close() |
|
629 | 629 | sock = None |
|
630 | 630 | |
|
631 | 631 | time.sleep(0.04) |
|
632 | 632 | |
|
633 | 633 | |
|
634 | 634 | def multi_procs_test1(self): |
|
635 | 635 | |
|
636 | 636 | """ |
|
637 | 637 | This function sends the beam number to all abs modules using multiprocessing. |
|
638 | 638 | """ |
|
639 | 639 | |
|
640 | 640 | #if __name__ == "__main__": |
|
641 | 641 | size = 10000000 # Number of random numbers to add |
|
642 | 642 | procs = 65 # (Number-1) of processes to create (absmodule) |
|
643 | 643 | |
|
644 | 644 | # Create a list of jobs and then iterate through |
|
645 | 645 | # the number of processes appending each process to |
|
646 | 646 | # the job list |
|
647 | 647 | jobs = [] |
|
648 | 648 | for i in range(1, procs): |
|
649 | 649 | |
|
650 | 650 | process = multiprocessing.Process(target=self.change_procs_test1,args=(i,)) |
|
651 | 651 | jobs.append(process) |
|
652 | 652 | #print jobs |
|
653 | 653 | |
|
654 | 654 | # Start the processes (i.e. calculate the random number lists) |
|
655 | 655 | for j in jobs: |
|
656 | 656 | #time.sleep(0.4) |
|
657 | 657 | #print j |
|
658 | 658 | j.start() |
|
659 | 659 | |
|
660 | 660 | # Ensure all of the processes have finished |
|
661 | 661 | for j in jobs: |
|
662 | 662 | j.join() |
|
663 | 663 | |
|
664 | 664 | print("List processing complete.") |
|
665 | 665 | return 1 |
|
666 | 666 | |
|
667 | 667 | |
|
668 | 668 | |
|
669 | 669 | def multi_procs_test2(self): |
|
670 | 670 | """ |
|
671 | 671 | This function use multiprocessing python library. Importing Pool we can select |
|
672 | 672 | the number of cores we want to use |
|
673 | 673 | After 'nproc' linux command, we know how many cores computer has. |
|
674 | 674 | NOT WORKING |
|
675 | 675 | """ |
|
676 | 676 | import multiprocessing |
|
677 | 677 | pool = multiprocessing.Pool(3) # cores |
|
678 | 678 | |
|
679 | 679 | tasks = [] |
|
680 | 680 | procs = 65 |
|
681 | 681 | for i in range(62, procs): |
|
682 | 682 | tasks.append( (i,)) |
|
683 | 683 | #print tasks |
|
684 | 684 | #pool.apply_async(self.change_procs_test1, 62) |
|
685 | 685 | results = [pool.apply( self.change_procs_test1, t ) for t in tasks] |
|
686 | 686 | #for result in results: |
|
687 | 687 | #result.get() |
|
688 | 688 | #(plotNum, plotFilename) = result.get() |
|
689 | 689 | #print("Result: plot %d written to %s" % (plotNum, plotFilename) ) |
|
690 | 690 | |
|
691 | 691 | return 1 |
|
692 | 692 | |
|
693 | 693 | |
|
694 | 694 | def multi_procs_test3(self): |
|
695 | 695 | """ |
|
696 | 696 | This function use multiprocessing python library. Importing Pool we can select |
|
697 | 697 | the number of cores we want to use |
|
698 | 698 | After 'nproc' linux command, we know how many cores computer has. |
|
699 | 699 | """ |
|
700 | 700 | from multiprocessing import Pool |
|
701 | 701 | import time |
|
702 | 702 | |
|
703 | 703 | def f(x): |
|
704 | 704 | return x*x |
|
705 | 705 | |
|
706 | 706 | tasks = [] |
|
707 | 707 | procs = 65 |
|
708 | 708 | pool = Pool(processes=4) |
|
709 | 709 | |
|
710 | 710 | #for i in range(62, procs): |
|
711 | 711 | #result = pool.map(f, range(62,65)) |
|
712 | 712 | it = pool.imap(self.change_procs_test1, range(62,65)) |
|
713 | 713 | #result.get(timeout=5) |
|
714 | 714 | |
|
715 | 715 | return 1 |
|
716 | 716 | |
|
717 | 717 | |
|
718 | 718 | def f(x): |
|
719 | 719 | print x |
|
720 | 720 | return |
|
721 | 721 | |
|
722 | 722 | def multi_procs_test4(self): |
|
723 | 723 | import multiprocessing as mp |
|
724 | 724 | |
|
725 | 725 | |
|
726 | 726 | num_workers = mp.cpu_count() |
|
727 | 727 | |
|
728 | 728 | pool = mp.Pool(num_workers) |
|
729 | 729 | procs = 65 |
|
730 | 730 | for i in range(62, procs): |
|
731 | 731 | #for task in tasks: |
|
732 | 732 | pool.apply_async(self.f, args = (i,)) |
|
733 | 733 | |
|
734 | 734 | pool.close() |
|
735 | 735 | pool.join() |
|
736 | 736 | |
|
737 | 737 | return 1 |
|
738 | 738 | |
|
739 | 739 | |
|
740 | 740 | def get_absolute_url_import(self): |
|
741 | 741 | return reverse('url_import_abs_conf', args=[str(self.id)]) |
|
742 | 742 | |
|
743 | 743 | |
|
744 | 744 | |
|
745 | 745 | |
|
746 | 746 | class ABSBeam(models.Model): |
|
747 | 747 | |
|
748 | 748 | name = models.CharField(max_length=60, default='Beam') |
|
749 | 749 | antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default) |
|
750 | 750 | abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration') |
|
751 | 751 | tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default) |
|
752 | 752 | rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default) |
|
753 | 753 | s_time = models.TimeField(verbose_name='Star Time', default='00:00:00') |
|
754 | 754 | e_time = models.TimeField(verbose_name='End Time', default='23:59:59') |
|
755 | 755 | modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default)) |
|
756 | 756 | ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default) |
|
757 | 757 | only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default) |
|
758 | 758 | |
|
759 | 759 | class Meta: |
|
760 | 760 | db_table = 'abs_beams' |
|
761 | 761 | |
|
762 | 762 | def __unicode__(self): |
|
763 | 763 | return u'%s' % (self.name) |
|
764 | 764 | |
|
765 | 765 | def parms_to_dict(self): |
|
766 | 766 | |
|
767 | 767 | #Update data |
|
768 | 768 | self.modules_6bits() |
|
769 | 769 | |
|
770 | 770 | parameters = {} |
|
771 | 771 | |
|
772 | 772 | parameters['name'] = self.name |
|
773 | 773 | parameters['antenna'] = ast.literal_eval(self.antenna) |
|
774 | 774 | parameters['abs_conf'] = self.abs_conf.name |
|
775 | 775 | parameters['tx'] = ast.literal_eval(self.tx) |
|
776 | 776 | parameters['rx'] = ast.literal_eval(self.rx) |
|
777 | 777 | parameters['s_time'] = self.s_time.strftime("%H:%M:%S") |
|
778 | 778 | parameters['e_time'] = self.e_time.strftime("%H:%M:%S") |
|
779 | 779 | parameters['configuration'] = ast.literal_eval(self.modules_conf) |
|
780 | 780 | parameters['ues'] = ast.literal_eval(self.ues) |
|
781 | 781 | parameters['only_rx'] = json.loads(self.only_rx) |
|
782 | 782 | |
|
783 | 783 | return parameters |
|
784 | 784 | |
|
785 | 785 | def dict_to_parms(self, parameters): |
|
786 | 786 | |
|
787 | 787 | self.name = parameters['name'] |
|
788 | 788 | self.antenna = json.dumps(parameters['antenna']) |
|
789 | 789 | #self.abs_conf = parameters['abs_conf'] |
|
790 | 790 | self.tx = json.dumps(parameters['tx']) |
|
791 | 791 | self.rx = json.dumps(parameters['rx']) |
|
792 | 792 | #self.s_time = parameters['s_time'] |
|
793 | 793 | #self.e_time = parameters['e_time'] |
|
794 | 794 | self.ues = json.dumps(parameters['ues']) |
|
795 | 795 | self.only_rx = json.dumps(parameters['only_rx']) |
|
796 | 796 | |
|
797 | 797 | self.modules_6bits() |
|
798 | 798 | self.save() |
|
799 | 799 | |
|
800 | 800 | |
|
801 | 801 | def clone(self, **kwargs): |
|
802 | 802 | |
|
803 | 803 | self.pk = None |
|
804 | 804 | self.id = None |
|
805 | 805 | for attr, value in kwargs.items(): |
|
806 | 806 | setattr(self, attr, value) |
|
807 | 807 | |
|
808 | 808 | self.save() |
|
809 | 809 | |
|
810 | 810 | return self |
|
811 | 811 | |
|
812 | 812 | def set_activebeam(self): |
|
813 | 813 | """ |
|
814 | 814 | This function change de active beam of ABS Configuration |
|
815 | 815 | """ |
|
816 | 816 | conf = self.abs_conf |
|
817 | 817 | active_beam = {} |
|
818 | 818 | active_beam['active_beam'] = self.id |
|
819 | 819 | conf.active_beam = json.dumps(active_beam) |
|
820 | 820 | conf.save() |
|
821 | 821 | |
|
822 | 822 | return |
|
823 | 823 | |
|
824 | 824 | |
|
825 | 825 | |
|
826 | 826 | def module_6bits(self, module): |
|
827 | 827 | """ |
|
828 | 828 | This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module |
|
829 | 829 | """ |
|
830 | 830 | if module > 64: |
|
831 | 831 | beam_bits = "" |
|
832 | 832 | return beam_bits |
|
833 | 833 | |
|
834 | 834 | data = ast.literal_eval(self.antenna) |
|
835 | 835 | up_data = data['antenna_up'] |
|
836 | 836 | down_data = data['antenna_down'] |
|
837 | 837 | |
|
838 | 838 | pos = ip2position(module) |
|
839 | 839 | up_value = up_data[pos[0]][pos[1]] |
|
840 | 840 | down_value = down_data[pos[0]][pos[1]] |
|
841 | 841 | |
|
842 | 842 | up_bits = up_conv_bits(up_value) |
|
843 | 843 | down_bits = down_conv_bits(down_value) |
|
844 | 844 | beam_bits = up_bits+down_bits |
|
845 | 845 | |
|
846 | 846 | return beam_bits |
|
847 | 847 | |
|
848 | 848 | def modules_6bits(self): |
|
849 | 849 | """ |
|
850 | 850 | This function returns 6bits from every abs module (1-64) in a dict |
|
851 | 851 | """ |
|
852 | 852 | modules_configuration = ast.literal_eval(self.modules_conf) |
|
853 | 853 | |
|
854 | 854 | for i in range(1,65): |
|
855 | 855 | modules_configuration[str(i)] = self.module_6bits(i) |
|
856 | 856 | |
|
857 | 857 | self.modules_conf = json.dumps(modules_configuration) |
|
858 | 858 | self.save() |
|
859 | 859 | |
|
860 | 860 | return self.modules_conf |
|
861 | 861 | |
|
862 | 862 | |
|
863 | 863 | def active_beam(self): |
|
864 | 864 | """ |
|
865 | 865 | This function set this beam as the active beam of its ABS Configuration. |
|
866 | 866 | """ |
|
867 | 867 | self.abs_conf.active_beam = json.dumps({'active_beam': self.id}) |
|
868 | 868 | self.abs_conf.save() |
|
869 | 869 | return |
|
870 | 870 | |
|
871 | 871 | @property |
|
872 | 872 | def get_upvalues(self): |
|
873 | 873 | """ |
|
874 | 874 | This function reads antenna pattern and show the up-value of one abs module |
|
875 | 875 | """ |
|
876 | 876 | |
|
877 | 877 | data = ast.literal_eval(self.antenna) |
|
878 | 878 | up_data = data['antenna_up'] |
|
879 | 879 | |
|
880 | 880 | up_values = [] |
|
881 | 881 | for data in up_data: |
|
882 | 882 | for i in range(0,8): |
|
883 | 883 | up_values.append(data[i]) |
|
884 | 884 | |
|
885 | 885 | return up_values |
|
886 | 886 | |
|
887 | 887 | @property |
|
888 | 888 | def antenna_upvalues(self): |
|
889 | 889 | """ |
|
890 | 890 | This function reads antenna pattern and show the up - values of one abs beam |
|
891 | 891 | in a particular order |
|
892 | 892 | """ |
|
893 | 893 | data = ast.literal_eval(self.antenna) |
|
894 | 894 | up_data = data['antenna_up'] |
|
895 | 895 | |
|
896 | 896 | return up_data |
|
897 | 897 | |
|
898 | 898 | @property |
|
899 | 899 | def antenna_downvalues(self): |
|
900 | 900 | """ |
|
901 | 901 | This function reads antenna pattern and show the down - values of one abs beam |
|
902 | 902 | in a particular order |
|
903 | 903 | """ |
|
904 | 904 | data = ast.literal_eval(self.antenna) |
|
905 | 905 | down_data = data['antenna_down'] |
|
906 | 906 | |
|
907 | 907 | return down_data |
|
908 | 908 | |
|
909 | 909 | @property |
|
910 | 910 | def get_downvalues(self): |
|
911 | 911 | """ |
|
912 | 912 | This function reads antenna pattern and show the down-value of one abs module |
|
913 | 913 | """ |
|
914 | 914 | |
|
915 | 915 | data = ast.literal_eval(self.antenna) |
|
916 | 916 | down_data = data['antenna_down'] |
|
917 | 917 | |
|
918 | 918 | down_values = [] |
|
919 | 919 | for data in down_data: |
|
920 | 920 | for i in range(0,8): |
|
921 | 921 | down_values.append(data[i]) |
|
922 | 922 | |
|
923 | 923 | return down_values |
|
924 | 924 | |
|
925 | 925 | @property |
|
926 | 926 | def get_up_ues(self): |
|
927 | 927 | """ |
|
928 | 928 | This function shows the up-ues-value of one beam |
|
929 | 929 | """ |
|
930 | 930 | data = ast.literal_eval(self.ues) |
|
931 | 931 | up_ues = data['up'] |
|
932 | 932 | |
|
933 | 933 | return up_ues |
|
934 | 934 | |
|
935 | 935 | @property |
|
936 | 936 | def get_down_ues(self): |
|
937 | 937 | """ |
|
938 | 938 | This function shows the down-ues-value of one beam |
|
939 | 939 | """ |
|
940 | 940 | data = ast.literal_eval(self.ues) |
|
941 | 941 | down_ues = data['down'] |
|
942 | 942 | |
|
943 | 943 | return down_ues |
|
944 | 944 | |
|
945 | 945 | @property |
|
946 | 946 | def get_up_onlyrx(self): |
|
947 | 947 | """ |
|
948 | 948 | This function shows the up-onlyrx-value of one beam |
|
949 | 949 | """ |
|
950 | 950 | data = json.loads(self.only_rx) |
|
951 | 951 | up_onlyrx = data['up'] |
|
952 | 952 | |
|
953 | 953 | return up_onlyrx |
|
954 | 954 | |
|
955 | 955 | @property |
|
956 | 956 | def get_down_onlyrx(self): |
|
957 | 957 | """ |
|
958 | 958 | This function shows the down-onlyrx-value of one beam |
|
959 | 959 | """ |
|
960 | 960 | data = json.loads(self.only_rx) |
|
961 | 961 | down_onlyrx = data['down'] |
|
962 | 962 | |
|
963 | 963 | return down_onlyrx |
|
964 | 964 | |
|
965 | 965 | @property |
|
966 | 966 | def get_tx(self): |
|
967 | 967 | """ |
|
968 | 968 | This function shows the tx-values of one beam |
|
969 | 969 | """ |
|
970 | 970 | data = json.loads(self.tx) |
|
971 | 971 | |
|
972 | 972 | return data |
|
973 | 973 | |
|
974 | 974 | @property |
|
975 | 975 | def get_uptx(self): |
|
976 | 976 | """ |
|
977 | 977 | This function shows the up-tx-values of one beam |
|
978 | 978 | """ |
|
979 | 979 | data = json.loads(self.tx) |
|
980 | 980 | up_data = data['up'] |
|
981 | 981 | |
|
982 | 982 | up_values = [] |
|
983 | 983 | for data in up_data: |
|
984 | 984 | for i in range(0,8): |
|
985 | 985 | up_values.append(data[i]) |
|
986 | 986 | |
|
987 | 987 | return up_values |
|
988 | 988 | |
|
989 | 989 | @property |
|
990 | 990 | def get_downtx(self): |
|
991 | 991 | """ |
|
992 | 992 | This function shows the down-tx-values of one beam |
|
993 | 993 | """ |
|
994 | 994 | data = json.loads(self.tx) |
|
995 | 995 | down_data = data['down'] |
|
996 | 996 | |
|
997 | 997 | down_values = [] |
|
998 | 998 | for data in down_data: |
|
999 | 999 | for i in range(0,8): |
|
1000 | 1000 | down_values.append(data[i]) |
|
1001 | 1001 | |
|
1002 | 1002 | return down_values |
|
1003 | 1003 | |
|
1004 | 1004 | |
|
1005 | 1005 | |
|
1006 | 1006 | @property |
|
1007 | 1007 | def get_rx(self): |
|
1008 | 1008 | """ |
|
1009 | 1009 | This function shows the rx-values of one beam |
|
1010 | 1010 | """ |
|
1011 | 1011 | data = json.loads(self.rx) |
|
1012 | 1012 | |
|
1013 | 1013 | return data |
|
1014 | 1014 | |
|
1015 | 1015 | @property |
|
1016 | 1016 | def get_uprx(self): |
|
1017 | 1017 | """ |
|
1018 | 1018 | This function shows the up-rx-values of one beam |
|
1019 | 1019 | """ |
|
1020 | 1020 | data = json.loads(self.rx) |
|
1021 | 1021 | up_data = data['up'] |
|
1022 | 1022 | |
|
1023 | 1023 | up_values = [] |
|
1024 | 1024 | for data in up_data: |
|
1025 | 1025 | for i in range(0,8): |
|
1026 | 1026 | up_values.append(data[i]) |
|
1027 | 1027 | |
|
1028 | 1028 | return up_values |
|
1029 | 1029 | |
|
1030 | 1030 | @property |
|
1031 | 1031 | def get_downrx(self): |
|
1032 | 1032 | """ |
|
1033 | 1033 | This function shows the down-rx-values of one beam |
|
1034 | 1034 | """ |
|
1035 | 1035 | data = json.loads(self.rx) |
|
1036 | 1036 | down_data = data['down'] |
|
1037 | 1037 | |
|
1038 | 1038 | down_values = [] |
|
1039 | 1039 | for data in down_data: |
|
1040 | 1040 | for i in range(0,8): |
|
1041 | 1041 | down_values.append(data[i]) |
|
1042 | 1042 | |
|
1043 | 1043 | return down_values |
@@ -1,87 +1,87 | |||
|
1 | 1 | """ |
|
2 | 2 | This script should run in the abs module embedded system |
|
3 | 3 | It creates a Web Application with API Restful Bottle to connect to SIR server. |
|
4 | 4 | It needs the following scripts: abs_gpio.py and bottle.py |
|
5 | 5 | """ |
|
6 | 6 | from bottle import route, run, request |
|
7 | 7 | from bottle import error |
|
8 | 8 | from abs_gpio import abs_read |
|
9 | 9 | |
|
10 | 10 | #Sockets |
|
11 | 11 | import socket |
|
12 | 12 | import sys |
|
13 | 13 | |
|
14 | 14 | import json |
|
15 | 15 | |
|
16 | 16 | module_ip = '192.168.1.xx' |
|
17 | 17 | module_num = 'xx' |
|
18 | 18 | module_port = 5500 |
|
19 | 19 | module_xx = ('192.168.1.xx',5500) |
|
20 | 20 | |
|
21 | 21 | @route('/') |
|
22 | 22 | @route('/hello') |
|
23 | 23 | def hello(): |
|
24 | 24 | return "Hello World!" |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | #---- Get Bits Function ---- |
|
28 | 28 | @route('/read', method='GET') |
|
29 | 29 | def readbits(): |
|
30 | 30 | |
|
31 | 31 | """ |
|
32 | 32 | This function reads the real values from the embedded system pins |
|
33 | 33 | with gpio class |
|
34 | 34 | """ |
|
35 | 35 | |
|
36 | 36 | #This function reads sent bits. |
|
37 | 37 | |
|
38 | 38 | #------Get Monitoring Values------ |
|
39 | 39 | #----------------UP--------------- |
|
40 | 40 | um2_value = abs_read(80) #(++) |
|
41 | 41 | um1_value = abs_read(82) |
|
42 | 42 | um0_value = abs_read(84) #(--) |
|
43 | 43 | #--------------DOWN--------------- |
|
44 | 44 | dm2_value = abs_read(94) #(++) |
|
45 | 45 | dm1_value = abs_read(88) |
|
46 | 46 | dm0_value = abs_read(86) #(--) |
|
47 | 47 | |
|
48 | 48 | allbits = [um2_value, um1_value, um0_value, dm2_value, dm1_value, dm0_value] |
|
49 | 49 | if "" not in allbits: |
|
50 | 50 | allbits = {"um2":int(um2_value), "um1": int(um1_value), "um0": int(um0_value), "dm2": int(dm2_value), "dm1": int(dm1_value), "dm0": int(dm0_value)} |
|
51 | 51 | #allbits = {"ub0":0, "ub1":0, "ub2":0, "db0":0, "db1":0, "db2":0} |
|
52 | 52 | return {"status": 1, "message": "Bits were successfully read", "allbits" : allbits} |
|
53 | 53 | else: |
|
54 | 54 | return {"status": 0, "message": "There's a problem reading bits", "allbits" : ""} |
|
55 | 55 | |
|
56 |
@route('/ |
|
|
57 |
def |
|
|
56 | @route('/write', method='POST') | |
|
57 | def writebits(): | |
|
58 | 58 | """ |
|
59 | 59 | This function sends configurations to the module tcp_ |
|
60 | 60 | """ |
|
61 | 61 | try: |
|
62 | 62 | header_rx = request.forms.get('header') |
|
63 | 63 | module_rx = request.forms.get('module') |
|
64 | 64 | beams_rx = request.forms.get('beams') |
|
65 | 65 | beams_rx = json.loads(beams_rx) |
|
66 | 66 | except: |
|
67 | 67 | return {"status":0, "message": "Could not accept configuration"} |
|
68 | 68 | #print header_rx, module_rx, beams_rx |
|
69 | 69 | message_tx = header_rx+"\n"+module_rx+"\n" |
|
70 | 70 | for i in range(1,len(beams_rx)+1): |
|
71 | 71 | message_tx = message_tx+beams_rx[str(i)]+"\n" |
|
72 | 72 | message_tx = message_tx+"0" |
|
73 | 73 | |
|
74 | 74 | # Create the datagram socket |
|
75 | 75 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
76 | 76 | print >>sys.stderr, 'sending "%s"' % message_tx |
|
77 | 77 | sock.connect(module_63) |
|
78 | 78 | sock.send(message_tx) |
|
79 | 79 | sock.close() |
|
80 | 80 | |
|
81 | 81 | return {"status":1, "message": "Configuration has been successfully sent"} |
|
82 | 82 | |
|
83 | 83 | @error(404) |
|
84 | 84 | def error404(error): |
|
85 | 85 | return "^^' Nothing here, try again." |
|
86 | 86 | |
|
87 | 87 | run(host=module_ip, port=8080, debug=True) |
General Comments 0
You need to be logged in to leave comments.
Login now