@@ -0,0 +1,370 | |||
|
1 | import os | |
|
2 | import library3 | |
|
3 | import time | |
|
4 | ||
|
5 | class ABSServer: | |
|
6 | ||
|
7 | def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500): | |
|
8 | ||
|
9 | self.ipSource = ipSource | |
|
10 | self.ipDestino = ipDestino | |
|
11 | self.portDestino = portDestino | |
|
12 | ||
|
13 | self.ipDestino2 = ipDestino2 | |
|
14 | self.portDestino2 = portDestino2 | |
|
15 | ||
|
16 | self.tx_buffer = "default" | |
|
17 | self.rx_buffer = "default" | |
|
18 | ||
|
19 | self.createObjects() | |
|
20 | ||
|
21 | def createObjects(self): | |
|
22 | ||
|
23 | asServer = True | |
|
24 | self.commServerObj = library3.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer) | |
|
25 | self.commClientObj = library3.TCPComm("Central_Control", self.ipDestino2, self.portDestino2) | |
|
26 | ||
|
27 | def waitRequest(self): | |
|
28 | ||
|
29 | # Using rx buffer | |
|
30 | ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitServer() | |
|
31 | ||
|
32 | if cmd == "SNDF": | |
|
33 | # datarpta = self.sendFile2Modules(mode=2, cmd = cmd) | |
|
34 | datarpta = self.__sendFile2Modules(cmd = cmd) | |
|
35 | ||
|
36 | if cmd == "CHGB": | |
|
37 | datarpta = self.__changeBeam(cmd = cmd) | |
|
38 | ||
|
39 | if cmd == "ANST": | |
|
40 | self.__getStatus(cmd = cmd) | |
|
41 | # Using tx buffer | |
|
42 | datarpta = self.tx_buffer | |
|
43 | ||
|
44 | self.commServerObj.sendData2(cmd=cmd, data=datarpta, ipDestino = ipSource) | |
|
45 | ||
|
46 | def checkModule(self, address): | |
|
47 | ||
|
48 | cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null" | |
|
49 | status = os.system(cmd) | |
|
50 | ||
|
51 | if status == 256: | |
|
52 | return False | |
|
53 | ||
|
54 | return True | |
|
55 | ||
|
56 | def __writeReport(self, enaModules): | |
|
57 | ||
|
58 | status_array = ["Status of modules\n"] | |
|
59 | status_array.append("----------------\n") | |
|
60 | ||
|
61 | for address in range(1,65): | |
|
62 | if address in enaModules: | |
|
63 | status_array.append("192.168.1." + str(address) + " [1 1]\n") | |
|
64 | else: | |
|
65 | status_array.append("192.168.1." + str(address) + " [0 0]\n") | |
|
66 | ||
|
67 | f = open("module_status.txt","w") | |
|
68 | f.writelines(status_array) | |
|
69 | f.close() | |
|
70 | ||
|
71 | def checkAntenna(self): | |
|
72 | ||
|
73 | """ | |
|
74 | Direccion de los modulos de las antenas: | |
|
75 | ||
|
76 | Norte : 01-16 | |
|
77 | Este : 17-32 | |
|
78 | Oeste: : 33-48 | |
|
79 | Sur : 49-64 | |
|
80 | ||
|
81 | """ | |
|
82 | ||
|
83 | enaModules = [] | |
|
84 | ||
|
85 | for address in range(1,65): | |
|
86 | if self.checkModule(address): | |
|
87 | enaModules.append(address) | |
|
88 | ||
|
89 | self.__writeReport(enaModules) | |
|
90 | return enaModules | |
|
91 | ||
|
92 | # def sendFile2Modules(self, mode, cmd): | |
|
93 | # | |
|
94 | # if mode == 1: | |
|
95 | # self.__sendFile2Modules1() | |
|
96 | # else: | |
|
97 | # self.__sendFile2Modules2(cmd) | |
|
98 | # | |
|
99 | # def __sendFile2Modules1(self): | |
|
100 | # | |
|
101 | # #Needed for the loop | |
|
102 | # rx_frame_list = self.rx_buffer.split('\n',2) | |
|
103 | # | |
|
104 | # self.experiment_name = rx_frame_list[0] | |
|
105 | # experiment_number = rx_frame_list[1] | |
|
106 | # str_control_modules = rx_frame_list[2] | |
|
107 | # | |
|
108 | # lst_control_modules = str_control_modules.split("------\n") | |
|
109 | # | |
|
110 | ## enaModules = self.checkAntenna() | |
|
111 | # enaModules = [11,12,13,14] | |
|
112 | # | |
|
113 | # for address in range(1,65): | |
|
114 | # | |
|
115 | # if address not in enaModules: | |
|
116 | # continue | |
|
117 | # | |
|
118 | # self.__writeModuleFile(self.experiment_name, lst_control_modules[address-1]) | |
|
119 | # | |
|
120 | # cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + self.experiment_name | |
|
121 | # print cmd | |
|
122 | # os.system(cmd) | |
|
123 | # | |
|
124 | # self.__loadFile() | |
|
125 | ||
|
126 | def __sendFile2Modules(self,cmd): | |
|
127 | ||
|
128 | #Needed for the loop | |
|
129 | rx_frame_list = self.rx_buffer.split('\n',2) | |
|
130 | correct = 0 | |
|
131 | failure = 0 | |
|
132 | header = rx_frame_list[0] + "\n" | |
|
133 | str_control_modules = rx_frame_list[2] | |
|
134 | ||
|
135 | lst_control_modules = str_control_modules.split("------\n") | |
|
136 | ||
|
137 | # enaModules = self.checkAntenna() | |
|
138 | enaModules = [11,12,13,14] | |
|
139 | ||
|
140 | for id in range(1,65): | |
|
141 | ||
|
142 | if id not in enaModules: | |
|
143 | continue | |
|
144 | #tcp client needed | |
|
145 | self.commClientObj.open_socket() | |
|
146 | ip = "192.168.1." + str(id) | |
|
147 | self.commClientObj.sendData2(cmd, header + lst_control_modules[id-1], ip) | |
|
148 | ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient() | |
|
149 | self.commClientObj.close_socket() | |
|
150 | ||
|
151 | if tmp == "OK": | |
|
152 | correct = correct + 1 | |
|
153 | else: | |
|
154 | failure = failure + 1 | |
|
155 | ||
|
156 | if correct == len(enaModules): | |
|
157 | rpta = "OK" | |
|
158 | else: | |
|
159 | rpta = "Failure" | |
|
160 | ||
|
161 | return rpta | |
|
162 | ||
|
163 | ||
|
164 | def __writeModuleFile(self, filename, str): | |
|
165 | ||
|
166 | fobj = open(filename,"w") | |
|
167 | fobj.write(filename + "\n") | |
|
168 | fobj.write("------\n") | |
|
169 | fobj.write(str) | |
|
170 | fobj.write("------\n") | |
|
171 | fobj.close() | |
|
172 | ||
|
173 | def __readModuleFile(self, filename): | |
|
174 | ||
|
175 | fobj1 = open(filename,"r") | |
|
176 | file_list_1 = fobj1.readlines() | |
|
177 | fobj1.close() | |
|
178 | content_str = ''.join(file_list_1[2:-1]) | |
|
179 | ||
|
180 | return content_str | |
|
181 | ||
|
182 | def __loadFile(self): | |
|
183 | ||
|
184 | #Working with the UDP socket | |
|
185 | self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":") | |
|
186 | self.commClientObj.sendData("none", "CAMBIA:0:") | |
|
187 | ||
|
188 | def __changeBeam(self, cmd): | |
|
189 | ||
|
190 | correct = 0 | |
|
191 | failure = 0 | |
|
192 | # enaModules = self.checkAntenna() | |
|
193 | enaModules = [11,12,13,14] | |
|
194 | ||
|
195 | for id in range(1,65): | |
|
196 | if id not in enaModules: | |
|
197 | continue | |
|
198 | ||
|
199 | self.commClientObj.open_socket() | |
|
200 | ip = "192.168.1." + str(id) | |
|
201 | self.commClientObj.sendData2(cmd, self.rx_buffer, ip) | |
|
202 | # ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData() | |
|
203 | ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient() | |
|
204 | self.commClientObj.close_socket() | |
|
205 | ||
|
206 | if tmp == "OK": | |
|
207 | correct = correct + 1 | |
|
208 | else: | |
|
209 | failure = failure + 1 | |
|
210 | ||
|
211 | if correct == len(enaModules): | |
|
212 | rpta = "OK" | |
|
213 | else: | |
|
214 | rpta = "Failure" | |
|
215 | ||
|
216 | return rpta | |
|
217 | ||
|
218 | # def __getStatus(self, mode, cmd): | |
|
219 | # | |
|
220 | # if mode == 1: | |
|
221 | # self.__getStsMode1() | |
|
222 | # elif mode == 2: | |
|
223 | # self.__getStsMode2() | |
|
224 | # elif mode == 3: | |
|
225 | # self.__getStsMode3() | |
|
226 | # else: | |
|
227 | # self.__getStsMode4(cmd) | |
|
228 | ||
|
229 | ||
|
230 | # def __getStsMode1(self): | |
|
231 | # #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") | |
|
232 | # self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":") | |
|
233 | # seconds = int (self.rx_buffer) | |
|
234 | # # Give 5 seconds to the control modules | |
|
235 | # time.sleep(seconds) | |
|
236 | # # Checking the module connection | |
|
237 | # module_list = self.connection_status(10) | |
|
238 | # #Generating the complete report | |
|
239 | # module = 1 | |
|
240 | # number_of_modules = 16 | |
|
241 | # filename1 = "Verificacion" | |
|
242 | # filename2 = "report.txt" | |
|
243 | # fobj2 = open(filename2,"w") | |
|
244 | # fobj2.write("Verification_file\n") | |
|
245 | # fobj2.write("-----------------\n") | |
|
246 | # fobj2.close() | |
|
247 | # while module <= number_of_modules: | |
|
248 | # if module_list[module -1] == "1": | |
|
249 | # #Preparing and doing the tftp command | |
|
250 | # cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1 | |
|
251 | # print cmd | |
|
252 | # os.system(cmd) | |
|
253 | # # Getting data from the control module file | |
|
254 | # fobj1 = open(filename1,"r") | |
|
255 | # file_list_1 = fobj1.readlines() | |
|
256 | # fobj1.close() | |
|
257 | # content = file_list_1[2:-1] | |
|
258 | # # | |
|
259 | # fobj2 = open(filename2,"a") | |
|
260 | # if base == 10: | |
|
261 | # fobj2.write("S" + str(module) + "\n") | |
|
262 | # else: | |
|
263 | # fobj2.write("N" + str(module) + "\n") | |
|
264 | # fobj2.writelines(content) | |
|
265 | # fobj2.write("------\n") | |
|
266 | # fobj2.close() | |
|
267 | # module = module + 1 | |
|
268 | ||
|
269 | # def __getStsMode2(self): | |
|
270 | # | |
|
271 | # #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") | |
|
272 | # self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":") | |
|
273 | # seconds = int (self.rx_buffer) | |
|
274 | # # Give 5 seconds to the control modules | |
|
275 | # time.sleep(seconds) | |
|
276 | # # Checking the module connection | |
|
277 | # enaModules = self.checkAntenna() | |
|
278 | # #Generating the complete report | |
|
279 | # filename1 = "Verificacion" | |
|
280 | # line1 = "Verification_file\n" | |
|
281 | # line2 = "-----------------\n" | |
|
282 | # report_list = [line1, line2] | |
|
283 | # | |
|
284 | # for address in range(1,65): | |
|
285 | # | |
|
286 | # if address not in enaModules: | |
|
287 | # continue | |
|
288 | # #Preparing and doing the tftp command | |
|
289 | # cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1 | |
|
290 | # print cmd | |
|
291 | # os.system(cmd) | |
|
292 | # #Sub_header | |
|
293 | # report_list.append("ABS_" + str(address) + "\n") | |
|
294 | # # Content | |
|
295 | # fobj1 = open(filename1,"r") | |
|
296 | # file_list_1 = fobj1.readlines() | |
|
297 | # fobj1.close() | |
|
298 | # content = ''.join(file_list_1[2:-1]) | |
|
299 | # report_list.append(content) | |
|
300 | # #Ending | |
|
301 | # report_list.append("------\n") | |
|
302 | # #print "\nFinalizado" | |
|
303 | # self.tx_buffer = ''.join(report_list) | |
|
304 | ||
|
305 | def __AddingHeader(self,content_list, title): | |
|
306 | ||
|
307 | line1 = title + "\n" | |
|
308 | line2 = "-----------------\n" | |
|
309 | header_list = [line1, line2] | |
|
310 | verification_list = header_list + content_list | |
|
311 | # Arming the frame | |
|
312 | self.tx_buffer = ''.join(verification_list) | |
|
313 | ||
|
314 | # def __getModuleFile(self, filename): | |
|
315 | # | |
|
316 | # enaModules = self.checkAntenna() | |
|
317 | # content_list = [] | |
|
318 | # for address in range(1,65): | |
|
319 | # | |
|
320 | # if address not in enaModules: | |
|
321 | # continue | |
|
322 | # #Preparing and doing the tftp command | |
|
323 | # cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename | |
|
324 | # print cmd | |
|
325 | # os.system(cmd) | |
|
326 | # #Sub_header | |
|
327 | # content_list.append("ABS_" + str(address) + "\n") | |
|
328 | # # From module file to list | |
|
329 | # content_str = self.__readModuleFile(filename) | |
|
330 | # content_list.append(content_str) | |
|
331 | # content_list.append("------\n") | |
|
332 | # | |
|
333 | # self.__AddingHeader(content_list, title = "Verification_file") | |
|
334 | ||
|
335 | # def __getStsMode3(self): | |
|
336 | # | |
|
337 | # self.commClientObj.sendData("none", "CHEQUEO:" + self.rx_buffer + ":") | |
|
338 | # seconds = int (self.rx_buffer) | |
|
339 | # # Give 5 seconds to the control modules | |
|
340 | # time.sleep(seconds) | |
|
341 | # | |
|
342 | # self.__getModuleFile(filename = "Verificacion") | |
|
343 | ||
|
344 | def __getStatus(self, cmd): | |
|
345 | ||
|
346 | content_str = "" | |
|
347 | # enaModules = self.checkAntenna() | |
|
348 | enaModules = [11,12,13,14] | |
|
349 | ||
|
350 | for id in range(1,65): | |
|
351 | if id not in enaModules: | |
|
352 | continue | |
|
353 | ||
|
354 | self.commClientObj.open_socket() | |
|
355 | ip = "192.168.1." + str(id) | |
|
356 | self.commClientObj.sendData2(cmd, self.rx_buffer, ip) | |
|
357 | ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient() | |
|
358 | self.commClientObj.close_socket() | |
|
359 | ||
|
360 | content_str = content_str + tmp | |
|
361 | # self.__AddingHeader(content_list, title = "Verification_file") | |
|
362 | #Using tx buffer | |
|
363 | self.tx_buffer = content_str | |
|
364 | ||
|
365 | if __name__ == '__main__': | |
|
366 | ||
|
367 | absObj = ABSServer() | |
|
368 | ||
|
369 | while 1: | |
|
370 | absObj.waitRequest() No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now