##// END OF EJS Templates
Removing unnecesary code
imanay -
r269:270
parent child
Show More
@@ -1,588 +1,536
1 1 import os
2 2 import library3
3 3 import time
4 4 from threading import Thread
5 5 import threading
6 6 import Queue
7 7
8 8 class ABSServer:
9 9
10 10 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500, rx_buffer = "default"):
11 11
12 12
13 13 self.ipSource = ipSource
14 14 self.ipDestino = ipDestino
15 15 self.portDestino = portDestino
16 16
17 17 self.ipDestino2 = ipDestino2
18 18 self.portDestino2 = portDestino2
19 19
20 20 self.tx_buffer = "default"
21 21 self.rx_buffer = rx_buffer
22 22 self.enaModules = []
23 23 self.bits = []
24 24 self.phase = []
25 25 self.txFile = []
26 26 self.rxFile = []
27 27
28 28 self.createObjects()
29 29
30 30 print "Checking control modules, please wait ..."
31 31 self.enaModules = self.checkAntenna()
32 32 print '\nThis modules are present : ' + str(self.enaModules) + '\n'
33 33 # print "Starting automatic control module status."
34 34 self.__StartingAutomaticControlModules()
35 35 # self.__AutomaticControlModules()
36 36
37 37 def createObjects(self):
38 38
39 39 asServer = True
40 40 self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer)
41 41 self.commClientObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino2, "CnMod01", self.portDestino2)
42 42 self.wFiles = library3.FilesStuff()
43 43
44 44 def waitRequest(self):
45 45
46 46 #Using rx buffer
47 47 # ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
48 48 ipSource, ipDestino, cmd, rx_buffer = self.commServerObj.waitData()
49 49
50 50 if cmd == "SNDF":
51 51 datarpta = self.__sendFile2Modules(cmd = cmd, rx_buffer = rx_buffer)
52 52
53 53 if cmd == "GETF":
54 54 datarpta = self.__getFileFromModules(cmd = cmd, rx_buffer = rx_buffer)
55 55
56 56 if cmd == "CHGB":
57 57 datarpta = self.__changeBeam(cmd = cmd, rx_buffer = rx_buffer)
58 58
59 59 if cmd == "ANST":
60 60 datarpta = self.__getControlModuleStatus_old(cmd = cmd, rx_buffer = rx_buffer)
61 61
62 62 if cmd == "BGPH":
63 63 datarpta = self.__getControlModuleBigPhase(cmd = cmd, rx_buffer = rx_buffer)
64 64
65 65 if cmd == "LWPH":
66 66 datarpta = self.__getControlModuleLowPhase(cmd = cmd, rx_buffer = rx_buffer)
67 67
68 68 if cmd == "NTST":
69 69 datarpta = self.__getConnectionStatus(cmd = cmd, rx_buffer = rx_buffer)
70 70
71 71 if cmd == "MNTR":
72 72 datarpta = self.__getControlModuleStatus_new(cmd = cmd, type = rx_buffer)
73 73
74 74 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
75 75
76 76 def __checkModule(self, address):
77 77
78 78 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
79 79 status = os.system(cmd)
80 80
81 81 if status == 256:
82 82 return False
83 83
84 84 return True
85 85
86 86 def __writeReport(self, enaModules):
87 87
88 88 status_array = ["Status of modules\n"]
89 89 status_array.append("----------------\n")
90 90
91 91 for address in range(1,65):
92 92 if address in enaModules:
93 93 status_array.append("192.168.1." + str(address) + " [1 1]\n")
94 94 else:
95 95 status_array.append("192.168.1." + str(address) + " [0 0]\n")
96 96
97 97 filename = "module_status.txt"
98 98 self.__writeFile(filename,status_array)
99 99 # f = open("module_status.txt","w")
100 100 # f.writelines(status_array)
101 101 # f.close()
102 102
103 103
104 104 def __CreateFile(self, filename):
105 105
106 106 fobj = open(filename,"w")
107 107 fobj.close()
108 108
109 109 def __writeNewFile(self, filename, data):
110 110
111 111 fobj = open(filename,"w")
112 112 fobj.writelines(data)
113 113 fobj.close()
114 114
115 115 def __writeFile(self, filename, data):
116 116
117 117 fobj = open(filename,"a")
118 118 fobj.writelines(data)
119 119 fobj.close()
120 120
121 121 def __checkAntenna(self):
122 122
123 123 """
124 124 Direccion de los modulos de las antenas:
125 125
126 126 Norte : 01-16
127 127 Este : 17-32
128 128 Oeste: : 33-48
129 129 Sur : 49-64
130 130
131 131 """
132 132
133 133 enaModules2 = []
134 134
135 135 for address in range(1,65):
136 136 if self.checkModule(address):
137 137 enaModules2.append(address)
138 138
139 139 self.__writeReport(enaModules2)
140 140 return enaModules2
141 141
142 142 def checkModule(self, arg, queue):
143 143 cmd = "ping -c 1 -w 1 192.168.1."+ str(arg) + " >> /dev/null"
144 144 status = os.system(cmd)
145 145 if status == 256:
146 146 result = "failed"
147 147 else:
148 148 result = "ok"
149 149 queue.put({arg: result})
150 150
151 151 def checkAntenna(self):
152 152
153 153 iD = range(1,65)
154 154 q = Queue.Queue()
155 155 threads = []
156 156 tOut = []
157 157 modules = []
158 158
159 159 for argument in iD:
160 160 t = Thread(target=self.checkModule, args=(argument, q))
161 161 t.start()
162 162 threads.append(t)
163 163
164 164 for t in threads:
165 165 t.join()
166 166
167 167 for _ in range(len(iD)):
168 168 tOut.append(q.get())
169 169
170 170 for i in tOut:
171 171 if i.values()[0] == 'ok':
172 172 modules.append(i.keys()[0])
173 173
174 174 return modules
175 175
176 176 def __ConnectionWithControlModules(self,data,cmd,id):
177 177
178 178 self.commClientObj.open_socket()
179 179 ip = "192.168.1." + str(id)
180 180 self.commClientObj.sendData(cmd, data, ip)
181 181 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
182 182 self.commClientObj.close_socket()
183 183
184 184 return tmp
185 185
186 186 def abs2ControlModuleFormatFile(self, filename):
187 187
188 188 #From matriz to control module format
189 189 self.wFiles.toCentralControlFormat(filename)
190 190 FileName = "CentralControlFormat.txt"
191 191 F_Obj = open(FileName,"r")
192 192 FileList = F_Obj.readlines()
193 193 F_Obj.close()
194 194 FileStr = "".join(FileList)
195 195
196 196 return FileStr
197 197
198 198 def __All2Blocks(self,input):
199 199
200 200 rx_frame_lst = input.split('\n',2)
201 201
202 202 header = rx_frame_lst[0] + "\n"
203 203 control_modules_str = rx_frame_lst[2]
204 204 control_modules_lst = control_modules_str.split("------\n")
205 205
206 206 return header, control_modules_lst
207 207
208 208
209 209 def __sendFile2Modules(self,cmd, rx_buffer):
210 210
211 211 # rx_buffer_lst = self.rx_buffer.split('\n',1)
212 212 rx_buffer_lst = rx_buffer.split('\n',1)
213 213 #Getting the filename from the begining of data
214 214 filename = rx_buffer_lst[0]
215 215 tmp = rx_buffer_lst[1]
216 216 self.__writeNewFile(filename,tmp)
217 217 data = self.abs2ControlModuleFormatFile(filename)
218 218
219 219 print data
220 220 #Needed for the loop
221 221 header, control_modules_lst = self.__All2Blocks(data)
222 222 correct = 0
223 223
224 224 for id in range(1,65):
225 225
226 226 if id not in self.enaModules:
227 227 continue
228 228
229 229 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
230 230 correct = correct + 1
231 231
232 232 if correct == len(self.enaModules):
233 233 rpta = "OK"
234 234 else:
235 235 rpta = "Failure"
236 236
237 237 return rpta
238 238
239 239 def __getFileFromModules(self, cmd, rx_buffer):
240 240
241 241 for id in range(1,65):
242 242 if id not in self.enaModules:
243 243 continue
244 244
245 245 file = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
246 246 del self.rxFile[id-1]
247 247 self.rxFile.insert(id-1, file)
248 248
249 249 return self.rxFile
250 250
251 251 def __changeBeam(self, cmd, rx_buffer):
252 252
253 253 correct = 0
254 254 # enaModules = self.checkAntenna()
255 255 # enaModules = [11,12,13,14]
256 256
257 257 for id in range(1,65):
258 258 if id not in self.enaModules:
259 259 continue
260 260
261 261 if self.__ConnectionWithControlModules(rx_buffer,cmd,id) == "OK":
262 262 correct = correct + 1
263 263
264 264 if correct == len(self.enaModules):
265 265 rpta = "OK"
266 266 else:
267 267 rpta = "Failure"
268 268
269 269 return rpta
270 270
271 271 def __getControlModuleStatus(self, cmd, rx_buffer):
272 272
273 273 # all_blocks = ""
274 274 # all_blocks = []
275 275 # enaModules = self.checkAntenna()
276 276 # enaModules = [11,12,13,14]
277 277
278 278 for id in range(1,65):
279 279 if id not in self.enaModules:
280 280 continue
281 281
282 282 bits = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
283 283 del self.bits[id-1]
284 284 self.bits.insert(id-1, bits)
285 285
286 286 # all_blocks.append(one_block)
287 287 #Using tx buffer
288 288
289 289 return self.bits
290 290
291 291 def __getControlModuleStatus_new(self, cmd, type):
292 292
293 293 cm_lst = self.__gettingDataFromControlModules(cmd = cmd, rx_buffer = type)
294 294 out_str = self.__processingCMdata(in_data = cm_lst, type = type)
295 295
296 296 return out_str
297 297
298 298 def __gettingDataFromControlModules(self, cmd, rx_buffer):
299 299
300 300 for id in range(1,65):
301 301 if id not in self.enaModules:
302 302 continue
303 303
304 304 cm = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
305 305 del self.test[id-1]
306 306 self.test.insert(id-1, cm)
307 307
308 308 return self.test
309 309
310 310 def __processingCMdata(self,in_data, type):
311 311
312 312 if (type == "0") or (type == "1") or (type == "2"):
313 313 out_str = "".join(in_data)
314 314 elif type == "3":
315 315 out_str = self.__TwoComparation(in_lst = in_data)
316 316 else:
317 317 out_str = self.__ThreeComparation(in_lst = in_data)
318 318
319 319 return out_str
320 320
321 321 def __TwoComparation(self, in_lst):
322 322
323
324 323 file = []
325 324 relay = []
326 325 status = []
327 mode = '0'
328
329 if mode == '0':
330
331 for i in in_lst:
332 if len(i) == 7:
333 file.append("xxxxxx")
334 relay.append("xxxxxx")
335 else:
336 file.append(i[16:22])
337 relay.append(i[23:29])
338 #Loop to generate a status
339 for i in range(64):
340 cnt = 0
341 if file[i] == "xxxxxx":
342 status.append("xx")
343 continue
326
327 for i in in_lst:
328 if len(i) == 7:
329 file.append("xxxxxx")
330 relay.append("xxxxxx")
331 else:
332 file.append(i[16:22])
333 relay.append(i[23:29])
334 #Loop to generate a status
335 for i in range(64):
336 cnt = 0
337 if file[i] == "xxxxxx":
338 status.append("xx")
339 continue
344 340
345 341 if cmp(file[i],relay[i]) == 0:
346 342 cnt = cnt + 1
347 343
348 344 if cmp(file[i][0:3],relay[i][0:3]) == 0:
349 345 cnt = cnt + 1
350 346
351 347 if cmp(file[i][3:6],relay[i][3:6]) == 0:
352 348 cnt = cnt + 2
353
354
355
349
356 350 if cnt == 4:
357 351 status.append("00")
358 352 elif cnt == 0:
359 353 status.append("11")
360 354 elif cnt == 1:
361 355 status.append("01")
362 356 else:
363 357 status.append("10")
364
365 else:
366
367 cm_up =[]
368 cm_dw =[]
369 relay_up =[]
370 relay_dw =[]
371
372 for i in in_lst:
373 if len(i) == 8:
374 relay_up.append("-")
375 relay_dw.append("-")
376 cm_up.append("-")
377 cm_dw.append("-")
378 continue
379 relay_up.append(i[14:17])
380 relay_dw.append(i[17:20])
381 cm_up.append(i[21:24])
382 cm_dw.append(i[24:27])
383
384 comp_up = []
385 comp_dw = []
386
387 for i in range(64):
388 if len(relay_up == 1):
389 comp_up.append('x')
390 else:
391 if cmp(relay_up[i], cm_up[i]) == 0:
392 comp_up.append('0')
393 else:
394 comp_up.append('1')
395
396 for i in range(64):
397 if len(relay_dw == 1):
398 comp_dw.append('x')
399 else:
400 if cmp(relay_dw[i], cm_dw[i]) == 0:
401 comp_dw.append('0')
402 else:
403 comp_dw.append('1')
404
405 # Example:
406 # comp_up = [1,0,x,1,1,1 ..,1,0,1,x]
407 #
408 print comp_up
409 print comp_dw
410 358
411 359 #Doing final format
412 360 return self.__FinalFormat(data1 = file, data2 = relay, data3 = status)
413 361
414 362 def __FinalFormat(self,data1,data2,data3):
415 363
416 364 header = "Device\tFile\tRelay\tStatus\n"
417 365 body = ""
418 366 for i in range(64):
419 367 if i<9:
420 368 device = "CM0" + str(i+1)
421 369 else:
422 370 device = "CM" + str(i+1)
423 371
424 372 line = device + '\t' + data1[i] + '\t' + data2[i] + '\t' + data3[i] + '\n'
425 373 body = body + line
426 374
427 375 report = header + body
428 376
429 377 return report
430 378
431 379 def __ThreeComparation(self, in_lst):
432 380 pass
433 381
434 382 def __getControlModuleBigPhase(self, cmd, rx_buffer):
435 383
436 384 # all_blocks = ""
437 385 all_blocks = []
438 386 # enaModules = self.checkAntenna()
439 387 # enaModules = [11,12,13,14]
440 388
441 389 for id in range(1,65):
442 390 if id not in self.enaModules:
443 391 continue
444 392
445 393 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
446 394
447 395 # all_blocks = all_blocks + one_block
448 396 all_blocks.append(one_block)
449 397 #Using tx buffer
450 398 return all_blocks
451 399
452 400 def __getControlModuleLowPhase(self, cmd, rx_buffer):
453 401
454 402 # all_blocks = ""
455 403 # all_blocks = []
456 404 # enaModules = self.checkAntenna()
457 405 # enaModules = [11,12,13,14]
458 406
459 407 for id in range(1,65):
460 408 if id not in self.enaModules:
461 409 continue
462 410
463 411 phase = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
464 412 del self.phase[id-1]
465 413 self.phase.insert(id-1, phase)
466 414 # all_blocks = all_blocks + one_block
467 415 # all_blocks.append(one_block)
468 416 #Using tx buffer
469 417 # return all_blocks
470 418
471 419 def __getConnectionStatus(self, cmd, rx_buffer):
472 420
473 421 ena = self.checkAntenna()
474 422 print ena
475 423 self.enaModules = ena
476 424
477 425 blockLst = []
478 426
479 427 for id in range(1,65):
480 428 if id not in self.enaModules:
481 429 continue
482 430
483 431 blockStr = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
484 432 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
485 433 #Using tx buffer
486 434 all_blocks = "".join(blockLst)
487 435
488 436 return all_blocks
489 437
490 438 def getConnectionStatus(self, cmd):
491 439
492 440 ena = self.checkAntenna()
493 441 self.enaModules = ena
494 442
495 443 blockLst = []
496 444
497 445 for id in range(1,65):
498 446 if id not in self.enaModules:
499 447 continue
500 448
501 449 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
502 450 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
503 451 #Using tx buffer
504 452 self.tx_buffer = "".join(blockLst)
505 453 print self.tx_buffer
506 454
507 455 return self.tx_buffer
508 456
509 457 def __getControlModuleStatus_old(self, cmd, rx_buffer):
510 458
511 459 all_blocks = ""
512 460 # enaModules = self.checkAntenna()
513 461 # enaModules = [11,12,13,14]
514 462
515 463 for id in range(1,65):
516 464 if id not in self.enaModules:
517 465 continue
518 466
519 467 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
520 468
521 469 all_blocks = all_blocks + one_block
522 470 #Using tx buffer
523 471 print all_blocks
524 472 self.tx_buffer = all_blocks
525 473
526 474 return all_blocks
527 475
528 476 def __StartingAutomaticControlModules(self):
529 477
530 478 #Starting file
531 479 self.__CreateFile("Monitoring.txt")
532 480 # data = "MOD.\t BITS\t\t PHASE\n"
533 481 # self.__writeFile("Monitoring.txt", data)
534 482 #Starting lists
535 483 self.txFile = list("------\n------\n------\n" for i in range(64))
536 484 self.rxFile = list("------\n------\n------\n" for i in range(64))
537 485 self.bits = list("------\n------\n------\n" for i in range(64))
538 486 self.phase = list("----- -----\n----- -----\n----- -----\n" for i in range(64))
539 487 self.test = list("------\n" for i in range(64))
540 488
541 489 def __AutomaticControlModules(self):
542 490
543 491 # cmd = "GETF"
544 492 # rx_buffer = "experimento1.ab1" + "\n"
545 493 # self.__getFileFromModules(cmd = cmd, rx_buffer = rx_buffer)
546 494 #
547 495 # print self.rxFile
548 496
549 497 cmd = "ANST"
550 498 rx_buffer = "1"
551 499 self.__getControlModuleStatus(cmd = cmd, rx_buffer = rx_buffer)
552 500
553 501 cmd = "LWPH"
554 502 rx_buffer = "0"
555 503 self.__getControlModuleLowPhase(cmd = cmd, rx_buffer = rx_buffer)
556 504 print "Saving file..."
557 505
558 506
559 507 print self.bits
560 508 print self.phase
561 509
562 510 self.__WritingMonitoringFile()
563 511
564 512 threading.Timer(60, self.__AutomaticControlModules).start()
565 513
566 514 def __WritingMonitoringFile(self):
567 515 filename = "Monitoring.txt"
568 516 data = '===============================' + '\n'
569 517 self.__writeFile(filename, data)
570 518 data = time.strftime('\t' + "%d%b%Y %I:%M:%S %p" + '\n', time.localtime())
571 519 self.__writeFile(filename, data)
572 520 data = "MOD.\t BITS\t\t PHASE\n"
573 521 self.__writeFile(filename, data)
574 522 data = '===============================' + '\n'
575 523 self.__writeFile(filename, data)
576 524 for i in range(64):
577 525 tmp = self.bits[i].split('\n',3)
578 526 self.__writeFile(filename, ' ' + str(i + 1) + '\t\t' + tmp[2])
579 527 tmp = self.phase[i].split('\n',3)
580 528 self.__writeFile(filename, '\t\t' + tmp[2] + '\n')
581 529
582 530
583 531 if __name__ == '__main__':
584 532
585 533 absObj = ABSServer()
586 534
587 535 while 1:
588 536 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now