##// END OF EJS Templates
Starting work with celery...
Fiorella Quino -
r202:deddc337364a
parent child
Show More
@@ -0,0 +1,19
1 from __future__ import absolute_import
2 from celery import task
3 from .models import Experiment
4
5 #@task
6 #def task_start(id_exp):
7
8 # exp = Experiment.objects.get(pk=id_exp)
9
10 # return exp.start()
11
12
13
14 #@task
15 #def task_stop(id_exp):
16
17 # exp = Experiment.objects.get(pk=id_exp)
18
19 # return exp.stop()
@@ -1,1103 +1,1103
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 OPERATION_MODES = (
177 OPERATION_MODES = (
178 (0, 'Manual'),
178 (0, 'Manual'),
179 (1, 'Automatic'),
179 (1, 'Automatic'),
180 )
180 )
181
181
182
182
183 class ABSConfiguration(Configuration):
183 class ABSConfiguration(Configuration):
184 active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}")
184 active_beam = models.CharField(verbose_name='Active Beam', max_length=20000, default="{}")
185 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default))
185 module_status = models.CharField(verbose_name='Module Status', max_length=10000, default=json.dumps(status_default))
186 operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0)
186 operation_mode = models.PositiveSmallIntegerField(verbose_name='Operation Mode', choices=OPERATION_MODES, default = 0)
187 operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True)
187 operation_value = models.FloatField(verbose_name='Periodic (seconds)', default="10", null=True, blank=True)
188
188
189 class Meta:
189 class Meta:
190 db_table = 'abs_configurations'
190 db_table = 'abs_configurations'
191
191
192 def get_absolute_url_plot(self):
192 def get_absolute_url_plot(self):
193 return reverse('url_plot_abs_patterns', args=[str(self.id)])
193 return reverse('url_plot_abs_patterns', args=[str(self.id)])
194
194
195
195
196 def parms_to_dict(self):
196 def parms_to_dict(self):
197
197
198 parameters = {}
198 parameters = {}
199
199
200 parameters['device_id'] = self.device.id
200 parameters['device_id'] = self.device.id
201 parameters['name'] = self.name
201 parameters['name'] = self.name
202 parameters['beams'] = {}
202 parameters['beams'] = {}
203
203
204 beams = ABSBeam.objects.filter(abs_conf=self)
204 beams = ABSBeam.objects.filter(abs_conf=self)
205 b=1
205 b=1
206 for beam in beams:
206 for beam in beams:
207 #absbeam = ABSBeam.objects.get(pk=beams[beam])
207 #absbeam = ABSBeam.objects.get(pk=beams[beam])
208 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
208 parameters['beams']['beam'+str(b)] = beam.parms_to_dict()#absbeam.parms_to_dict()
209 b+=1
209 b+=1
210
210
211 return parameters
211 return parameters
212
212
213 def dict_to_parms(self, parameters):
213 def dict_to_parms(self, parameters):
214
214
215 self.name = parameters['name']
215 self.name = parameters['name']
216
216
217 absbeams = ABSBeam.objects.filter(abs_conf=self)
217 absbeams = ABSBeam.objects.filter(abs_conf=self)
218 beams = parameters['beams']
218 beams = parameters['beams']
219
219
220 if absbeams:
220 if absbeams:
221 beams_number = len(beams)
221 beams_number = len(beams)
222 absbeams_number = len(absbeams)
222 absbeams_number = len(absbeams)
223 if beams_number==absbeams_number:
223 if beams_number==absbeams_number:
224 i = 1
224 i = 1
225 for absbeam in absbeams:
225 for absbeam in absbeams:
226 absbeam.dict_to_parms(beams['beam'+str(i)])
226 absbeam.dict_to_parms(beams['beam'+str(i)])
227 i = i+1
227 i = i+1
228 elif beams_number > absbeams_number:
228 elif beams_number > absbeams_number:
229 i = 1
229 i = 1
230 for absbeam in absbeams:
230 for absbeam in absbeams:
231 absbeam.dict_to_parms(beams['beam'+str(i)])
231 absbeam.dict_to_parms(beams['beam'+str(i)])
232 i=i+1
232 i=i+1
233 for x in range(i,beams_number+1):
233 for x in range(i,beams_number+1):
234 new_beam = ABSBeam(
234 new_beam = ABSBeam(
235 name =beams['beam'+str(i)]['name'],
235 name =beams['beam'+str(i)]['name'],
236 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
236 antenna =json.dumps(beams['beam'+str(i)]['antenna']),
237 abs_conf = self,
237 abs_conf = self,
238 tx =json.dumps(beams['beam'+str(i)]['tx']),
238 tx =json.dumps(beams['beam'+str(i)]['tx']),
239 rx =json.dumps(beams['beam'+str(i)]['rx']),
239 rx =json.dumps(beams['beam'+str(i)]['rx']),
240 ues =json.dumps(beams['beam'+str(i)]['ues']),
240 ues =json.dumps(beams['beam'+str(i)]['ues']),
241 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
241 only_rx =json.dumps(beams['beam'+str(i)]['only_rx'])
242 )
242 )
243 new_beam.save()
243 new_beam.save()
244 i=i+1
244 i=i+1
245 else: #beams_number < absbeams_number:
245 else: #beams_number < absbeams_number:
246 i = 1
246 i = 1
247 for absbeam in absbeams:
247 for absbeam in absbeams:
248 if i <= beams_number:
248 if i <= beams_number:
249 absbeam.dict_to_parms(beams['beam'+str(i)])
249 absbeam.dict_to_parms(beams['beam'+str(i)])
250 i=i+1
250 i=i+1
251 else:
251 else:
252 absbeam.delete()
252 absbeam.delete()
253 else:
253 else:
254 for beam in beams:
254 for beam in beams:
255 new_beam = ABSBeam(
255 new_beam = ABSBeam(
256 name =beams[beam]['name'],
256 name =beams[beam]['name'],
257 antenna =json.dumps(beams[beam]['antenna']),
257 antenna =json.dumps(beams[beam]['antenna']),
258 abs_conf = self,
258 abs_conf = self,
259 tx =json.dumps(beams[beam]['tx']),
259 tx =json.dumps(beams[beam]['tx']),
260 rx =json.dumps(beams[beam]['rx']),
260 rx =json.dumps(beams[beam]['rx']),
261 ues =json.dumps(beams[beam]['ues']),
261 ues =json.dumps(beams[beam]['ues']),
262 only_rx =json.dumps(beams[beam]['only_rx'])
262 only_rx =json.dumps(beams[beam]['only_rx'])
263 )
263 )
264 new_beam.save()
264 new_beam.save()
265
265
266
266
267
267
268 def update_from_file(self, parameters):
268 def update_from_file(self, parameters):
269
269
270 self.dict_to_parms(parameters)
270 self.dict_to_parms(parameters)
271 self.save()
271 self.save()
272
272
273
273
274 def get_beams(self, **kwargs):
274 def get_beams(self, **kwargs):
275 '''
275 '''
276 This function returns ABS Configuration beams
276 This function returns ABS Configuration beams
277 '''
277 '''
278 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
278 return ABSBeam.objects.filter(abs_conf=self.pk, **kwargs)
279
279
280 def clone(self, **kwargs):
280 def clone(self, **kwargs):
281
281
282 beams = self.get_beams()
282 beams = self.get_beams()
283 self.pk = None
283 self.pk = None
284 self.id = None
284 self.id = None
285 for attr, value in kwargs.items():
285 for attr, value in kwargs.items():
286 setattr(self, attr, value)
286 setattr(self, attr, value)
287 self.save()
287 self.save()
288
288
289 for beam in beams:
289 for beam in beams:
290 beam.clone(abs_conf=self)
290 beam.clone(abs_conf=self)
291
291
292 #-----For Active Beam-----
292 #-----For Active Beam-----
293 active_beam = json.loads(self.active_beam)
293 active_beam = json.loads(self.active_beam)
294 new_beams = ABSBeam.objects.filter(abs_conf=self)
294 new_beams = ABSBeam.objects.filter(abs_conf=self)
295 active_beam['active_beam'] = new_beams[0].id
295 active_beam['active_beam'] = new_beams[0].id
296 self.active_beam = json.dumps(active_beam)
296 self.active_beam = json.dumps(active_beam)
297 self.save()
297 self.save()
298 #-----For Active Beam-----
298 #-----For Active Beam-----
299
299
300 return self
300 return self
301
301
302
302
303 def module_conf(self, module_num, beams):
303 def module_conf(self, module_num, beams):
304 """
304 """
305 This function creates beam configurations for one abs module.
305 This function creates beam configurations for one abs module.
306 """
306 """
307 ip_address = self.device.ip_address
307 ip_address = self.device.ip_address
308 ip_address = ip_address.split('.')
308 ip_address = ip_address.split('.')
309 module_seq = (ip_address[0],ip_address[1],ip_address[2])
309 module_seq = (ip_address[0],ip_address[1],ip_address[2])
310 dot = '.'
310 dot = '.'
311 module_ip = dot.join(module_seq)+'.'+str(module_num)
311 module_ip = dot.join(module_seq)+'.'+str(module_num)
312 module_port = self.device.port_address
312 module_port = self.device.port_address
313 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
313 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
314
314
315 #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
315 #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
316 #module = 'ABS_'+str(module_num)
316 #module = 'ABS_'+str(module_num)
317 bs = '' #{}
317 bs = '' #{}
318 i=1
318 i=1
319
319
320 for beam in beams:
320 for beam in beams:
321 #bs[i] = fromBinary2Char(beam.module_6bits(module_num))
321 #bs[i] = fromBinary2Char(beam.module_6bits(module_num))
322 bs = bs + fromBinary2Char(beam.module_6bits(module_num))
322 bs = bs + fromBinary2Char(beam.module_6bits(module_num))
323 i=i+1
323 i=i+1
324
324
325 beams = bs
325 beams = bs
326
326
327 parameters = {}
327 parameters = {}
328 parameters['beams'] = beams #json.dumps(beams)
328 parameters['beams'] = beams #json.dumps(beams)
329 print parameters
329 print parameters
330 answer = ''
330 answer = ''
331
331
332 try:
332 try:
333 #r_write = requests.post(write_route, parameters, timeout=0.5)
333 #r_write = requests.post(write_route, parameters, timeout=0.5)
334 r_write = requests.post(write_route, json = parameters, timeout=0.5)
334 r_write = requests.post(write_route, json = parameters, timeout=0.5)
335 answer = r_write.json()
335 answer = r_write.json()
336 #self.message = answer['message']
336 #self.message = answer['message']
337 except:
337 except:
338 #self.message = "Could not write ABS parameters"
338 #self.message = "Could not write ABS parameters"
339 return False
339 return False
340
340
341 return answer
341 return answer
342
342
343
343
344
344
345 def write_device(self):
345 def write_device(self):
346 """
346 """
347 This function sends the beams list to every abs module.
347 This function sends the beams list to every abs module.
348 It needs 'module_conf' function
348 It needs 'module_conf' function
349 """
349 """
350
350
351 beams = ABSBeam.objects.filter(abs_conf=self)
351 beams = ABSBeam.objects.filter(abs_conf=self)
352
352
353 #-------------Write each abs module-----------
353 #-------------Write each abs module-----------
354 if beams:
354 if beams:
355 beams_status = ast.literal_eval(self.module_status)
355 beams_status = ast.literal_eval(self.module_status)
356 disconnected_modules = 0
356 disconnected_modules = 0
357 for i in range(1,65): #(62,65)
357 for i in range(1,65): #(62,65)
358 #--------------JUEVES-------------
358 #--------------JUEVES-------------
359 if beams_status[str(i)] != 0:
359 if beams_status[str(i)] != 0:
360 try:
360 try:
361 answer = self.module_conf(i,beams)
361 answer = self.module_conf(i,beams)
362 if answer:
362 if answer:
363 if answer['status']:
363 if answer['status']:
364 beams_status[str(i)] = 3
364 beams_status[str(i)] = 3
365
365
366 except:
366 except:
367 beams_status[str(i)] = 1
367 beams_status[str(i)] = 1
368
368
369 pass
369 pass
370 else:
370 else:
371 disconnected_modules += 1
371 disconnected_modules += 1
372 #--------------JUEVES-------------
372 #--------------JUEVES-------------
373 #try:
373 #try:
374 # answer = self.module_conf(i, beams)
374 # answer = self.module_conf(i, beams)
375 # beams_status[str(i)] = 1
375 # beams_status[str(i)] = 1
376 # self.module_status = json.dumps(beams_status)
376 # self.module_status = json.dumps(beams_status)
377 # self.save()
377 # self.save()
378
378
379 #except:
379 #except:
380 # beams_status[str(i)] = 0
380 # beams_status[str(i)] = 0
381 # self.module_status = json.dumps(beams_status)
381 # self.module_status = json.dumps(beams_status)
382 # self.save()
382 # self.save()
383 # answer = 0
383 # answer = 0
384 # return 0
384 # return 0
385 else:
385 else:
386 self.message = "ABS Configuration does not have beams"
386 self.message = "ABS Configuration does not have beams"
387 return False
387 return False
388
388
389 #self.device.status = 1
389 #self.device.status = 1
390 ##
390 ##
391 #-------------Jueves-------------
391 #-------------Jueves-------------
392 if disconnected_modules == 64:
392 if disconnected_modules == 64:
393 self.message = "Could not write ABS Modules"
393 self.message = "Could not write ABS Modules"
394 return False
394 return False
395 else:
395 else:
396 self.message = "ABS Beams List have been sent to ABS Modules"
396 self.message = "ABS Beams List have been sent to ABS Modules"
397 beams[0].set_as_activebeam()
397 beams[0].set_as_activebeam()
398
398
399 self.module_status = json.dumps(beams_status)
399 self.module_status = json.dumps(beams_status)
400 self.save()
400 self.save()
401
401
402 #-------------Jueves-------------
402 #-------------Jueves-------------
403 #if answer==1:
403 #if answer==1:
404 # self.message = "ABS Beams List have been sent to ABS Modules"
404 # self.message = "ABS Beams List have been sent to ABS Modules"
405 # beams[0].set_as_activebeam()
405 # beams[0].set_as_activebeam()
406 #else:
406 #else:
407 # self.message = "Could not read ABS parameters"
407 # self.message = "Could not read ABS parameters"
408 ##
408 ##
409 self.save()
409 self.save()
410 return True
410 return True
411
411
412
412
413
413
414
414
415 def read_module(self, module):
415 def read_module(self, module):
416
416
417 """
417 """
418 Read out-bits (up-down) of 1 abs module NOT for Configuration
418 Read out-bits (up-down) of 1 abs module NOT for Configuration
419 """
419 """
420
420
421 parameters = {}
421 parameters = {}
422 ip_address = self.device.ip_address
422 ip_address = self.device.ip_address
423 ip_address = ip_address.split('.')
423 ip_address = ip_address.split('.')
424 module_seq = (ip_address[0],ip_address[1],ip_address[2])
424 module_seq = (ip_address[0],ip_address[1],ip_address[2])
425 dot = '.'
425 dot = '.'
426 module_ip = dot.join(module_seq)+'.'+str(module)
426 module_ip = dot.join(module_seq)+'.'+str(module)
427 module_port = self.device.port_address
427 module_port = self.device.port_address
428 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
428 read_route = 'http://'+module_ip+':'+str(module_port)+'/read'
429
429
430 print(read_route)
430 print(read_route)
431
431
432 answer = ''
432 answer = ''
433 module_bits = ''
433 module_bits = ''
434
434
435 try:
435 try:
436 r_write = requests.get(read_route, timeout=0.7)
436 r_write = requests.get(read_route, timeout=0.7)
437 answer = r_write.json()
437 answer = r_write.json()
438 self.message = answer['message']
438 self.message = answer['message']
439 module_bits = answer['allbits']
439 module_bits = answer['allbits']
440 except:
440 except:
441 #message = "Could not read ABS parameters"
441 #message = "Could not read ABS parameters"
442 answer = r_write.json()
442 answer = r_write.json()
443 return 0
443 return 0
444
444
445 return module_bits
445 return module_bits
446
446
447
447
448 def absmodule_status(self):
448 def absmodule_status(self):
449 """
449 """
450 This function gets the status of each abs module. It sends GET method to Web Application
450 This function gets the status of each abs module. It sends GET method to Web Application
451 in Python Bottle.
451 in Python Bottle.
452 This function updates "module_status" field from ABSconfiguration.
452 This function updates "module_status" field from ABSconfiguration.
453 """
453 """
454 ip_address = self.device.ip_address
454 ip_address = self.device.ip_address
455 ip_address = ip_address.split('.')
455 ip_address = ip_address.split('.')
456 module_seq = (ip_address[0],ip_address[1],ip_address[2])
456 module_seq = (ip_address[0],ip_address[1],ip_address[2])
457 dot = '.'
457 dot = '.'
458 module_port = self.device.port_address
458 module_port = self.device.port_address
459
459
460 modules_status = json.loads(self.module_status)
460 modules_status = json.loads(self.module_status)
461
461
462 for i in range(1,65):
462 for i in range(1,65):
463 module_ip = dot.join(module_seq)+'.'+str(i)
463 module_ip = dot.join(module_seq)+'.'+str(i)
464 print module_ip
464 print module_ip
465
465
466 route = 'http://'+module_ip+':'+str(module_port)+'/hello'
466 route = 'http://'+module_ip+':'+str(module_port)+'/hello'
467
467
468 try:
468 try:
469 r = requests.get(route, timeout=0.7)
469 r = requests.get(route, timeout=0.7)
470 modules_status[str(i)] = 1
470 modules_status[str(i)] = 1
471 except:
471 except:
472 modules_status[str(i)] = 0
472 modules_status[str(i)] = 0
473 pass
473 pass
474
474
475
475
476 self.module_status=json.dumps(modules_status)
476 self.module_status=json.dumps(modules_status)
477 self.save()
477 self.save()
478
478
479 return
479 return
480
480
481
481
482 def connected_modules(self):
482 def connected_modules(self):
483 """
483 """
484 This function returns the number of connected abs-modules without updating.
484 This function returns the number of connected abs-modules without updating.
485 """
485 """
486 modules_status = json.loads(self.module_status)
486 modules_status = json.loads(self.module_status)
487 num = 0
487 num = 0
488 for status in modules_status:
488 for status in modules_status:
489 if modules_status[status] != 0:
489 if modules_status[status] != 0:
490 num += 1
490 num += 1
491
491
492 return num
492 return num
493
493
494
494
495 def status_device(self):
495 def status_device(self):
496 """
496 """
497 This function returns the status of all abs-modules as one.
497 This function returns the status of all abs-modules as one.
498 If at least one module is connected, its answer is "1"
498 If at least one module is connected, its answer is "1"
499 """
499 """
500 self.absmodule_status()
500 self.absmodule_status()
501 connected_modules = self.connected_modules()
501 connected_modules = self.connected_modules()
502 if connected_modules>0:
502 if connected_modules>0:
503 self.message = 'ABS modules Status have been updated.'
503 self.message = 'ABS modules Status have been updated.'
504 return 1
504 return 1
505 self.message = 'No ABS module is connected.'
505 self.message = 'No ABS module is connected.'
506 return 0
506 return 0
507
507
508
508
509
509
510 def write_module(self, module):
510 def write_module(self, module):
511
511
512 """
512 """
513 Send configuration to one abs module
513 Send configuration to one abs module
514 """
514 """
515
515
516 parameters = {}
516 parameters = {}
517 ip_address = self.abs_conf.device.ip_address
517 ip_address = self.abs_conf.device.ip_address
518 ip_address = ip_address.split('.')
518 ip_address = ip_address.split('.')
519 module_seq = (ip_address[0],ip_address[1],ip_address[2])
519 module_seq = (ip_address[0],ip_address[1],ip_address[2])
520 dot = '.'
520 dot = '.'
521 module_ip = dot.join(module_seq)+'.'+str(module)
521 module_ip = dot.join(module_seq)+'.'+str(module)
522 module_port = self.abs_conf.device.port_address
522 module_port = self.abs_conf.device.port_address
523 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
523 write_route = 'http://'+module_ip+':'+str(module_port)+'/write'
524
524
525 #print write_route
525 #print write_route
526
526
527 header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
527 #header = 'JROABSCeCnModCnMod01000108SNDFexperimento1.ab1'
528 module = 'ABS_'+str(module)
528 #module = 'ABS_'+str(module)
529 beams = {1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
529 beams = '!`*3<ENW'#{1: '001000', 2: '010001', 3: '010010', 4: '000011', 5: '101100', 6: '101101',
530 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
530 # 7: '110110', 8: '111111', 9: '000000', 10: '001001', 11: '010010', 12: '011011'}
531
531
532 parameters['header'] = header
532 #parameters['header'] = header
533 parameters['module'] = module
533 parameters['module'] = module
534 parameters['beams'] = json.dumps(beams)
534 parameters['beams'] = json.dumps(beams)
535
535
536 answer = ''
536 answer = ''
537
537
538 try:
538 try:
539 r_write = requests.post(write_route, parameters, timeout=0.5)
539 r_write = requests.post(write_route, parameters, timeout=0.5)
540 answer = r_write.json()
540 answer = r_write.json()
541 self.message = answer['message']
541 self.message = answer['message']
542 except:
542 except:
543 self.message = "Could not write ABS parameters"
543 self.message = "Could not write ABS parameters"
544 return 0
544 return 0
545
545
546
546
547 #self.device.status = int(answer['status'])
547 #self.device.status = int(answer['status'])
548
548
549 return 1
549 return 1
550
550
551
551
552 def beam_selector(self, module, beam_pos):
552 def beam_selector(self, module, beam_pos):
553 """
553 """
554 This function selects the beam number for one absmodule.
554 This function selects the beam number for one absmodule.
555 """
555 """
556
556
557 if beam_pos > 0:
557 if beam_pos > 0:
558 beam_pos = beam_pos - 1
558 beam_pos = beam_pos - 1
559 else:
559 else:
560 beam_pos = 0
560 beam_pos = 0
561
561
562 #El indice del apunte debe ser menor que el numero total de apuntes
562 #El indice del apunte debe ser menor que el numero total de apuntes
563 #El servidor tcp en el embebido comienza a contar desde 0
563 #El servidor tcp en el embebido comienza a contar desde 0
564 beams_list = ABSBeam.objects.filter(abs_conf=self)
564 beams_list = ABSBeam.objects.filter(abs_conf=self)
565 if len(beams_list) < beam_pos:
565 if len(beams_list) < beam_pos:
566 return 0
566 return 0
567
567
568 flag = 1
568 flag = 1
569 if beam_pos>9:
569 if beam_pos>9:
570 flag = 2
570 flag = 2
571
571
572 module_address = ('192.168.1.'+str(module), 5500)
572 module_address = ('192.168.1.'+str(module), 5500)
573 header = 'JROABSCeCnModCnMod0100000'
573 header = 'JROABSCeCnModCnMod0100000'
574 numbers = len(str(beam_pos))
574 numbers = len(str(beam_pos))
575 function = 'CHGB'
575 function = 'CHGB'
576
576
577 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
577 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
578
578
579 # Create the datagram socket
579 # Create the datagram socket
580 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
580 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
581 #sock.connect(module_address)
581 #sock.connect(module_address)
582 try:
582 try:
583 sock.connect(module_address)
583 sock.connect(module_address)
584 sock.send(message_tx)
584 sock.send(message_tx)
585 sock.close()
585 sock.close()
586 print("Writing abs module:"+module_address[0]+"...")
586 print("Writing abs module:"+module_address[0]+"...")
587 except:
587 except:
588 sock = None
588 sock = None
589 print("Problem writing abs module:"+module_address[0])
589 print("Problem writing abs module:"+module_address[0])
590 return 0
590 return 0
591
591
592 return 1
592 return 1
593
593
594
594
595 def change_beam(self, beam_pos):
595 def change_beam(self, beam_pos):
596 """
596 """
597 This function selects the beam number for all absmodules.
597 This function selects the beam number for all absmodules.
598 """
598 """
599 for i in range(1,65):
599 for i in range(1,65):
600 try:
600 try:
601 self.beam_selector(i,beam_pos)
601 self.beam_selector(i,beam_pos)
602 except:
602 except:
603 print("Problem with module: 192.168.1."+str(i))
603 print("Problem with module: 192.168.1."+str(i))
604 self.message = "Problem with module: 192.168.1."+str(i)
604 self.message = "Problem with module: 192.168.1."+str(i)
605 #return 0
605 #return 0
606 return 1
606 return 1
607
607
608
608
609 def send_beam_num(self, beam_pos):
609 def send_beam_num(self, beam_pos):
610 """
610 """
611 This function connects to a multicast group and sends the beam number
611 This function connects to a multicast group and sends the beam number
612 to all abs modules.
612 to all abs modules.
613 """
613 """
614
614
615 if beam_pos > 0:
615 if beam_pos > 0:
616 beam_pos = beam_pos - 1
616 beam_pos = beam_pos - 1
617 else:
617 else:
618 beam_pos = 0
618 beam_pos = 0
619
619
620 #El indice del apunte debe ser menor que el numero total de apuntes
620 #El indice del apunte debe ser menor que el numero total de apuntes
621 #El servidor tcp en el embebido comienza a contar desde 0
621 #El servidor tcp en el embebido comienza a contar desde 0
622 beams_list = ABSBeam.objects.filter(abs_conf=self)
622 beams_list = ABSBeam.objects.filter(abs_conf=self)
623 if len(beams_list) < beam_pos:
623 if len(beams_list) < beam_pos:
624 return 0
624 return 0
625
625
626 flag = 1
626 flag = 1
627 if beam_pos>9:
627 if beam_pos>9:
628 flag = 2
628 flag = 2
629
629
630 header = 'JROABSCeCnModCnMod0100000'
630 header = 'JROABSCeCnModCnMod0100000'
631 flag = str(flag)
631 flag = str(flag)
632 function = 'CHGB'
632 function = 'CHGB'
633 message_tx = header+flag+function+str(beam_pos)+'0'
633 message_tx = header+flag+function+str(beam_pos)+'0'
634
634
635 multicast_group = '224.3.29.71'
635 multicast_group = '224.3.29.71'
636 server_address = ('',10000)
636 server_address = ('',10000)
637
637
638 # Create the socket
638 # Create the socket
639 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
639 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
640 # Bind to the server address
640 # Bind to the server address
641 sock.bind(server_address)
641 sock.bind(server_address)
642 # Telling the OS add the socket to the multicast on all interfaces
642 # Telling the OS add the socket to the multicast on all interfaces
643 group = socket.inet_aton(multicast_group)
643 group = socket.inet_aton(multicast_group)
644 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
644 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
645 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
645 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
646
646
647 #print 'sending acknowledgement to all: \n' + message_tx
647 #print 'sending acknowledgement to all: \n' + message_tx
648 sock.sendto(message_tx, (multicast_group, 10000))
648 sock.sendto(message_tx, (multicast_group, 10000))
649 sock.close()
649 sock.close()
650 sock = None
650 sock = None
651
651
652 return 1
652 return 1
653
653
654 def test1(self):
654 def test1(self):
655 t1 = time.time()
655 t1 = time.time()
656 t2 = 0
656 t2 = 0
657 while (t2-t1)<100:#300
657 while (t2-t1)<100:#300
658 t2 = time.time()
658 t2 = time.time()
659 self.send_beam_num(2)
659 self.send_beam_num(2)
660 time.sleep(0.04)
660 time.sleep(0.04)
661 self.send_beam_num(1)
661 self.send_beam_num(1)
662 time.sleep(0.04)
662 time.sleep(0.04)
663 return
663 return
664
664
665 def change_procs_test1(self, module):
665 def change_procs_test1(self, module):
666
666
667 for i in range (1,300):#300
667 for i in range (1,300):#300
668 beam_pos = 1
668 beam_pos = 1
669 module_address = ('192.168.1.'+str(module), 5500)
669 module_address = ('192.168.1.'+str(module), 5500)
670 header = 'JROABSCeCnModCnMod0100000'
670 header = 'JROABSCeCnModCnMod0100000'
671 numbers = len(str(beam_pos))
671 numbers = len(str(beam_pos))
672 function = 'CHGB'
672 function = 'CHGB'
673
673
674 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
674 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
675
675
676 # Create the datagram socket
676 # Create the datagram socket
677 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
677 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
678 sock.connect(module_address)
678 sock.connect(module_address)
679
679
680 sock.send(message_tx)
680 sock.send(message_tx)
681 #t = sock.recv(1024)
681 #t = sock.recv(1024)
682 sock.close()
682 sock.close()
683 sock = None
683 sock = None
684
684
685
685
686 time.sleep(0.04)
686 time.sleep(0.04)
687
687
688
688
689 beam_pos = 0
689 beam_pos = 0
690 numbers = len(str(beam_pos))
690 numbers = len(str(beam_pos))
691
691
692 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
692 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
693
693
694 # Create the datagram socket
694 # Create the datagram socket
695 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
695 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
696 sock.connect(module_address)
696 sock.connect(module_address)
697 sock.send(message_tx)
697 sock.send(message_tx)
698 sock.close()
698 sock.close()
699 sock = None
699 sock = None
700
700
701 time.sleep(0.04)
701 time.sleep(0.04)
702
702
703
703
704 def multi_procs_test1(self):
704 def multi_procs_test1(self):
705
705
706 """
706 """
707 This function sends the beam number to all abs modules using multiprocessing.
707 This function sends the beam number to all abs modules using multiprocessing.
708 """
708 """
709
709
710 #if __name__ == "__main__":
710 #if __name__ == "__main__":
711 size = 10000000 # Number of random numbers to add
711 size = 10000000 # Number of random numbers to add
712 procs = 65 # (Number-1) of processes to create (absmodule)
712 procs = 65 # (Number-1) of processes to create (absmodule)
713
713
714 # Create a list of jobs and then iterate through
714 # Create a list of jobs and then iterate through
715 # the number of processes appending each process to
715 # the number of processes appending each process to
716 # the job list
716 # the job list
717 jobs = []
717 jobs = []
718 for i in range(1, procs):
718 for i in range(1, procs):
719
719
720 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
720 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
721 jobs.append(process)
721 jobs.append(process)
722 #print jobs
722 #print jobs
723
723
724 # Start the processes (i.e. calculate the random number lists)
724 # Start the processes (i.e. calculate the random number lists)
725 for j in jobs:
725 for j in jobs:
726 #time.sleep(0.4)
726 #time.sleep(0.4)
727 #print j
727 #print j
728 j.start()
728 j.start()
729
729
730 # Ensure all of the processes have finished
730 # Ensure all of the processes have finished
731 for j in jobs:
731 for j in jobs:
732 j.join()
732 j.join()
733
733
734 print("List processing complete.")
734 print("List processing complete.")
735 return 1
735 return 1
736
736
737
737
738
738
739 def multi_procs_test2(self):
739 def multi_procs_test2(self):
740 """
740 """
741 This function use multiprocessing python library. Importing Pool we can select
741 This function use multiprocessing python library. Importing Pool we can select
742 the number of cores we want to use
742 the number of cores we want to use
743 After 'nproc' linux command, we know how many cores computer has.
743 After 'nproc' linux command, we know how many cores computer has.
744 NOT WORKING
744 NOT WORKING
745 """
745 """
746 import multiprocessing
746 import multiprocessing
747 pool = multiprocessing.Pool(3) # cores
747 pool = multiprocessing.Pool(3) # cores
748
748
749 tasks = []
749 tasks = []
750 procs = 65
750 procs = 65
751 for i in range(62, procs):
751 for i in range(62, procs):
752 tasks.append( (i,))
752 tasks.append( (i,))
753 #print tasks
753 #print tasks
754 #pool.apply_async(self.change_procs_test1, 62)
754 #pool.apply_async(self.change_procs_test1, 62)
755 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
755 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
756 #for result in results:
756 #for result in results:
757 #result.get()
757 #result.get()
758 #(plotNum, plotFilename) = result.get()
758 #(plotNum, plotFilename) = result.get()
759 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
759 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
760
760
761 return 1
761 return 1
762
762
763
763
764 def multi_procs_test3(self):
764 def multi_procs_test3(self):
765 """
765 """
766 This function use multiprocessing python library. Importing Pool we can select
766 This function use multiprocessing python library. Importing Pool we can select
767 the number of cores we want to use
767 the number of cores we want to use
768 After 'nproc' linux command, we know how many cores computer has.
768 After 'nproc' linux command, we know how many cores computer has.
769 """
769 """
770 from multiprocessing import Pool
770 from multiprocessing import Pool
771 import time
771 import time
772
772
773 def f(x):
773 def f(x):
774 return x*x
774 return x*x
775
775
776 tasks = []
776 tasks = []
777 procs = 65
777 procs = 65
778 pool = Pool(processes=4)
778 pool = Pool(processes=4)
779
779
780 #for i in range(62, procs):
780 #for i in range(62, procs):
781 #result = pool.map(f, range(62,65))
781 #result = pool.map(f, range(62,65))
782 it = pool.imap(self.change_procs_test1, range(62,65))
782 it = pool.imap(self.change_procs_test1, range(62,65))
783 #result.get(timeout=5)
783 #result.get(timeout=5)
784
784
785 return 1
785 return 1
786
786
787
787
788 def f(x):
788 def f(x):
789 print x
789 print x
790 return
790 return
791
791
792 def multi_procs_test4(self):
792 def multi_procs_test4(self):
793 import multiprocessing as mp
793 import multiprocessing as mp
794
794
795
795
796 num_workers = mp.cpu_count()
796 num_workers = mp.cpu_count()
797
797
798 pool = mp.Pool(num_workers)
798 pool = mp.Pool(num_workers)
799 procs = 65
799 procs = 65
800 for i in range(62, procs):
800 for i in range(62, procs):
801 #for task in tasks:
801 #for task in tasks:
802 pool.apply_async(self.f, args = (i,))
802 pool.apply_async(self.f, args = (i,))
803
803
804 pool.close()
804 pool.close()
805 pool.join()
805 pool.join()
806
806
807 return 1
807 return 1
808
808
809
809
810 def get_absolute_url_import(self):
810 def get_absolute_url_import(self):
811 return reverse('url_import_abs_conf', args=[str(self.id)])
811 return reverse('url_import_abs_conf', args=[str(self.id)])
812
812
813
813
814
814
815
815
816 class ABSBeam(models.Model):
816 class ABSBeam(models.Model):
817
817
818 name = models.CharField(max_length=60, default='Beam')
818 name = models.CharField(max_length=60, default='Beam')
819 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
819 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
820 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
820 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
821 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
821 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
822 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
822 rx = models.CharField(verbose_name='Rx', max_length=1000, default=rx_default)
823 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
823 s_time = models.TimeField(verbose_name='Star Time', default='00:00:00')
824 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
824 e_time = models.TimeField(verbose_name='End Time', default='23:59:59')
825 modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default))
825 modules_conf = models.CharField(verbose_name='Modules', max_length=2000, default=json.dumps(conf_default))
826 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
826 ues = models.CharField(verbose_name='Ues', max_length=100, default=ues_default)
827 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
827 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
828
828
829 class Meta:
829 class Meta:
830 db_table = 'abs_beams'
830 db_table = 'abs_beams'
831
831
832 def __unicode__(self):
832 def __unicode__(self):
833 return u'%s' % (self.name)
833 return u'%s' % (self.name)
834
834
835 def parms_to_dict(self):
835 def parms_to_dict(self):
836
836
837 #Update data
837 #Update data
838 self.modules_6bits()
838 self.modules_6bits()
839
839
840 parameters = {}
840 parameters = {}
841
841
842 parameters['name'] = self.name
842 parameters['name'] = self.name
843 parameters['antenna'] = ast.literal_eval(self.antenna)
843 parameters['antenna'] = ast.literal_eval(self.antenna)
844 parameters['abs_conf'] = self.abs_conf.name
844 parameters['abs_conf'] = self.abs_conf.name
845 parameters['tx'] = ast.literal_eval(self.tx)
845 parameters['tx'] = ast.literal_eval(self.tx)
846 parameters['rx'] = ast.literal_eval(self.rx)
846 parameters['rx'] = ast.literal_eval(self.rx)
847 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
847 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
848 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
848 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
849 parameters['configuration'] = ast.literal_eval(self.modules_conf)
849 parameters['configuration'] = ast.literal_eval(self.modules_conf)
850 parameters['ues'] = ast.literal_eval(self.ues)
850 parameters['ues'] = ast.literal_eval(self.ues)
851 parameters['only_rx'] = json.loads(self.only_rx)
851 parameters['only_rx'] = json.loads(self.only_rx)
852
852
853 return parameters
853 return parameters
854
854
855 def dict_to_parms(self, parameters):
855 def dict_to_parms(self, parameters):
856
856
857 self.name = parameters['name']
857 self.name = parameters['name']
858 self.antenna = json.dumps(parameters['antenna'])
858 self.antenna = json.dumps(parameters['antenna'])
859 #self.abs_conf = parameters['abs_conf']
859 #self.abs_conf = parameters['abs_conf']
860 self.tx = json.dumps(parameters['tx'])
860 self.tx = json.dumps(parameters['tx'])
861 self.rx = json.dumps(parameters['rx'])
861 self.rx = json.dumps(parameters['rx'])
862 #self.s_time = parameters['s_time']
862 #self.s_time = parameters['s_time']
863 #self.e_time = parameters['e_time']
863 #self.e_time = parameters['e_time']
864 self.ues = json.dumps(parameters['ues'])
864 self.ues = json.dumps(parameters['ues'])
865 self.only_rx = json.dumps(parameters['only_rx'])
865 self.only_rx = json.dumps(parameters['only_rx'])
866
866
867 self.modules_6bits()
867 self.modules_6bits()
868 self.save()
868 self.save()
869
869
870
870
871 def clone(self, **kwargs):
871 def clone(self, **kwargs):
872
872
873 self.pk = None
873 self.pk = None
874 self.id = None
874 self.id = None
875 for attr, value in kwargs.items():
875 for attr, value in kwargs.items():
876 setattr(self, attr, value)
876 setattr(self, attr, value)
877
877
878 self.save()
878 self.save()
879
879
880 return self
880 return self
881
881
882
882
883
883
884 def module_6bits(self, module):
884 def module_6bits(self, module):
885 """
885 """
886 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
886 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
887 """
887 """
888 if module > 64:
888 if module > 64:
889 beam_bits = ""
889 beam_bits = ""
890 return beam_bits
890 return beam_bits
891
891
892 data = ast.literal_eval(self.antenna)
892 data = ast.literal_eval(self.antenna)
893 up_data = data['antenna_up']
893 up_data = data['antenna_up']
894 down_data = data['antenna_down']
894 down_data = data['antenna_down']
895
895
896 pos = ip2position(module)
896 pos = ip2position(module)
897 up_value = up_data[pos[0]][pos[1]]
897 up_value = up_data[pos[0]][pos[1]]
898 down_value = down_data[pos[0]][pos[1]]
898 down_value = down_data[pos[0]][pos[1]]
899
899
900 up_bits = up_conv_bits(up_value)
900 up_bits = up_conv_bits(up_value)
901 down_bits = down_conv_bits(down_value)
901 down_bits = down_conv_bits(down_value)
902 beam_bits = up_bits+down_bits
902 beam_bits = up_bits+down_bits
903
903
904 return beam_bits
904 return beam_bits
905
905
906 def modules_6bits(self):
906 def modules_6bits(self):
907 """
907 """
908 This function returns 6bits from every abs module (1-64) in a dict
908 This function returns 6bits from every abs module (1-64) in a dict
909 """
909 """
910 modules_configuration = ast.literal_eval(self.modules_conf)
910 modules_configuration = ast.literal_eval(self.modules_conf)
911
911
912 for i in range(1,65):
912 for i in range(1,65):
913 modules_configuration[str(i)] = self.module_6bits(i)
913 modules_configuration[str(i)] = self.module_6bits(i)
914
914
915 self.modules_conf = json.dumps(modules_configuration)
915 self.modules_conf = json.dumps(modules_configuration)
916 self.save()
916 self.save()
917
917
918 return self.modules_conf
918 return self.modules_conf
919
919
920
920
921 def set_as_activebeam(self):
921 def set_as_activebeam(self):
922 """
922 """
923 This function set this beam as the active beam of its ABS Configuration.
923 This function set this beam as the active beam of its ABS Configuration.
924 """
924 """
925 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
925 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
926 self.abs_conf.save()
926 self.abs_conf.save()
927
927
928 return
928 return
929
929
930
930
931 @property
931 @property
932 def get_upvalues(self):
932 def get_upvalues(self):
933 """
933 """
934 This function reads antenna pattern and show the up-value of one abs module
934 This function reads antenna pattern and show the up-value of one abs module
935 """
935 """
936
936
937 data = ast.literal_eval(self.antenna)
937 data = ast.literal_eval(self.antenna)
938 up_data = data['antenna_up']
938 up_data = data['antenna_up']
939
939
940 up_values = []
940 up_values = []
941 for data in up_data:
941 for data in up_data:
942 for i in range(0,8):
942 for i in range(0,8):
943 up_values.append(data[i])
943 up_values.append(data[i])
944
944
945 return up_values
945 return up_values
946
946
947 @property
947 @property
948 def antenna_upvalues(self):
948 def antenna_upvalues(self):
949 """
949 """
950 This function reads antenna pattern and show the up - values of one abs beam
950 This function reads antenna pattern and show the up - values of one abs beam
951 in a particular order
951 in a particular order
952 """
952 """
953 data = ast.literal_eval(self.antenna)
953 data = ast.literal_eval(self.antenna)
954 up_data = data['antenna_up']
954 up_data = data['antenna_up']
955
955
956 return up_data
956 return up_data
957
957
958 @property
958 @property
959 def antenna_downvalues(self):
959 def antenna_downvalues(self):
960 """
960 """
961 This function reads antenna pattern and show the down - values of one abs beam
961 This function reads antenna pattern and show the down - values of one abs beam
962 in a particular order
962 in a particular order
963 """
963 """
964 data = ast.literal_eval(self.antenna)
964 data = ast.literal_eval(self.antenna)
965 down_data = data['antenna_down']
965 down_data = data['antenna_down']
966
966
967 return down_data
967 return down_data
968
968
969 @property
969 @property
970 def get_downvalues(self):
970 def get_downvalues(self):
971 """
971 """
972 This function reads antenna pattern and show the down-value of one abs module
972 This function reads antenna pattern and show the down-value of one abs module
973 """
973 """
974
974
975 data = ast.literal_eval(self.antenna)
975 data = ast.literal_eval(self.antenna)
976 down_data = data['antenna_down']
976 down_data = data['antenna_down']
977
977
978 down_values = []
978 down_values = []
979 for data in down_data:
979 for data in down_data:
980 for i in range(0,8):
980 for i in range(0,8):
981 down_values.append(data[i])
981 down_values.append(data[i])
982
982
983 return down_values
983 return down_values
984
984
985 @property
985 @property
986 def get_up_ues(self):
986 def get_up_ues(self):
987 """
987 """
988 This function shows the up-ues-value of one beam
988 This function shows the up-ues-value of one beam
989 """
989 """
990 data = ast.literal_eval(self.ues)
990 data = ast.literal_eval(self.ues)
991 up_ues = data['up']
991 up_ues = data['up']
992
992
993 return up_ues
993 return up_ues
994
994
995 @property
995 @property
996 def get_down_ues(self):
996 def get_down_ues(self):
997 """
997 """
998 This function shows the down-ues-value of one beam
998 This function shows the down-ues-value of one beam
999 """
999 """
1000 data = ast.literal_eval(self.ues)
1000 data = ast.literal_eval(self.ues)
1001 down_ues = data['down']
1001 down_ues = data['down']
1002
1002
1003 return down_ues
1003 return down_ues
1004
1004
1005 @property
1005 @property
1006 def get_up_onlyrx(self):
1006 def get_up_onlyrx(self):
1007 """
1007 """
1008 This function shows the up-onlyrx-value of one beam
1008 This function shows the up-onlyrx-value of one beam
1009 """
1009 """
1010 data = json.loads(self.only_rx)
1010 data = json.loads(self.only_rx)
1011 up_onlyrx = data['up']
1011 up_onlyrx = data['up']
1012
1012
1013 return up_onlyrx
1013 return up_onlyrx
1014
1014
1015 @property
1015 @property
1016 def get_down_onlyrx(self):
1016 def get_down_onlyrx(self):
1017 """
1017 """
1018 This function shows the down-onlyrx-value of one beam
1018 This function shows the down-onlyrx-value of one beam
1019 """
1019 """
1020 data = json.loads(self.only_rx)
1020 data = json.loads(self.only_rx)
1021 down_onlyrx = data['down']
1021 down_onlyrx = data['down']
1022
1022
1023 return down_onlyrx
1023 return down_onlyrx
1024
1024
1025 @property
1025 @property
1026 def get_tx(self):
1026 def get_tx(self):
1027 """
1027 """
1028 This function shows the tx-values of one beam
1028 This function shows the tx-values of one beam
1029 """
1029 """
1030 data = json.loads(self.tx)
1030 data = json.loads(self.tx)
1031
1031
1032 return data
1032 return data
1033
1033
1034 @property
1034 @property
1035 def get_uptx(self):
1035 def get_uptx(self):
1036 """
1036 """
1037 This function shows the up-tx-values of one beam
1037 This function shows the up-tx-values of one beam
1038 """
1038 """
1039 data = json.loads(self.tx)
1039 data = json.loads(self.tx)
1040 up_data = data['up']
1040 up_data = data['up']
1041
1041
1042 up_values = []
1042 up_values = []
1043 for data in up_data:
1043 for data in up_data:
1044 for i in range(0,8):
1044 for i in range(0,8):
1045 up_values.append(data[i])
1045 up_values.append(data[i])
1046
1046
1047 return up_values
1047 return up_values
1048
1048
1049 @property
1049 @property
1050 def get_downtx(self):
1050 def get_downtx(self):
1051 """
1051 """
1052 This function shows the down-tx-values of one beam
1052 This function shows the down-tx-values of one beam
1053 """
1053 """
1054 data = json.loads(self.tx)
1054 data = json.loads(self.tx)
1055 down_data = data['down']
1055 down_data = data['down']
1056
1056
1057 down_values = []
1057 down_values = []
1058 for data in down_data:
1058 for data in down_data:
1059 for i in range(0,8):
1059 for i in range(0,8):
1060 down_values.append(data[i])
1060 down_values.append(data[i])
1061
1061
1062 return down_values
1062 return down_values
1063
1063
1064
1064
1065
1065
1066 @property
1066 @property
1067 def get_rx(self):
1067 def get_rx(self):
1068 """
1068 """
1069 This function shows the rx-values of one beam
1069 This function shows the rx-values of one beam
1070 """
1070 """
1071 data = json.loads(self.rx)
1071 data = json.loads(self.rx)
1072
1072
1073 return data
1073 return data
1074
1074
1075 @property
1075 @property
1076 def get_uprx(self):
1076 def get_uprx(self):
1077 """
1077 """
1078 This function shows the up-rx-values of one beam
1078 This function shows the up-rx-values of one beam
1079 """
1079 """
1080 data = json.loads(self.rx)
1080 data = json.loads(self.rx)
1081 up_data = data['up']
1081 up_data = data['up']
1082
1082
1083 up_values = []
1083 up_values = []
1084 for data in up_data:
1084 for data in up_data:
1085 for i in range(0,8):
1085 for i in range(0,8):
1086 up_values.append(data[i])
1086 up_values.append(data[i])
1087
1087
1088 return up_values
1088 return up_values
1089
1089
1090 @property
1090 @property
1091 def get_downrx(self):
1091 def get_downrx(self):
1092 """
1092 """
1093 This function shows the down-rx-values of one beam
1093 This function shows the down-rx-values of one beam
1094 """
1094 """
1095 data = json.loads(self.rx)
1095 data = json.loads(self.rx)
1096 down_data = data['down']
1096 down_data = data['down']
1097
1097
1098 down_values = []
1098 down_values = []
1099 for data in down_data:
1099 for data in down_data:
1100 for i in range(0,8):
1100 for i in range(0,8):
1101 down_values.append(data[i])
1101 down_values.append(data[i])
1102
1102
1103 return down_values
1103 return down_values
General Comments 0
You need to be logged in to leave comments. Login now