##// END OF EJS Templates
Monitoring test
imanay -
r259:260
parent child
Show More
@@ -1,548 +1,549
1 1 # Needed for the FilesStuff class
2 2 import os
3 3 import numpy as np
4 4 # Needed for the TCPComm class
5 5 import socket
6 6
7 7 class TCPComm:
8 8
9 9 __HEADER = "JRO"
10 10 __TYPE = "ABS"
11 11
12 12 def __init__(self, ipSource, iDSource, ipDestino, iDDestino, portDestino, asServer=False):
13 13
14 14 self.ipSource = ipSource
15 15 self.iDSource = iDSource
16 16 self.ipDestino = ipDestino
17 17 self.iDDestino = iDDestino
18 18 self.portDestino = portDestino
19 19 self.addr = (ipDestino,portDestino)
20 20
21 21 self.sc = "none"
22 22 self.answer = "none" #test
23 23 self.asServer = False
24 24 self.len = "000000"
25 25 self.crc = "0"
26 26
27 27 self.openSocket(asServer)
28 28
29 29 def openSocket(self, asServer):
30 30
31 31 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
32 32 # self.socket_c = socket.socket()
33 33 # self.socket_c.connect((self.ipDestino, self.portDestino))
34 34
35 35 if asServer:
36 36 self.socket_c = socket.socket()
37 37 # self.configAsServer()
38 38 self.socket_c.bind(self.addr)
39 39 self.asServer = True
40 40 else:
41 41 # self.configAsClient()
42 42 self.asServer = False #Socket is opened at the sendData function
43 43
44 44 # def configAsClient(self):
45 45 #Buscar broadcast TCP
46 46 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
47 47 # self.socket_c.connect(self.addr)
48 48 # pass
49 49
50 50 # def configAsServer(self):
51 51 #
52 52 # self.socket_c.bind(self.addr)
53 53
54 54 def waitData(self):
55 55 if self.asServer == True:
56 56 trama_rx, l = self.waitAsServer()
57 57 else:
58 58 trama_rx, l = self.waitAsClient()
59 59
60 60 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx, l)
61 61
62 62 return ipSource, ipDestino, cmd, data
63 63
64 64 def waitAsClient2(self, nbytes = 1024):
65 65 print "\nWaiting the server."
66 66 # Short data through ethernet
67 67 trama_rx = self.socket_c.recv(nbytes)
68 68 print "\nThis socket has received this data: " + str(trama_rx)
69 69
70 70 return trama_rx
71 71
72 72 def waitAsServer2(self, nbytes = 1024):
73 73 print "\nWaiting some client."
74 74 self.socket_c.listen(1)
75 75 sc, addr = self.socket_c.accept()
76 76 self.sc = sc
77 77 self.answer = addr
78 78 # Big data through ethernet
79 79 trama_rx = ""
80 80 while True:
81 81 tmp = self.sc.recv(nbytes)
82 82 trama_rx = trama_rx + tmp
83 83 if trama_rx[-4:] == "quit":
84 84 break
85 85
86 86 print "\nThis socket has received some data from: " + str(self.answer)
87 87
88 88 return trama_rx
89 89
90 90 def waitAsServer(self, nbytes = 1024):
91 91 print "\nWaiting some client."
92 92 self.socket_c.listen(1)
93 93 sc, addr = self.socket_c.accept()
94 94 self.sc = sc
95 95 self.answer = addr
96 96 # Big data through ethernet
97 97 cnt = 0;
98 98 first = 0;
99 99 trama_rx = ""
100 100 while True:
101 101 tmp = self.sc.recv(nbytes)
102 102 trama_rx = trama_rx + tmp
103 103 cnt = len(trama_rx)
104 104 if first == 0:
105 105 first = 1
106 106 lng = int(trama_rx[20:26])
107 107 frm_lng= lng + 31 # 31 bytes are fixed and added to the data size to get the frame size
108 108 if cnt == frm_lng:
109 109 break
110 110
111 111 print "\nThis socket has received some data from: " + str(self.answer)
112 print trama_rx
112 113
113 114 return trama_rx, lng
114 115
115 116 def waitAsClient(self, nbytes = 2048):
116 117 print "\nWaiting the server."
117 118 # Short data through ethernet
118 119 try:
119 120 trama_rx = self.socket_c.recv(nbytes)
120 121 lng = int(trama_rx[20:26])
121 122 except:
122 123 print "Waiting error"
123 124 trama_rx = "Error"
124 125 lng = 0
125 126 else:
126 127 print "\nThis socket has received this data: " + str(trama_rx)
127 128
128 129 return trama_rx, lng
129 130
130 131 def sendData2(self, cmd, data, ipDestino):
131 132
132 133 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
133 134 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
134 135
135 136 if self.asServer == True:
136 137 self.SendAsServer(trama_tx)
137 138 else:
138 139 self.SendAsClient(ipDestino, trama_tx)
139 140
140 141 def sendData(self, cmd, data, ipDestino):
141 142
142 143 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
143 144 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
144 145
145 146 self.len = self.int2str(len(data))
146 147
147 148 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
148 149 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
149 150
150 151 if self.asServer == True:
151 152 self.SendAsServer(trama_tx)
152 153 else:
153 154 self.SendAsClient(ipDestino, trama_tx)
154 155
155 156 def SendAsServer(self, trama_tx):
156 157
157 158 self.sc.send(trama_tx)
158 159 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
159 160
160 161
161 162 def SendAsClient(self, ipDestino, trama_tx):
162 163
163 164 try:
164 165 self.socket_c.connect((ipDestino, self.portDestino))
165 166 except:
166 167 print "Connection error with:" + ipDestino
167 168 else:
168 169 self.socket_c.send(trama_tx)
169 170 print "Sending message:[" + trama_tx + "] to: " + ipDestino
170 171
171 172 def __getTrama2(self, trama):
172 173
173 174 FrameList = trama.split(':')
174 175
175 176 header = FrameList[0]
176 177 TypeOfInstrument = FrameList[1]
177 178 ipSource = FrameList[2]
178 179 ipDestino = FrameList[3]
179 180 len = FrameList[4]
180 181 cmd = FrameList[5]
181 182 data = FrameList[6]
182 183 crc = FrameList[7]
183 184 trash = FrameList[8]
184 185
185 186 return ipSource, ipDestino, cmd, data
186 187
187 188 def __getTrama(self, trama, l):
188 189
189 190 header = trama[0:3]
190 191 TypeOfInstrument = trama[3:6]
191 192 ipSource = trama[6:13]
192 193 ipDestino = trama[13:20]
193 194 len = trama[20:26]
194 195 cmd = trama[26:30]
195 196 data = trama[30:30+int(l)]
196 197 crc = trama[30+ int(l):]
197 198
198 199 return ipSource, ipDestino, cmd, data
199 200
200 201 def close_socket(self):
201 202 self.socket_c.close()
202 203
203 204 def open_socket(self):
204 205 self.socket_c = socket.socket()
205 206
206 207 def int2str(self, n):
207 208
208 209 str_n = str(n)
209 210 l_n = len(str_n)
210 211 if l_n == 1:
211 212 str_n = "00000" + str_n
212 213 elif l_n == 2:
213 214 str_n = "0000" + str_n
214 215 elif l_n == 3:
215 216 str_n = "000" + str_n
216 217 elif l_n == 4:
217 218 str_n = "00" + str_n
218 219 elif l_n == 5:
219 220 str_n = "0" + str_n
220 221 return str_n
221 222
222 223 class FilesStuff():
223 224
224 225 def lst2string(self, lst):
225 226 string=''
226 227 for i in lst:
227 228 string=string+i
228 229 return string
229 230
230 231 def string2lst(self, string):
231 232 lst = []
232 233 for i in string:
233 234 lst.append(i)
234 235 return lst
235 236
236 237
237 238 def file1(self, filename, type):
238 239 lst = self.string2lst(filename)
239 240 fin = -1
240 241 t = len(lst)
241 242 for i in np.arange(-1,-t,-1):
242 243 if lst[i]=='/':
243 244 fin=i
244 245 break
245 246 if type == '1':
246 247 nombre2 = lst[fin+1:]
247 248 nombre2[-1]='s'
248 249 nombre2 = self.lst2string(nombre2)
249 250 return nombre2
250 251 if type == '2':
251 252 nombre2 = lst[fin+1:]
252 253 nombre2[-1]='1'
253 254 nombre2 = self.lst2string(nombre2)
254 255 return nombre2
255 256
256 257
257 258 def EliminaSaltoDeLinea(self,cadena):
258 259 i = 0
259 260 for elemento in cadena:
260 261 if elemento =='\n' or elemento =='\r':
261 262 pass
262 263 else:
263 264 i=i+1
264 265 return cadena [:i]
265 266
266 267 def NumeroDeExperimentos(self, path):
267 268 fichero1=open(path,'r')
268 269 cont=0
269 270 for cadena in fichero1:
270 271 cont=cont+1
271 272 if cont==3:
272 273 nexp=''
273 274 pos=0
274 275 for elemento in cadena:
275 276 pos=pos+1
276 277 if elemento=='=':
277 278 nexp=int(cadena[pos:])
278 279 return nexp
279 280 fichero1.close()
280 281
281 282 def Paridad(self, numero):
282 283 if numero%2==0: return 'par'
283 284 elif numero%2==1: return 'impar'
284 285
285 286 def EvaluaCadena(self,cadena):
286 287 if len(cadena)>35:
287 288 if cadena[-1]=='$':
288 289 return cadena[-35:-2]
289 290 elif cadena[-1]==']':
290 291 return cadena[-34:-1]
291 292 else:
292 293 return None
293 294
294 295 def GuardaEnLista(self,path):
295 296 fichero=open(path,'r')
296 297 lista=[]
297 298 for cadena in fichero:
298 299 cadena = self.EliminaSaltoDeLinea(cadena)
299 300 cadena = self.EvaluaCadena(cadena)
300 301 if cadena != None:
301 302 lista.append(cadena)
302 303 fichero.close()
303 304 return lista
304 305
305 306 def CreaFicherosPrevios(self, path, archivo):
306 307 vector = self.GuardaEnLista(archivo)
307 308 for i in range(1,self.NumeroDeExperimentos(archivo)+1):
308 309 fichero =open(path + str(i)+ '.txt','w')
309 310 for j in range(0,16):
310 311 fichero.write(vector[j+16*(i-1)]+'\n')
311 312 fichero.close()
312 313
313 314 def CapturaValoresEnArchivo(self, path, polarizacion='up'):
314 315 #
315 316 # N01 N02 N03 N04 E01 E02 E03 E04 M01 M02 M03 M04 M05 M06 M07 M08
316 317 # N05 N06 N07 N08 E05 E06 E07 E08 M09 M10 M11 M12 M13 M14 M15 M16
317 318 # N09 N10 N11 N12 E09 E10 E11 E12 M17 M18 M19 M20 M21 M22 M23 M24
318 319 # N13 N14 N15 N16 E13 E14 E15 E16 M25 M26 M27 M28 M29 M30 M31 M32
319 320 #
320 321 # W01 W02 W03 W04 S01 S02 S03 S04 M33 M34 M35 M36 M37 M38 M39 M40
321 322 # W05 W06 W07 W08 S05 S06 S07 S08 M41 M42 M43 M44 M45 M46 M47 M48
322 323 # W09 W10 W11 W12 S09 S10 S11 S12 M49 M50 M51 M52 M53 M54 M55 M56
323 324 # W13 W14 W15 W16 S13 S14 S15 S16 M57 M58 M59 M60 M61 M62 M63 M64
324 325 #
325 326 fichero =open(path,'r')
326 327 cnt=0
327 328 lstup=[]
328 329 lstdw=[]
329 330 for cadena in fichero:
330 331 cnt=cnt+1
331 332 if cnt==1:
332 333 nu01=cadena[1:4]
333 334 nu02=cadena[5:8]
334 335 nu03=cadena[9:12]
335 336 nu04=cadena[13:16]
336 337 eu01=cadena[17:20]
337 338 eu02=cadena[21:24]
338 339 eu03=cadena[25:28]
339 340 eu04=cadena[29:32]
340 341 if cnt==2:
341 342 nu05=cadena[1:4]
342 343 nu06=cadena[5:8]
343 344 nu07=cadena[9:12]
344 345 nu08=cadena[13:16]
345 346 eu05=cadena[17:20]
346 347 eu06=cadena[21:24]
347 348 eu07=cadena[25:28]
348 349 eu08=cadena[29:32]
349 350 if cnt==3:
350 351 nu09=cadena[1:4]
351 352 nu10=cadena[5:8]
352 353 nu11=cadena[9:12]
353 354 nu12=cadena[13:16]
354 355 eu09=cadena[17:20]
355 356 eu10=cadena[21:24]
356 357 eu11=cadena[25:28]
357 358 eu12=cadena[29:32]
358 359 if cnt==4:
359 360 nu13=cadena[1:4]
360 361 nu14=cadena[5:8]
361 362 nu15=cadena[9:12]
362 363 nu16=cadena[13:16]
363 364 eu13=cadena[17:20]
364 365 eu14=cadena[21:24]
365 366 eu15=cadena[25:28]
366 367 eu16=cadena[29:32]
367 368 if cnt==5:
368 369 wu01=cadena[1:4]
369 370 wu02=cadena[5:8]
370 371 wu03=cadena[9:12]
371 372 wu04=cadena[13:16]
372 373 su01=cadena[17:20]
373 374 su02=cadena[21:24]
374 375 su03=cadena[25:28]
375 376 su04=cadena[29:32]
376 377 if cnt==6:
377 378 wu05=cadena[1:4]
378 379 wu06=cadena[5:8]
379 380 wu07=cadena[9:12]
380 381 wu08=cadena[13:16]
381 382 su05=cadena[17:20]
382 383 su06=cadena[21:24]
383 384 su07=cadena[25:28]
384 385 su08=cadena[29:32]
385 386 if cnt==7:
386 387 wu09=cadena[1:4]
387 388 wu10=cadena[5:8]
388 389 wu11=cadena[9:12]
389 390 wu12=cadena[13:16]
390 391 su09=cadena[17:20]
391 392 su10=cadena[21:24]
392 393 su11=cadena[25:28]
393 394 su12=cadena[29:32]
394 395 if cnt==8:
395 396 wu13=cadena[1:4]
396 397 wu14=cadena[5:8]
397 398 wu15=cadena[9:12]
398 399 wu16=cadena[13:16]
399 400 su13=cadena[17:20]
400 401 su14=cadena[21:24]
401 402 su15=cadena[25:28]
402 403 su16=cadena[29:32]
403 404 if cnt==9:
404 405 nd01=cadena[1:4]
405 406 nd02=cadena[5:8]
406 407 nd03=cadena[9:12]
407 408 nd04=cadena[13:16]
408 409 ed01=cadena[17:20]
409 410 ed02=cadena[21:24]
410 411 ed03=cadena[25:28]
411 412 ed04=cadena[29:32]
412 413 if cnt==10:
413 414 nd05=cadena[1:4]
414 415 nd06=cadena[5:8]
415 416 nd07=cadena[9:12]
416 417 nd08=cadena[13:16]
417 418 ed05=cadena[17:20]
418 419 ed06=cadena[21:24]
419 420 ed07=cadena[25:28]
420 421 ed08=cadena[29:32]
421 422 if cnt==11:
422 423 nd09=cadena[1:4]
423 424 nd10=cadena[5:8]
424 425 nd11=cadena[9:12]
425 426 nd12=cadena[13:16]
426 427 ed09=cadena[17:20]
427 428 ed10=cadena[21:24]
428 429 ed11=cadena[25:28]
429 430 ed12=cadena[29:32]
430 431 if cnt==12:
431 432 nd13=cadena[1:4]
432 433 nd14=cadena[5:8]
433 434 nd15=cadena[9:12]
434 435 nd16=cadena[13:16]
435 436 ed13=cadena[17:20]
436 437 ed14=cadena[21:24]
437 438 ed15=cadena[25:28]
438 439 ed16=cadena[29:32]
439 440 if cnt==13:
440 441 wd01=cadena[1:4]
441 442 wd02=cadena[5:8]
442 443 wd03=cadena[9:12]
443 444 wd04=cadena[13:16]
444 445 sd01=cadena[17:20]
445 446 sd02=cadena[21:24]
446 447 sd03=cadena[25:28]
447 448 sd04=cadena[29:32]
448 449 if cnt==14:
449 450 wd05=cadena[1:4]
450 451 wd06=cadena[5:8]
451 452 wd07=cadena[9:12]
452 453 wd08=cadena[13:16]
453 454 sd05=cadena[17:20]
454 455 sd06=cadena[21:24]
455 456 sd07=cadena[25:28]
456 457 sd08=cadena[29:32]
457 458 if cnt==15:
458 459 wd09=cadena[1:4]
459 460 wd10=cadena[5:8]
460 461 wd11=cadena[9:12]
461 462 wd12=cadena[13:16]
462 463 sd09=cadena[17:20]
463 464 sd10=cadena[21:24]
464 465 sd11=cadena[25:28]
465 466 sd12=cadena[29:32]
466 467 if cnt==16:
467 468 wd13=cadena[1:4]
468 469 wd14=cadena[5:8]
469 470 wd15=cadena[9:12]
470 471 wd16=cadena[13:16]
471 472 sd13=cadena[17:20]
472 473 sd14=cadena[21:24]
473 474 sd15=cadena[25:28]
474 475 sd16=cadena[29:32]
475 476 blck_1_up = [nu01,nu02,nu03,nu04,eu01,eu02,eu03,eu04,nu05,nu06,nu07,nu08,eu05,eu06,eu07,eu08]
476 477 blck_1_dw = [nd01,nd02,nd03,nd04,ed01,ed02,ed03,ed04,nd05,nd06,nd07,nd08,ed05,ed06,ed07,ed08]
477 478 blck_2_up = [nu09,nu10,nu11,nu12,eu09,eu10,eu11,eu12,nu13,nu14,nu15,nu16,eu13,eu14,eu15,eu16]
478 479 blck_2_dw = [nd09,nd10,nd11,nd12,ed09,ed10,ed11,ed12,nd13,nd14,nd15,nd16,ed13,ed14,ed15,ed16]
479 480 blck_3_up = [wu01,wu02,wu03,wu04,su01,su02,su03,su04,wu05,wu06,wu07,wu08,su05,su06,su07,su08]
480 481 blck_3_dw = [wd01,wd02,wd03,wd04,sd01,sd02,sd03,sd04,wd05,wd06,wd07,wd08,sd05,sd06,sd07,sd08]
481 482 blck_4_up = [wu09,wu10,wu11,wu12,su09,su10,su11,su12,wu13,wu14,wu15,wu16,su13,su14,su15,su16]
482 483 blck_4_dw = [wd09,wd10,wd11,wd12,sd09,sd10,sd11,sd12,wd13,wd14,wd15,wd16,sd13,sd14,sd15,sd16]
483 484
484 485 lstup = blck_1_up + blck_2_up + blck_3_up + blck_4_up
485 486 lstdw = blck_1_dw + blck_2_dw + blck_3_dw + blck_4_dw
486 487
487 488 if polarizacion=='up':
488 489 return lstup
489 490 elif polarizacion=='dw':
490 491 return lstdw
491 492 fichero.close()
492 493
493 494 def CreaFormatoFinal(self, path, filename):
494 495 ne=self.NumeroDeExperimentos(filename)
495 496
496 497 #nombre01 = file1(archivo,'1')
497 498 nombre02 = self.file1(filename,'2')
498 499 fichero=open(path + 'CentralControlFormat.txt','w')
499 500 fichero.write(nombre02+'\n')
500 501 fichero.write(str(ne)+'\n')
501 502
502 503 for i in range(1,65):
503 504
504 505 if i<10:
505 506 nmod = '0'+str(i)
506 507 else: nmod = str(i)
507 508
508 509 fichero.write("ABS_" + nmod+'\n')
509 510
510 511 for j in range(1,ne+1):
511 512 ruta=path+str(j)+'.txt'
512 513 lu=self.CapturaValoresEnArchivo(ruta,polarizacion='up')
513 514 ld=self.CapturaValoresEnArchivo(ruta,polarizacion='dw')
514 515 part1=''
515 516 part2=''
516 517 if lu[i-1]=='1.0': part1='000'
517 518 if lu[i-1]=='2.0': part1='001'
518 519 if lu[i-1]=='3.0': part1='010'
519 520 if lu[i-1]=='0.0': part1='011'
520 521 if lu[i-1]=='0.5': part1='100'
521 522 if lu[i-1]=='1.5': part1='101'
522 523 if lu[i-1]=='2.5': part1='110'
523 524 if lu[i-1]=='3.5': part1='111'
524 525 if ld[i-1]=='1.0': part2='000'
525 526 if ld[i-1]=='2.0': part2='001'
526 527 if ld[i-1]=='3.0': part2='010'
527 528 if ld[i-1]=='0.0': part2='011'
528 529 if ld[i-1]=='0.5': part2='100'
529 530 if ld[i-1]=='1.5': part2='101'
530 531 if ld[i-1]=='2.5': part2='110'
531 532 if ld[i-1]=='3.5': part2='111'
532 533 fichero.write(part1+part2+'\n')
533 534 fichero.write('------'+'\n')
534 535 fichero.close()
535 536
536 537 def EliminaArchivosEnLaCarpeta(self, path, filename):
537 538 ne=self.NumeroDeExperimentos(filename)
538 539 for i in range(1,ne+1):
539 540 os.remove(path + str(i)+'.txt')
540 541
541 542
542 543 def toCentralControlFormat(self, filename):
543 544 """ Funcion que genera un archivo para el control central"""
544 545
545 546 path = os.getcwd() + '/'
546 547 self.CreaFicherosPrevios(path, filename)
547 548 self.CreaFormatoFinal(path, filename)
548 549 self.EliminaArchivosEnLaCarpeta(path, filename)
General Comments 0
You need to be logged in to leave comments. Login now