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