##// END OF EJS Templates
Estandarizar write function...
Fiorella Quino -
r191:db8a330d7538
parent child
Show More
@@ -1,1043 +1,1043
1 from django.db import models
1 from django.db import models
2 from apps.main.models import Configuration
2 from apps.main.models import Configuration
3 from django.core.urlresolvers import reverse
3 from django.core.urlresolvers import reverse
4 # Create your models here.
4 # Create your models here.
5
5
6 import ast
6 import ast
7 import socket
7 import socket
8 import json
8 import json
9 import requests
9 import requests
10 import struct
10 import struct
11 import sys, time
11 import sys, time
12
12
13 import multiprocessing
13 import multiprocessing
14
14
15
15
16 antenna_default = json.dumps({
16 antenna_default = json.dumps({
17 "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
17 "antenna_up": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
18 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
18 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
19 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
19 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
20 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
20 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
21 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
21 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
22 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
22 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
23 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
23 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0],
24 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0]
24 [0.5,0.5,0.5,0.5,1.0,1.0,1.0,1.0]
25 ]
25 ]
26 ,
26 ,
27 "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
27 "antenna_down": [[0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
28 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
28 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
29 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
29 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
30 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
30 [0.0,0.0,0.0,0.0,0.5,0.5,0.5,0.5],
31 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
31 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
32 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
32 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
33 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
33 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0],
34 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]],
34 [0.5,0.5,0.5,0.5,3.0,3.0,3.0,3.0]],
35 })
35 })
36
36
37
37
38 tx_default = json.dumps({
38 tx_default = json.dumps({
39 "up": [[1,1,1,1,0,0,0,0],
39 "up": [[1,1,1,1,0,0,0,0],
40 [1,1,1,1,0,0,0,0],
40 [1,1,1,1,0,0,0,0],
41 [1,1,1,1,0,0,0,0],
41 [1,1,1,1,0,0,0,0],
42 [1,1,1,1,0,0,0,0],
42 [1,1,1,1,0,0,0,0],
43 [0,0,0,0,1,1,1,1],
43 [0,0,0,0,1,1,1,1],
44 [0,0,0,0,1,1,1,1],
44 [0,0,0,0,1,1,1,1],
45 [0,0,0,0,1,1,1,1],
45 [0,0,0,0,1,1,1,1],
46 [0,0,0,0,1,1,1,1]],
46 [0,0,0,0,1,1,1,1]],
47
47
48 "down": [[1,1,1,1,0,0,0,0],
48 "down": [[1,1,1,1,0,0,0,0],
49 [1,1,1,1,0,0,0,0],
49 [1,1,1,1,0,0,0,0],
50 [1,1,1,1,0,0,0,0],
50 [1,1,1,1,0,0,0,0],
51 [1,1,1,1,0,0,0,0],
51 [1,1,1,1,0,0,0,0],
52 [0,0,0,0,1,1,1,1],
52 [0,0,0,0,1,1,1,1],
53 [0,0,0,0,1,1,1,1],
53 [0,0,0,0,1,1,1,1],
54 [0,0,0,0,1,1,1,1],
54 [0,0,0,0,1,1,1,1],
55 [0,0,0,0,1,1,1,1]],
55 [0,0,0,0,1,1,1,1]],
56 })
56 })
57
57
58 rx_default = json.dumps({
58 rx_default = json.dumps({
59 "up": [[1,1,1,1,0,0,0,0],
59 "up": [[1,1,1,1,0,0,0,0],
60 [1,1,1,1,0,0,0,0],
60 [1,1,1,1,0,0,0,0],
61 [1,1,1,1,0,0,0,0],
61 [1,1,1,1,0,0,0,0],
62 [1,1,1,1,0,0,0,0],
62 [1,1,1,1,0,0,0,0],
63 [0,0,0,0,1,1,1,1],
63 [0,0,0,0,1,1,1,1],
64 [0,0,0,0,1,1,1,1],
64 [0,0,0,0,1,1,1,1],
65 [0,0,0,0,1,1,1,1],
65 [0,0,0,0,1,1,1,1],
66 [0,0,0,0,1,1,1,1]],
66 [0,0,0,0,1,1,1,1]],
67
67
68 "down": [[1,1,1,1,0,0,0,0],
68 "down": [[1,1,1,1,0,0,0,0],
69 [1,1,1,1,0,0,0,0],
69 [1,1,1,1,0,0,0,0],
70 [1,1,1,1,0,0,0,0],
70 [1,1,1,1,0,0,0,0],
71 [1,1,1,1,0,0,0,0],
71 [1,1,1,1,0,0,0,0],
72 [0,0,0,0,1,1,1,1],
72 [0,0,0,0,1,1,1,1],
73 [0,0,0,0,1,1,1,1],
73 [0,0,0,0,1,1,1,1],
74 [0,0,0,0,1,1,1,1],
74 [0,0,0,0,1,1,1,1],
75 [0,0,0,0,1,1,1,1]],
75 [0,0,0,0,1,1,1,1]],
76 })
76 })
77
77
78 conf_default = {}
78 conf_default = {}
79 status_default = {}
79 status_default = {}
80 for i in range(1,65):
80 for i in range(1,65):
81 conf_default[str(i)] = ""
81 conf_default[str(i)] = ""
82 status_default[str(i)] = 0
82 status_default[str(i)] = 0
83
83
84 ues_default = json.dumps({
84 ues_default = json.dumps({
85 "up": [0.533333,0.00000,1.06667,0.00000],
85 "up": [0.533333,0.00000,1.06667,0.00000],
86 "down": [0.533333,0.00000,1.06667,0.00000]
86 "down": [0.533333,0.00000,1.06667,0.00000]
87 })
87 })
88
88
89 onlyrx_default = json.dumps({
89 onlyrx_default = json.dumps({
90 "up": False,
90 "up": False,
91 "down": False
91 "down": False
92 })
92 })
93
93
94 def up_convertion(cadena):
94 def up_convertion(cadena):
95 valores = []
95 valores = []
96 for c in cadena:
96 for c in cadena:
97 if c == 1.0: valores=valores+['000']
97 if c == 1.0: valores=valores+['000']
98 if c == 2.0: valores=valores+['001']
98 if c == 2.0: valores=valores+['001']
99 if c == 3.0: valores=valores+['010']
99 if c == 3.0: valores=valores+['010']
100 if c == 0.0: valores=valores+['011']
100 if c == 0.0: valores=valores+['011']
101 if c == 0.5: valores=valores+['100']
101 if c == 0.5: valores=valores+['100']
102 if c == 1.5: valores=valores+['101']
102 if c == 1.5: valores=valores+['101']
103 if c == 2.5: valores=valores+['110']
103 if c == 2.5: valores=valores+['110']
104 if c == 3.5: valores=valores+['111']
104 if c == 3.5: valores=valores+['111']
105
105
106 return valores
106 return valores
107
107
108 def up_conv_bits(value):
108 def up_conv_bits(value):
109
109
110 if value == 1.0: bits="000"
110 if value == 1.0: bits="000"
111 if value == 2.0: bits="001"
111 if value == 2.0: bits="001"
112 if value == 3.0: bits="010"
112 if value == 3.0: bits="010"
113 if value == 0.0: bits="011"
113 if value == 0.0: bits="011"
114 if value == 0.5: bits="100"
114 if value == 0.5: bits="100"
115 if value == 1.5: bits="101"
115 if value == 1.5: bits="101"
116 if value == 2.5: bits="110"
116 if value == 2.5: bits="110"
117 if value == 3.5: bits="111"
117 if value == 3.5: bits="111"
118
118
119 return bits
119 return bits
120
120
121 def down_convertion(cadena):
121 def down_convertion(cadena):
122 valores = []
122 valores = []
123 for c in cadena:
123 for c in cadena:
124 if c == 1.0: valores=valores+['000']
124 if c == 1.0: valores=valores+['000']
125 if c == 2.0: valores=valores+['001']
125 if c == 2.0: valores=valores+['001']
126 if c == 3.0: valores=valores+['010']
126 if c == 3.0: valores=valores+['010']
127 if c == 0.0: valores=valores+['011']
127 if c == 0.0: valores=valores+['011']
128 if c == 0.5: valores=valores+['100']
128 if c == 0.5: valores=valores+['100']
129 if c == 1.5: valores=valores+['101']
129 if c == 1.5: valores=valores+['101']
130 if c == 2.5: valores=valores+['110']
130 if c == 2.5: valores=valores+['110']
131 if c == 3.5: valores=valores+['111']
131 if c == 3.5: valores=valores+['111']
132
132
133 return valores
133 return valores
134
134
135 def down_conv_bits(value):
135 def down_conv_bits(value):
136
136
137 if value == 1.0: bits="000"
137 if value == 1.0: bits="000"
138 if value == 2.0: bits="001"
138 if value == 2.0: bits="001"
139 if value == 3.0: bits="010"
139 if value == 3.0: bits="010"
140 if value == 0.0: bits="011"
140 if value == 0.0: bits="011"
141 if value == 0.5: bits="100"
141 if value == 0.5: bits="100"
142 if value == 1.5: bits="101"
142 if value == 1.5: bits="101"
143 if value == 2.5: bits="110"
143 if value == 2.5: bits="110"
144 if value == 3.5: bits="111"
144 if value == 3.5: bits="111"
145
145
146 return bits
146 return bits
147
147
148 def ip2position(module_number):
148 def ip2position(module_number):
149 j=0
149 j=0
150 i=0
150 i=0
151 for x in range(0,module_number-1):
151 for x in range(0,module_number-1):
152 j=j+1
152 j=j+1
153 if j==8:
153 if j==8:
154 i=i+1
154 i=i+1
155 j=0
155 j=0
156
156
157 pos = [i,j]
157 pos = [i,j]
158 return pos
158 return pos
159
159
160
160
161 def fromBinary2Char(binary_string):
161 def fromBinary2Char(binary_string):
162 number = int(binary_string, 2)
162 number = int(binary_string, 2)
163 #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f'
163 #Plus 33 to avoid more than 1 characters values such as: '\x01'-'\x1f'
164 number = number + 33
164 number = number + 33
165 char = chr(number)
165 char = chr(number)
166 return char
166 return char
167
167
168 def fromChar2Binary(char):
168 def fromChar2Binary(char):
169 number = ord(char) - 33
169 number = ord(char) - 33
170 #Minus 33 to get the real value
170 #Minus 33 to get the real value
171 bits = bin(number)[2:]
171 bits = bin(number)[2:]
172 #To ensure we have a string with 6bits
172 #To ensure we have a string with 6bits
173 if len(bits) < 6:
173 if len(bits) < 6:
174 bits = bits.zfill(6)
174 bits = bits.zfill(6)
175 return bits
175 return bits
176
176
177
177
178 class ABSConfiguration(Configuration):
178 class ABSConfiguration(Configuration):
179 active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}")
179 active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}")
180 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default))
180 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default))
181
181
182 class Meta:
182 class Meta:
183 db_table = 'abs_configurations'
183 db_table = 'abs_configurations'
184
184
185 def get_absolute_url_plot(self):
185 def get_absolute_url_plot(self):
186 return reverse('url_plot_abs_patterns', args=[str(self.id)])
186 return reverse('url_plot_abs_patterns', args=[str(self.id)])
187
187
188
188
189 def parms_to_dict(self):
189 def parms_to_dict(self):
190
190
191 parameters = {}
191 parameters = {}
192
192
193 parameters['device_id'] = self.device.id
193 parameters['device_id'] = self.device.id
194 parameters['name'] = self.name
194 parameters['name'] = self.name
195 parameters['beams'] = {}
195 parameters['beams'] = {}
196
196
197 beams = ABSBeam.objects.filter(abs_conf=self)
197 beams = ABSBeam.objects.filter(abs_conf=self)
198 b=1
198 b=1
199 for beam in beams:
199 for beam in beams:
200 #absbeam = ABSBeam.objects.get(pk=beams[beam])
200 #absbeam = ABSBeam.objects.get(pk=beams[beam])
201 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
201 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
202 b+=1
202 b+=1
203
203
204 return parameters
204 return parameters
205
205
206 def dict_to_parms(self, parameters):
206 def dict_to_parms(self, parameters):
207
207
208 self.name = parameters['name']
208 self.name = parameters['name']
209
209
210 absbeams = ABSBeam.objects.filter(abs_conf=self)
210 absbeams = ABSBeam.objects.filter(abs_conf=self)
211 beams = parameters['beams']
211 beams = parameters['beams']
212
212
213 if absbeams:
213 if absbeams:
214 beams_number = len(beams)
214 beams_number = len(beams)
215 absbeams_number = len(absbeams)
215 absbeams_number = len(absbeams)
216 if beams_number==absbeams_number:
216 if beams_number==absbeams_number:
217 i = 1
217 i = 1
218 for absbeam in absbeams:
218 for absbeam in absbeams:
219 absbeam.dict_to_parms(beams['beam'+str(i)])
219 absbeam.dict_to_parms(beams['beam'+str(i)])
220 i = i+1
220 i = i+1
221 elif beams_number > absbeams_number:
221 elif beams_number > absbeams_number:
222 i = 1
222 i = 1
223 for absbeam in absbeams:
223 for absbeam in absbeams:
224 absbeam.dict_to_parms(beams['beam'+str(i)])
224 absbeam.dict_to_parms(beams['beam'+str(i)])
225 i=i+1
225 i=i+1
226 for x in range(i,beams_number+1):
226 for x in range(i,beams_number+1):
227 new_beam = ABSBeam(
227 new_beam = ABSBeam(
228 name =beams['beam'+str(i)]['name'],
228 name =beams['beam'+str(i)]['name'],
229 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
229 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
230 abs_conf = self,
230 abs_conf = self,
231 tx =json.dumps(beams['beam'+str(i)]['tx']),
231 tx =json.dumps(beams['beam'+str(i)]['tx']),
232 rx =json.dumps(beams['beam'+str(i)]['rx']),
232 rx =json.dumps(beams['beam'+str(i)]['rx']),
233 ues =json.dumps(beams['beam'+str(i)]['ues']),
233 ues =json.dumps(beams['beam'+str(i)]['ues']),
234 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
234 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
235 )
235 )
236 new_beam.save()
236 new_beam.save()
237 i=i+1
237 i=i+1
238 else: #beams_number < absbeams_number:
238 else: #beams_number < absbeams_number:
239 i = 1
239 i = 1
240 for absbeam in absbeams:
240 for absbeam in absbeams:
241 if i <= beams_number:
241 if i <= beams_number:
242 absbeam.dict_to_parms(beams['beam'+str(i)])
242 absbeam.dict_to_parms(beams['beam'+str(i)])
243 i=i+1
243 i=i+1
244 else:
244 else:
245 absbeam.delete()
245 absbeam.delete()
246 else:
246 else:
247 for beam in beams:
247 for beam in beams:
248 new_beam = ABSBeam(
248 new_beam = ABSBeam(
249 name =beams[beam]['name'],
249 name =beams[beam]['name'],
250 antenna =json.dumps(beams[beam]['antenna']),
250 antenna =json.dumps(beams[beam]['antenna']),
251 abs_conf = self,
251 abs_conf = self,
252 tx =json.dumps(beams[beam]['tx']),
252 tx =json.dumps(beams[beam]['tx']),
253 rx =json.dumps(beams[beam]['rx']),
253 rx =json.dumps(beams[beam]['rx']),
254 ues =json.dumps(beams[beam]['ues']),
254 ues =json.dumps(beams[beam]['ues']),
255 only_rx =json.dumps(beams[beam]['only_rx'])
255 only_rx =json.dumps(beams[beam]['only_rx'])
256 )
256 )
257 new_beam.save()
257 new_beam.save()
258
258
259
259
260
260
261 def update_from_file(self, parameters):
261 def update_from_file(self, parameters):
262
262
263 self.dict_to_parms(parameters)
263 self.dict_to_parms(parameters)
264 self.save()
264 self.save()
265
265
266
266
267 def get_beams(self, **kwargs):
267 def get_beams(self, **kwargs):
268 '''
268 '''
269 This function returns ABS Configuration beams
269 This function returns ABS Configuration beams
270 '''
270 '''
271 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
271 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
272
272
273 def clone(self, **kwargs):
273 def clone(self, **kwargs):
274
274
275 beams = self.get_beams()
275 beams = self.get_beams()
276 self.pk = None
276 self.pk = None
277 self.id = None
277 self.id = None
278 for attr, value in kwargs.items():
278 for attr, value in kwargs.items():
279 setattr(self, attr, value)
279 setattr(self, attr, value)
280 self.save()
280 self.save()
281
281
282 for beam in beams:
282 for beam in beams:
283 beam.clone(abs_conf=self)
283 beam.clone(abs_conf=self)
284
284
285 return self
285 return self
286
286
287
287
288 def module_conf(self, module_num, beams):
288 def module_conf(self, module_num, beams):
289 """
289 """
290 This function creates beam configurations for one abs module.
290 This function creates beam configurations for one abs module.
291 """
291 """
292 ip_address = self.device.ip_address
292 ip_address = self.device.ip_address
293 ip_address = ip_address.split('.')
293 ip_address = ip_address.split('.')
294 module_seq = (ip_address[0],ip_address[1],ip_address[2])
294 module_seq = (ip_address[0],ip_address[1],ip_address[2])
295 dot = '.'
295 dot = '.'
296 module_ip = dot.join(module_seq)+'.'+str(module_num)
296 module_ip = dot.join(module_seq)+'.'+str(module_num)
297 module_port = self.device.port_address
297 module_port = self.device.port_address
298 write_route = 'http://'+module_ip+':'+str(module_port)+'/configure'
298 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
299
299
300 header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
300 header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
301 module = 'ABS_'+str(module_num)
301 module = 'ABS_'+str(module_num)
302 bs = '' #{}
302 bs = '' #{}
303 i=1
303 i=1
304 #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
304 #beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
305 # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
305 # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
306 for beam in beams:
306 for beam in beams:
307 #bs[i] = fromBinary2Char(beam.module_6bits(module_num))
307 #bs[i] = fromBinary2Char(beam.module_6bits(module_num))
308 bs = bs + fromBinary2Char(beam.module_6bits(module_num))
308 bs = bs + fromBinary2Char(beam.module_6bits(module_num))
309 i=i+1
309 i=i+1
310
310
311 beams = bs
311 beams = bs
312
312
313 parameters = {}
313 parameters = {}
314 parameters['header'] = header
314 parameters['header'] = header
315 parameters['module'] = module
315 parameters['module'] = module
316 parameters['beams'] = beams #json.dumps(beams)
316 parameters['beams'] = beams #json.dumps(beams)
317 print parameters['beams']
317 print parameters['beams']
318 answer = ''
318 answer = ''
319
319
320 try:
320 try:
321 r_write = requests.post(write_route, parameters, timeout=0.5)
321 r_write = requests.post(write_route, parameters, timeout=0.5)
322 answer = r_write.json()
322 answer = r_write.json()
323 self.message = answer['message']
323 self.message = answer['message']
324 except:
324 except:
325 self.message = "Could not write ABS parameters"
325 self.message = "Could not write ABS parameters"
326 return 0
326 return 0
327 return 1
327 return 1
328
328
329 def read_module(self, module):
329 def read_module(self, module):
330
330
331 """
331 """
332 Read out-bits (up-down) of 1 abs module NOT for Configuration
332 Read out-bits (up-down) of 1 abs module NOT for Configuration
333 """
333 """
334
334
335 parameters = {}
335 parameters = {}
336 ip_address = self.device.ip_address
336 ip_address = self.device.ip_address
337 ip_address = ip_address.split('.')
337 ip_address = ip_address.split('.')
338 module_seq = (ip_address[0],ip_address[1],ip_address[2])
338 module_seq = (ip_address[0],ip_address[1],ip_address[2])
339 dot = '.'
339 dot = '.'
340 module_ip = dot.join(module_seq)+'.'+str(module)
340 module_ip = dot.join(module_seq)+'.'+str(module)
341 module_port = self.device.port_address
341 module_port = self.device.port_address
342 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
342 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
343
343
344 print(read_route)
344 print(read_route)
345
345
346 answer = ''
346 answer = ''
347 module_bits = ''
347 module_bits = ''
348
348
349 try:
349 try:
350 r_write = requests.get(read_route, timeout=0.7)
350 r_write = requests.get(read_route, timeout=0.7)
351 answer = r_write.json()
351 answer = r_write.json()
352 self.message = answer['message']
352 self.message = answer['message']
353 module_bits = answer['allbits']
353 module_bits = answer['allbits']
354 except:
354 except:
355 #message = "Could not read ABS parameters"
355 #message = "Could not read ABS parameters"
356 return 0
356 return 0
357
357
358 return module_bits
358 return module_bits
359
359
360 def status_device(self):
360 def status_device(self):
361 """
361 """
362 This function gets the status of each abs module. It sends GET method to Web Application
362 This function gets the status of each abs module. It sends GET method to Web Application
363 in Python Bottle.
363 in Python Bottle.
364 """
364 """
365 ip_address = self.device.ip_address
365 ip_address = self.device.ip_address
366 ip_address = ip_address.split('.')
366 ip_address = ip_address.split('.')
367 module_seq = (ip_address[0],ip_address[1],ip_address[2])
367 module_seq = (ip_address[0],ip_address[1],ip_address[2])
368 dot = '.'
368 dot = '.'
369 module_port = self.device.port_address
369 module_port = self.device.port_address
370
370
371 modules_status = json.loads(self.module_status)
371 modules_status = json.loads(self.module_status)
372
372
373 for i in range(1,65):
373 for i in range(1,65):
374 module_ip = dot.join(module_seq)+'.'+str(i)
374 module_ip = dot.join(module_seq)+'.'+str(i)
375 print module_ip
375 print module_ip
376
376
377 route = 'http://'+module_ip+':'+str(module_port)+'/hello'
377 route = 'http://'+module_ip+':'+str(module_port)+'/hello'
378
378
379 try:
379 try:
380 r = requests.get(route, timeout=0.7)
380 r = requests.get(route, timeout=0.7)
381 modules_status[str(i)] = 1
381 modules_status[str(i)] = 1
382 except:
382 except:
383 modules_status[str(i)] = 0
383 modules_status[str(i)] = 0
384 pass
384 pass
385
385
386 self.message = 'ABS modules Status have been updated.'
386 self.message = 'ABS modules Status have been updated.'
387 self.module_status=json.dumps(modules_status)
387 self.module_status=json.dumps(modules_status)
388 self.save()
388 self.save()
389
389
390 return
390 return
391
391
392
392
393 def write_device(self):
393 def write_device(self):
394 """
394 """
395 This function sends the beams list to every abs module.
395 This function sends the beams list to every abs module.
396 It needs 'module_conf' function
396 It needs 'module_conf' function
397 """
397 """
398
398
399 ###beams_list = ast.literal_eval(self.beams)
399 ###beams_list = ast.literal_eval(self.beams)
400 ###beams = []
400 ###beams = []
401 beams = ABSBeam.objects.filter(abs_conf=self)
401 beams = ABSBeam.objects.filter(abs_conf=self)
402 ###for bl in range(1,len(beams_list)+1):
402 ###for bl in range(1,len(beams_list)+1):
403 ### b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)])
403 ### b = ABSBeam.objects.get(pk=beams_list['beam'+str(bl)])
404 ### beams.append(b)
404 ### beams.append(b)
405
405
406 #---Write each abs module---
406 #---Write each abs module---
407 if beams:
407 if beams:
408 beams_status = ast.literal_eval(self.module_status)
408 beams_status = ast.literal_eval(self.module_status)
409 for i in range(62,65): #(62,65)
409 for i in range(62,65): #(62,65)
410 try:
410 try:
411 answer = self.module_conf(i, beams)
411 answer = self.module_conf(i, beams)
412 beams_status[str(i)] = 1
412 beams_status[str(i)] = 1
413 self.module_status = json.dumps(beams_status)
413 self.module_status = json.dumps(beams_status)
414 self.save()
414 self.save()
415 #self.module_conf(63,beams)
415 #self.module_conf(63,beams)
416 #beams_status[str(63)] = 1
416 #beams_status[str(63)] = 1
417 #self.module_status = json.dumps(beams_status)
417 #self.module_status = json.dumps(beams_status)
418 except:
418 except:
419 beams_status[str(i)] = 0
419 beams_status[str(i)] = 0
420 self.module_status = json.dumps(beams_status)
420 self.module_status = json.dumps(beams_status)
421 self.save()
421 self.save()
422 answer = 0
422 answer = 0
423 return 0
423 return 0
424 else:
424 else:
425 self.message = "ABS Configuration does not have beams"
425 self.message = "ABS Configuration does not have beams"
426 return 0
426 return 0
427
427
428 #self.device.status = 1
428 #self.device.status = 1
429 ##
429 ##
430 if answer==1:
430 if answer==1:
431 self.message = "ABS Beams List have been sent to ABS Modules"
431 self.message = "ABS Beams List have been sent to ABS Modules"
432 else:
432 else:
433 self.message = "Could not read ABS parameters"
433 self.message = "Could not read ABS parameters"
434
434
435 ##
435 ##
436 self.save()
436 self.save()
437 return 1
437 return 1
438
438
439
439
440 def write_module(self, module):
440 def write_module(self, module):
441
441
442 """
442 """
443 Send configuration to one abs module
443 Send configuration to one abs module
444 """
444 """
445
445
446 parameters = {}
446 parameters = {}
447 ip_address = self.abs_conf.device.ip_address
447 ip_address = self.abs_conf.device.ip_address
448 ip_address = ip_address.split('.')
448 ip_address = ip_address.split('.')
449 module_seq = (ip_address[0],ip_address[1],ip_address[2])
449 module_seq = (ip_address[0],ip_address[1],ip_address[2])
450 dot = '.'
450 dot = '.'
451 module_ip = dot.join(module_seq)+'.'+str(module)
451 module_ip = dot.join(module_seq)+'.'+str(module)
452 module_port = self.abs_conf.device.port_address
452 module_port = self.abs_conf.device.port_address
453 write_route = 'http://'+module_ip+':'+str(module_port)+'/configure'
453 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
454
454
455 #print write_route
455 #print write_route
456
456
457 header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
457 header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
458 module = 'ABS_'+str(module)
458 module = 'ABS_'+str(module)
459 beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
459 beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
460 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
460 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
461
461
462 parameters['header'] = header
462 parameters['header'] = header
463 parameters['module'] = module
463 parameters['module'] = module
464 parameters['beams'] = json.dumps(beams)
464 parameters['beams'] = json.dumps(beams)
465
465
466 answer = ''
466 answer = ''
467
467
468 try:
468 try:
469 r_write = requests.post(write_route, parameters, timeout=0.5)
469 r_write = requests.post(write_route, parameters, timeout=0.5)
470 answer = r_write.json()
470 answer = r_write.json()
471 self.message = answer['message']
471 self.message = answer['message']
472 except:
472 except:
473 self.message = "Could not write ABS parameters"
473 self.message = "Could not write ABS parameters"
474 return 0
474 return 0
475
475
476
476
477 #self.device.status = int(answer['status'])
477 #self.device.status = int(answer['status'])
478
478
479 return 1
479 return 1
480
480
481
481
482 def beam_selector(self, module, beam_pos):
482 def beam_selector(self, module, beam_pos):
483 """
483 """
484 This function selects the beam number for one absmodule.
484 This function selects the beam number for one absmodule.
485 """
485 """
486
486
487 if beam_pos > 0:
487 if beam_pos > 0:
488 beam_pos = beam_pos - 1
488 beam_pos = beam_pos - 1
489 else:
489 else:
490 beam_pos = 0
490 beam_pos = 0
491
491
492 #El indice del apunte debe ser menor que el numero total de apuntes
492 #El indice del apunte debe ser menor que el numero total de apuntes
493 #El servidor tcp en el embebido comienza a contar desde 0
493 #El servidor tcp en el embebido comienza a contar desde 0
494 beams_list = ABSBeam.objects.filter(abs_conf=self)
494 beams_list = ABSBeam.objects.filter(abs_conf=self)
495 if len(beams_list) < beam_pos:
495 if len(beams_list) < beam_pos:
496 return 0
496 return 0
497
497
498 flag = 1
498 flag = 1
499 if beam_pos>9:
499 if beam_pos>9:
500 flag = 2
500 flag = 2
501
501
502 module_address = ('192.168.1.'+str(module), 5500)
502 module_address = ('192.168.1.'+str(module), 5500)
503 header = 'JROABSCeCnModCnMod0100000'
503 header = 'JROABSCeCnModCnMod0100000'
504 numbers = len(str(beam_pos))
504 numbers = len(str(beam_pos))
505 function = 'CHGB'
505 function = 'CHGB'
506
506
507 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
507 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
508
508
509 # Create the datagram socket
509 # Create the datagram socket
510 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
510 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
511 #sock.connect(module_address)
511 #sock.connect(module_address)
512 try:
512 try:
513 sock.connect(module_address)
513 sock.connect(module_address)
514 sock.send(message_tx)
514 sock.send(message_tx)
515 sock.close()
515 sock.close()
516 print("Writing abs module:"+module_address[0]+"...")
516 print("Writing abs module:"+module_address[0]+"...")
517 except:
517 except:
518 sock = None
518 sock = None
519 print("Problem writing abs module:"+module_address[0])
519 print("Problem writing abs module:"+module_address[0])
520 return 0
520 return 0
521
521
522 return 1
522 return 1
523
523
524
524
525 def change_beam(self, beam_pos):
525 def change_beam(self, beam_pos):
526 """
526 """
527 This function selects the beam number for all absmodules.
527 This function selects the beam number for all absmodules.
528 """
528 """
529 for i in range(1,65):
529 for i in range(1,65):
530 try:
530 try:
531 self.beam_selector(i,beam_pos)
531 self.beam_selector(i,beam_pos)
532 except:
532 except:
533 print("Problem with module: 192.168.1."+str(i))
533 print("Problem with module: 192.168.1."+str(i))
534 self.message = "Problem with module: 192.168.1."+str(i)
534 self.message = "Problem with module: 192.168.1."+str(i)
535 #return 0
535 #return 0
536 return 1
536 return 1
537
537
538
538
539 def send_beam_num(self, beam_pos):
539 def send_beam_num(self, beam_pos):
540 """
540 """
541 This function connects to a multicast group and sends the beam number
541 This function connects to a multicast group and sends the beam number
542 to all abs modules.
542 to all abs modules.
543 """
543 """
544
544
545 if beam_pos > 0:
545 if beam_pos > 0:
546 beam_pos = beam_pos - 1
546 beam_pos = beam_pos - 1
547 else:
547 else:
548 beam_pos = 0
548 beam_pos = 0
549
549
550 #El indice del apunte debe ser menor que el numero total de apuntes
550 #El indice del apunte debe ser menor que el numero total de apuntes
551 #El servidor tcp en el embebido comienza a contar desde 0
551 #El servidor tcp en el embebido comienza a contar desde 0
552 beams_list = ABSBeam.objects.filter(abs_conf=self)
552 beams_list = ABSBeam.objects.filter(abs_conf=self)
553 if len(beams_list) < beam_pos:
553 if len(beams_list) < beam_pos:
554 return 0
554 return 0
555
555
556 flag = 1
556 flag = 1
557 if beam_pos>9:
557 if beam_pos>9:
558 flag = 2
558 flag = 2
559
559
560 header = 'JROABSCeCnModCnMod0100000'
560 header = 'JROABSCeCnModCnMod0100000'
561 flag = str(flag)
561 flag = str(flag)
562 function = 'CHGB'
562 function = 'CHGB'
563 message_tx = header+flag+function+str(beam_pos)+'0'
563 message_tx = header+flag+function+str(beam_pos)+'0'
564
564
565 multicast_group = '224.3.29.71'
565 multicast_group = '224.3.29.71'
566 server_address = ('',10000)
566 server_address = ('',10000)
567
567
568 # Create the socket
568 # Create the socket
569 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
569 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
570 # Bind to the server address
570 # Bind to the server address
571 sock.bind(server_address)
571 sock.bind(server_address)
572 # Telling the OS add the socket to the multicast on all interfaces
572 # Telling the OS add the socket to the multicast on all interfaces
573 group = socket.inet_aton(multicast_group)
573 group = socket.inet_aton(multicast_group)
574 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
574 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
575 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
575 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
576
576
577 #print 'sending acknowledgement to all: \n' + message_tx
577 #print 'sending acknowledgement to all: \n' + message_tx
578 sock.sendto(message_tx, (multicast_group, 10000))
578 sock.sendto(message_tx, (multicast_group, 10000))
579 sock.close()
579 sock.close()
580 sock = None
580 sock = None
581
581
582 return 1
582 return 1
583
583
584 def test1(self):
584 def test1(self):
585 t1 = time.time()
585 t1 = time.time()
586 t2 = 0
586 t2 = 0
587 while (t2-t1)<100:#300
587 while (t2-t1)<100:#300
588 t2 = time.time()
588 t2 = time.time()
589 self.send_beam_num(2)
589 self.send_beam_num(2)
590 time.sleep(0.04)
590 time.sleep(0.04)
591 self.send_beam_num(1)
591 self.send_beam_num(1)
592 time.sleep(0.04)
592 time.sleep(0.04)
593 return
593 return
594
594
595 def change_procs_test1(self, module):
595 def change_procs_test1(self, module):
596
596
597 for i in range (1,300):#300
597 for i in range (1,300):#300
598 beam_pos = 1
598 beam_pos = 1
599 module_address = ('192.168.1.'+str(module), 5500)
599 module_address = ('192.168.1.'+str(module), 5500)
600 header = 'JROABSCeCnModCnMod0100000'
600 header = 'JROABSCeCnModCnMod0100000'
601 numbers = len(str(beam_pos))
601 numbers = len(str(beam_pos))
602 function = 'CHGB'
602 function = 'CHGB'
603
603
604 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
604 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
605
605
606 # Create the datagram socket
606 # Create the datagram socket
607 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
607 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
608 sock.connect(module_address)
608 sock.connect(module_address)
609
609
610 sock.send(message_tx)
610 sock.send(message_tx)
611 #t = sock.recv(1024)
611 #t = sock.recv(1024)
612 sock.close()
612 sock.close()
613 sock = None
613 sock = None
614
614
615
615
616 time.sleep(0.04)
616 time.sleep(0.04)
617
617
618
618
619 beam_pos = 0
619 beam_pos = 0
620 numbers = len(str(beam_pos))
620 numbers = len(str(beam_pos))
621
621
622 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
622 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
623
623
624 # Create the datagram socket
624 # Create the datagram socket
625 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
625 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
626 sock.connect(module_address)
626 sock.connect(module_address)
627 sock.send(message_tx)
627 sock.send(message_tx)
628 sock.close()
628 sock.close()
629 sock = None
629 sock = None
630
630
631 time.sleep(0.04)
631 time.sleep(0.04)
632
632
633
633
634 def multi_procs_test1(self):
634 def multi_procs_test1(self):
635
635
636 """
636 """
637 This function sends the beam number to all abs modules using multiprocessing.
637 This function sends the beam number to all abs modules using multiprocessing.
638 """
638 """
639
639
640 #if __name__ == "__main__":
640 #if __name__ == "__main__":
641 size = 10000000 # Number of random numbers to add
641 size = 10000000 # Number of random numbers to add
642 procs = 65 # (Number-1) of processes to create (absmodule)
642 procs = 65 # (Number-1) of processes to create (absmodule)
643
643
644 # Create a list of jobs and then iterate through
644 # Create a list of jobs and then iterate through
645 # the number of processes appending each process to
645 # the number of processes appending each process to
646 # the job list
646 # the job list
647 jobs = []
647 jobs = []
648 for i in range(1, procs):
648 for i in range(1, procs):
649
649
650 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
650 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
651 jobs.append(process)
651 jobs.append(process)
652 #print jobs
652 #print jobs
653
653
654 # Start the processes (i.e. calculate the random number lists)
654 # Start the processes (i.e. calculate the random number lists)
655 for j in jobs:
655 for j in jobs:
656 #time.sleep(0.4)
656 #time.sleep(0.4)
657 #print j
657 #print j
658 j.start()
658 j.start()
659
659
660 # Ensure all of the processes have finished
660 # Ensure all of the processes have finished
661 for j in jobs:
661 for j in jobs:
662 j.join()
662 j.join()
663
663
664 print("List processing complete.")
664 print("List processing complete.")
665 return 1
665 return 1
666
666
667
667
668
668
669 def multi_procs_test2(self):
669 def multi_procs_test2(self):
670 """
670 """
671 This function use multiprocessing python library. Importing Pool we can select
671 This function use multiprocessing python library. Importing Pool we can select
672 the number of cores we want to use
672 the number of cores we want to use
673 After 'nproc' linux command, we know how many cores computer has.
673 After 'nproc' linux command, we know how many cores computer has.
674 NOT WORKING
674 NOT WORKING
675 """
675 """
676 import multiprocessing
676 import multiprocessing
677 pool = multiprocessing.Pool(3) # cores
677 pool = multiprocessing.Pool(3) # cores
678
678
679 tasks = []
679 tasks = []
680 procs = 65
680 procs = 65
681 for i in range(62, procs):
681 for i in range(62, procs):
682 tasks.append( (i,))
682 tasks.append( (i,))
683 #print tasks
683 #print tasks
684 #pool.apply_async(self.change_procs_test1, 62)
684 #pool.apply_async(self.change_procs_test1, 62)
685 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
685 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
686 #for result in results:
686 #for result in results:
687 #result.get()
687 #result.get()
688 #(plotNum, plotFilename) = result.get()
688 #(plotNum, plotFilename) = result.get()
689 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
689 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
690
690
691 return 1
691 return 1
692
692
693
693
694 def multi_procs_test3(self):
694 def multi_procs_test3(self):
695 """
695 """
696 This function use multiprocessing python library. Importing Pool we can select
696 This function use multiprocessing python library. Importing Pool we can select
697 the number of cores we want to use
697 the number of cores we want to use
698 After 'nproc' linux command, we know how many cores computer has.
698 After 'nproc' linux command, we know how many cores computer has.
699 """
699 """
700 from multiprocessing import Pool
700 from multiprocessing import Pool
701 import time
701 import time
702
702
703 def f(x):
703 def f(x):
704 return x*x
704 return x*x
705
705
706 tasks = []
706 tasks = []
707 procs = 65
707 procs = 65
708 pool = Pool(processes=4)
708 pool = Pool(processes=4)
709
709
710 #for i in range(62, procs):
710 #for i in range(62, procs):
711 #result = pool.map(f, range(62,65))
711 #result = pool.map(f, range(62,65))
712 it = pool.imap(self.change_procs_test1, range(62,65))
712 it = pool.imap(self.change_procs_test1, range(62,65))
713 #result.get(timeout=5)
713 #result.get(timeout=5)
714
714
715 return 1
715 return 1
716
716
717
717
718 def f(x):
718 def f(x):
719 print x
719 print x
720 return
720 return
721
721
722 def multi_procs_test4(self):
722 def multi_procs_test4(self):
723 import multiprocessing as mp
723 import multiprocessing as mp
724
724
725
725
726 num_workers = mp.cpu_count()
726 num_workers = mp.cpu_count()
727
727
728 pool = mp.Pool(num_workers)
728 pool = mp.Pool(num_workers)
729 procs = 65
729 procs = 65
730 for i in range(62, procs):
730 for i in range(62, procs):
731 #for task in tasks:
731 #for task in tasks:
732 pool.apply_async(self.f, args = (i,))
732 pool.apply_async(self.f, args = (i,))
733
733
734 pool.close()
734 pool.close()
735 pool.join()
735 pool.join()
736
736
737 return 1
737 return 1
738
738
739
739
740 def get_absolute_url_import(self):
740 def get_absolute_url_import(self):
741 return reverse('url_import_abs_conf', args=[str(self.id)])
741 return reverse('url_import_abs_conf', args=[str(self.id)])
742
742
743
743
744
744
745
745
746 class ABSBeam(models.Model):
746 class ABSBeam(models.Model):
747
747
748 name = models.CharField(max_length=60, default='Beam')
748 name = models.CharField(max_length=60, default='Beam')
749 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
749 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
750 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
750 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
751 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
751 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
752 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
752 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
753 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
753 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
754 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
754 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
755 modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default))
755 modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default))
756 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
756 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
757 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
757 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
758
758
759 class Meta:
759 class Meta:
760 db_table = 'abs_beams'
760 db_table = 'abs_beams'
761
761
762 def __unicode__(self):
762 def __unicode__(self):
763 return u'%s' % (self.name)
763 return u'%s' % (self.name)
764
764
765 def parms_to_dict(self):
765 def parms_to_dict(self):
766
766
767 #Update data
767 #Update data
768 self.modules_6bits()
768 self.modules_6bits()
769
769
770 parameters = {}
770 parameters = {}
771
771
772 parameters['name'] = self.name
772 parameters['name'] = self.name
773 parameters['antenna'] = ast.literal_eval(self.antenna)
773 parameters['antenna'] = ast.literal_eval(self.antenna)
774 parameters['abs_conf'] = self.abs_conf.name
774 parameters['abs_conf'] = self.abs_conf.name
775 parameters['tx'] = ast.literal_eval(self.tx)
775 parameters['tx'] = ast.literal_eval(self.tx)
776 parameters['rx'] = ast.literal_eval(self.rx)
776 parameters['rx'] = ast.literal_eval(self.rx)
777 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
777 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
778 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
778 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
779 parameters['configuration'] = ast.literal_eval(self.modules_conf)
779 parameters['configuration'] = ast.literal_eval(self.modules_conf)
780 parameters['ues'] = ast.literal_eval(self.ues)
780 parameters['ues'] = ast.literal_eval(self.ues)
781 parameters['only_rx'] = json.loads(self.only_rx)
781 parameters['only_rx'] = json.loads(self.only_rx)
782
782
783 return parameters
783 return parameters
784
784
785 def dict_to_parms(self, parameters):
785 def dict_to_parms(self, parameters):
786
786
787 self.name = parameters['name']
787 self.name = parameters['name']
788 self.antenna = json.dumps(parameters['antenna'])
788 self.antenna = json.dumps(parameters['antenna'])
789 #self.abs_conf = parameters['abs_conf']
789 #self.abs_conf = parameters['abs_conf']
790 self.tx = json.dumps(parameters['tx'])
790 self.tx = json.dumps(parameters['tx'])
791 self.rx = json.dumps(parameters['rx'])
791 self.rx = json.dumps(parameters['rx'])
792 #self.s_time = parameters['s_time']
792 #self.s_time = parameters['s_time']
793 #self.e_time = parameters['e_time']
793 #self.e_time = parameters['e_time']
794 self.ues = json.dumps(parameters['ues'])
794 self.ues = json.dumps(parameters['ues'])
795 self.only_rx = json.dumps(parameters['only_rx'])
795 self.only_rx = json.dumps(parameters['only_rx'])
796
796
797 self.modules_6bits()
797 self.modules_6bits()
798 self.save()
798 self.save()
799
799
800
800
801 def clone(self, **kwargs):
801 def clone(self, **kwargs):
802
802
803 self.pk = None
803 self.pk = None
804 self.id = None
804 self.id = None
805 for attr, value in kwargs.items():
805 for attr, value in kwargs.items():
806 setattr(self, attr, value)
806 setattr(self, attr, value)
807
807
808 self.save()
808 self.save()
809
809
810 return self
810 return self
811
811
812 def set_activebeam(self):
812 def set_activebeam(self):
813 """
813 """
814 This function change de active beam of ABS Configuration
814 This function change de active beam of ABS Configuration
815 """
815 """
816 conf = self.abs_conf
816 conf = self.abs_conf
817 active_beam = {}
817 active_beam = {}
818 active_beam['active_beam'] = self.id
818 active_beam['active_beam'] = self.id
819 conf.active_beam = json.dumps(active_beam)
819 conf.active_beam = json.dumps(active_beam)
820 conf.save()
820 conf.save()
821
821
822 return
822 return
823
823
824
824
825
825
826 def module_6bits(self, module):
826 def module_6bits(self, module):
827 """
827 """
828 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
828 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
829 """
829 """
830 if module > 64:
830 if module > 64:
831 beam_bits = ""
831 beam_bits = ""
832 return beam_bits
832 return beam_bits
833
833
834 data = ast.literal_eval(self.antenna)
834 data = ast.literal_eval(self.antenna)
835 up_data = data['antenna_up']
835 up_data = data['antenna_up']
836 down_data = data['antenna_down']
836 down_data = data['antenna_down']
837
837
838 pos = ip2position(module)
838 pos = ip2position(module)
839 up_value = up_data[pos[0]][pos[1]]
839 up_value = up_data[pos[0]][pos[1]]
840 down_value = down_data[pos[0]][pos[1]]
840 down_value = down_data[pos[0]][pos[1]]
841
841
842 up_bits = up_conv_bits(up_value)
842 up_bits = up_conv_bits(up_value)
843 down_bits = down_conv_bits(down_value)
843 down_bits = down_conv_bits(down_value)
844 beam_bits = up_bits+down_bits
844 beam_bits = up_bits+down_bits
845
845
846 return beam_bits
846 return beam_bits
847
847
848 def modules_6bits(self):
848 def modules_6bits(self):
849 """
849 """
850 This function returns 6bits from every abs module (1-64) in a dict
850 This function returns 6bits from every abs module (1-64) in a dict
851 """
851 """
852 modules_configuration = ast.literal_eval(self.modules_conf)
852 modules_configuration = ast.literal_eval(self.modules_conf)
853
853
854 for i in range(1,65):
854 for i in range(1,65):
855 modules_configuration[str(i)] = self.module_6bits(i)
855 modules_configuration[str(i)] = self.module_6bits(i)
856
856
857 self.modules_conf = json.dumps(modules_configuration)
857 self.modules_conf = json.dumps(modules_configuration)
858 self.save()
858 self.save()
859
859
860 return self.modules_conf
860 return self.modules_conf
861
861
862
862
863 def active_beam(self):
863 def active_beam(self):
864 """
864 """
865 This function set this beam as the active beam of its ABS Configuration.
865 This function set this beam as the active beam of its ABS Configuration.
866 """
866 """
867 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
867 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
868 self.abs_conf.save()
868 self.abs_conf.save()
869 return
869 return
870
870
871 @property
871 @property
872 def get_upvalues(self):
872 def get_upvalues(self):
873 """
873 """
874 This function reads antenna pattern and show the up-value of one abs module
874 This function reads antenna pattern and show the up-value of one abs module
875 """
875 """
876
876
877 data = ast.literal_eval(self.antenna)
877 data = ast.literal_eval(self.antenna)
878 up_data = data['antenna_up']
878 up_data = data['antenna_up']
879
879
880 up_values = []
880 up_values = []
881 for data in up_data:
881 for data in up_data:
882 for i in range(0,8):
882 for i in range(0,8):
883 up_values.append(data[i])
883 up_values.append(data[i])
884
884
885 return up_values
885 return up_values
886
886
887 @property
887 @property
888 def antenna_upvalues(self):
888 def antenna_upvalues(self):
889 """
889 """
890 This function reads antenna pattern and show the up - values of one abs beam
890 This function reads antenna pattern and show the up - values of one abs beam
891 in a particular order
891 in a particular order
892 """
892 """
893 data = ast.literal_eval(self.antenna)
893 data = ast.literal_eval(self.antenna)
894 up_data = data['antenna_up']
894 up_data = data['antenna_up']
895
895
896 return up_data
896 return up_data
897
897
898 @property
898 @property
899 def antenna_downvalues(self):
899 def antenna_downvalues(self):
900 """
900 """
901 This function reads antenna pattern and show the down - values of one abs beam
901 This function reads antenna pattern and show the down - values of one abs beam
902 in a particular order
902 in a particular order
903 """
903 """
904 data = ast.literal_eval(self.antenna)
904 data = ast.literal_eval(self.antenna)
905 down_data = data['antenna_down']
905 down_data = data['antenna_down']
906
906
907 return down_data
907 return down_data
908
908
909 @property
909 @property
910 def get_downvalues(self):
910 def get_downvalues(self):
911 """
911 """
912 This function reads antenna pattern and show the down-value of one abs module
912 This function reads antenna pattern and show the down-value of one abs module
913 """
913 """
914
914
915 data = ast.literal_eval(self.antenna)
915 data = ast.literal_eval(self.antenna)
916 down_data = data['antenna_down']
916 down_data = data['antenna_down']
917
917
918 down_values = []
918 down_values = []
919 for data in down_data:
919 for data in down_data:
920 for i in range(0,8):
920 for i in range(0,8):
921 down_values.append(data[i])
921 down_values.append(data[i])
922
922
923 return down_values
923 return down_values
924
924
925 @property
925 @property
926 def get_up_ues(self):
926 def get_up_ues(self):
927 """
927 """
928 This function shows the up-ues-value of one beam
928 This function shows the up-ues-value of one beam
929 """
929 """
930 data = ast.literal_eval(self.ues)
930 data = ast.literal_eval(self.ues)
931 up_ues = data['up']
931 up_ues = data['up']
932
932
933 return up_ues
933 return up_ues
934
934
935 @property
935 @property
936 def get_down_ues(self):
936 def get_down_ues(self):
937 """
937 """
938 This function shows the down-ues-value of one beam
938 This function shows the down-ues-value of one beam
939 """
939 """
940 data = ast.literal_eval(self.ues)
940 data = ast.literal_eval(self.ues)
941 down_ues = data['down']
941 down_ues = data['down']
942
942
943 return down_ues
943 return down_ues
944
944
945 @property
945 @property
946 def get_up_onlyrx(self):
946 def get_up_onlyrx(self):
947 """
947 """
948 This function shows the up-onlyrx-value of one beam
948 This function shows the up-onlyrx-value of one beam
949 """
949 """
950 data = json.loads(self.only_rx)
950 data = json.loads(self.only_rx)
951 up_onlyrx = data['up']
951 up_onlyrx = data['up']
952
952
953 return up_onlyrx
953 return up_onlyrx
954
954
955 @property
955 @property
956 def get_down_onlyrx(self):
956 def get_down_onlyrx(self):
957 """
957 """
958 This function shows the down-onlyrx-value of one beam
958 This function shows the down-onlyrx-value of one beam
959 """
959 """
960 data = json.loads(self.only_rx)
960 data = json.loads(self.only_rx)
961 down_onlyrx = data['down']
961 down_onlyrx = data['down']
962
962
963 return down_onlyrx
963 return down_onlyrx
964
964
965 @property
965 @property
966 def get_tx(self):
966 def get_tx(self):
967 """
967 """
968 This function shows the tx-values of one beam
968 This function shows the tx-values of one beam
969 """
969 """
970 data = json.loads(self.tx)
970 data = json.loads(self.tx)
971
971
972 return data
972 return data
973
973
974 @property
974 @property
975 def get_uptx(self):
975 def get_uptx(self):
976 """
976 """
977 This function shows the up-tx-values of one beam
977 This function shows the up-tx-values of one beam
978 """
978 """
979 data = json.loads(self.tx)
979 data = json.loads(self.tx)
980 up_data = data['up']
980 up_data = data['up']
981
981
982 up_values = []
982 up_values = []
983 for data in up_data:
983 for data in up_data:
984 for i in range(0,8):
984 for i in range(0,8):
985 up_values.append(data[i])
985 up_values.append(data[i])
986
986
987 return up_values
987 return up_values
988
988
989 @property
989 @property
990 def get_downtx(self):
990 def get_downtx(self):
991 """
991 """
992 This function shows the down-tx-values of one beam
992 This function shows the down-tx-values of one beam
993 """
993 """
994 data = json.loads(self.tx)
994 data = json.loads(self.tx)
995 down_data = data['down']
995 down_data = data['down']
996
996
997 down_values = []
997 down_values = []
998 for data in down_data:
998 for data in down_data:
999 for i in range(0,8):
999 for i in range(0,8):
1000 down_values.append(data[i])
1000 down_values.append(data[i])
1001
1001
1002 return down_values
1002 return down_values
1003
1003
1004
1004
1005
1005
1006 @property
1006 @property
1007 def get_rx(self):
1007 def get_rx(self):
1008 """
1008 """
1009 This function shows the rx-values of one beam
1009 This function shows the rx-values of one beam
1010 """
1010 """
1011 data = json.loads(self.rx)
1011 data = json.loads(self.rx)
1012
1012
1013 return data
1013 return data
1014
1014
1015 @property
1015 @property
1016 def get_uprx(self):
1016 def get_uprx(self):
1017 """
1017 """
1018 This function shows the up-rx-values of one beam
1018 This function shows the up-rx-values of one beam
1019 """
1019 """
1020 data = json.loads(self.rx)
1020 data = json.loads(self.rx)
1021 up_data = data['up']
1021 up_data = data['up']
1022
1022
1023 up_values = []
1023 up_values = []
1024 for data in up_data:
1024 for data in up_data:
1025 for i in range(0,8):
1025 for i in range(0,8):
1026 up_values.append(data[i])
1026 up_values.append(data[i])
1027
1027
1028 return up_values
1028 return up_values
1029
1029
1030 @property
1030 @property
1031 def get_downrx(self):
1031 def get_downrx(self):
1032 """
1032 """
1033 This function shows the down-rx-values of one beam
1033 This function shows the down-rx-values of one beam
1034 """
1034 """
1035 data = json.loads(self.rx)
1035 data = json.loads(self.rx)
1036 down_data = data['down']
1036 down_data = data['down']
1037
1037
1038 down_values = []
1038 down_values = []
1039 for data in down_data:
1039 for data in down_data:
1040 for i in range(0,8):
1040 for i in range(0,8):
1041 down_values.append(data[i])
1041 down_values.append(data[i])
1042
1042
1043 return down_values
1043 return down_values
@@ -1,87 +1,87
1 """
1 """
2 This script should run in the abs module embedded system
2 This script should run in the abs module embedded system
3 It creates a Web Application with API Restful Bottle to connect to SIR server.
3 It creates a Web Application with API Restful Bottle to connect to SIR server.
4 It needs the following scripts: abs_gpio.py and bottle.py
4 It needs the following scripts: abs_gpio.py and bottle.py
5 """
5 """
6 from bottle import route, run, request
6 from bottle import route, run, request
7 from bottle import error
7 from bottle import error
8 from abs_gpio import abs_read
8 from abs_gpio import abs_read
9
9
10 #Sockets
10 #Sockets
11 import socket
11 import socket
12 import sys
12 import sys
13
13
14 import json
14 import json
15
15
16 module_ip = '192.168.1.xx'
16 module_ip = '192.168.1.xx'
17 module_num = 'xx'
17 module_num = 'xx'
18 module_port = 5500
18 module_port = 5500
19 module_xx = ('192.168.1.xx',5500)
19 module_xx = ('192.168.1.xx',5500)
20
20
21 @route('/')
21 @route('/')
22 @route('/hello')
22 @route('/hello')
23 def hello():
23 def hello():
24 return "Hello World!"
24 return "Hello World!"
25
25
26
26
27 #---- Get Bits Function ----
27 #---- Get Bits Function ----
28 @route('/read', method='GET')
28 @route('/read', method='GET')
29 def readbits():
29 def readbits():
30
30
31 """
31 """
32 This function reads the real values from the embedded system pins
32 This function reads the real values from the embedded system pins
33 with gpio class
33 with gpio class
34 """
34 """
35
35
36 #This function reads sent bits.
36 #This function reads sent bits.
37
37
38 #------Get Monitoring Values------
38 #------Get Monitoring Values------
39 #----------------UP---------------
39 #----------------UP---------------
40 um2_value = abs_read(80) #(++)
40 um2_value = abs_read(80) #(++)
41 um1_value = abs_read(82)
41 um1_value = abs_read(82)
42 um0_value = abs_read(84) #(--)
42 um0_value = abs_read(84) #(--)
43 #--------------DOWN---------------
43 #--------------DOWN---------------
44 dm2_value = abs_read(94) #(++)
44 dm2_value = abs_read(94) #(++)
45 dm1_value = abs_read(88)
45 dm1_value = abs_read(88)
46 dm0_value = abs_read(86) #(--)
46 dm0_value = abs_read(86) #(--)
47
47
48 allbits = [um2_value, um1_value, um0_value, dm2_value, dm1_value, dm0_value]
48 allbits = [um2_value, um1_value, um0_value, dm2_value, dm1_value, dm0_value]
49 if "" not in allbits:
49 if "" not in allbits:
50 allbits = {"um2":int(um2_value), "um1": int(um1_value), "um0": int(um0_value), "dm2": int(dm2_value), "dm1": int(dm1_value), "dm0": int(dm0_value)}
50 allbits = {"um2":int(um2_value), "um1": int(um1_value), "um0": int(um0_value), "dm2": int(dm2_value), "dm1": int(dm1_value), "dm0": int(dm0_value)}
51 #allbits = {"ub0":0, "ub1":0, "ub2":0, "db0":0, "db1":0, "db2":0}
51 #allbits = {"ub0":0, "ub1":0, "ub2":0, "db0":0, "db1":0, "db2":0}
52 return {"status": 1, "message": "Bits were successfully read", "allbits" : allbits}
52 return {"status": 1, "message": "Bits were successfully read", "allbits" : allbits}
53 else:
53 else:
54 return {"status": 0, "message": "There's a problem reading bits", "allbits" : ""}
54 return {"status": 0, "message": "There's a problem reading bits", "allbits" : ""}
55
55
56 @route('/configure', method='POST')
56 @route('/write', method='POST')
57 def configurebits():
57 def writebits():
58 """
58 """
59 This function sends configurations to the module tcp_
59 This function sends configurations to the module tcp_
60 """
60 """
61 try:
61 try:
62 header_rx = request.forms.get('header')
62 header_rx = request.forms.get('header')
63 module_rx = request.forms.get('module')
63 module_rx = request.forms.get('module')
64 beams_rx = request.forms.get('beams')
64 beams_rx = request.forms.get('beams')
65 beams_rx = json.loads(beams_rx)
65 beams_rx = json.loads(beams_rx)
66 except:
66 except:
67 return {"status":0, "message": "Could not accept configuration"}
67 return {"status":0, "message": "Could not accept configuration"}
68 #print header_rx, module_rx, beams_rx
68 #print header_rx, module_rx, beams_rx
69 message_tx = header_rx+"\n"+module_rx+"\n"
69 message_tx = header_rx+"\n"+module_rx+"\n"
70 for i in range(1,len(beams_rx)+1):
70 for i in range(1,len(beams_rx)+1):
71 message_tx = message_tx+beams_rx[str(i)]+"\n"
71 message_tx = message_tx+beams_rx[str(i)]+"\n"
72 message_tx = message_tx+"0"
72 message_tx = message_tx+"0"
73
73
74 # Create the datagram socket
74 # Create the datagram socket
75 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
75 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
76 print >>sys.stderr, 'sending "%s"' % message_tx
76 print >>sys.stderr, 'sending "%s"' % message_tx
77 sock.connect(module_63)
77 sock.connect(module_63)
78 sock.send(message_tx)
78 sock.send(message_tx)
79 sock.close()
79 sock.close()
80
80
81 return {"status":1, "message": "Configuration has been successfully sent"}
81 return {"status":1, "message": "Configuration has been successfully sent"}
82
82
83 @error(404)
83 @error(404)
84 def error404(error):
84 def error404(error):
85 return "^^' Nothing here, try again."
85 return "^^' Nothing here, try again."
86
86
87 run(host=module_ip, port=8080, debug=True)
87 run(host=module_ip, port=8080, debug=True)
General Comments 0
You need to be logged in to leave comments. Login now