##// END OF EJS Templates
Improve readability of the server3
imanay -
r86:87
parent child
Show More
@@ -1,194 +1,187
1 1 import os
2 2 import library3
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
7 # def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
8 def __init__(self,ipSource="localhost", ipDestino="100.100.12.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
8 9
9 10 self.ipSource = ipSource
10 11 self.ipDestino = ipDestino
11 12 self.portDestino = portDestino
12 13
13 14 self.ipDestino2 = ipDestino2
14 15 self.portDestino2 = portDestino2
15 16
16 17 self.tx_buffer = "default"
17 18 self.rx_buffer = "default"
18 19 self.enaModules = []
19 20
20 21 self.createObjects()
21 22
22 23 def createObjects(self):
23 24
24 25 asServer = True
25 26 self.commServerObj = library3.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer)
26 27 self.commClientObj = library3.TCPComm("Central_Control", self.ipDestino2, self.portDestino2)
27 28
28 29 def waitRequest(self):
29 30
30 31 #Using rx buffer
31 32 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitData()
32 33
33 34 if cmd == "SNDF":
34 35 datarpta = self.__sendFile2Modules(cmd = cmd)
35 36
36 37 if cmd == "CHGB":
37 38 datarpta = self.__changeBeam(cmd = cmd)
38 39
39 40 if cmd == "ANST":
40 41 self.__getControlModuleStatus(cmd = cmd)
41 42 #Using tx buffer
42 43 datarpta = self.tx_buffer
43 44
44 45 if cmd == "NTST":
45 46 self.enaModules = self.__getConnectionStatus()
46 47 #Using tx buffer
47 48 datarpta = self.enaModules
48 49
49 50 self.commServerObj.sendData(cmd=cmd, data=datarpta, ipDestino = ipSource)
50 51
51 52 def checkModule(self, address):
52 53
53 54 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
54 55 status = os.system(cmd)
55 56
56 57 if status == 256:
57 58 return False
58 59
59 60 return True
60 61
61 62 def __writeReport(self, enaModules):
62 63
63 64 status_array = ["Status of modules\n"]
64 65 status_array.append("----------------\n")
65 66
66 67 for address in range(1,65):
67 68 if address in enaModules:
68 69 status_array.append("192.168.1." + str(address) + " [1 1]\n")
69 70 else:
70 71 status_array.append("192.168.1." + str(address) + " [0 0]\n")
71 72
72 73 f = open("module_status.txt","w")
73 74 f.writelines(status_array)
74 75 f.close()
75 76
76 77 def checkAntenna(self):
77 78
78 79 """
79 80 Direccion de los modulos de las antenas:
80 81
81 82 Norte : 01-16
82 83 Este : 17-32
83 84 Oeste: : 33-48
84 85 Sur : 49-64
85 86
86 87 """
87 88
88 89 enaModules2 = []
89 90
90 91 for address in range(1,65):
91 92 if self.checkModule(address):
92 93 enaModules2.append(address)
93 94
94 95 self.__writeReport(enaModules2)
95 96 return enaModules2
97
98 def __ConnectionWithControlModules(self,data,cmd,id):
96 99
100 self.commClientObj.open_socket()
101 ip = "192.168.1." + str(id)
102 self.commClientObj.sendData(cmd, data, ip)
103 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
104 self.commClientObj.close_socket()
105
106 return tmp
107
108 def __All2Blocks(self,input):
109
110 rx_frame_lst = input.split('\n',2)
111
112 header = rx_frame_lst[0] + "\n"
113 control_modules_str = rx_frame_lst[2]
114 control_modules_lst = control_modules_str.split("------\n")
115
116 return header, control_modules_lst
117
118
97 119 def __sendFile2Modules(self,cmd):
98 120
99 121 #Needed for the loop
100 rx_frame_list = self.rx_buffer.split('\n',2)
122 header, control_modules_lst = self.__All2Blocks(self.rx_buffer)
101 123 correct = 0
102 failure = 0
103 header = rx_frame_list[0] + "\n"
104 str_control_modules = rx_frame_list[2]
105
106 lst_control_modules = str_control_modules.split("------\n")
107
108 # enaModules = self.checkAntenna()
109 # enaModules = [11,12,13,14]
110 124
111 125 for id in range(1,65):
112 126
113 127 if id not in self.enaModules:
114 128 continue
115 #tcp client needed
116 self.commClientObj.open_socket()
117 ip = "192.168.1." + str(id)
118 self.commClientObj.sendData(cmd, header + lst_control_modules[id-1], ip)
119 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
120 self.commClientObj.close_socket()
121 129
122 if tmp == "OK":
130 if self.__ConnectionWithControlModules(header + control_modules_lst[id-1], cmd, id) == "OK":
123 131 correct = correct + 1
124 else:
125 failure = failure + 1
132
133 if correct == len(self.enaModules):
134 rpta = "OK"
135 else:
136 rpta = "Failure"
137
138 return rpta
139
140 def __changeBeam(self, cmd):
141
142 correct = 0
143 # enaModules = self.checkAntenna()
144 # enaModules = [11,12,13,14]
145
146 for id in range(1,65):
147 if id not in self.enaModules:
148 continue
149
150 if self.__ConnectionWithControlModules(self.rx_buffer,cmd,id) == "OK":
151 correct = correct + 1
126 152
127 153 if correct == len(self.enaModules):
128 154 rpta = "OK"
129 155 else:
130 156 rpta = "Failure"
131 157
132 158 return rpta
133
134 def __changeBeam(self, cmd):
135
136 correct = 0
137 failure = 0
159
160 def __getControlModuleStatus(self, cmd):
161
162 all_blocks = ""
138 163 # enaModules = self.checkAntenna()
139 164 # enaModules = [11,12,13,14]
140 165
141 166 for id in range(1,65):
142 167 if id not in self.enaModules:
143 168 continue
144 169
145 self.commClientObj.open_socket()
146 ip = "192.168.1." + str(id)
147 self.commClientObj.sendData(cmd, self.rx_buffer, ip)
148 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
149 self.commClientObj.close_socket()
170 one_block = self.__ConnectionWithControlModules(self.rx_buffer,cmd,id)
150 171
151 if tmp == "OK":
152 correct = correct + 1
153 else:
154 failure = failure + 1
155
156 if correct == len(self.enaModules):
157 rpta = "OK"
158 else:
159 rpta = "Failure"
160
161 return rpta
162
163 def __getControlModuleStatus(self, cmd):
164
165 content_str = ""
166 # enaModules = self.checkAntenna()
167 # enaModules = [11,12,13,14]
168
169 for id in range(1,65):
170 if id not in self.enaModules:
171 continue
172
173 self.commClientObj.open_socket()
174 ip = "192.168.1." + str(id)
175 self.commClientObj.sendData(cmd, self.rx_buffer, ip)
176 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
177 self.commClientObj.close_socket()
178
179 content_str = content_str + tmp
172 all_blocks = all_blocks + one_block
180 173 #Using tx buffer
181 self.tx_buffer = content_str
174 self.tx_buffer = all_blocks
182 175
183 176 def __getConnectionStatus(self):
184 177
185 178 enaModules = self.checkAntenna()
186 179
187 180 return enaModules
188 181
189 182 if __name__ == '__main__':
190 183
191 184 absObj = ABSServer()
192 185
193 186 while 1:
194 187 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now