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