##// END OF EJS Templates
Trying frame without semicolon.
imanay -
r101:102
parent child
Show More
@@ -1,70 +1,72
1 1 import library3
2 2
3 3 class ABSClient:
4 4
5 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000):
5 def __init__(self,ipSource="192.168.1.117", iDSource="Clnt_01", ipDestino="192.168.1.117", iDDestino = "CeCnMod", portDestino=7000):
6 6
7 7 self.ipSource = ipSource
8 self.iDSource = iDSource
8 9 self.ipDestino = ipDestino
10 self.iDDestino = iDDestino
9 11 self.portDestino = portDestino
10 12
11 13 self.createObjects()
12 14
13 15 def createObjects(self):
14 16
15 self.commObj = library3.TCPComm(self.ipSource, self.ipDestino, self.portDestino)
17 self.commObj = library3.TCPComm(self.ipSource, self.iDSource, self.ipDestino, self.iDDestino, self.portDestino)
16 18 self.wFiles = library3.FilesStuff()
17 19
18 20 def __ConnectionWithCentralControl(self, cmd, data):
19 21
20 22 self.commObj.open_socket()
21 23 self.commObj.sendData(cmd = cmd, data = data, ipDestino = self.ipDestino)
22 24 ipSource, ipDestino, cmd, output = self.commObj.waitData()
23 25 self.commObj.close_socket()
24 26
25 27 return output
26 28
27 29 def abs2ControlModuleFormatFile(self, filename):
28 30
29 31 #From matriz to control module format
30 32 self.wFiles.toCentralControlFormat(filename)
31 33 FileName = "CentralControlFormat.txt"
32 34 F_Obj = open(FileName,"r")
33 35 FileList = F_Obj.readlines()
34 36 F_Obj.close()
35 37 FileStr = "".join(FileList)
36 38
37 39 return FileStr
38 40
39 41 def sendFile(self, filename):
40 42
41 43 data = self.abs2ControlModuleFormatFile(filename)
42 44
43 45 self.__ConnectionWithCentralControl(cmd = "SNDF", data = data)
44 46
45 47 def changeBeam(self, newBeam):
46 48
47 49 self.__ConnectionWithCentralControl(cmd = "CHGB", data = newBeam)
48 50
49 51 def __writeFile(self, filename, data):
50 52
51 53 fobj = open(filename,"w")
52 54 fobj.writelines(data)
53 55 fobj.close()
54 56
55 57 def getControlModuleStatus(self, data):
56 58
57 59 data = self.__ConnectionWithCentralControl(cmd = "ANST", data = data)
58 60 self.__writeFile("report.txt", data)
59 61
60 62 def getConnectionStatus(self):
61 63
62 64 data = self.__ConnectionWithCentralControl(cmd = "NTST", data = "none")
63 65 self.__writeFile("connection_status.txt", data)
64 66
65 67 if __name__ == '__main__':
66 68
67 69 filename = "experimento1.abs"
68 70
69 71 absObj = ABSClient()
70 72 absObj.sendFile(filename)
@@ -1,447 +1,527
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 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
12 def __init__(self, ipSource, iDSource, ipDestino, iDDestino, portDestino, asServer=False):
13 13
14 14 self.ipSource = ipSource
15 self.iDSource = iDSource
15 16 self.ipDestino = ipDestino
17 self.iDDestino = iDDestino
16 18 self.portDestino = portDestino
17 19 self.addr = (ipDestino,portDestino)
18 20
19 21 self.sc = "none"
20 22 self.answer = "none" #test
21 23 self.asServer = False
22 self.len = 0
23 self.crc = 0
24 self.len = "000000"
25 self.crc = "0"
24 26
25 27 self.openSocket(asServer)
26 28
27 29 def openSocket(self, asServer):
28 30
29 31 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
30 32 # self.socket_c = socket.socket()
31 33 # self.socket_c.connect((self.ipDestino, self.portDestino))
32 34
33 35 if asServer:
34 36 self.socket_c = socket.socket()
35 37 # self.configAsServer()
36 38 self.socket_c.bind(self.addr)
37 39 self.asServer = True
38 40 else:
39 41 # self.configAsClient()
40 42 self.asServer = False #Socket is opened at the sendData function
41 43
42 44 # def configAsClient(self):
43 45 #Buscar broadcast TCP
44 46 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
45 47 # self.socket_c.connect(self.addr)
46 48 # pass
47 49
48 50 # def configAsServer(self):
49 51 #
50 52 # self.socket_c.bind(self.addr)
51 53
52 54 def waitData(self):
53 55 if self.asServer == True:
54 trama_rx = self.waitAsServer()
56 trama_rx, l = self.waitAsServer()
55 57 else:
56 trama_rx = self.waitAsClient()
57
58 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
58 trama_rx, l = self.waitAsClient()
59
60 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx, l)
59 61
60 62 return ipSource, ipDestino, cmd, data
61
62 def waitAsServer(self, nbytes = 1024):
63
64 def waitAsClient2(self, nbytes = 1024):
65 print "\nWaiting the server."
66 # Short data through ethernet
67 trama_rx = self.socket_c.recv(nbytes)
68 print "\nThis socket has received this data: " + str(trama_rx)
69
70 return trama_rx
71
72 def waitAsServer2(self, nbytes = 1024):
63 73 print "\nWaiting some client."
64 74 self.socket_c.listen(1)
65 75 sc, addr = self.socket_c.accept()
66 76 self.sc = sc
67 77 self.answer = addr
68 78 # Big data through ethernet
69 79 trama_rx = ""
70 80 while True:
71 81 tmp = self.sc.recv(nbytes)
72 82 trama_rx = trama_rx + tmp
73 83 if trama_rx[-4:] == "quit":
74 84 break
75 85
76 86 print "\nThis socket has received some data from: " + str(self.answer)
77 87
78 return trama_rx
88 return trama_rx
89
90 def waitAsServer(self, nbytes = 1024):
91 print "\nWaiting some client."
92 self.socket_c.listen(1)
93 sc, addr = self.socket_c.accept()
94 self.sc = sc
95 self.answer = addr
96 # Big data through ethernet
97 cnt = 0;
98 first = 0;
99 trama_rx = ""
100 while True:
101 tmp = self.sc.recv(nbytes)
102 trama_rx = trama_rx + tmp
103 cnt = len(trama_rx)
104 if first == 0:
105 first = 1
106 lng = int(trama_rx[20:26])
107 if cnt == lng:
108 break
109
110 print "\nThis socket has received some data from: " + str(self.answer)
111
112 return trama_rx, lng
79 113
80 114 def waitAsClient(self, nbytes = 1024):
81 115 print "\nWaiting the server."
82 116 # Short data through ethernet
83 117 trama_rx = self.socket_c.recv(nbytes)
118 lng = int(trama_rx[20:26])
84 119 print "\nThis socket has received this data: " + str(trama_rx)
85 120
86 return trama_rx
87
88 def sendData(self, cmd, data, ipDestino):
121 return trama_rx, lng
122
123 def sendData2(self, cmd, data, ipDestino):
89 124
90 125 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
91 126 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
92 127
93 128 if self.asServer == True:
94 129 self.SendAsServer(trama_tx)
95 130 else:
96 131 self.SendAsClient(ipDestino, trama_tx)
132
133 def sendData(self, cmd, data, ipDestino):
134
135 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
136 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
137
138 self.len = self.int2str(len(trama_tx))
139
140 trama_tx = self.__HEADER + self.__TYPE + self.iDSource + \
141 self.iDDestino + self.len + str(cmd) + str(data) + self.crc
142
143 if self.asServer == True:
144 self.SendAsServer(trama_tx)
145 else:
146 self.SendAsClient(ipDestino, trama_tx)
97 147
98 148 def SendAsServer(self, trama_tx):
99 149
100 150 self.sc.send(trama_tx)
101 151 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
102 152
103 153
104 154 def SendAsClient(self, ipDestino, trama_tx):
105 155
106 156 self.socket_c.connect((ipDestino, self.portDestino))
107 157 self.socket_c.send(trama_tx)
108 158 print "Sending message:[" + trama_tx + "] to: " + ipDestino
109 159
110 def __getTrama(self, trama):
160 def __getTrama2(self, trama):
111 161
112 162 FrameList = trama.split(':')
113 163
114 164 header = FrameList[0]
115 165 TypeOfInstrument = FrameList[1]
116 166 ipSource = FrameList[2]
117 167 ipDestino = FrameList[3]
118 168 len = FrameList[4]
119 169 cmd = FrameList[5]
120 170 data = FrameList[6]
121 171 crc = FrameList[7]
122 172 trash = FrameList[8]
123 173
124 174 return ipSource, ipDestino, cmd, data
125 175
176 def __getTrama(self, trama, l):
177
178 header = trama[0:3]
179 TypeOfInstrument = trama[3:6]
180 ipSource = trama[6:13]
181 ipDestino = trama[13:20]
182 len = trama[20:26]
183 cmd = trama[26:30]
184 ldata = l-31
185 data = trama[30:30+ldata]
186 crc = trama[30+ldata:]
187
188 return ipSource, ipDestino, cmd, data
189
126 190 def close_socket(self):
127 191 self.socket_c.close()
128 192
129 193 def open_socket(self):
130 194 self.socket_c = socket.socket()
195
196 def int2str(self, n):
197
198 str_n = str(n)
199 l_n = len(str_n)
200 if l_n == 1:
201 str_n = "00000" + str_n
202 elif l_n == 2:
203 str_n = "0000" + str_n
204 elif l_n == 3:
205 str_n = "000" + str_n
206 elif l_n == 4:
207 str_n = "00" + str_n
208 elif l_n == 5:
209 str_n = "0" + str_n
210 return str_n
131 211
132 212 class FilesStuff():
133 213
134 214 def lst2string(self, lst):
135 215 string=''
136 216 for i in lst:
137 217 string=string+i
138 218 return string
139 219
140 220 def string2lst(self, string):
141 221 lst = []
142 222 for i in string:
143 223 lst.append(i)
144 224 return lst
145 225
146 226
147 227 def file1(self, filename, type):
148 228 lst = self.string2lst(filename)
149 229 fin = -1
150 230 t = len(lst)
151 231 for i in np.arange(-1,-t,-1):
152 232 if lst[i]=='/':
153 233 fin=i
154 234 break
155 235 if type == '1':
156 236 nombre2 = lst[fin+1:]
157 237 nombre2[-1]='s'
158 238 nombre2 = self.lst2string(nombre2)
159 239 return nombre2
160 240 if type == '2':
161 241 nombre2 = lst[fin+1:]
162 242 nombre2[-1]='1'
163 243 nombre2 = self.lst2string(nombre2)
164 244 return nombre2
165 245
166 246
167 247 def EliminaSaltoDeLinea(self,cadena):
168 248 i = 0
169 249 for elemento in cadena:
170 250 if elemento =='\n' or elemento =='\r':
171 251 pass
172 252 else:
173 253 i=i+1
174 254 return cadena [:i]
175 255
176 256 def NumeroDeExperimentos(self, path):
177 257 fichero1=open(path,'r')
178 258 cont=0
179 259 for cadena in fichero1:
180 260 cont=cont+1
181 261 if cont==3:
182 262 nexp=''
183 263 pos=0
184 264 for elemento in cadena:
185 265 pos=pos+1
186 266 if elemento=='=':
187 267 nexp=int(cadena[pos:])
188 268 return nexp
189 269 fichero1.close()
190 270
191 271 def Paridad(numero):
192 272 if numero%2==0: return 'par'
193 273 elif numero%2==1: return 'impar'
194 274
195 275 def EvaluaCadena(self,cadena):
196 276 if len(cadena)>35:
197 277 if cadena[-1]=='$':
198 278 return cadena[-35:-2]
199 279 elif cadena[-1]==']':
200 280 return cadena[-34:-1]
201 281 else:
202 282 return None
203 283
204 284 def GuardaEnLista(self,path):
205 285 fichero=open(path,'r')
206 286 lista=[]
207 287 for cadena in fichero:
208 288 cadena = self.EliminaSaltoDeLinea(cadena)
209 289 cadena = self.EvaluaCadena(cadena)
210 290 if cadena != None:
211 291 lista.append(cadena)
212 292 fichero.close()
213 293 return lista
214 294
215 295 def CreaFicherosPrevios(self, path, archivo):
216 296 vector = self.GuardaEnLista(archivo)
217 297 for i in range(1,self.NumeroDeExperimentos(archivo)+1):
218 298 fichero =open(path + str(i)+ '.txt','w')
219 299 for j in range(0,16):
220 300 fichero.write(vector[j+16*(i-1)]+'\n')
221 301 fichero.close()
222 302
223 303 def CapturaValoresEnArchivo(self, path, polarizacion='up'):
224 304 fichero =open(path,'r')
225 305 cnt=0
226 306 lstup=[]
227 307 lstdw=[]
228 308 for cadena in fichero:
229 309 cnt=cnt+1
230 310 if cnt==1:
231 311 nu01=cadena[1:4]
232 312 nu02=cadena[5:8]
233 313 nu03=cadena[9:12]
234 314 nu04=cadena[13:16]
235 315 eu01=cadena[17:20]
236 316 eu02=cadena[21:24]
237 317 eu03=cadena[25:28]
238 318 eu04=cadena[29:32]
239 319 if cnt==2:
240 320 nu05=cadena[1:4]
241 321 nu06=cadena[5:8]
242 322 nu07=cadena[9:12]
243 323 nu08=cadena[13:16]
244 324 eu05=cadena[17:20]
245 325 eu06=cadena[21:24]
246 326 eu07=cadena[25:28]
247 327 eu08=cadena[29:32]
248 328 if cnt==3:
249 329 nu09=cadena[1:4]
250 330 nu10=cadena[5:8]
251 331 nu11=cadena[9:12]
252 332 nu12=cadena[13:16]
253 333 eu09=cadena[17:20]
254 334 eu10=cadena[21:24]
255 335 eu11=cadena[25:28]
256 336 eu12=cadena[29:32]
257 337 if cnt==4:
258 338 nu13=cadena[1:4]
259 339 nu14=cadena[5:8]
260 340 nu15=cadena[9:12]
261 341 nu16=cadena[13:16]
262 342 eu13=cadena[17:20]
263 343 eu14=cadena[21:24]
264 344 eu15=cadena[25:28]
265 345 eu16=cadena[29:32]
266 346 if cnt==5:
267 347 wu01=cadena[1:4]
268 348 wu02=cadena[5:8]
269 349 wu03=cadena[9:12]
270 350 wu04=cadena[13:16]
271 351 su01=cadena[17:20]
272 352 su02=cadena[21:24]
273 353 su03=cadena[25:28]
274 354 su04=cadena[29:32]
275 355 if cnt==6:
276 356 wu05=cadena[1:4]
277 357 wu06=cadena[5:8]
278 358 wu07=cadena[9:12]
279 359 wu08=cadena[13:16]
280 360 su05=cadena[17:20]
281 361 su06=cadena[21:24]
282 362 su07=cadena[25:28]
283 363 su08=cadena[29:32]
284 364 if cnt==7:
285 365 wu09=cadena[1:4]
286 366 wu10=cadena[5:8]
287 367 wu11=cadena[9:12]
288 368 wu12=cadena[13:16]
289 369 su09=cadena[17:20]
290 370 su10=cadena[21:24]
291 371 su11=cadena[25:28]
292 372 su12=cadena[29:32]
293 373 if cnt==8:
294 374 wu13=cadena[1:4]
295 375 wu14=cadena[5:8]
296 376 wu15=cadena[9:12]
297 377 wu16=cadena[13:16]
298 378 su13=cadena[17:20]
299 379 su14=cadena[21:24]
300 380 su15=cadena[25:28]
301 381 su16=cadena[29:32]
302 382 if cnt==9:
303 383 nd01=cadena[1:4]
304 384 nd02=cadena[5:8]
305 385 nd03=cadena[9:12]
306 386 nd04=cadena[13:16]
307 387 ed01=cadena[17:20]
308 388 ed02=cadena[21:24]
309 389 ed03=cadena[25:28]
310 390 ed04=cadena[29:32]
311 391 if cnt==10:
312 392 nd05=cadena[1:4]
313 393 nd06=cadena[5:8]
314 394 nd07=cadena[9:12]
315 395 nd08=cadena[13:16]
316 396 ed05=cadena[17:20]
317 397 ed06=cadena[21:24]
318 398 ed07=cadena[25:28]
319 399 ed08=cadena[29:32]
320 400 if cnt==11:
321 401 nd09=cadena[1:4]
322 402 nd10=cadena[5:8]
323 403 nd11=cadena[9:12]
324 404 nd12=cadena[13:16]
325 405 ed09=cadena[17:20]
326 406 ed10=cadena[21:24]
327 407 ed11=cadena[25:28]
328 408 ed12=cadena[29:32]
329 409 if cnt==12:
330 410 nd13=cadena[1:4]
331 411 nd14=cadena[5:8]
332 412 nd15=cadena[9:12]
333 413 nd16=cadena[13:16]
334 414 ed13=cadena[17:20]
335 415 ed14=cadena[21:24]
336 416 ed15=cadena[25:28]
337 417 ed16=cadena[29:32]
338 418 if cnt==13:
339 419 wd01=cadena[1:4]
340 420 wd02=cadena[5:8]
341 421 wd03=cadena[9:12]
342 422 wd04=cadena[13:16]
343 423 sd01=cadena[17:20]
344 424 sd02=cadena[21:24]
345 425 sd03=cadena[25:28]
346 426 sd04=cadena[29:32]
347 427 if cnt==14:
348 428 wd05=cadena[1:4]
349 429 wd06=cadena[5:8]
350 430 wd07=cadena[9:12]
351 431 wd08=cadena[13:16]
352 432 sd05=cadena[17:20]
353 433 sd06=cadena[21:24]
354 434 sd07=cadena[25:28]
355 435 sd08=cadena[29:32]
356 436 if cnt==15:
357 437 wd09=cadena[1:4]
358 438 wd10=cadena[5:8]
359 439 wd11=cadena[9:12]
360 440 wd12=cadena[13:16]
361 441 sd09=cadena[17:20]
362 442 sd10=cadena[21:24]
363 443 sd11=cadena[25:28]
364 444 sd12=cadena[29:32]
365 445 if cnt==16:
366 446 wd13=cadena[1:4]
367 447 wd14=cadena[5:8]
368 448 wd15=cadena[9:12]
369 449 wd16=cadena[13:16]
370 450 sd13=cadena[17:20]
371 451 sd14=cadena[21:24]
372 452 sd15=cadena[25:28]
373 453 sd16=cadena[29:32]
374 454 blck_1_up = [nu01,nu02,nu03,nu04,eu01,eu02,eu03,eu04,nu05,nu06,nu07,nu08,eu05,eu06,eu07,eu08]
375 455 blck_1_dw = [nd01,nd02,nd03,nd04,ed01,ed02,ed03,ed04,nd05,nd06,nd07,nd08,ed05,ed06,ed07,ed08]
376 456 blck_2_up = [nu09,nu10,nu11,nu12,eu09,eu10,eu11,eu12,nu13,nu14,nu15,nu16,eu13,eu14,eu15,eu16]
377 457 blck_2_dw = [nd09,nd10,nd11,nd12,ed09,ed10,ed11,ed12,nd13,nd14,nd15,nd16,ed13,ed14,ed15,ed16]
378 458 blck_3_up = [wu01,wu02,wu03,wu04,su01,su02,su03,su04,wu05,wu06,wu07,wu08,su05,su06,su07,su08]
379 459 blck_3_dw = [wd01,wd02,wd03,wd04,sd01,sd02,sd03,sd04,wd05,wd06,wd07,wd08,sd05,sd06,sd07,sd08]
380 460 blck_4_up = [wu09,wu10,wu11,wu12,su09,su10,su11,su12,wu13,wu14,wu15,wu16,su13,su14,su15,su16]
381 461 blck_4_dw = [wd09,wd10,wd11,wd12,sd09,sd10,sd11,sd12,wd13,wd14,wd15,wd16,sd13,sd14,sd15,sd16]
382 462
383 463 lstup = blck_1_up + blck_2_up + blck_3_up + blck_4_up
384 464 lstdw = blck_1_dw + blck_2_dw + blck_3_dw + blck_4_dw
385 465
386 466 if polarizacion=='up':
387 467 return lstup
388 468 elif polarizacion=='dw':
389 469 return lstdw
390 470 fichero.close()
391 471
392 472 def CreaFormatoFinal(self, path, filename):
393 473 ne=self.NumeroDeExperimentos(filename)
394 474
395 475 #nombre01 = file1(archivo,'1')
396 476 nombre02 = self.file1(filename,'2')
397 477 fichero=open(path + 'CentralControlFormat.txt','w')
398 478 fichero.write(nombre02+'\n')
399 479 fichero.write(str(ne)+'\n')
400 480
401 481 for i in range(1,65):
402 482
403 483 if i<10:
404 484 nmod = '0'+str(i)
405 485 else: nmod = str(i)
406 486
407 487 fichero.write("ABS_" + nmod+'\n')
408 488
409 489 for j in range(1,ne+1):
410 490 ruta=path+str(j)+'.txt'
411 491 lu=self.CapturaValoresEnArchivo(ruta,polarizacion='up')
412 492 ld=self.CapturaValoresEnArchivo(ruta,polarizacion='dw')
413 493 part1=''
414 494 part2=''
415 495 if lu[i-1]=='1.0': part1='000'
416 496 if lu[i-1]=='2.0': part1='001'
417 497 if lu[i-1]=='3.0': part1='010'
418 498 if lu[i-1]=='0.0': part1='011'
419 499 if lu[i-1]=='0.5': part1='100'
420 500 if lu[i-1]=='1.5': part1='101'
421 501 if lu[i-1]=='2.5': part1='110'
422 502 if lu[i-1]=='3.5': part1='111'
423 503 if ld[i-1]=='1.0': part2='000'
424 504 if ld[i-1]=='2.0': part2='001'
425 505 if ld[i-1]=='3.0': part2='010'
426 506 if ld[i-1]=='0.0': part2='011'
427 507 if ld[i-1]=='0.5': part2='100'
428 508 if ld[i-1]=='1.5': part2='101'
429 509 if ld[i-1]=='2.5': part2='110'
430 510 if ld[i-1]=='3.5': part2='111'
431 511 fichero.write(part1+part2+'\n')
432 512 fichero.write('------'+'\n')
433 513 fichero.close()
434 514
435 515 def EliminaArchivosEnLaCarpeta(self, path, filename):
436 516 ne=self.NumeroDeExperimentos(filename)
437 517 for i in range(1,ne+1):
438 518 os.remove(path + str(i)+'.txt')
439 519
440 520
441 521 def toCentralControlFormat(self, filename):
442 522 """ Funcion que genera un archivo para el control central"""
443 523
444 524 path = os.getcwd() + '/'
445 525 self.CreaFicherosPrevios(path, filename)
446 526 self.CreaFormatoFinal(path, filename)
447 527 self.EliminaArchivosEnLaCarpeta(path, filename)
@@ -1,186 +1,186
1 1 import os
2 2 import library3
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
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.tx_buffer = "default"
17 17 self.rx_buffer = "default"
18 18 self.enaModules = []
19 19
20 20 self.createObjects()
21 21
22 22 def createObjects(self):
23 23
24 24 asServer = True
25 self.commServerObj = library3.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer)
26 self.commClientObj = library3.TCPComm("Central_Control", self.ipDestino2, self.portDestino2)
25 self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer)
26 self.commClientObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino2, "CnMod01", self.portDestino2)
27 27
28 28 def waitRequest(self):
29 29
30 30 #Using rx buffer
31 31 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
32 32
33 33 if cmd == "SNDF":
34 34 datarpta = self.__sendFile2Modules(cmd = cmd)
35 35
36 36 if cmd == "CHGB":
37 37 datarpta = self.__changeBeam(cmd = cmd)
38 38
39 39 if cmd == "ANST":
40 40 self.__getControlModuleStatus(cmd = cmd)
41 41 #Using tx buffer
42 42 datarpta = self.tx_buffer
43 43
44 44 if cmd == "NTST":
45 45 self.enaModules = self.__getConnectionStatus()
46 46 #Using tx buffer
47 47 datarpta = self.enaModules
48 48
49 49 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
50 50
51 51 def checkModule(self, address):
52 52
53 53 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
54 54 status = os.system(cmd)
55 55
56 56 if status == 256:
57 57 return False
58 58
59 59 return True
60 60
61 61 def __writeReport(self, enaModules):
62 62
63 63 status_array = ["Status of modules\n"]
64 64 status_array.append("----------------\n")
65 65
66 66 for address in range(1,65):
67 67 if address in enaModules:
68 68 status_array.append("192.168.1." + str(address) + " [1 1]\n")
69 69 else:
70 70 status_array.append("192.168.1." + str(address) + " [0 0]\n")
71 71
72 72 f = open("module_status.txt","w")
73 73 f.writelines(status_array)
74 74 f.close()
75 75
76 76 def checkAntenna(self):
77 77
78 78 """
79 79 Direccion de los modulos de las antenas:
80 80
81 81 Norte : 01-16
82 82 Este : 17-32
83 83 Oeste: : 33-48
84 84 Sur : 49-64
85 85
86 86 """
87 87
88 88 enaModules2 = []
89 89
90 90 for address in range(1,65):
91 91 if self.checkModule(address):
92 92 enaModules2.append(address)
93 93
94 94 self.__writeReport(enaModules2)
95 95 return enaModules2
96 96
97 97 def __ConnectionWithControlModules(self,data,cmd,id):
98 98
99 99 self.commClientObj.open_socket()
100 100 ip = "192.168.1." + str(id)
101 101 self.commClientObj.sendData(cmd, data, ip)
102 102 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
103 103 self.commClientObj.close_socket()
104 104
105 105 return tmp
106 106
107 107 def __All2Blocks(self,input):
108 108
109 109 rx_frame_lst = input.split('\n',2)
110 110
111 111 header = rx_frame_lst[0] + "\n"
112 112 control_modules_str = rx_frame_lst[2]
113 113 control_modules_lst = control_modules_str.split("------\n")
114 114
115 115 return header, control_modules_lst
116 116
117 117
118 118 def __sendFile2Modules(self,cmd):
119 119
120 120 #Needed for the loop
121 121 header, control_modules_lst = self.__All2Blocks(self.rx_buffer)
122 122 correct = 0
123 123
124 124 for id in range(1,65):
125 125
126 126 if id not in self.enaModules:
127 127 continue
128 128
129 129 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
130 130 correct = correct + 1
131 131
132 132 if correct == len(self.enaModules):
133 133 rpta = "OK"
134 134 else:
135 135 rpta = "Failure"
136 136
137 137 return rpta
138 138
139 139 def __changeBeam(self, cmd):
140 140
141 141 correct = 0
142 142 # enaModules = self.checkAntenna()
143 143 # enaModules = [11,12,13,14]
144 144
145 145 for id in range(1,65):
146 146 if id not in self.enaModules:
147 147 continue
148 148
149 149 if self.__ConnectionWithControlModules(self.rx_buffer,cmd,id) == "OK":
150 150 correct = correct + 1
151 151
152 152 if correct == len(self.enaModules):
153 153 rpta = "OK"
154 154 else:
155 155 rpta = "Failure"
156 156
157 157 return rpta
158 158
159 159 def __getControlModuleStatus(self, cmd):
160 160
161 161 all_blocks = ""
162 162 # enaModules = self.checkAntenna()
163 163 # enaModules = [11,12,13,14]
164 164
165 165 for id in range(1,65):
166 166 if id not in self.enaModules:
167 167 continue
168 168
169 169 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
170 170
171 171 all_blocks = all_blocks + one_block
172 172 #Using tx buffer
173 173 self.tx_buffer = all_blocks
174 174
175 175 def __getConnectionStatus(self):
176 176
177 177 enaModules = self.checkAntenna()
178 178
179 179 return enaModules
180 180
181 181 if __name__ == '__main__':
182 182
183 183 absObj = ABSServer()
184 184
185 185 while 1:
186 186 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now