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