##// END OF EJS Templates
imanay -
r54:55
parent child
Show More
@@ -1,481 +1,488
1 1 # imports needed for the file convertion
2 2 import os
3 3 import sys
4 4 import time
5 5 import numpy as np
6 6
7 7 import library
8 8
9 9 class ABSClient:
10 10
11 11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.255", portDestino=7000):
12 12
13 13 self.ipSource = ipSource
14 14 self.ipDestino = ipDestino
15 15 self.portDestino = portDestino
16 16
17 17 self.createObjects()
18 18
19 19 def createObjects(self):
20 20
21 21 self.commObj = library.UDPComm(self.ipSource, self.ipDestino, self.portDestino)
22 22
23 23 def sendFile(self, filename):
24 24
25 25 #From matriz to control module format
26 26 self.FuncionMaestra_GeneraFormatoControlCentral(filename)
27 27 FileName = "FormatoControlCentral.txt"
28 28 F_Obj = open(FileName,"r")
29 29 FileList = F_Obj.readlines()
30 30 F_Obj.close()
31 31 FileStr = "".join(FileList)
32 32 data = FileStr
33 33
34 34 self.commObj.sendData(cmd="SNDF", data=data)
35 35 self.commObj.waitData()
36 36
37 37 def changeBeam(self, newBeam):
38 38
39 39 self.commObj.sendData(cmd="CHGB", data=newBeam)
40 40 self.commObj.waitData()
41 41
42 def __writeFile(self, filename, data):
43
44 fobj = open(filename,"w")
45 fobj.writelines(data)
46 fobj.close()
47
42 48 def getStatus(self, data):
43 49
44 50 self.commObj.sendData(cmd="ANST", data = data)
45 self.commObj.waitData2()
51 ipSource, ipDestino, cmd, data = self.commObj.waitData()
52 self.__writeFile("report.txt", data)
46 53
47 54 ##########
48 55
49 56 def FuncionMaestra_GeneraFormatoControlCentral(self,archivo):
50 57 """ Funcion que genera un archivo para el control central"""
51 58
52 59 # CarpetaDeTrabajo='/home/redes/ABS_Control_2012_09_24/Control_Module_v1_Client_09_24/'
53 60 CarpetaDeTrabajo = os.getcwd() + '/'
54 61 #print CarpetaDeTrabajo
55 62 #CarpetaDeTrabajo='/home/redes/workspace/ABS_Client_v2/Debug/'
56 63
57 64 def lst2string(lst):
58 65 string=''
59 66 for i in lst:
60 67 string=string+i
61 68 return string
62 69
63 70 def string2lst(string):
64 71 lst = []
65 72 for i in string:
66 73 lst.append(i)
67 74 return lst
68 75
69 76
70 77 def file1(string, type):
71 78 lst = string2lst(archivo)
72 79 fin = -1
73 80 t = len(lst)
74 81 for i in np.arange(-1,-t,-1):
75 82 if lst[i]=='/':
76 83 fin=i
77 84 break
78 85 if type == '1':
79 86 nombre2 = lst[fin+1:]
80 87 nombre2[-1]='s'
81 88 nombre2 = lst2string(nombre2)
82 89 return nombre2
83 90 if type == '2':
84 91 nombre2 = lst[fin+1:]
85 92 nombre2[-1]='1'
86 93 nombre2 = lst2string(nombre2)
87 94 return nombre2
88 95
89 96
90 97 def EliminaSaltoDeLinea(cadena):
91 98 i = 0
92 99 for elemento in cadena:
93 100 if elemento =='\n' or elemento =='\r':
94 101 pass
95 102 else:
96 103 i=i+1
97 104 return cadena [:i]
98 105
99 106 def NumeroDeExperimentos(path):
100 107 fichero1=open(path,'r')
101 108 cont=0
102 109 for cadena in fichero1:
103 110 cont=cont+1
104 111 if cont==3:
105 112 nexp=''
106 113 pos=0
107 114 for elemento in cadena:
108 115 pos=pos+1
109 116 if elemento=='=':
110 117 nexp=int(cadena[pos:])
111 118 return nexp
112 119 fichero1.close()
113 120
114 121 def Paridad(numero):
115 122 if numero%2==0: return 'par'
116 123 elif numero%2==1: return 'impar'
117 124
118 125 def EvaluaCadena(cadena):
119 126 if len(cadena)>35:
120 127 if cadena[-1]=='$':
121 128 return cadena[-35:-2]
122 129 elif cadena[-1]==']':
123 130 return cadena[-34:-1]
124 131 else:
125 132 return None
126 133
127 134 def GuardaEnLista(path):
128 135 fichero=open(path,'r')
129 136 lista=[]
130 137 for cadena in fichero:
131 138 cadena = EliminaSaltoDeLinea(cadena)
132 139 cadena = EvaluaCadena(cadena)
133 140 if cadena != None:
134 141 lista.append(cadena)
135 142 fichero.close()
136 143 return lista
137 144
138 145 def CreaFicherosPrevios():
139 146 vector = GuardaEnLista(archivo)
140 147 for i in range(1,NumeroDeExperimentos(archivo)+1):
141 148 fichero =open(CarpetaDeTrabajo+str(i)+'.txt','w')
142 149 for j in range(0,16):
143 150 fichero.write(vector[j+16*(i-1)]+'\n')
144 151 fichero.close()
145 152
146 153 def CapturaValoresEnArchivo(path,polarizacion='up'):
147 154 fichero =open(path,'r')
148 155 cnt=0
149 156 lstup=[]
150 157 lstdw=[]
151 158 for cadena in fichero:
152 159 cnt=cnt+1
153 160 if cnt==5:
154 161 su01=cadena[17:20]
155 162 su02=cadena[21:24]
156 163 su03=cadena[25:28]
157 164 su04=cadena[29:32]
158 165 if cnt==6:
159 166 su05=cadena[17:20]
160 167 su06=cadena[21:24]
161 168 su07=cadena[25:28]
162 169 su08=cadena[29:32]
163 170 if cnt==7:
164 171 su09=cadena[17:20]
165 172 su10=cadena[21:24]
166 173 su11=cadena[25:28]
167 174 su12=cadena[29:32]
168 175 if cnt==8:
169 176 su13=cadena[17:20]
170 177 su14=cadena[21:24]
171 178 su15=cadena[25:28]
172 179 su16=cadena[29:32]
173 180 if cnt==13:
174 181 sd01=cadena[17:20]
175 182 sd02=cadena[21:24]
176 183 sd03=cadena[25:28]
177 184 sd04=cadena[29:32]
178 185 if cnt==14:
179 186 sd05=cadena[17:20]
180 187 sd06=cadena[21:24]
181 188 sd07=cadena[25:28]
182 189 sd08=cadena[29:32]
183 190 if cnt==15:
184 191 sd09=cadena[17:20]
185 192 sd10=cadena[21:24]
186 193 sd11=cadena[25:28]
187 194 sd12=cadena[29:32]
188 195 if cnt==16:
189 196 sd13=cadena[17:20]
190 197 sd14=cadena[21:24]
191 198 sd15=cadena[25:28]
192 199 sd16=cadena[29:32]
193 200 lstup=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16]
194 201 lstdw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16]
195 202 if polarizacion=='up':
196 203 return lstup
197 204 elif polarizacion=='dw':
198 205 return lstdw
199 206 fichero.close()
200 207
201 208 def CapturaValoresEnArchivo2(path,polarizacion='up'):
202 209 fichero =open(path,'r')
203 210 cnt=0
204 211 lstup=[]
205 212 lstdw=[]
206 213 for cadena in fichero:
207 214 cnt=cnt+1
208 215 if cnt==1:
209 216 nu01=cadena[1:4]
210 217 nu02=cadena[5:8]
211 218 nu03=cadena[9:12]
212 219 nu04=cadena[13:16]
213 220 eu01=cadena[17:20]
214 221 eu02=cadena[21:24]
215 222 eu03=cadena[25:28]
216 223 eu04=cadena[29:32]
217 224 if cnt==2:
218 225 nu05=cadena[1:4]
219 226 nu06=cadena[5:8]
220 227 nu07=cadena[9:12]
221 228 nu08=cadena[13:16]
222 229 eu05=cadena[17:20]
223 230 eu06=cadena[21:24]
224 231 eu07=cadena[25:28]
225 232 eu08=cadena[29:32]
226 233 if cnt==3:
227 234 nu09=cadena[1:4]
228 235 nu10=cadena[5:8]
229 236 nu11=cadena[9:12]
230 237 nu12=cadena[13:16]
231 238 eu09=cadena[17:20]
232 239 eu10=cadena[21:24]
233 240 eu11=cadena[25:28]
234 241 eu12=cadena[29:32]
235 242 if cnt==4:
236 243 nu13=cadena[1:4]
237 244 nu14=cadena[5:8]
238 245 nu15=cadena[9:12]
239 246 nu16=cadena[13:16]
240 247 eu13=cadena[17:20]
241 248 eu14=cadena[21:24]
242 249 eu15=cadena[25:28]
243 250 eu16=cadena[29:32]
244 251 if cnt==5:
245 252 wu01=cadena[1:4]
246 253 wu02=cadena[5:8]
247 254 wu03=cadena[9:12]
248 255 wu04=cadena[13:16]
249 256 su01=cadena[17:20]
250 257 su02=cadena[21:24]
251 258 su03=cadena[25:28]
252 259 su04=cadena[29:32]
253 260 if cnt==6:
254 261 wu05=cadena[1:4]
255 262 wu06=cadena[5:8]
256 263 wu07=cadena[9:12]
257 264 wu08=cadena[13:16]
258 265 su05=cadena[17:20]
259 266 su06=cadena[21:24]
260 267 su07=cadena[25:28]
261 268 su08=cadena[29:32]
262 269 if cnt==7:
263 270 wu09=cadena[1:4]
264 271 wu10=cadena[5:8]
265 272 wu11=cadena[9:12]
266 273 wu12=cadena[13:16]
267 274 su09=cadena[17:20]
268 275 su10=cadena[21:24]
269 276 su11=cadena[25:28]
270 277 su12=cadena[29:32]
271 278 if cnt==8:
272 279 wu13=cadena[1:4]
273 280 wu14=cadena[5:8]
274 281 wu15=cadena[9:12]
275 282 wu16=cadena[13:16]
276 283 su13=cadena[17:20]
277 284 su14=cadena[21:24]
278 285 su15=cadena[25:28]
279 286 su16=cadena[29:32]
280 287 if cnt==9:
281 288 nd01=cadena[1:4]
282 289 nd02=cadena[5:8]
283 290 nd03=cadena[9:12]
284 291 nd04=cadena[13:16]
285 292 ed01=cadena[17:20]
286 293 ed02=cadena[21:24]
287 294 ed03=cadena[25:28]
288 295 ed04=cadena[29:32]
289 296 if cnt==10:
290 297 nd05=cadena[1:4]
291 298 nd06=cadena[5:8]
292 299 nd07=cadena[9:12]
293 300 nd08=cadena[13:16]
294 301 ed05=cadena[17:20]
295 302 ed06=cadena[21:24]
296 303 ed07=cadena[25:28]
297 304 ed08=cadena[29:32]
298 305 if cnt==11:
299 306 nd09=cadena[1:4]
300 307 nd10=cadena[5:8]
301 308 nd11=cadena[9:12]
302 309 nd12=cadena[13:16]
303 310 ed09=cadena[17:20]
304 311 ed10=cadena[21:24]
305 312 ed11=cadena[25:28]
306 313 ed12=cadena[29:32]
307 314 if cnt==12:
308 315 nd13=cadena[1:4]
309 316 nd14=cadena[5:8]
310 317 nd15=cadena[9:12]
311 318 nd16=cadena[13:16]
312 319 ed13=cadena[17:20]
313 320 ed14=cadena[21:24]
314 321 ed15=cadena[25:28]
315 322 ed16=cadena[29:32]
316 323 if cnt==13:
317 324 wd01=cadena[1:4]
318 325 wd02=cadena[5:8]
319 326 wd03=cadena[9:12]
320 327 wd04=cadena[13:16]
321 328 sd01=cadena[17:20]
322 329 sd02=cadena[21:24]
323 330 sd03=cadena[25:28]
324 331 sd04=cadena[29:32]
325 332 if cnt==14:
326 333 wd05=cadena[1:4]
327 334 wd06=cadena[5:8]
328 335 wd07=cadena[9:12]
329 336 wd08=cadena[13:16]
330 337 sd05=cadena[17:20]
331 338 sd06=cadena[21:24]
332 339 sd07=cadena[25:28]
333 340 sd08=cadena[29:32]
334 341 if cnt==15:
335 342 wd09=cadena[1:4]
336 343 wd10=cadena[5:8]
337 344 wd11=cadena[9:12]
338 345 wd12=cadena[13:16]
339 346 sd09=cadena[17:20]
340 347 sd10=cadena[21:24]
341 348 sd11=cadena[25:28]
342 349 sd12=cadena[29:32]
343 350 if cnt==16:
344 351 wd13=cadena[1:4]
345 352 wd14=cadena[5:8]
346 353 wd15=cadena[9:12]
347 354 wd16=cadena[13:16]
348 355 sd13=cadena[17:20]
349 356 sd14=cadena[21:24]
350 357 sd15=cadena[25:28]
351 358 sd16=cadena[29:32]
352 359 lst_n_up=[nu01,nu02,nu03,nu04,nu05,nu06,nu07,nu08,nu09,nu10,nu11,nu12,nu13,nu14,nu15,nu16]
353 360 lst_n_dw=[nd01,nd02,nd03,nd04,nd05,nd06,nd07,nd08,nd09,nd10,nd11,nd12,nd13,nd14,nd15,nd16]
354 361 lst_s_up=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16]
355 362 lst_s_dw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16]
356 363 lst_w_up=[wu01,wu02,wu03,wu04,wu05,wu06,wu07,wu08,wu09,wu10,wu11,wu12,wu13,wu14,wu15,wu16]
357 364 lst_w_dw=[wd01,wd02,wd03,wd04,wd05,wd06,wd07,wd08,wd09,wd10,wd11,wd12,wd13,wd14,wd15,wd16]
358 365 lst_e_up=[eu01,eu02,eu03,eu04,eu05,eu06,eu07,eu08,eu09,eu10,eu11,eu12,eu13,eu14,eu15,eu16]
359 366 lst_e_dw=[ed01,ed02,ed03,ed04,ed05,ed06,ed07,ed08,ed09,ed10,ed11,ed12,ed13,ed14,ed15,ed16]
360 367
361 368 lstup = lst_s_up + lst_w_up + lst_n_up + lst_e_up
362 369 lstdw = lst_s_dw + lst_w_dw + lst_n_up + lst_e_up
363 370
364 371 if polarizacion=='up':
365 372 return lstup
366 373 elif polarizacion=='dw':
367 374 return lstdw
368 375 fichero.close()
369 376
370 377
371 378 def CreaFormatoFinal():
372 379 ne=NumeroDeExperimentos(archivo)
373 380
374 381 #nombre01 = file1(archivo,'1')
375 382 nombre02 = file1(archivo,'2')
376 383 fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w')
377 384 fichero.write(nombre02+'\n')
378 385 fichero.write(str(ne)+'\n')
379 386 for i in range(1,17):
380 387
381 388 if i<10:
382 389 nmod = '0'+str(i)
383 390 else: nmod = str(i)
384 391
385 392
386 393 fichero.write('S'+nmod+'\n')
387 394 for j in range(1,ne+1):
388 395 ruta=CarpetaDeTrabajo+str(j)+'.txt'
389 396 lu=CapturaValoresEnArchivo(ruta,polarizacion='up')
390 397 ld=CapturaValoresEnArchivo(ruta,polarizacion='dw')
391 398 part1=''
392 399 part2=''
393 400 if lu[i-1]=='1.0': part1='000'
394 401 if lu[i-1]=='2.0': part1='001'
395 402 if lu[i-1]=='3.0': part1='010'
396 403 if lu[i-1]=='0.0': part1='011'
397 404 if lu[i-1]=='0.5': part1='100'
398 405 if lu[i-1]=='1.5': part1='101'
399 406 if lu[i-1]=='2.5': part1='110'
400 407 if lu[i-1]=='3.5': part1='111'
401 408 if ld[i-1]=='1.0': part2='000'
402 409 if ld[i-1]=='2.0': part2='001'
403 410 if ld[i-1]=='3.0': part2='010'
404 411 if ld[i-1]=='0.0': part2='011'
405 412 if ld[i-1]=='0.5': part2='100'
406 413 if ld[i-1]=='1.5': part2='101'
407 414 if ld[i-1]=='2.5': part2='110'
408 415 if ld[i-1]=='3.5': part2='111'
409 416 fichero.write(part1+part2+'\n')
410 417 fichero.write('------'+'\n')
411 418 fichero.close()
412 419
413 420 def CreaFormatoFinal2():
414 421 ne=NumeroDeExperimentos(archivo)
415 422
416 423 #nombre01 = file1(archivo,'1')
417 424 nombre02 = file1(archivo,'2')
418 425 fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w')
419 426 fichero.write(nombre02+'\n')
420 427 fichero.write(str(ne)+'\n')
421 428
422 429 for i in range(1,65):
423 430
424 431 if i<10:
425 432 nmod = '0'+str(i)
426 433 else: nmod = str(i)
427 434
428 435 fichero.write("ABS_" + nmod+'\n')
429 436
430 437 for j in range(1,ne+1):
431 438 ruta=CarpetaDeTrabajo+str(j)+'.txt'
432 439 lu=CapturaValoresEnArchivo2(ruta,polarizacion='up')
433 440 ld=CapturaValoresEnArchivo2(ruta,polarizacion='dw')
434 441 part1=''
435 442 part2=''
436 443 if lu[i-1]=='1.0': part1='000'
437 444 if lu[i-1]=='2.0': part1='001'
438 445 if lu[i-1]=='3.0': part1='010'
439 446 if lu[i-1]=='0.0': part1='011'
440 447 if lu[i-1]=='0.5': part1='100'
441 448 if lu[i-1]=='1.5': part1='101'
442 449 if lu[i-1]=='2.5': part1='110'
443 450 if lu[i-1]=='3.5': part1='111'
444 451 if ld[i-1]=='1.0': part2='000'
445 452 if ld[i-1]=='2.0': part2='001'
446 453 if ld[i-1]=='3.0': part2='010'
447 454 if ld[i-1]=='0.0': part2='011'
448 455 if ld[i-1]=='0.5': part2='100'
449 456 if ld[i-1]=='1.5': part2='101'
450 457 if ld[i-1]=='2.5': part2='110'
451 458 if ld[i-1]=='3.5': part2='111'
452 459 fichero.write(part1+part2+'\n')
453 460 fichero.write('------'+'\n')
454 461 fichero.close()
455 462
456 463 def EliminaArchivosEnLaCarpeta():
457 464 ne=NumeroDeExperimentos(archivo)
458 465 for i in range(1,ne+1):
459 466 os.remove(CarpetaDeTrabajo+str(i)+'.txt')
460 467
461 468 CreaFicherosPrevios()
462 469 CreaFormatoFinal2()
463 470 EliminaArchivosEnLaCarpeta()
464 471
465 472 ##########
466 473
467 474 if __name__ == '__main__':
468 475
469 476 filename = "experimento1.abs"
470 477
471 478 absObj = ABSClient()
472 479 absObj.sendFile(filename)
473 480 # absObj.changeBeam("0")
474 481 # absObj.changeBeam("1")
475 482 # absObj.changeBeam("2")
476 483 # absObj.changeBeam("3")
477 484 # absObj.changeBeam("4")
478 485 # absObj.changeBeam("5")
479 486 # absObj.changeBeam("6")
480 487 # absObj.changeBeam("7")
481 488 # absObj.getStatus(5) No newline at end of file
@@ -1,114 +1,100
1 1 #import tftpy
2 2 import socket
3 3
4 4 class UDPComm:
5 5
6 6 __HEADER = "ABS"
7 7
8 8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9 9
10 10 self.ipSource = ipSource
11 11 self.ipDestino = ipDestino
12 12 self.portDestino = portDestino
13 13 self.addr = (ipDestino,portDestino)
14 14 self.answer = ipDestino #test
15 self.mode = "none"
15 16
16 17 self.openSocket(asServer)
17 18
18 19 def openSocket(self, asServer):
19 20
20 21 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
21 22 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
22 23 # self.socket_c.connect((self.ipDestino, self.portDestino))
23 24
24 25 if asServer:
25 26 self.configAsServer()
27 self.mode = "server"
26 28 else:
27 29 self.configAsClient()
30 self.mode = "client"
28 31
29 32 def configAsClient(self):
30 33 #Configurar broadcast
31 34 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
32 35
33 36 def configAsServer(self):
34 37
35 38 self.socket_c.bind(self.addr)
36 39 print "\nServer initialized"
37 40
38 41 def waitData(self, nbytes = 16384):
39 42
40 43 print "\nWaiting some data"
41 44 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
42 45 print "\nThis socket has received some data"
46 print self.answer
43 47 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
44
45 return ipSource, ipDestino, cmd, data
46
47 def waitData2(self, nbytes = 16384):
48
49 print "\nWaiting some data"
50 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
51 print "\nThis socket has received some data"
52 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
53 filename = "report.txt"
54 fobj = open(filename,"w")
55 fobj.writelines(data)
56 fobj.close()
57 48
58 49 return ipSource, ipDestino, cmd, data
59 50
60 51 def sendData(self, cmd, data):
61 52
62 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
53 if self.portDestino == 7000:
54 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
55 else:
56 trama_tx = data
57
58 if self.mode == "client":
59 destiny = self.addr
60 else:
61 destiny = self.answer
63 62 # Send messages
64 if(self.socket_c.sendto(trama_tx, self.addr)):
63 if(self.socket_c.sendto(trama_tx, destiny)):
65 64 print "Sending message:[" + trama_tx + "]"
66
67 def sendData2(self, cmd, data):
68
69 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
70 # Send messages
71 if(self.socket_c.sendto(trama_tx, self.answer)):
72 print "Sending message:[" + trama_tx + "]"
73
74 def sendData2ControlModules(self, trama_tx):
75
76 # Send messages
77 if(self.socket_c.sendto(trama_tx, self.addr)):
78 print "Sending message:[" + trama_tx + "]"
79 65
80 66 def __getTrama(self, trama):
81 67
82 68 FrameList = trama.split(':')
83 69
84 70 header = FrameList[0]
85 71 ipSource = FrameList[1]
86 72 ipDestino = FrameList[2]
87 73 cmd = FrameList[3]
88 74 data = FrameList[4]
89 75 trash = FrameList[5]
90 76
91 77 return ipSource, ipDestino, cmd, data
92 78
93 79 #class FTPComm:
94 80 #
95 81 # ftp_servidor = 'ftp.servidor.com'
96 82 # ftp_usuario = 'miusuario'
97 83 # ftp_clave = 'miclave'
98 84 # ftp_raiz = '/public_html'
99 85 #
100 86 # def __init__(self):
101 87 #
102 88 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
103 89 #
104 90 #
105 91 # def sendFile(self, filename):
106 92 #
107 93 # self.client.upload(filename)
108 94 #
109 95 #if __name__ == '__main__':
110 96 #
111 97 # obj = FTPComm()
112 98
113 99
114 100 No newline at end of file
@@ -1,320 +1,282
1 1 import os
2 2 import library
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.255", portDestino=7000, ipDestino2="192.168.1.255", portDestino2=5500, ftpPortDestino=None):
8 8
9 9 self.ipSource = ipSource
10 10 self.ipDestino = ipDestino
11 11 self.portDestino = portDestino
12 12
13 13 self.ipDestino2 = ipDestino2
14 14 self.portDestino2 = portDestino2
15 15
16 16 self.ftpPortDestino = ftpPortDestino
17 17 self.experiment_name = "default"
18 18 self.tx_buffer = "default"
19 19
20 20 self.createObjects()
21 21
22 22 def createObjects(self):
23 23
24 24 asServer = True
25 25 self.commServerObj = library.UDPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
26 26 self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
27 27 #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
28
29 def sendFile(self, base):
30
31 #Needed for the loop
32 rx_frame_list = self.datarx.split('\n',2)
33
34 experiment_name = rx_frame_list[0]
35 experiment_number = rx_frame_list[1]
36 str_control_modules = rx_frame_list[2]
37
38 lst_control_modules = str_control_modules.split("------\n")
39 #Setting variables of the loop
40 i =3
41 module = 1
42 number_of_modules = 16
43 number_of_beams = int (experiment_number)
44 module_list = self.connection_status(10)
45 #Loop for creating and sending the control module files
46 while module <= number_of_modules:
47 #Generating the control modules files
48 fobj = open(experiment_name,"w")
49 fobj.write(experiment_name + "\n")
50 fobj.write("------\n")
51 # tmp_list = rx_frame_list[i:i + number_of_beams + 1]
52 fobj.write(lst_control_modules[module-1])
53 fobj.write("------\n")
54 fobj.close()
55 if module_list[module -1] == "1":
56 #Preparing and doing the tftp command
57 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c put " + experiment_name
58 print cmd
59 os.system(cmd)
60 #Updating variables of the loop
61 i = i + number_of_beams + 2
62 module += 1
63 #Working with the UDP socket
64 # self.commClientObj.sendData2ControlModules("CARGA:" + experiment_name + ":")
65 # self.commClientObj.sendData2ControlModules("CAMBIA:0:")
66 #Another tftp way
67 # self.ftpCommObj.sendFile(filename)
68 # rpta = self.commClientObj.sendTxRxCommand(cmd# filename2 = "report.txt"
69 #fobj2 = open(filename2,"w")
70 #fobj2.write("Verification_file\n")
71 #fobj2.write("-----------------\n")
72 #fobj2.close()='CARGA', data=filename)
73 self.experiment_name = experiment_name
74 28
75 29
76 30 def waitRequest(self):
77 31
78 32 ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData()
79 33
80 34 datarpta = "OK"
81 35
82 36 if cmd == "SNDF":
83 37 self.sendFile2Modules()
84 38
85 39 if cmd == "CHGB":
86 40 self.changeBeam()
87 41
88 42 if cmd == "ANST":
89 self.getStatus(10, mode=2)
43 self.getStatus(mode=3)
90 44 datarpta = self.tx_buffer
91 45
92 self.commServerObj.sendData2(cmd=cmd, data=datarpta)
93
46 self.commServerObj.sendData(cmd=cmd, data=datarpta)
47
48 def checkModule(self, address):
49
50 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
51 status = os.system(cmd)
52
53 if status == 256:
54 return False
55
56 return True
94 57
95 58 def __writeReport(self, enaModules):
96 59
97 60 status_array = ["Status of modules\n"]
98 61 status_array.append("----------------\n")
99 62
100 63 for address in range(1,65):
101 64 if address in enaModules:
102 65 # status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n")
103 66 status_array.append("192.168.1." + str(address) + " [1 1]\n")
104 67 else:
105 68 status_array.append("192.168.1." + str(address) + " [0 0]\n")
106 69
107 70 f = open("module_status.txt","w")
108 71 f.writelines(status_array)
109 72 f.close()
110
111 def checkModule(self, address):
112
113 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
114 status = os.system(cmd)
115
116 if status == 256:
117 return False
118
119 return True
120 73
121 74 def checkAntenna(self):
122 75
123 76 """
124 77 Direccion de los modulos de las antenas:
125 78
126 79 Norte : 01-16
127 80 Este : 17-32
128 81 Oeste: : 33-48
129 82 Sur : 49-64
130 83
131 84 """
132 85
133 86 enaModules = []
134 87
135 88 for address in range(1,65):
136 89 if self.checkModule(address):
137 90 enaModules.append(address)
138 91
139 92 self.__writeReport(enaModules)
140 93 return enaModules
141
142 def __loadFile(self):
143
144 #Working with the UDP socket
145 self.commClientObj.sendData2ControlModules("CARGA:" + self.experiment_name + ":")
146 self.commClientObj.sendData2ControlModules("CAMBIA:0:")
94
95 def sendFile2Modules(self):
96
97 #Needed for the loop
98 rx_frame_list = self.datarx.split('\n',2)
99
100 experiment_name = rx_frame_list[0]
101 experiment_number = rx_frame_list[1]
102 str_control_modules = rx_frame_list[2]
103
104 lst_control_modules = str_control_modules.split("------\n")
105
106 enaModules = self.checkAntenna()
107
108 for address in range(1,65):
109
110 if address not in enaModules:
111 continue
112
113 self.__writeModuleFile(experiment_name, lst_control_modules[address-1])
114
115 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + experiment_name
116 print cmd
117 os.system(cmd)
118
119 self.experiment_name = experiment_name
120
121 self.__loadFile()
147 122
148 123 def __writeModuleFile(self, filename, str):
149 124
150 125 fobj = open(filename,"w")
151 126 fobj.write(filename + "\n")
152 127 fobj.write("------\n")
153 128 fobj.write(str)
154 129 fobj.write("------\n")
155 130 fobj.close()
156
157 def sendFile2Modules(self):
158
159 #Needed for the loop
160 rx_frame_list = self.datarx.split('\n',2)
161
162 experiment_name = rx_frame_list[0]
163 experiment_number = rx_frame_list[1]
164 str_control_modules = rx_frame_list[2]
165
166 lst_control_modules = str_control_modules.split("------\n")
167
168 enaModules = self.checkAntenna()
169
170 for address in range(1,65):
171
172 if address not in enaModules:
173 continue
174
175 self.__writeModuleFile(experiment_name, lst_control_modules[address-1])
176
177 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + experiment_name
178 print cmd
179 os.system(cmd)
180
181 self.experiment_name = experiment_name
182
183 self.__loadFile()
131
132 def __readModuleFile(self, filename):
133
134 fobj1 = open(filename,"r")
135 file_list_1 = fobj1.readlines()
136 fobj1.close()
137 content_str = ''.join(file_list_1[2:-1])
138
139 return content_str
140
141 def __loadFile(self):
142
143 #Working with the UDP socket
144 self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":")
145 self.commClientObj.sendData("none", "CAMBIA:0:")
184 146
185 147 def changeBeam(self):
186 148
187 149 #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
188 self.commClientObj.sendData2ControlModules("CAMBIA:" + self.datarx + ":")
150 self.commClientObj.sendData("CAMBIA:" + self.datarx + ":")
151
152 def getStatus(self,mode):
153
154 if mode == 1:
155 self.__getStsMode1()
156 elif mode == 2:
157 self.__getStsMode2()
158 else:
159 self.__getStsMode3()
160
189 161
190 162 def __getStsMode1(self):
191 163 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
192 self.commClientObj.sendData2ControlModules("CHEQUEO:" + self.datarx + ":")
164 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
193 165 seconds = int (self.datarx)
194 166 # Give 5 seconds to the control modules
195 167 time.sleep(seconds)
196 168 # Checking the module connection
197 169 module_list = self.connection_status(10)
198 170 #Generating the complete report
199 171 module = 1
200 172 number_of_modules = 16
201 173 filename1 = "Verificacion"
202 174 filename2 = "report.txt"
203 175 fobj2 = open(filename2,"w")
204 176 fobj2.write("Verification_file\n")
205 177 fobj2.write("-----------------\n")
206 178 fobj2.close()
207 179 while module <= number_of_modules:
208 180 if module_list[module -1] == "1":
209 181 #Preparing and doing the tftp command
210 182 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1
211 183 print cmd
212 184 os.system(cmd)
213 185 # Getting data from the control module file
214 186 fobj1 = open(filename1,"r")
215 187 file_list_1 = fobj1.readlines()
216 188 fobj1.close()
217 189 content = file_list_1[2:-1]
218 190 #
219 191 fobj2 = open(filename2,"a")
220 192 if base == 10:
221 193 fobj2.write("S" + str(module) + "\n")
222 194 else:
223 195 fobj2.write("N" + str(module) + "\n")
224 196 fobj2.writelines(content)
225 197 fobj2.write("------\n")
226 198 fobj2.close()
227 199 module = module + 1
228 200
229 201 def __getStsMode2(self):
230 202
231 203 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
232 self.commClientObj.sendData2ControlModules("CHEQUEO:" + self.datarx + ":")
204 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
233 205 seconds = int (self.datarx)
234 206 # Give 5 seconds to the control modules
235 207 time.sleep(seconds)
236 208 # Checking the module connection
237 209 enaModules = self.checkAntenna()
238 210 #Generating the complete report
239 module = 1
240 number_of_modules = 16
241 211 filename1 = "Verificacion"
242 212 line1 = "Verification_file\n"
243 213 line2 = "-----------------\n"
244 214 report_list = [line1, line2]
245 215
246 216 for address in range(1,65):
247 217
248 218 if address not in enaModules:
249 219 continue
250 220 #Preparing and doing the tftp command
251 221 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1
252 222 print cmd
253 223 os.system(cmd)
254 224 #Sub_header
255 225 report_list.append("ABS_" + str(address) + "\n")
256 226 # Content
257 227 fobj1 = open(filename1,"r")
258 228 file_list_1 = fobj1.readlines()
259 229 fobj1.close()
260 230 content = ''.join(file_list_1[2:-1])
261 231 report_list.append(content)
262 232 #Ending
263 233 report_list.append("------\n")
264 234 #print "\nFinalizado"
265 self.tx_buffer = ''.join(report_list)
266
235 self.tx_buffer = ''.join(report_list)
236
237 def __AddingHeader(self,content_list, title):
238
239 line1 = title + "\n"
240 line2 = "-----------------\n"
241 header_list = [line1, line2]
242 verification_list = header_list + content_list
243 # Arming the frame
244 self.tx_buffer = ''.join(verification_list)
245
246 def __getModuleFile(self, filename):
247
248 enaModules = self.checkAntenna()
249 content_list = []
250 for address in range(1,65):
251
252 if address not in enaModules:
253 continue
254 #Preparing and doing the tftp command
255 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename
256 print cmd
257 os.system(cmd)
258 #Sub_header
259 content_list.append("ABS_" + str(address) + "\n")
260 # From module file to list
261 content_str = self.__readModuleFile(filename)
262 content_list.append(content_str)
263 content_list.append("------\n")
264
265 self.__AddingHeader(content_list, title = "Verification_file")
266
267 267 def __getStsMode3(self):
268 268
269 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
270 self.commClientObj.sendData2ControlModules("CHEQUEO:" + self.datarx + ":")
269 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
271 270 seconds = int (self.datarx)
272 271 # Give 5 seconds to the control modules
273 272 time.sleep(seconds)
274 # Checking the module connection
275 module_list = self.connection_status(10)
276 #Generating the complete report
277 module = 1
278 number_of_modules = 16
279 filename1 = "Verificacion"
280 line1 = "Verification_file\n"
281 line2 = "-----------------\n"
282 report_list = [line1, line2]
283 while module <= number_of_modules:
284 if module_list[module -1] == "1":
285 #Preparing and doing the tftp command
286 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1
287 print cmd
288 os.system(cmd)
289 #Sub_header
290 if base == 10:
291 report_list.append("S" + str(module) + "\n")
292 else:
293 report_list.append("N" + str(module) + "\n")
294 # Content
295 fobj1 = open(filename1,"r")
296 file_list_1 = fobj1.readlines()
297 fobj1.close()
298 content = ''.join(file_list_1[2:-1])
299 # content = "010101\n"
300 report_list.append(content)
301 #Ending
302 report_list.append("------\n")
303 module = module + 1
304 #print "\nFinalizado"
305 self.tx_buffer = ''.join(report_list)
306
307 def getStatus(self, base, mode):
308
309 if mode == 1:
310 self.__getStsMode1()
311 else:
312 self.__getStsMode2()
273
274 self.__getModuleFile(filename = "Verificacion")
313 275
314 276
315 277 if __name__ == '__main__':
316 278
317 279 absObj = ABSServer()
318 280
319 281 while 1:
320 282 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now