##// END OF EJS Templates
changes to integrate to the web interface
imanay -
r141:142
parent child
Show More
@@ -1,88 +1,87
1 1 import library3
2 2
3 3 class ABSClient:
4 4
5 5 def __init__(self,ipSource="192.168.1.117", iDSource="Clnt_01", ipDestino="192.168.1.117", iDDestino = "CeCnMod", portDestino=7000):
6 6
7 7 self.ipSource = ipSource
8 8 self.iDSource = iDSource
9 9 self.ipDestino = ipDestino
10 10 self.iDDestino = iDDestino
11 11 self.portDestino = portDestino
12 12
13 13 self.createObjects()
14 14
15 15 def createObjects(self):
16 16
17 17 self.commObj = library3.TCPComm(self.ipSource, self.iDSource, self.ipDestino, self.iDDestino, self.portDestino)
18 18 # self.wFiles = library3.FilesStuff()
19 19
20 20 def __ConnectionWithCentralControl(self, cmd, data):
21 21
22 22 self.commObj.open_socket()
23 23 self.commObj.sendData(cmd = cmd, data = data, ipDestino = self.ipDestino)
24 24 ipSource, ipDestino, cmd, output = self.commObj.waitData()
25 25 self.commObj.close_socket()
26 26
27 27 return output
28 28
29 29 # def abs2ControlModuleFormatFile(self, filename):
30 30 #
31 31 # #From matriz to control module format
32 32 # self.wFiles.toCentralControlFormat(filename)
33 33 # FileName = "CentralControlFormat.txt"
34 34 # F_Obj = open(FileName,"r")
35 35 # FileList = F_Obj.readlines()
36 36 # F_Obj.close()
37 37 # FileStr = "".join(FileList)
38 38 #
39 39 # return FileStr
40 40
41 41 def sendFile(self, filename):
42 42
43 # data = self.abs2ControlModuleFormatFile(filename)
44 tmp = self.__readFile(filename)
45 #Sending content of file and the file name
46 data = filename + '\n' + tmp
43 data = self.__readFile(filename)
47 44 self.__ConnectionWithCentralControl(cmd = "SNDF", data = data)
48 45
49 46 def changeBeam(self, newBeam):
50 47
51 48 self.__ConnectionWithCentralControl(cmd = "CHGB", data = newBeam)
52 49
53 50 def __writeFile(self, filename, data):
54 51
55 52 fobj = open(filename,"w")
56 53 fobj.writelines(data)
57 54 fobj.close()
58 55
59 56 def __readFile(self, filename):
60 57
61 58 fobj = open(filename,"r")
62 59 listData = fobj.readlines()
63 60 fobj.close()
64 data = "".join(listData)
61 tmp = "".join(listData)
62 #Adding filename to the begining of the data
63 data = filename + '\n' + tmp
65 64 return data
66 65
67 66
68 67 def getControlModuleStatus(self, data):
69 68
70 69 data = self.__ConnectionWithCentralControl(cmd = "ANST", data = data)
71 70 self.__writeFile("report.txt", data)
72 71
73 72 def getControlModulePhase(self, data):
74 73
75 74 data = self.__ConnectionWithCentralControl(cmd = "ANPH", data = data)
76 75 # self.__writeFile("report.txt", data)
77 76
78 77 def getConnectionStatus(self):
79 78
80 79 data = self.__ConnectionWithCentralControl(cmd = "NTST", data = "none")
81 80 self.__writeFile("connection_status.txt", data)
82 81
83 82 if __name__ == '__main__':
84 83
85 84 filename = "experimento1.abs"
86 85
87 86 absObj = ABSClient()
88 87 absObj.sendFile(filename)
@@ -1,289 +1,290
1 1 import os
2 2 import library3
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500, rx_buffer = "default"):
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.tx_buffer = "default"
17 17 # self.rx_buffer = "default"
18 18 self.rx_buffer = rx_buffer
19 19 self.enaModules = []
20 20
21 21 self.createObjects()
22 22
23 23 def createObjects(self):
24 24
25 25 asServer = True
26 26 self.commServerObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino, "CnMod01", self.portDestino, asServer)
27 27 self.commClientObj = library3.TCPComm("Central_Control", "CeCnMod", self.ipDestino2, "CnMod01", self.portDestino2)
28 28 self.wFiles = library3.FilesStuff()
29 29
30 30 def waitRequest(self):
31 31
32 32 #Using rx buffer
33 33 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
34 34
35 35 if cmd == "SNDF":
36 36 datarpta = self.__sendFile2Modules(cmd = cmd)
37 37
38 38 if cmd == "CHGB":
39 39 datarpta = self.__changeBeam(cmd = cmd)
40 40
41 41 if cmd == "ANST":
42 42 self.__getControlModuleStatus(cmd = cmd)
43 43 #Using tx buffer
44 44 datarpta = self.tx_buffer
45 45
46 46 if cmd == "ANPH":
47 47 self.__getControlModulePhase(cmd = cmd)
48 48 #Using tx buffer
49 49 datarpta = self.tx_buffer
50 50
51 51 if cmd == "NTST":
52 52 #Using tx buffer
53 53 datarpta = self.__getConnectionStatus(cmd = cmd)
54 54
55 55 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
56 56
57 57 def checkModule(self, address):
58 58
59 59 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
60 60 status = os.system(cmd)
61 61
62 62 if status == 256:
63 63 return False
64 64
65 65 return True
66 66
67 67 def __writeReport(self, enaModules):
68 68
69 69 status_array = ["Status of modules\n"]
70 70 status_array.append("----------------\n")
71 71
72 72 for address in range(1,65):
73 73 if address in enaModules:
74 74 status_array.append("192.168.1." + str(address) + " [1 1]\n")
75 75 else:
76 76 status_array.append("192.168.1." + str(address) + " [0 0]\n")
77 77
78 78 filename = "module_status.txt"
79 79 self.__writeFile(filename,status_array)
80 80 # f = open("module_status.txt","w")
81 81 # f.writelines(status_array)
82 82 # f.close()
83 83
84 84
85 85 def __writeFile(self, filename, data):
86 86
87 87 fobj = open(filename,"w")
88 88 fobj.writelines(data)
89 89 fobj.close()
90 90
91 91 def checkAntenna(self):
92 92
93 93 """
94 94 Direccion de los modulos de las antenas:
95 95
96 96 Norte : 01-16
97 97 Este : 17-32
98 98 Oeste: : 33-48
99 99 Sur : 49-64
100 100
101 101 """
102 102
103 103 enaModules2 = []
104 104
105 105 for address in range(1,65):
106 106 if self.checkModule(address):
107 107 enaModules2.append(address)
108 108
109 109 self.__writeReport(enaModules2)
110 110 return enaModules2
111 111
112 112 def __ConnectionWithControlModules(self,data,cmd,id):
113 113
114 114 self.commClientObj.open_socket()
115 115 ip = "192.168.1." + str(id)
116 116 self.commClientObj.sendData(cmd, data, ip)
117 117 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
118 118 self.commClientObj.close_socket()
119 119
120 120 return tmp
121 121
122 122 def abs2ControlModuleFormatFile(self, filename):
123 123
124 124 #From matriz to control module format
125 125 self.wFiles.toCentralControlFormat(filename)
126 126 FileName = "CentralControlFormat.txt"
127 127 F_Obj = open(FileName,"r")
128 128 FileList = F_Obj.readlines()
129 129 F_Obj.close()
130 130 FileStr = "".join(FileList)
131 131
132 132 return FileStr
133 133
134 134 def __All2Blocks(self,input):
135 135
136 136 rx_frame_lst = input.split('\n',2)
137 137
138 138 header = rx_frame_lst[0] + "\n"
139 139 control_modules_str = rx_frame_lst[2]
140 140 control_modules_lst = control_modules_str.split("------\n")
141 141
142 142 return header, control_modules_lst
143 143
144 144
145 145 def __sendFile2Modules(self,cmd):
146 146
147 147 rx_buffer_lst = self.rx_buffer.split('\n',1)
148 #Getting the filename from the begining of data
148 149 filename = rx_buffer_lst[0]
149 150 tmp = rx_buffer_lst[1]
150 151 self.__writeFile(filename,tmp)
151 152 data = self.abs2ControlModuleFormatFile(filename)
152 153 #Needed for the loop
153 154 header, control_modules_lst = self.__All2Blocks(data)
154 155 correct = 0
155 156
156 157 for id in range(1,65):
157 158
158 159 if id not in self.enaModules:
159 160 continue
160 161
161 162 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
162 163 correct = correct + 1
163 164
164 165 if correct == len(self.enaModules):
165 166 rpta = "OK"
166 167 else:
167 168 rpta = "Failure"
168 169
169 170 return rpta
170 171
171 172 def __changeBeam(self, cmd):
172 173
173 174 correct = 0
174 175 # enaModules = self.checkAntenna()
175 176 # enaModules = [11,12,13,14]
176 177
177 178 for id in range(1,65):
178 179 if id not in self.enaModules:
179 180 continue
180 181
181 182 if self.__ConnectionWithControlModules(self.rx_buffer,cmd,id) == "OK":
182 183 correct = correct + 1
183 184
184 185 if correct == len(self.enaModules):
185 186 rpta = "OK"
186 187 else:
187 188 rpta = "Failure"
188 189
189 190 return rpta
190 191
191 192 def __getControlModuleStatus(self, cmd):
192 193
193 194 all_blocks = ""
194 195 # enaModules = self.checkAntenna()
195 196 # enaModules = [11,12,13,14]
196 197
197 198 for id in range(1,65):
198 199 if id not in self.enaModules:
199 200 continue
200 201
201 202 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
202 203
203 204 all_blocks = all_blocks + one_block
204 205 #Using tx buffer
205 206 print all_blocks
206 207 self.tx_buffer = all_blocks
207 208
208 209 def __getControlModulePhase(self, cmd):
209 210
210 211 all_blocks = ""
211 212 # enaModules = self.checkAntenna()
212 213 # enaModules = [11,12,13,14]
213 214
214 215 for id in range(1,65):
215 216 if id not in self.enaModules:
216 217 continue
217 218
218 219 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
219 220
220 221 all_blocks = all_blocks + one_block
221 222 #Using tx buffer
222 223 print all_blocks
223 224 self.tx_buffer = all_blocks
224 225
225 226 def __getConnectionStatus(self, cmd):
226 227
227 228 ena = self.checkAntenna()
228 229 print ena
229 230 self.enaModules = ena
230 231
231 232 blockLst = []
232 233
233 234 for id in range(1,65):
234 235 if id not in self.enaModules:
235 236 continue
236 237
237 238 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
238 239 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
239 240 #Using tx buffer
240 241 self.tx_buffer = "".join(blockLst)
241 242 print self.tx_buffer
242 243
243 244 return self.tx_buffer
244 245
245 246 def getConnectionStatus(self, cmd):
246 247
247 248 ena = self.checkAntenna()
248 249 self.enaModules = ena
249 250
250 251 blockLst = []
251 252
252 253 for id in range(1,65):
253 254 if id not in self.enaModules:
254 255 continue
255 256
256 257 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
257 258 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
258 259 #Using tx buffer
259 260 self.tx_buffer = "".join(blockLst)
260 261 print self.tx_buffer
261 262
262 263 return self.tx_buffer
263 264
264 265 def getControlModuleStatus(self, cmd):
265 266
266 267 all_blocks = ""
267 268 # enaModules = self.checkAntenna()
268 269 # enaModules = [11,12,13,14]
269 270
270 271 for id in range(1,65):
271 272 if id not in self.enaModules:
272 273 continue
273 274
274 275 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
275 276
276 277 all_blocks = all_blocks + one_block
277 278 #Using tx buffer
278 279 print all_blocks
279 280 self.tx_buffer = all_blocks
280 281
281 282 return all_blocks
282 283
283 284
284 285 if __name__ == '__main__':
285 286
286 287 absObj = ABSServer()
287 288
288 289 while 1:
289 290 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now