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