##// END OF EJS Templates
imanay -
r233:234
parent child
Show More
@@ -1,110 +1,110
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 def readFile(self, filename):
48 def readFile(self):
49 49
50 50 data = "optional"
51 51 file = self.__ConnectionWithCentralControl(cmd = "GETF", data = data)
52 52
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 88 def getControlModulePhase(self, opt, u = "50", pw = "10"):
89 89
90 90 if opt == '0':
91 91 data = self.__ConnectionWithCentralControl(cmd = "LWPH", data = u + '/' + pw + '/')
92 92 elif opt == '1':
93 93 data = self.__ConnectionWithCentralControl(cmd = "BGPH", data = u + '/' + pw + '/')
94 94 elif opt == '2':
95 95 data = self.__ConnectionWithCentralControl(cmd = "cBPH", data = u + '/' + pw + '/')
96 96 else:
97 97 data = self.__ConnectionWithCentralControl(cmd = "cLPH", data = u + '/' + pw + '/')
98 98 # self.__writeFile("report.txt", data)
99 99
100 100 def getConnectionStatus(self):
101 101
102 102 data = self.__ConnectionWithCentralControl(cmd = "NTST", data = "none")
103 103 self.__writeFile("connection_status.txt", data)
104 104
105 105 if __name__ == '__main__':
106 106
107 107 filename = "experimento1.abs"
108 108
109 109 absObj = ABSClient()
110 110 print absObj.sendFile(filename)
@@ -1,435 +1,437
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 self.rxFile.insert(id-1, file)
241 self.rxFile.insert(id-1, file)
242
243 return self.rxFile
242 244
243 245 def __changeBeam(self, cmd, rx_buffer):
244 246
245 247 correct = 0
246 248 # enaModules = self.checkAntenna()
247 249 # enaModules = [11,12,13,14]
248 250
249 251 for id in range(1,65):
250 252 if id not in self.enaModules:
251 253 continue
252 254
253 255 if self.__ConnectionWithControlModules(rx_buffer,cmd,id) == "OK":
254 256 correct = correct + 1
255 257
256 258 if correct == len(self.enaModules):
257 259 rpta = "OK"
258 260 else:
259 261 rpta = "Failure"
260 262
261 263 return rpta
262 264
263 265 def __getControlModuleStatus(self, cmd, rx_buffer):
264 266
265 267 # all_blocks = ""
266 268 # all_blocks = []
267 269 # enaModules = self.checkAntenna()
268 270 # enaModules = [11,12,13,14]
269 271
270 272 for id in range(1,65):
271 273 if id not in self.enaModules:
272 274 continue
273 275
274 276 bits = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
275 277 del self.bits[id-1]
276 278 self.bits.insert(id-1, bits)
277 279
278 280 # all_blocks.append(one_block)
279 281 #Using tx buffer
280 282
281 283 return self.bits
282 284
283 285 def __getControlModuleBigPhase(self, cmd, rx_buffer):
284 286
285 287 # all_blocks = ""
286 288 all_blocks = []
287 289 # enaModules = self.checkAntenna()
288 290 # enaModules = [11,12,13,14]
289 291
290 292 for id in range(1,65):
291 293 if id not in self.enaModules:
292 294 continue
293 295
294 296 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
295 297
296 298 # all_blocks = all_blocks + one_block
297 299 all_blocks.append(one_block)
298 300 #Using tx buffer
299 301 return all_blocks
300 302
301 303 def __getControlModuleLowPhase(self, cmd, rx_buffer):
302 304
303 305 # all_blocks = ""
304 306 # all_blocks = []
305 307 # enaModules = self.checkAntenna()
306 308 # enaModules = [11,12,13,14]
307 309
308 310 for id in range(1,65):
309 311 if id not in self.enaModules:
310 312 continue
311 313
312 314 phase = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
313 315 del self.phase[id-1]
314 316 self.phase.insert(id-1, phase)
315 317 # all_blocks = all_blocks + one_block
316 318 # all_blocks.append(one_block)
317 319 #Using tx buffer
318 320 # return all_blocks
319 321
320 322 def __getConnectionStatus(self, cmd, rx_buffer):
321 323
322 324 ena = self.checkAntenna()
323 325 print ena
324 326 self.enaModules = ena
325 327
326 328 blockLst = []
327 329
328 330 for id in range(1,65):
329 331 if id not in self.enaModules:
330 332 continue
331 333
332 334 blockStr = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
333 335 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
334 336 #Using tx buffer
335 337 all_blocks = "".join(blockLst)
336 338
337 339 return all_blocks
338 340
339 341 def getConnectionStatus(self, cmd):
340 342
341 343 ena = self.checkAntenna()
342 344 self.enaModules = ena
343 345
344 346 blockLst = []
345 347
346 348 for id in range(1,65):
347 349 if id not in self.enaModules:
348 350 continue
349 351
350 352 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
351 353 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
352 354 #Using tx buffer
353 355 self.tx_buffer = "".join(blockLst)
354 356 print self.tx_buffer
355 357
356 358 return self.tx_buffer
357 359
358 360 def __getControlModuleStatus_old(self, cmd, rx_buffer):
359 361
360 362 all_blocks = ""
361 363 # enaModules = self.checkAntenna()
362 364 # enaModules = [11,12,13,14]
363 365
364 366 for id in range(1,65):
365 367 if id not in self.enaModules:
366 368 continue
367 369
368 370 one_block = self.__ConnectionWithControlModules(rx_buffer,cmd,id)
369 371
370 372 all_blocks = all_blocks + one_block
371 373 #Using tx buffer
372 374 print all_blocks
373 375 self.tx_buffer = all_blocks
374 376
375 377 return all_blocks
376 378
377 379 def __StartingAutomaticControlModules(self):
378 380
379 381 #Starting file
380 382 self.__CreateFile("Monitoring.txt")
381 383 # data = "MOD.\t BITS\t\t PHASE\n"
382 384 # self.__writeFile("Monitoring.txt", data)
383 385 #Starting lists
384 386 self.txFile = list("------\n------\n------\n" for i in range(64))
385 387 self.rxFile = list("------\n------\n------\n" for i in range(64))
386 388 self.bits = list("------\n------\n------\n" for i in range(64))
387 389 self.phase = list("----- -----\n----- -----\n----- -----\n" for i in range(64))
388 390
389 391 def __AutomaticControlModules(self):
390 392
391 393 # cmd = "GETF"
392 394 # rx_buffer = "experimento1.ab1" + "\n"
393 395 # self.__getFileFromModules(cmd = cmd, rx_buffer = rx_buffer)
394 396 #
395 397 # print self.rxFile
396 398
397 399 cmd = "ANST"
398 400 rx_buffer = "1"
399 401 self.__getControlModuleStatus(cmd = cmd, rx_buffer = rx_buffer)
400 402
401 403 cmd = "LWPH"
402 404 rx_buffer = "0"
403 405 self.__getControlModuleLowPhase(cmd = cmd, rx_buffer = rx_buffer)
404 406 print "Saving file..."
405 407
406 408
407 409 print self.bits
408 410 print self.phase
409 411
410 412 self.__WritingMonitoringFile()
411 413
412 414 threading.Timer(60, self.__AutomaticControlModules).start()
413 415
414 416 def __WritingMonitoringFile(self):
415 417 filename = "Monitoring.txt"
416 418 data = '===============================' + '\n'
417 419 self.__writeFile(filename, data)
418 420 data = time.strftime('\t' + "%d%b%Y %I:%M:%S %p" + '\n', time.localtime())
419 421 self.__writeFile(filename, data)
420 422 data = "MOD.\t BITS\t\t PHASE\n"
421 423 self.__writeFile(filename, data)
422 424 data = '===============================' + '\n'
423 425 self.__writeFile(filename, data)
424 426 for i in range(64):
425 427 tmp = self.bits[i].split('\n',3)
426 428 self.__writeFile(filename, ' ' + str(i + 1) + '\t\t' + tmp[2])
427 429 tmp = self.phase[i].split('\n',3)
428 430 self.__writeFile(filename, '\t\t' + tmp[2] + '\n')
429 431
430 432 if __name__ == '__main__':
431 433
432 434 absObj = ABSServer()
433 435
434 436 while 1:
435 437 absObj.waitRequest() No newline at end of file
@@ -1,39 +1,28
1 1
2 2 import optparse, os, sys
3 3
4 4 pathFile = os.path.dirname(os.path.abspath(__file__))
5 5 sys.path.append(os.path.split(pathFile)[0])
6 6
7 7 from bin.client3 import ABSClient
8 8
9 9 class readFile(object):
10 10
11 11 def __init__(self):
12 12 self.output = ""
13 13
14 def execute(self, filename):
15 if os.path.exists(filename):
16 #absObj = ABSClient(ipDestino="10.10.10.97")
17 absObj = ABSClient(ipDestino="10.10.20.27")
18 self.output = absObj.readFile(filename)
19 #pass
20 else:
21 print "No file"
14 def execute(self):
15 absObj = ABSClient(ipDestino="10.10.20.27")
16 self.output = absObj.readFile()
22 17
23 18 def getOutput(self):
24 19 return self.output
25 20
26 21 usage = "::::::::::::\n"
27 22
28 23 if __name__ == '__main__':
29 24
30 parser = optparse.OptionParser(usage=usage)
31
32 parser.add_option("-f", "--filename", dest="filename", type="string", default="", help="Filename")
25 app = readFile()
26 app.execute()
33 27
34 (options, args) = parser.parse_args()
35
36 filename = options.filename
37
38 app = readFile()
39 app.execute(filename)
28 print app.output
General Comments 0
You need to be logged in to leave comments. Login now