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