##// END OF EJS Templates
imanay -
r221:222
parent child
Show More
@@ -1,435 +1,435
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 self.ipSource = ipSource
13 13 self.ipDestino = ipDestino
14 14 self.portDestino = portDestino
15 15
16 16 self.ipDestino2 = ipDestino2
17 17 self.portDestino2 = portDestino2
18 18
19 19 self.tx_buffer = "default"
20 20 self.rx_buffer = rx_buffer
21 21 self.enaModules = []
22 22 self.bits = []
23 23 self.phase = []
24 24 self.txFile = []
25 25 self.rxFile = []
26 26
27 27 self.createObjects()
28 28
29 29 print "Checking control modules, please wait ..."
30 30 self.enaModules = self.checkAntenna()
31 31 print '\nThis modules are present : ' + str(self.enaModules) + '\n'
32 32 # print "Starting automatic control module status."
33 33 self.__StartingAutomaticControlModules()
34 34 # self.__AutomaticControlModules()
35 35
36 36 def createObjects(self):
37 37
38 38 asServer = True
39 39 self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer)
40 40 self.commClientObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino2, "CnMod01", self.portDestino2)
41 41 self.wFiles = library3.FilesStuff()
42 42
43 43 def waitRequest(self):
44 44
45 45 #Using rx buffer
46 46 # ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
47 47 ipSource, ipDestino, cmd, rx_buffer = self.commServerObj.waitData()
48 48
49 49 if cmd == "SNDF":
50 50 datarpta = self.__sendFile2Modules(cmd = cmd, rx_buffer = rx_buffer)
51 51
52 52 if cmd == "GETF":
53 53 datarpta = self.__getFileFromModules(cmd = cmd, rx_buffer = rx_buffer)
54 54
55 55 if cmd == "CHGB":
56 56 datarpta = self.__changeBeam(cmd = cmd, rx_buffer = rx_buffer)
57 57
58 58 if cmd == "ANST":
59 59 datarpta = self.__getControlModuleStatus_old(cmd = cmd, rx_buffer = rx_buffer)
60 60
61 61 if cmd == "BGPH":
62 62 datarpta = self.__getControlModuleBigPhase(cmd = cmd, rx_buffer = rx_buffer)
63 63
64 64 if cmd == "LWPH":
65 65 datarpta = self.__getControlModuleLowPhase(cmd = cmd, rx_buffer = rx_buffer)
66 66
67 67 if cmd == "NTST":
68 68 datarpta = self.__getConnectionStatus(cmd = cmd, rx_buffer = rx_buffer)
69 69
70 70 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
71 71
72 72 def __checkModule(self, address):
73 73
74 74 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
75 75 status = os.system(cmd)
76 76
77 77 if status == 256:
78 78 return False
79 79
80 80 return True
81 81
82 82 def __writeReport(self, enaModules):
83 83
84 84 status_array = ["Status of modules\n"]
85 85 status_array.append("----------------\n")
86 86
87 87 for address in range(1,65):
88 88 if address in enaModules:
89 89 status_array.append("192.168.1." + str(address) + " [1 1]\n")
90 90 else:
91 91 status_array.append("192.168.1." + str(address) + " [0 0]\n")
92 92
93 93 filename = "module_status.txt"
94 94 self.__writeFile(filename,status_array)
95 95 # f = open("module_status.txt","w")
96 96 # f.writelines(status_array)
97 97 # f.close()
98 98
99 99
100 100 def __CreateFile(self, filename):
101 101
102 102 fobj = open(filename,"w")
103 103 fobj.close()
104 104
105 105 def __writeNewFile(self, filename, data):
106 106
107 107 fobj = open(filename,"w")
108 108 fobj.writelines(data)
109 109 fobj.close()
110 110
111 111 def __writeFile(self, filename, data):
112 112
113 113 fobj = open(filename,"a")
114 114 fobj.writelines(data)
115 115 fobj.close()
116 116
117 117 def __checkAntenna(self):
118 118
119 119 """
120 120 Direccion de los modulos de las antenas:
121 121
122 122 Norte : 01-16
123 123 Este : 17-32
124 124 Oeste: : 33-48
125 125 Sur : 49-64
126 126
127 127 """
128 128
129 129 enaModules2 = []
130 130
131 131 for address in range(1,65):
132 132 if self.checkModule(address):
133 133 enaModules2.append(address)
134 134
135 135 self.__writeReport(enaModules2)
136 136 return enaModules2
137 137
138 138 def checkModule(self, arg, queue):
139 139 cmd = "ping -c 1 -w 1 192.168.1."+ str(arg) + " >> /dev/null"
140 140 status = os.system(cmd)
141 141 if status == 256:
142 142 result = "failed"
143 143 else:
144 144 result = "ok"
145 145 queue.put({arg: result})
146 146
147 147 def checkAntenna(self):
148 148
149 149 iD = range(1,65)
150 150 q = Queue.Queue()
151 151 threads = []
152 152 tOut = []
153 153 modules = []
154 154
155 155 for argument in iD:
156 156 t = Thread(target=self.checkModule, args=(argument, q))
157 157 t.start()
158 158 threads.append(t)
159 159
160 160 for t in threads:
161 161 t.join()
162 162
163 163 for _ in range(len(iD)):
164 164 tOut.append(q.get())
165 165
166 166 for i in tOut:
167 167 if i.values()[0] == 'ok':
168 168 modules.append(i.keys()[0])
169 169
170 170 return modules
171 171
172 172 def __ConnectionWithControlModules(self,data,cmd,id):
173 173
174 174 self.commClientObj.open_socket()
175 175 ip = "192.168.1." + str(id)
176 176 self.commClientObj.sendData(cmd, data, ip)
177 177 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
178 178 self.commClientObj.close_socket()
179 179
180 180 return tmp
181 181
182 182 def abs2ControlModuleFormatFile(self, filename):
183 183
184 184 #From matriz to control module format
185 185 self.wFiles.toCentralControlFormat(filename)
186 186 FileName = "CentralControlFormat.txt"
187 187 F_Obj = open(FileName,"r")
188 188 FileList = F_Obj.readlines()
189 189 F_Obj.close()
190 190 FileStr = "".join(FileList)
191 191
192 192 return FileStr
193 193
194 194 def __All2Blocks(self,input):
195 195
196 196 rx_frame_lst = input.split('\n',2)
197 197
198 198 header = rx_frame_lst[0] + "\n"
199 199 control_modules_str = rx_frame_lst[2]
200 200 control_modules_lst = control_modules_str.split("------\n")
201 201
202 202 return header, control_modules_lst
203 203
204 204
205 205 def __sendFile2Modules(self,cmd, rx_buffer):
206 206
207 207 # rx_buffer_lst = self.rx_buffer.split('\n',1)
208 208 rx_buffer_lst = rx_buffer.split('\n',1)
209 209 #Getting the filename from the begining of data
210 210 filename = rx_buffer_lst[0]
211 211 tmp = rx_buffer_lst[1]
212 212 self.__writeFile(filename,tmp)
213 213 data = self.abs2ControlModuleFormatFile(filename)
214 214 #Needed for the loop
215 215 header, control_modules_lst = self.__All2Blocks(data)
216 216 correct = 0
217 217
218 218 for id in range(1,65):
219 219
220 220 if id not in self.enaModules:
221 221 continue
222 222
223 223 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
224 224 correct = correct + 1
225 225
226 226 if correct == len(self.enaModules):
227 227 rpta = "OK"
228 228 else:
229 229 rpta = "Failure"
230 230
231 231 return rpta
232 232
233 233 def __getFileFromModules(self, cmd, rx_buffer):
234 234
235 235 for id in range(1,65):
236 236 if id not in self.enaModules:
237 237 continue
238 238
239 239 file = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
240 240 del self.rxFile[id-1]
241 241 self.rxFile.insert(id-1, file)
242 242
243 243 def __changeBeam(self, cmd, rx_buffer):
244 244
245 245 correct = 0
246 246 # enaModules = self.checkAntenna()
247 247 # enaModules = [11,12,13,14]
248 248
249 249 for id in range(1,65):
250 250 if id not in self.enaModules:
251 251 continue
252 252
253 253 if self.__ConnectionWithControlModules(rx_buffer,cmd,id) == "OK":
254 254 correct = correct + 1
255 255
256 256 if correct == len(self.enaModules):
257 257 rpta = "OK"
258 258 else:
259 259 rpta = "Failure"
260 260
261 261 return rpta
262 262
263 263 def __getControlModuleStatus(self, cmd, rx_buffer):
264 264
265 265 # all_blocks = ""
266 266 # all_blocks = []
267 267 # enaModules = self.checkAntenna()
268 268 # enaModules = [11,12,13,14]
269 269
270 270 for id in range(1,65):
271 271 if id not in self.enaModules:
272 272 continue
273 273
274 274 bits = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
275 275 del self.bits[id-1]
276 276 self.bits.insert(id-1, bits)
277 277
278 278 # all_blocks.append(one_block)
279 279 #Using tx buffer
280 280
281 281 return self.bits
282 282
283 283 def __getControlModuleBigPhase(self, cmd, rx_buffer):
284 284
285 285 # all_blocks = ""
286 286 all_blocks = []
287 287 # enaModules = self.checkAntenna()
288 288 # enaModules = [11,12,13,14]
289 289
290 290 for id in range(1,65):
291 291 if id not in self.enaModules:
292 292 continue
293 293
294 294 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
295 295
296 296 # all_blocks = all_blocks + one_block
297 297 all_blocks.append(one_block)
298 298 #Using tx buffer
299 299 return all_blocks
300 300
301 301 def __getControlModuleLowPhase(self, cmd, rx_buffer):
302 302
303 303 # all_blocks = ""
304 304 # all_blocks = []
305 305 # enaModules = self.checkAntenna()
306 306 # enaModules = [11,12,13,14]
307 307
308 308 for id in range(1,65):
309 309 if id not in self.enaModules:
310 310 continue
311 311
312 312 phase = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
313 313 del self.phase[id-1]
314 314 self.phase.insert(id-1, phase)
315 315 # all_blocks = all_blocks + one_block
316 316 # all_blocks.append(one_block)
317 317 #Using tx buffer
318 318 # return all_blocks
319 319
320 320 def __getConnectionStatus(self, cmd, rx_buffer):
321 321
322 322 ena = self.checkAntenna()
323 323 print ena
324 324 self.enaModules = ena
325 325
326 326 blockLst = []
327 327
328 328 for id in range(1,65):
329 329 if id not in self.enaModules:
330 330 continue
331 331
332 332 blockStr = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
333 333 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
334 334 #Using tx buffer
335 335 all_blocks = "".join(blockLst)
336 336
337 337 return all_blocks
338 338
339 339 def getConnectionStatus(self, cmd):
340 340
341 341 ena = self.checkAntenna()
342 342 self.enaModules = ena
343 343
344 344 blockLst = []
345 345
346 346 for id in range(1,65):
347 347 if id not in self.enaModules:
348 348 continue
349 349
350 350 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
351 351 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
352 352 #Using tx buffer
353 353 self.tx_buffer = "".join(blockLst)
354 354 print self.tx_buffer
355 355
356 356 return self.tx_buffer
357 357
358 def __getControlModuleStatus_old(self, cmd):
358 def __getControlModuleStatus_old(self, cmd, rx_buffer):
359 359
360 360 all_blocks = ""
361 361 # enaModules = self.checkAntenna()
362 362 # enaModules = [11,12,13,14]
363 363
364 364 for id in range(1,65):
365 365 if id not in self.enaModules:
366 366 continue
367 367
368 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
368 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
369 369
370 370 all_blocks = all_blocks + one_block
371 371 #Using tx buffer
372 372 print all_blocks
373 373 self.tx_buffer = all_blocks
374 374
375 375 return all_blocks
376 376
377 377 def __StartingAutomaticControlModules(self):
378 378
379 379 #Starting file
380 380 self.__CreateFile("Monitoring.txt")
381 381 # data = "MOD.\t BITS\t\t PHASE\n"
382 382 # self.__writeFile("Monitoring.txt", data)
383 383 #Starting lists
384 384 self.txFile = list("------\n------\n------\n" for i in range(64))
385 385 self.rxFile = list("------\n------\n------\n" for i in range(64))
386 386 self.bits = list("------\n------\n------\n" for i in range(64))
387 387 self.phase = list("----- -----\n----- -----\n----- -----\n" for i in range(64))
388 388
389 389 def __AutomaticControlModules(self):
390 390
391 391 # cmd = "GETF"
392 392 # rx_buffer = "experimento1.ab1" + "\n"
393 393 # self.__getFileFromModules(cmd = cmd, rx_buffer = rx_buffer)
394 394 #
395 395 # print self.rxFile
396 396
397 397 cmd = "ANST"
398 398 rx_buffer = "1"
399 399 self.__getControlModuleStatus(cmd = cmd, rx_buffer = rx_buffer)
400 400
401 401 cmd = "LWPH"
402 402 rx_buffer = "0"
403 403 self.__getControlModuleLowPhase(cmd = cmd, rx_buffer = rx_buffer)
404 404 print "Saving file..."
405 405
406 406
407 407 print self.bits
408 408 print self.phase
409 409
410 410 self.__WritingMonitoringFile()
411 411
412 412 threading.Timer(60, self.__AutomaticControlModules).start()
413 413
414 414 def __WritingMonitoringFile(self):
415 415 filename = "Monitoring.txt"
416 416 data = '===============================' + '\n'
417 417 self.__writeFile(filename, data)
418 418 data = time.strftime('\t' + "%d%b%Y %I:%M:%S %p" + '\n', time.localtime())
419 419 self.__writeFile(filename, data)
420 420 data = "MOD.\t BITS\t\t PHASE\n"
421 421 self.__writeFile(filename, data)
422 422 data = '===============================' + '\n'
423 423 self.__writeFile(filename, data)
424 424 for i in range(64):
425 425 tmp = self.bits[i].split('\n',3)
426 426 self.__writeFile(filename, ' ' + str(i + 1) + '\t\t' + tmp[2])
427 427 tmp = self.phase[i].split('\n',3)
428 428 self.__writeFile(filename, '\t\t' + tmp[2] + '\n')
429 429
430 430 if __name__ == '__main__':
431 431
432 432 absObj = ABSServer()
433 433
434 434 while 1:
435 435 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now