##// END OF EJS Templates
changes to integrate to the web interface
imanay -
r139:140
parent child
Show More
@@ -1,86 +1,88
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 43 # data = self.abs2ControlModuleFormatFile(filename)
44 data = self.__readFile(filename)
44 tmp = self.__readFile(filename)
45 #Sending content of file and the file name
46 data = filename + '\n' + tmp
45 47 self.__ConnectionWithCentralControl(cmd = "SNDF", data = data)
46 48
47 49 def changeBeam(self, newBeam):
48 50
49 51 self.__ConnectionWithCentralControl(cmd = "CHGB", data = newBeam)
50 52
51 53 def __writeFile(self, filename, data):
52 54
53 55 fobj = open(filename,"w")
54 56 fobj.writelines(data)
55 57 fobj.close()
56 58
57 59 def __readFile(self, filename):
58 60
59 61 fobj = open(filename,"r")
60 62 listData = fobj.readlines()
61 63 fobj.close()
62 64 data = "".join(listData)
63 65 return data
64 66
65 67
66 68 def getControlModuleStatus(self, data):
67 69
68 70 data = self.__ConnectionWithCentralControl(cmd = "ANST", data = data)
69 71 self.__writeFile("report.txt", data)
70 72
71 73 def getControlModulePhase(self, data):
72 74
73 75 data = self.__ConnectionWithCentralControl(cmd = "ANPH", data = data)
74 76 # self.__writeFile("report.txt", data)
75 77
76 78 def getConnectionStatus(self):
77 79
78 80 data = self.__ConnectionWithCentralControl(cmd = "NTST", data = "none")
79 81 self.__writeFile("connection_status.txt", data)
80 82
81 83 if __name__ == '__main__':
82 84
83 85 filename = "experimento1.abs"
84 86
85 87 absObj = ABSClient()
86 88 absObj.sendFile(filename)
@@ -1,287 +1,289
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 filename = "tmp.txt"
148 self.__writeFile(filename,self.rx_buffer)
147 rx_buffer_lst = self.rx_buffer.split('\n',1)
148 filename = rx_buffer_lst[0]
149 tmp = rx_buffer_lst[1]
150 self.__writeFile(filename,tmp)
149 151 data = self.abs2ControlModuleFormatFile(filename)
150 152 #Needed for the loop
151 153 header, control_modules_lst = self.__All2Blocks(data)
152 154 correct = 0
153 155
154 156 for id in range(1,65):
155 157
156 158 if id not in self.enaModules:
157 159 continue
158 160
159 161 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
160 162 correct = correct + 1
161 163
162 164 if correct == len(self.enaModules):
163 165 rpta = "OK"
164 166 else:
165 167 rpta = "Failure"
166 168
167 169 return rpta
168 170
169 171 def __changeBeam(self, cmd):
170 172
171 173 correct = 0
172 174 # enaModules = self.checkAntenna()
173 175 # enaModules = [11,12,13,14]
174 176
175 177 for id in range(1,65):
176 178 if id not in self.enaModules:
177 179 continue
178 180
179 181 if self.__ConnectionWithControlModules(self.rx_buffer,cmd,id) == "OK":
180 182 correct = correct + 1
181 183
182 184 if correct == len(self.enaModules):
183 185 rpta = "OK"
184 186 else:
185 187 rpta = "Failure"
186 188
187 189 return rpta
188 190
189 191 def __getControlModuleStatus(self, cmd):
190 192
191 193 all_blocks = ""
192 194 # enaModules = self.checkAntenna()
193 195 # enaModules = [11,12,13,14]
194 196
195 197 for id in range(1,65):
196 198 if id not in self.enaModules:
197 199 continue
198 200
199 201 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
200 202
201 203 all_blocks = all_blocks + one_block
202 204 #Using tx buffer
203 205 print all_blocks
204 206 self.tx_buffer = all_blocks
205 207
206 208 def __getControlModulePhase(self, cmd):
207 209
208 210 all_blocks = ""
209 211 # enaModules = self.checkAntenna()
210 212 # enaModules = [11,12,13,14]
211 213
212 214 for id in range(1,65):
213 215 if id not in self.enaModules:
214 216 continue
215 217
216 218 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
217 219
218 220 all_blocks = all_blocks + one_block
219 221 #Using tx buffer
220 222 print all_blocks
221 223 self.tx_buffer = all_blocks
222 224
223 225 def __getConnectionStatus(self, cmd):
224 226
225 227 ena = self.checkAntenna()
226 228 print ena
227 229 self.enaModules = ena
228 230
229 231 blockLst = []
230 232
231 233 for id in range(1,65):
232 234 if id not in self.enaModules:
233 235 continue
234 236
235 237 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
236 238 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
237 239 #Using tx buffer
238 240 self.tx_buffer = "".join(blockLst)
239 241 print self.tx_buffer
240 242
241 243 return self.tx_buffer
242 244
243 245 def getConnectionStatus(self, cmd):
244 246
245 247 ena = self.checkAntenna()
246 248 self.enaModules = ena
247 249
248 250 blockLst = []
249 251
250 252 for id in range(1,65):
251 253 if id not in self.enaModules:
252 254 continue
253 255
254 256 blockStr = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
255 257 blockLst.append(blockStr + ", 192.168.1." + str(id) + "\n")
256 258 #Using tx buffer
257 259 self.tx_buffer = "".join(blockLst)
258 260 print self.tx_buffer
259 261
260 262 return self.tx_buffer
261 263
262 264 def getControlModuleStatus(self, cmd):
263 265
264 266 all_blocks = ""
265 267 # enaModules = self.checkAntenna()
266 268 # enaModules = [11,12,13,14]
267 269
268 270 for id in range(1,65):
269 271 if id not in self.enaModules:
270 272 continue
271 273
272 274 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
273 275
274 276 all_blocks = all_blocks + one_block
275 277 #Using tx buffer
276 278 print all_blocks
277 279 self.tx_buffer = all_blocks
278 280
279 281 return all_blocks
280 282
281 283
282 284 if __name__ == '__main__':
283 285
284 286 absObj = ABSServer()
285 287
286 288 while 1:
287 289 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now