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