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