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