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