##// END OF EJS Templates
imanay -
r218:219
parent child
Show More
@@ -1,435 +1,436
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(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 print self.bits
281 282 return self.bits
282 283
283 284 def __getControlModuleBigPhase(self, cmd, rx_buffer):
284 285
285 286 # all_blocks = ""
286 287 all_blocks = []
287 288 # enaModules = self.checkAntenna()
288 289 # enaModules = [11,12,13,14]
289 290
290 291 for id in range(1,65):
291 292 if id not in self.enaModules:
292 293 continue
293 294
294 295 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
295 296
296 297 # all_blocks = all_blocks + one_block
297 298 all_blocks.append(one_block)
298 299 #Using tx buffer
299 300 return all_blocks
300 301
301 302 def __getControlModuleLowPhase(self, cmd, rx_buffer):
302 303
303 304 # all_blocks = ""
304 305 # all_blocks = []
305 306 # enaModules = self.checkAntenna()
306 307 # enaModules = [11,12,13,14]
307 308
308 309 for id in range(1,65):
309 310 if id not in self.enaModules:
310 311 continue
311 312
312 313 phase = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
313 314 del self.phase[id-1]
314 315 self.phase.insert(id-1, phase)
315 316 # all_blocks = all_blocks + one_block
316 317 # all_blocks.append(one_block)
317 318 #Using tx buffer
318 319 # return all_blocks
319 320
320 321 def __getConnectionStatus(self, cmd, rx_buffer):
321 322
322 323 ena = self.checkAntenna()
323 324 print ena
324 325 self.enaModules = ena
325 326
326 327 blockLst = []
327 328
328 329 for id in range(1,65):
329 330 if id not in self.enaModules:
330 331 continue
331 332
332 333 blockStr = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
333 334 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
334 335 #Using tx buffer
335 336 all_blocks = "".join(blockLst)
336 337
337 338 return all_blocks
338 339
339 340 def getConnectionStatus(self, cmd):
340 341
341 342 ena = self.checkAntenna()
342 343 self.enaModules = ena
343 344
344 345 blockLst = []
345 346
346 347 for id in range(1,65):
347 348 if id not in self.enaModules:
348 349 continue
349 350
350 351 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
351 352 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
352 353 #Using tx buffer
353 354 self.tx_buffer = "".join(blockLst)
354 355 print self.tx_buffer
355 356
356 357 return self.tx_buffer
357 358
358 359 def getControlModuleStatus(self, cmd):
359 360
360 361 all_blocks = ""
361 362 # enaModules = self.checkAntenna()
362 363 # enaModules = [11,12,13,14]
363 364
364 365 for id in range(1,65):
365 366 if id not in self.enaModules:
366 367 continue
367 368
368 369 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
369 370
370 371 all_blocks = all_blocks + one_block
371 372 #Using tx buffer
372 373 print all_blocks
373 374 self.tx_buffer = all_blocks
374 375
375 376 return all_blocks
376 377
377 378 def __StartingAutomaticControlModules(self):
378 379
379 380 #Starting file
380 381 self.__CreateFile("Monitoring.txt")
381 382 # data = "MOD.\t BITS\t\t PHASE\n"
382 383 # self.__writeFile("Monitoring.txt", data)
383 384 #Starting lists
384 385 self.txFile = list("------\n------\n------\n" for i in range(64))
385 386 self.rxFile = list("------\n------\n------\n" for i in range(64))
386 387 self.bits = list("------\n------\n------\n" for i in range(64))
387 388 self.phase = list("----- -----\n----- -----\n----- -----\n" for i in range(64))
388 389
389 390 def __AutomaticControlModules(self):
390 391
391 392 # cmd = "GETF"
392 393 # rx_buffer = "experimento1.ab1" + "\n"
393 394 # self.__getFileFromModules(cmd = cmd, rx_buffer = rx_buffer)
394 395 #
395 396 # print self.rxFile
396 397
397 398 cmd = "ANST"
398 399 rx_buffer = "1"
399 400 self.__getControlModuleStatus(cmd = cmd, rx_buffer = rx_buffer)
400 401
401 402 cmd = "LWPH"
402 403 rx_buffer = "0"
403 404 self.__getControlModuleLowPhase(cmd = cmd, rx_buffer = rx_buffer)
404 405 print "Saving file..."
405 406
406 407
407 408 print self.bits
408 409 print self.phase
409 410
410 411 self.__WritingMonitoringFile()
411 412
412 413 threading.Timer(60, self.__AutomaticControlModules).start()
413 414
414 415 def __WritingMonitoringFile(self):
415 416 filename = "Monitoring.txt"
416 417 data = '===============================' + '\n'
417 418 self.__writeFile(filename, data)
418 419 data = time.strftime('\t' + "%d%b%Y %I:%M:%S %p" + '\n', time.localtime())
419 420 self.__writeFile(filename, data)
420 421 data = "MOD.\t BITS\t\t PHASE\n"
421 422 self.__writeFile(filename, data)
422 423 data = '===============================' + '\n'
423 424 self.__writeFile(filename, data)
424 425 for i in range(64):
425 426 tmp = self.bits[i].split('\n',3)
426 427 self.__writeFile(filename, ' ' + str(i + 1) + '\t\t' + tmp[2])
427 428 tmp = self.phase[i].split('\n',3)
428 429 self.__writeFile(filename, '\t\t' + tmp[2] + '\n')
429 430
430 431 if __name__ == '__main__':
431 432
432 433 absObj = ABSServer()
433 434
434 435 while 1:
435 436 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now