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