##// END OF EJS Templates
imanay -
r55:56
parent child
Show More
@@ -1,100 +1,100
1 #import tftpy
1 #import tftpy
2 import socket
2 import socket
3
3
4 class UDPComm:
4 class UDPComm:
5
5
6 __HEADER = "ABS"
6 __HEADER = "ABS"
7
7
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9
9
10 self.ipSource = ipSource
10 self.ipSource = ipSource
11 self.ipDestino = ipDestino
11 self.ipDestino = ipDestino
12 self.portDestino = portDestino
12 self.portDestino = portDestino
13 self.addr = (ipDestino,portDestino)
13 self.addr = (ipDestino,portDestino)
14 self.answer = ipDestino #test
14 self.answer = ipDestino #test
15 self.mode = "none"
15 self.mode = "none"
16
16
17 self.openSocket(asServer)
17 self.openSocket(asServer)
18
18
19 def openSocket(self, asServer):
19 def openSocket(self, asServer):
20
20
21 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
21 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
22 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
22 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
23 # self.socket_c.connect((self.ipDestino, self.portDestino))
23 # self.socket_c.connect((self.ipDestino, self.portDestino))
24
24
25 if asServer:
25 if asServer:
26 self.configAsServer()
26 self.configAsServer()
27 self.mode = "server"
27 self.mode = "server"
28 else:
28 else:
29 self.configAsClient()
29 self.configAsClient()
30 self.mode = "client"
30 self.mode = "client"
31
31
32 def configAsClient(self):
32 def configAsClient(self):
33 #Configurar broadcast
33 #Configurar broadcast
34 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
34 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
35
35
36 def configAsServer(self):
36 def configAsServer(self):
37
37
38 self.socket_c.bind(self.addr)
38 self.socket_c.bind(self.addr)
39 print "\nServer initialized"
39 print "\nServer initialized"
40
40
41 def waitData(self, nbytes = 16384):
41 def waitData(self, nbytes = 16384):
42
42
43 print "\nWaiting some data"
43 print "\nWaiting some data"
44 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
44 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
45 print "\nThis socket has received some data"
45 print "\nThis socket has received some data from:"
46 print self.answer
46 print self.answer
47 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
47 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
48
48
49 return ipSource, ipDestino, cmd, data
49 return ipSource, ipDestino, cmd, data
50
50
51 def sendData(self, cmd, data):
51 def sendData(self, cmd, data):
52
52
53 if self.portDestino == 7000:
53 if self.portDestino == 7000:
54 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
54 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
55 else:
55 else:
56 trama_tx = data
56 trama_tx = data
57
57
58 if self.mode == "client":
58 if self.mode == "client":
59 destiny = self.addr
59 destiny = self.addr
60 else:
60 else:
61 destiny = self.answer
61 destiny = self.answer
62 # Send messages
62 # Send messages
63 if(self.socket_c.sendto(trama_tx, destiny)):
63 if(self.socket_c.sendto(trama_tx, destiny)):
64 print "Sending message:[" + trama_tx + "]"
64 print "Sending message:[" + trama_tx + "] to " + str(destiny)
65
65
66 def __getTrama(self, trama):
66 def __getTrama(self, trama):
67
67
68 FrameList = trama.split(':')
68 FrameList = trama.split(':')
69
69
70 header = FrameList[0]
70 header = FrameList[0]
71 ipSource = FrameList[1]
71 ipSource = FrameList[1]
72 ipDestino = FrameList[2]
72 ipDestino = FrameList[2]
73 cmd = FrameList[3]
73 cmd = FrameList[3]
74 data = FrameList[4]
74 data = FrameList[4]
75 trash = FrameList[5]
75 trash = FrameList[5]
76
76
77 return ipSource, ipDestino, cmd, data
77 return ipSource, ipDestino, cmd, data
78
78
79 #class FTPComm:
79 #class FTPComm:
80 #
80 #
81 # ftp_servidor = 'ftp.servidor.com'
81 # ftp_servidor = 'ftp.servidor.com'
82 # ftp_usuario = 'miusuario'
82 # ftp_usuario = 'miusuario'
83 # ftp_clave = 'miclave'
83 # ftp_clave = 'miclave'
84 # ftp_raiz = '/public_html'
84 # ftp_raiz = '/public_html'
85 #
85 #
86 # def __init__(self):
86 # def __init__(self):
87 #
87 #
88 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
88 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
89 #
89 #
90 #
90 #
91 # def sendFile(self, filename):
91 # def sendFile(self, filename):
92 #
92 #
93 # self.client.upload(filename)
93 # self.client.upload(filename)
94 #
94 #
95 #if __name__ == '__main__':
95 #if __name__ == '__main__':
96 #
96 #
97 # obj = FTPComm()
97 # obj = FTPComm()
98
98
99
99
100 No newline at end of file
100
@@ -1,282 +1,280
1 import os
1 import os
2 import library
2 import library
3 import time
3 import time
4
4
5 class ABSServer:
5 class ABSServer:
6
6
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.255", portDestino=7000, ipDestino2="192.168.1.255", portDestino2=5500, ftpPortDestino=None):
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.255", portDestino=7000, ipDestino2="192.168.1.255", portDestino2=5500, ftpPortDestino=None):
8
8
9 self.ipSource = ipSource
9 self.ipSource = ipSource
10 self.ipDestino = ipDestino
10 self.ipDestino = ipDestino
11 self.portDestino = portDestino
11 self.portDestino = portDestino
12
12
13 self.ipDestino2 = ipDestino2
13 self.ipDestino2 = ipDestino2
14 self.portDestino2 = portDestino2
14 self.portDestino2 = portDestino2
15
15
16 self.ftpPortDestino = ftpPortDestino
16 self.ftpPortDestino = ftpPortDestino
17 self.experiment_name = "default"
17 self.experiment_name = "default"
18 self.tx_buffer = "default"
18 self.tx_buffer = "default"
19
19
20 self.createObjects()
20 self.createObjects()
21
21
22 def createObjects(self):
22 def createObjects(self):
23
23
24 asServer = True
24 asServer = True
25 self.commServerObj = library.UDPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
25 self.commServerObj = library.UDPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
26 self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
26 self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
27 #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
27 #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
28
28
29
29
30 def waitRequest(self):
30 def waitRequest(self):
31
31
32 ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData()
32 ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData()
33
33
34 datarpta = "OK"
34 datarpta = "OK"
35
35
36 if cmd == "SNDF":
36 if cmd == "SNDF":
37 self.sendFile2Modules()
37 self.sendFile2Modules()
38
38
39 if cmd == "CHGB":
39 if cmd == "CHGB":
40 self.changeBeam()
40 self.changeBeam()
41
41
42 if cmd == "ANST":
42 if cmd == "ANST":
43 self.getStatus(mode=3)
43 self.getStatus(mode=3)
44 datarpta = self.tx_buffer
44 datarpta = self.tx_buffer
45
45
46 self.commServerObj.sendData(cmd=cmd, data=datarpta)
46 self.commServerObj.sendData(cmd=cmd, data=datarpta)
47
47
48 def checkModule(self, address):
48 def checkModule(self, address):
49
49
50 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
50 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
51 status = os.system(cmd)
51 status = os.system(cmd)
52
52
53 if status == 256:
53 if status == 256:
54 return False
54 return False
55
55
56 return True
56 return True
57
57
58 def __writeReport(self, enaModules):
58 def __writeReport(self, enaModules):
59
59
60 status_array = ["Status of modules\n"]
60 status_array = ["Status of modules\n"]
61 status_array.append("----------------\n")
61 status_array.append("----------------\n")
62
62
63 for address in range(1,65):
63 for address in range(1,65):
64 if address in enaModules:
64 if address in enaModules:
65 # status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n")
65 # status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n")
66 status_array.append("192.168.1." + str(address) + " [1 1]\n")
66 status_array.append("192.168.1." + str(address) + " [1 1]\n")
67 else:
67 else:
68 status_array.append("192.168.1." + str(address) + " [0 0]\n")
68 status_array.append("192.168.1." + str(address) + " [0 0]\n")
69
69
70 f = open("module_status.txt","w")
70 f = open("module_status.txt","w")
71 f.writelines(status_array)
71 f.writelines(status_array)
72 f.close()
72 f.close()
73
73
74 def checkAntenna(self):
74 def checkAntenna(self):
75
75
76 """
76 """
77 Direccion de los modulos de las antenas:
77 Direccion de los modulos de las antenas:
78
78
79 Norte : 01-16
79 Norte : 01-16
80 Este : 17-32
80 Este : 17-32
81 Oeste: : 33-48
81 Oeste: : 33-48
82 Sur : 49-64
82 Sur : 49-64
83
83
84 """
84 """
85
85
86 enaModules = []
86 enaModules = []
87
87
88 for address in range(1,65):
88 for address in range(1,65):
89 if self.checkModule(address):
89 if self.checkModule(address):
90 enaModules.append(address)
90 enaModules.append(address)
91
91
92 self.__writeReport(enaModules)
92 self.__writeReport(enaModules)
93 return enaModules
93 return enaModules
94
94
95 def sendFile2Modules(self):
95 def sendFile2Modules(self):
96
96
97 #Needed for the loop
97 #Needed for the loop
98 rx_frame_list = self.datarx.split('\n',2)
98 rx_frame_list = self.datarx.split('\n',2)
99
99
100 experiment_name = rx_frame_list[0]
100 self.experiment_name = rx_frame_list[0]
101 experiment_number = rx_frame_list[1]
101 experiment_number = rx_frame_list[1]
102 str_control_modules = rx_frame_list[2]
102 str_control_modules = rx_frame_list[2]
103
103
104 lst_control_modules = str_control_modules.split("------\n")
104 lst_control_modules = str_control_modules.split("------\n")
105
105
106 enaModules = self.checkAntenna()
106 enaModules = self.checkAntenna()
107
107
108 for address in range(1,65):
108 for address in range(1,65):
109
109
110 if address not in enaModules:
110 if address not in enaModules:
111 continue
111 continue
112
112
113 self.__writeModuleFile(experiment_name, lst_control_modules[address-1])
113 self.__writeModuleFile(self.experiment_name, lst_control_modules[address-1])
114
114
115 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + experiment_name
115 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + self.experiment_name
116 print cmd
116 print cmd
117 os.system(cmd)
117 os.system(cmd)
118
119 self.experiment_name = experiment_name
120
118
121 self.__loadFile()
119 self.__loadFile()
122
120
123 def __writeModuleFile(self, filename, str):
121 def __writeModuleFile(self, filename, str):
124
122
125 fobj = open(filename,"w")
123 fobj = open(filename,"w")
126 fobj.write(filename + "\n")
124 fobj.write(filename + "\n")
127 fobj.write("------\n")
125 fobj.write("------\n")
128 fobj.write(str)
126 fobj.write(str)
129 fobj.write("------\n")
127 fobj.write("------\n")
130 fobj.close()
128 fobj.close()
131
129
132 def __readModuleFile(self, filename):
130 def __readModuleFile(self, filename):
133
131
134 fobj1 = open(filename,"r")
132 fobj1 = open(filename,"r")
135 file_list_1 = fobj1.readlines()
133 file_list_1 = fobj1.readlines()
136 fobj1.close()
134 fobj1.close()
137 content_str = ''.join(file_list_1[2:-1])
135 content_str = ''.join(file_list_1[2:-1])
138
136
139 return content_str
137 return content_str
140
138
141 def __loadFile(self):
139 def __loadFile(self):
142
140
143 #Working with the UDP socket
141 #Working with the UDP socket
144 self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":")
142 self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":")
145 self.commClientObj.sendData("none", "CAMBIA:0:")
143 self.commClientObj.sendData("none", "CAMBIA:0:")
146
144
147 def changeBeam(self):
145 def changeBeam(self):
148
146
149 #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
147 #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
150 self.commClientObj.sendData("CAMBIA:" + self.datarx + ":")
148 self.commClientObj.sendData("CAMBIA:" + self.datarx + ":")
151
149
152 def getStatus(self,mode):
150 def getStatus(self,mode):
153
151
154 if mode == 1:
152 if mode == 1:
155 self.__getStsMode1()
153 self.__getStsMode1()
156 elif mode == 2:
154 elif mode == 2:
157 self.__getStsMode2()
155 self.__getStsMode2()
158 else:
156 else:
159 self.__getStsMode3()
157 self.__getStsMode3()
160
158
161
159
162 def __getStsMode1(self):
160 def __getStsMode1(self):
163 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
161 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
164 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
162 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
165 seconds = int (self.datarx)
163 seconds = int (self.datarx)
166 # Give 5 seconds to the control modules
164 # Give 5 seconds to the control modules
167 time.sleep(seconds)
165 time.sleep(seconds)
168 # Checking the module connection
166 # Checking the module connection
169 module_list = self.connection_status(10)
167 module_list = self.connection_status(10)
170 #Generating the complete report
168 #Generating the complete report
171 module = 1
169 module = 1
172 number_of_modules = 16
170 number_of_modules = 16
173 filename1 = "Verificacion"
171 filename1 = "Verificacion"
174 filename2 = "report.txt"
172 filename2 = "report.txt"
175 fobj2 = open(filename2,"w")
173 fobj2 = open(filename2,"w")
176 fobj2.write("Verification_file\n")
174 fobj2.write("Verification_file\n")
177 fobj2.write("-----------------\n")
175 fobj2.write("-----------------\n")
178 fobj2.close()
176 fobj2.close()
179 while module <= number_of_modules:
177 while module <= number_of_modules:
180 if module_list[module -1] == "1":
178 if module_list[module -1] == "1":
181 #Preparing and doing the tftp command
179 #Preparing and doing the tftp command
182 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1
180 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1
183 print cmd
181 print cmd
184 os.system(cmd)
182 os.system(cmd)
185 # Getting data from the control module file
183 # Getting data from the control module file
186 fobj1 = open(filename1,"r")
184 fobj1 = open(filename1,"r")
187 file_list_1 = fobj1.readlines()
185 file_list_1 = fobj1.readlines()
188 fobj1.close()
186 fobj1.close()
189 content = file_list_1[2:-1]
187 content = file_list_1[2:-1]
190 #
188 #
191 fobj2 = open(filename2,"a")
189 fobj2 = open(filename2,"a")
192 if base == 10:
190 if base == 10:
193 fobj2.write("S" + str(module) + "\n")
191 fobj2.write("S" + str(module) + "\n")
194 else:
192 else:
195 fobj2.write("N" + str(module) + "\n")
193 fobj2.write("N" + str(module) + "\n")
196 fobj2.writelines(content)
194 fobj2.writelines(content)
197 fobj2.write("------\n")
195 fobj2.write("------\n")
198 fobj2.close()
196 fobj2.close()
199 module = module + 1
197 module = module + 1
200
198
201 def __getStsMode2(self):
199 def __getStsMode2(self):
202
200
203 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
201 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
204 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
202 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
205 seconds = int (self.datarx)
203 seconds = int (self.datarx)
206 # Give 5 seconds to the control modules
204 # Give 5 seconds to the control modules
207 time.sleep(seconds)
205 time.sleep(seconds)
208 # Checking the module connection
206 # Checking the module connection
209 enaModules = self.checkAntenna()
207 enaModules = self.checkAntenna()
210 #Generating the complete report
208 #Generating the complete report
211 filename1 = "Verificacion"
209 filename1 = "Verificacion"
212 line1 = "Verification_file\n"
210 line1 = "Verification_file\n"
213 line2 = "-----------------\n"
211 line2 = "-----------------\n"
214 report_list = [line1, line2]
212 report_list = [line1, line2]
215
213
216 for address in range(1,65):
214 for address in range(1,65):
217
215
218 if address not in enaModules:
216 if address not in enaModules:
219 continue
217 continue
220 #Preparing and doing the tftp command
218 #Preparing and doing the tftp command
221 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1
219 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1
222 print cmd
220 print cmd
223 os.system(cmd)
221 os.system(cmd)
224 #Sub_header
222 #Sub_header
225 report_list.append("ABS_" + str(address) + "\n")
223 report_list.append("ABS_" + str(address) + "\n")
226 # Content
224 # Content
227 fobj1 = open(filename1,"r")
225 fobj1 = open(filename1,"r")
228 file_list_1 = fobj1.readlines()
226 file_list_1 = fobj1.readlines()
229 fobj1.close()
227 fobj1.close()
230 content = ''.join(file_list_1[2:-1])
228 content = ''.join(file_list_1[2:-1])
231 report_list.append(content)
229 report_list.append(content)
232 #Ending
230 #Ending
233 report_list.append("------\n")
231 report_list.append("------\n")
234 #print "\nFinalizado"
232 #print "\nFinalizado"
235 self.tx_buffer = ''.join(report_list)
233 self.tx_buffer = ''.join(report_list)
236
234
237 def __AddingHeader(self,content_list, title):
235 def __AddingHeader(self,content_list, title):
238
236
239 line1 = title + "\n"
237 line1 = title + "\n"
240 line2 = "-----------------\n"
238 line2 = "-----------------\n"
241 header_list = [line1, line2]
239 header_list = [line1, line2]
242 verification_list = header_list + content_list
240 verification_list = header_list + content_list
243 # Arming the frame
241 # Arming the frame
244 self.tx_buffer = ''.join(verification_list)
242 self.tx_buffer = ''.join(verification_list)
245
243
246 def __getModuleFile(self, filename):
244 def __getModuleFile(self, filename):
247
245
248 enaModules = self.checkAntenna()
246 enaModules = self.checkAntenna()
249 content_list = []
247 content_list = []
250 for address in range(1,65):
248 for address in range(1,65):
251
249
252 if address not in enaModules:
250 if address not in enaModules:
253 continue
251 continue
254 #Preparing and doing the tftp command
252 #Preparing and doing the tftp command
255 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename
253 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename
256 print cmd
254 print cmd
257 os.system(cmd)
255 os.system(cmd)
258 #Sub_header
256 #Sub_header
259 content_list.append("ABS_" + str(address) + "\n")
257 content_list.append("ABS_" + str(address) + "\n")
260 # From module file to list
258 # From module file to list
261 content_str = self.__readModuleFile(filename)
259 content_str = self.__readModuleFile(filename)
262 content_list.append(content_str)
260 content_list.append(content_str)
263 content_list.append("------\n")
261 content_list.append("------\n")
264
262
265 self.__AddingHeader(content_list, title = "Verification_file")
263 self.__AddingHeader(content_list, title = "Verification_file")
266
264
267 def __getStsMode3(self):
265 def __getStsMode3(self):
268
266
269 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
267 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
270 seconds = int (self.datarx)
268 seconds = int (self.datarx)
271 # Give 5 seconds to the control modules
269 # Give 5 seconds to the control modules
272 time.sleep(seconds)
270 time.sleep(seconds)
273
271
274 self.__getModuleFile(filename = "Verificacion")
272 self.__getModuleFile(filename = "Verificacion")
275
273
276
274
277 if __name__ == '__main__':
275 if __name__ == '__main__':
278
276
279 absObj = ABSServer()
277 absObj = ABSServer()
280
278
281 while 1:
279 while 1:
282 absObj.waitRequest() No newline at end of file
280 absObj.waitRequest()
General Comments 0
You need to be logged in to leave comments. Login now