##// END OF EJS Templates
requests delay...
Fiorella Quino -
r218:e5fffce61e83
parent child
Show More
@@ -1,1210 +1,1210
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.5)#, 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 if beam_pos > 0:
722 if beam_pos > 0:
723 beam_pos = beam_pos - 1
723 beam_pos = beam_pos - 1
724 else:
724 else:
725 beam_pos = 0
725 beam_pos = 0
726
726
727 #El indice del apunte debe ser menor que el numero total de apuntes
727 #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
728 #El servidor tcp en el embebido comienza a contar desde 0
729 beams_list = ABSBeam.objects.filter(abs_conf=self)
729 beams_list = ABSBeam.objects.filter(abs_conf=self)
730 if len(beams_list) < beam_pos:
730 if len(beams_list) < beam_pos:
731 return 0
731 return 0
732
732
733 flag = 1
733 flag = 1
734 if beam_pos>9:
734 if beam_pos>9:
735 flag = 2
735 flag = 2
736
736
737 header = 'JROABSCeCnModCnMod0100000'
737 header = 'JROABSCeCnModCnMod0100000'
738 flag = str(flag)
738 flag = str(flag)
739 function = 'CHGB'
739 function = 'CHGB'
740 message_tx = header+flag+function+str(beam_pos)+'0'
740 message_tx = header+flag+function+str(beam_pos)+'0'
741
741
742 multicast_group = '224.3.29.71'
742 multicast_group = '224.3.29.71'
743 server_address = ('',10000)
743 server_address = ('',10000)
744
744
745 # Create the socket
745 # Create the socket
746 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
746 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
747 # Bind to the server address
747 # Bind to the server address
748 sock.bind(server_address)
748 sock.bind(server_address)
749 # Telling the OS add the socket to the multicast on all interfaces
749 # Telling the OS add the socket to the multicast on all interfaces
750 group = socket.inet_aton(multicast_group)
750 group = socket.inet_aton(multicast_group)
751 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
751 mreq = struct.pack('4sL', group, socket.INADDR_ANY)
752 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
752 sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
753
753
754 #print 'sending acknowledgement to all: \n' + message_tx
754 #print 'sending acknowledgement to all: \n' + message_tx
755 sock.sendto(message_tx, (multicast_group, 10000))
755 sock.sendto(message_tx, (multicast_group, 10000))
756 sock.close()
756 sock.close()
757 sock = None
757 sock = None
758
758
759 return 1
759 return 1
760
760
761 def test1(self):
761 def test1(self):
762 t1 = time.time()
762 t1 = time.time()
763 t2 = 0
763 t2 = 0
764 while (t2-t1)<100:#300
764 while (t2-t1)<100:#300
765 t2 = time.time()
765 t2 = time.time()
766 self.send_beam_num(2)
766 self.send_beam_num(2)
767 time.sleep(0.04)
767 time.sleep(0.04)
768 self.send_beam_num(1)
768 self.send_beam_num(1)
769 time.sleep(0.04)
769 time.sleep(0.04)
770 return
770 return
771
771
772 def change_procs_test1(self, module):
772 def change_procs_test1(self, module):
773
773
774 for i in range (1,300):#300
774 for i in range (1,300):#300
775 beam_pos = 1
775 beam_pos = 1
776 module_address = ('192.168.1.'+str(module), 5500)
776 module_address = ('192.168.1.'+str(module), 5500)
777 header = 'JROABSCeCnModCnMod0100000'
777 header = 'JROABSCeCnModCnMod0100000'
778 numbers = len(str(beam_pos))
778 numbers = len(str(beam_pos))
779 function = 'CHGB'
779 function = 'CHGB'
780
780
781 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
781 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
782
782
783 # Create the datagram socket
783 # Create the datagram socket
784 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
784 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
785 sock.connect(module_address)
785 sock.connect(module_address)
786
786
787 sock.send(message_tx)
787 sock.send(message_tx)
788 #t = sock.recv(1024)
788 #t = sock.recv(1024)
789 sock.close()
789 sock.close()
790 sock = None
790 sock = None
791
791
792
792
793 time.sleep(0.04)
793 time.sleep(0.04)
794
794
795
795
796 beam_pos = 0
796 beam_pos = 0
797 numbers = len(str(beam_pos))
797 numbers = len(str(beam_pos))
798
798
799 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
799 message_tx = header+str(numbers)+function+str(beam_pos)+'0'
800
800
801 # Create the datagram socket
801 # Create the datagram socket
802 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
802 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
803 sock.connect(module_address)
803 sock.connect(module_address)
804 sock.send(message_tx)
804 sock.send(message_tx)
805 sock.close()
805 sock.close()
806 sock = None
806 sock = None
807
807
808 time.sleep(0.04)
808 time.sleep(0.04)
809
809
810
810
811 def multi_procs_test1(self):
811 def multi_procs_test1(self):
812
812
813 """
813 """
814 This function sends the beam number to all abs modules using multiprocessing.
814 This function sends the beam number to all abs modules using multiprocessing.
815 """
815 """
816
816
817 #if __name__ == "__main__":
817 #if __name__ == "__main__":
818 size = 10000000 # Number of random numbers to add
818 size = 10000000 # Number of random numbers to add
819 procs = 65 # (Number-1) of processes to create (absmodule)
819 procs = 65 # (Number-1) of processes to create (absmodule)
820
820
821 # Create a list of jobs and then iterate through
821 # Create a list of jobs and then iterate through
822 # the number of processes appending each process to
822 # the number of processes appending each process to
823 # the job list
823 # the job list
824 jobs = []
824 jobs = []
825 for i in range(1, procs):
825 for i in range(1, procs):
826
826
827 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
827 process = multiprocessing.Process(target=self.change_procs_test1,args=(i,))
828 jobs.append(process)
828 jobs.append(process)
829 #print jobs
829 #print jobs
830
830
831 # Start the processes (i.e. calculate the random number lists)
831 # Start the processes (i.e. calculate the random number lists)
832 for j in jobs:
832 for j in jobs:
833 #time.sleep(0.4)
833 #time.sleep(0.4)
834 #print j
834 #print j
835 j.start()
835 j.start()
836
836
837 # Ensure all of the processes have finished
837 # Ensure all of the processes have finished
838 for j in jobs:
838 for j in jobs:
839 j.join()
839 j.join()
840
840
841 print("List processing complete.")
841 print("List processing complete.")
842 return 1
842 return 1
843
843
844
844
845
845
846 def multi_procs_test2(self):
846 def multi_procs_test2(self):
847 """
847 """
848 This function use multiprocessing python library. Importing Pool we can select
848 This function use multiprocessing python library. Importing Pool we can select
849 the number of cores we want to use
849 the number of cores we want to use
850 After 'nproc' linux command, we know how many cores computer has.
850 After 'nproc' linux command, we know how many cores computer has.
851 NOT WORKING
851 NOT WORKING
852 """
852 """
853 import multiprocessing
853 import multiprocessing
854 pool = multiprocessing.Pool(3) # cores
854 pool = multiprocessing.Pool(3) # cores
855
855
856 tasks = []
856 tasks = []
857 procs = 65
857 procs = 65
858 for i in range(62, procs):
858 for i in range(62, procs):
859 tasks.append( (i,))
859 tasks.append( (i,))
860 #print tasks
860 #print tasks
861 #pool.apply_async(self.change_procs_test1, 62)
861 #pool.apply_async(self.change_procs_test1, 62)
862 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
862 results = [pool.apply( self.change_procs_test1, t ) for t in tasks]
863 #for result in results:
863 #for result in results:
864 #result.get()
864 #result.get()
865 #(plotNum, plotFilename) = result.get()
865 #(plotNum, plotFilename) = result.get()
866 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
866 #print("Result: plot %d written to %s" % (plotNum, plotFilename) )
867
867
868 return 1
868 return 1
869
869
870
870
871 def multi_procs_test3(self):
871 def multi_procs_test3(self):
872 """
872 """
873 This function use multiprocessing python library. Importing Pool we can select
873 This function use multiprocessing python library. Importing Pool we can select
874 the number of cores we want to use
874 the number of cores we want to use
875 After 'nproc' linux command, we know how many cores computer has.
875 After 'nproc' linux command, we know how many cores computer has.
876 """
876 """
877 from multiprocessing import Pool
877 from multiprocessing import Pool
878 import time
878 import time
879
879
880 def f(x):
880 def f(x):
881 return x*x
881 return x*x
882
882
883 tasks = []
883 tasks = []
884 procs = 65
884 procs = 65
885 pool = Pool(processes=4)
885 pool = Pool(processes=4)
886
886
887 #for i in range(62, procs):
887 #for i in range(62, procs):
888 #result = pool.map(f, range(62,65))
888 #result = pool.map(f, range(62,65))
889 it = pool.imap(self.change_procs_test1, range(62,65))
889 it = pool.imap(self.change_procs_test1, range(62,65))
890 #result.get(timeout=5)
890 #result.get(timeout=5)
891
891
892 return 1
892 return 1
893
893
894
894
895 def f(x):
895 def f(x):
896 print x
896 print x
897 return
897 return
898
898
899 def multi_procs_test4(self):
899 def multi_procs_test4(self):
900 import multiprocessing as mp
900 import multiprocessing as mp
901
901
902
902
903 num_workers = mp.cpu_count()
903 num_workers = mp.cpu_count()
904
904
905 pool = mp.Pool(num_workers)
905 pool = mp.Pool(num_workers)
906 procs = 65
906 procs = 65
907 for i in range(62, procs):
907 for i in range(62, procs):
908 #for task in tasks:
908 #for task in tasks:
909 pool.apply_async(self.f, args = (i,))
909 pool.apply_async(self.f, args = (i,))
910
910
911 pool.close()
911 pool.close()
912 pool.join()
912 pool.join()
913
913
914 return 1
914 return 1
915
915
916
916
917 def get_absolute_url_import(self):
917 def get_absolute_url_import(self):
918 return reverse('url_import_abs_conf', args=[str(self.id)])
918 return reverse('url_import_abs_conf', args=[str(self.id)])
919
919
920
920
921
921
922
922
923 class ABSBeam(models.Model):
923 class ABSBeam(models.Model):
924
924
925 name = models.CharField(max_length=60, default='Beam')
925 name = models.CharField(max_length=60, default='Beam')
926 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
926 antenna = models.CharField(verbose_name='Antenna', max_length=1000, default=antenna_default)
927 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
927 abs_conf = models.ForeignKey(ABSConfiguration, null=True, verbose_name='ABS Configuration')
928 tx = models.CharField(verbose_name='Tx', max_length=1000, default=tx_default)
928 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)
929 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')
930 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')
931 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))
932 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)
933 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)
934 only_rx = models.CharField(verbose_name='Only RX', max_length=40, default=onlyrx_default)
935
935
936 class Meta:
936 class Meta:
937 db_table = 'abs_beams'
937 db_table = 'abs_beams'
938
938
939 def __unicode__(self):
939 def __unicode__(self):
940 return u'%s' % (self.name)
940 return u'%s' % (self.name)
941
941
942 def parms_to_dict(self):
942 def parms_to_dict(self):
943
943
944 #Update data
944 #Update data
945 self.modules_6bits()
945 self.modules_6bits()
946
946
947 parameters = {}
947 parameters = {}
948
948
949 parameters['name'] = self.name
949 parameters['name'] = self.name
950 parameters['antenna'] = ast.literal_eval(self.antenna)
950 parameters['antenna'] = ast.literal_eval(self.antenna)
951 parameters['abs_conf'] = self.abs_conf.name
951 parameters['abs_conf'] = self.abs_conf.name
952 parameters['tx'] = ast.literal_eval(self.tx)
952 parameters['tx'] = ast.literal_eval(self.tx)
953 parameters['rx'] = ast.literal_eval(self.rx)
953 parameters['rx'] = ast.literal_eval(self.rx)
954 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
954 parameters['s_time'] = self.s_time.strftime("%H:%M:%S")
955 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
955 parameters['e_time'] = self.e_time.strftime("%H:%M:%S")
956 parameters['configuration'] = ast.literal_eval(self.modules_conf)
956 parameters['configuration'] = ast.literal_eval(self.modules_conf)
957 parameters['ues'] = ast.literal_eval(self.ues)
957 parameters['ues'] = ast.literal_eval(self.ues)
958 parameters['only_rx'] = json.loads(self.only_rx)
958 parameters['only_rx'] = json.loads(self.only_rx)
959
959
960 return parameters
960 return parameters
961
961
962 def dict_to_parms(self, parameters):
962 def dict_to_parms(self, parameters):
963
963
964 self.name = parameters['name']
964 self.name = parameters['name']
965 self.antenna = json.dumps(parameters['antenna'])
965 self.antenna = json.dumps(parameters['antenna'])
966 #self.abs_conf = parameters['abs_conf']
966 #self.abs_conf = parameters['abs_conf']
967 self.tx = json.dumps(parameters['tx'])
967 self.tx = json.dumps(parameters['tx'])
968 self.rx = json.dumps(parameters['rx'])
968 self.rx = json.dumps(parameters['rx'])
969 #self.s_time = parameters['s_time']
969 #self.s_time = parameters['s_time']
970 #self.e_time = parameters['e_time']
970 #self.e_time = parameters['e_time']
971 self.ues = json.dumps(parameters['ues'])
971 self.ues = json.dumps(parameters['ues'])
972 self.only_rx = json.dumps(parameters['only_rx'])
972 self.only_rx = json.dumps(parameters['only_rx'])
973
973
974 self.modules_6bits()
974 self.modules_6bits()
975 self.save()
975 self.save()
976
976
977
977
978 def clone(self, **kwargs):
978 def clone(self, **kwargs):
979
979
980 self.pk = None
980 self.pk = None
981 self.id = None
981 self.id = None
982 for attr, value in kwargs.items():
982 for attr, value in kwargs.items():
983 setattr(self, attr, value)
983 setattr(self, attr, value)
984
984
985 self.save()
985 self.save()
986
986
987 return self
987 return self
988
988
989
989
990
990
991 def module_6bits(self, module):
991 def module_6bits(self, module):
992 """
992 """
993 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
993 This function reads antenna pattern and choose 6bits (upbits-downbits) for one abs module
994 """
994 """
995 if module > 64:
995 if module > 64:
996 beam_bits = ""
996 beam_bits = ""
997 return beam_bits
997 return beam_bits
998
998
999 data = ast.literal_eval(self.antenna)
999 data = ast.literal_eval(self.antenna)
1000 up_data = data['antenna_up']
1000 up_data = data['antenna_up']
1001 down_data = data['antenna_down']
1001 down_data = data['antenna_down']
1002
1002
1003 pos = ip2position(module)
1003 pos = ip2position(module)
1004 up_value = up_data[pos[0]][pos[1]]
1004 up_value = up_data[pos[0]][pos[1]]
1005 down_value = down_data[pos[0]][pos[1]]
1005 down_value = down_data[pos[0]][pos[1]]
1006
1006
1007 up_bits = up_conv_bits(up_value)
1007 up_bits = up_conv_bits(up_value)
1008 down_bits = down_conv_bits(down_value)
1008 down_bits = down_conv_bits(down_value)
1009 beam_bits = up_bits+down_bits
1009 beam_bits = up_bits+down_bits
1010
1010
1011 return beam_bits
1011 return beam_bits
1012
1012
1013 def modules_6bits(self):
1013 def modules_6bits(self):
1014 """
1014 """
1015 This function returns 6bits from every abs module (1-64) in a dict
1015 This function returns 6bits from every abs module (1-64) in a dict
1016 """
1016 """
1017 modules_configuration = ast.literal_eval(self.modules_conf)
1017 modules_configuration = ast.literal_eval(self.modules_conf)
1018
1018
1019 for i in range(1,65):
1019 for i in range(1,65):
1020 modules_configuration[str(i)] = self.module_6bits(i)
1020 modules_configuration[str(i)] = self.module_6bits(i)
1021
1021
1022 self.modules_conf = json.dumps(modules_configuration)
1022 self.modules_conf = json.dumps(modules_configuration)
1023 self.save()
1023 self.save()
1024
1024
1025 return self.modules_conf
1025 return self.modules_conf
1026
1026
1027
1027
1028 def set_as_activebeam(self):
1028 def set_as_activebeam(self):
1029 """
1029 """
1030 This function set this beam as the active beam of its ABS Configuration.
1030 This function set this beam as the active beam of its ABS Configuration.
1031 """
1031 """
1032 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
1032 self.abs_conf.active_beam = json.dumps({'active_beam': self.id})
1033 self.abs_conf.save()
1033 self.abs_conf.save()
1034
1034
1035 return
1035 return
1036
1036
1037
1037
1038 @property
1038 @property
1039 def get_upvalues(self):
1039 def get_upvalues(self):
1040 """
1040 """
1041 This function reads antenna pattern and show the up-value of one abs module
1041 This function reads antenna pattern and show the up-value of one abs module
1042 """
1042 """
1043
1043
1044 data = ast.literal_eval(self.antenna)
1044 data = ast.literal_eval(self.antenna)
1045 up_data = data['antenna_up']
1045 up_data = data['antenna_up']
1046
1046
1047 up_values = []
1047 up_values = []
1048 for data in up_data:
1048 for data in up_data:
1049 for i in range(0,8):
1049 for i in range(0,8):
1050 up_values.append(data[i])
1050 up_values.append(data[i])
1051
1051
1052 return up_values
1052 return up_values
1053
1053
1054 @property
1054 @property
1055 def antenna_upvalues(self):
1055 def antenna_upvalues(self):
1056 """
1056 """
1057 This function reads antenna pattern and show the up - values of one abs beam
1057 This function reads antenna pattern and show the up - values of one abs beam
1058 in a particular order
1058 in a particular order
1059 """
1059 """
1060 data = ast.literal_eval(self.antenna)
1060 data = ast.literal_eval(self.antenna)
1061 up_data = data['antenna_up']
1061 up_data = data['antenna_up']
1062
1062
1063 return up_data
1063 return up_data
1064
1064
1065 @property
1065 @property
1066 def antenna_downvalues(self):
1066 def antenna_downvalues(self):
1067 """
1067 """
1068 This function reads antenna pattern and show the down - values of one abs beam
1068 This function reads antenna pattern and show the down - values of one abs beam
1069 in a particular order
1069 in a particular order
1070 """
1070 """
1071 data = ast.literal_eval(self.antenna)
1071 data = ast.literal_eval(self.antenna)
1072 down_data = data['antenna_down']
1072 down_data = data['antenna_down']
1073
1073
1074 return down_data
1074 return down_data
1075
1075
1076 @property
1076 @property
1077 def get_downvalues(self):
1077 def get_downvalues(self):
1078 """
1078 """
1079 This function reads antenna pattern and show the down-value of one abs module
1079 This function reads antenna pattern and show the down-value of one abs module
1080 """
1080 """
1081
1081
1082 data = ast.literal_eval(self.antenna)
1082 data = ast.literal_eval(self.antenna)
1083 down_data = data['antenna_down']
1083 down_data = data['antenna_down']
1084
1084
1085 down_values = []
1085 down_values = []
1086 for data in down_data:
1086 for data in down_data:
1087 for i in range(0,8):
1087 for i in range(0,8):
1088 down_values.append(data[i])
1088 down_values.append(data[i])
1089
1089
1090 return down_values
1090 return down_values
1091
1091
1092 @property
1092 @property
1093 def get_up_ues(self):
1093 def get_up_ues(self):
1094 """
1094 """
1095 This function shows the up-ues-value of one beam
1095 This function shows the up-ues-value of one beam
1096 """
1096 """
1097 data = ast.literal_eval(self.ues)
1097 data = ast.literal_eval(self.ues)
1098 up_ues = data['up']
1098 up_ues = data['up']
1099
1099
1100 return up_ues
1100 return up_ues
1101
1101
1102 @property
1102 @property
1103 def get_down_ues(self):
1103 def get_down_ues(self):
1104 """
1104 """
1105 This function shows the down-ues-value of one beam
1105 This function shows the down-ues-value of one beam
1106 """
1106 """
1107 data = ast.literal_eval(self.ues)
1107 data = ast.literal_eval(self.ues)
1108 down_ues = data['down']
1108 down_ues = data['down']
1109
1109
1110 return down_ues
1110 return down_ues
1111
1111
1112 @property
1112 @property
1113 def get_up_onlyrx(self):
1113 def get_up_onlyrx(self):
1114 """
1114 """
1115 This function shows the up-onlyrx-value of one beam
1115 This function shows the up-onlyrx-value of one beam
1116 """
1116 """
1117 data = json.loads(self.only_rx)
1117 data = json.loads(self.only_rx)
1118 up_onlyrx = data['up']
1118 up_onlyrx = data['up']
1119
1119
1120 return up_onlyrx
1120 return up_onlyrx
1121
1121
1122 @property
1122 @property
1123 def get_down_onlyrx(self):
1123 def get_down_onlyrx(self):
1124 """
1124 """
1125 This function shows the down-onlyrx-value of one beam
1125 This function shows the down-onlyrx-value of one beam
1126 """
1126 """
1127 data = json.loads(self.only_rx)
1127 data = json.loads(self.only_rx)
1128 down_onlyrx = data['down']
1128 down_onlyrx = data['down']
1129
1129
1130 return down_onlyrx
1130 return down_onlyrx
1131
1131
1132 @property
1132 @property
1133 def get_tx(self):
1133 def get_tx(self):
1134 """
1134 """
1135 This function shows the tx-values of one beam
1135 This function shows the tx-values of one beam
1136 """
1136 """
1137 data = json.loads(self.tx)
1137 data = json.loads(self.tx)
1138
1138
1139 return data
1139 return data
1140
1140
1141 @property
1141 @property
1142 def get_uptx(self):
1142 def get_uptx(self):
1143 """
1143 """
1144 This function shows the up-tx-values of one beam
1144 This function shows the up-tx-values of one beam
1145 """
1145 """
1146 data = json.loads(self.tx)
1146 data = json.loads(self.tx)
1147 up_data = data['up']
1147 up_data = data['up']
1148
1148
1149 up_values = []
1149 up_values = []
1150 for data in up_data:
1150 for data in up_data:
1151 for i in range(0,8):
1151 for i in range(0,8):
1152 up_values.append(data[i])
1152 up_values.append(data[i])
1153
1153
1154 return up_values
1154 return up_values
1155
1155
1156 @property
1156 @property
1157 def get_downtx(self):
1157 def get_downtx(self):
1158 """
1158 """
1159 This function shows the down-tx-values of one beam
1159 This function shows the down-tx-values of one beam
1160 """
1160 """
1161 data = json.loads(self.tx)
1161 data = json.loads(self.tx)
1162 down_data = data['down']
1162 down_data = data['down']
1163
1163
1164 down_values = []
1164 down_values = []
1165 for data in down_data:
1165 for data in down_data:
1166 for i in range(0,8):
1166 for i in range(0,8):
1167 down_values.append(data[i])
1167 down_values.append(data[i])
1168
1168
1169 return down_values
1169 return down_values
1170
1170
1171
1171
1172
1172
1173 @property
1173 @property
1174 def get_rx(self):
1174 def get_rx(self):
1175 """
1175 """
1176 This function shows the rx-values of one beam
1176 This function shows the rx-values of one beam
1177 """
1177 """
1178 data = json.loads(self.rx)
1178 data = json.loads(self.rx)
1179
1179
1180 return data
1180 return data
1181
1181
1182 @property
1182 @property
1183 def get_uprx(self):
1183 def get_uprx(self):
1184 """
1184 """
1185 This function shows the up-rx-values of one beam
1185 This function shows the up-rx-values of one beam
1186 """
1186 """
1187 data = json.loads(self.rx)
1187 data = json.loads(self.rx)
1188 up_data = data['up']
1188 up_data = data['up']
1189
1189
1190 up_values = []
1190 up_values = []
1191 for data in up_data:
1191 for data in up_data:
1192 for i in range(0,8):
1192 for i in range(0,8):
1193 up_values.append(data[i])
1193 up_values.append(data[i])
1194
1194
1195 return up_values
1195 return up_values
1196
1196
1197 @property
1197 @property
1198 def get_downrx(self):
1198 def get_downrx(self):
1199 """
1199 """
1200 This function shows the down-rx-values of one beam
1200 This function shows the down-rx-values of one beam
1201 """
1201 """
1202 data = json.loads(self.rx)
1202 data = json.loads(self.rx)
1203 down_data = data['down']
1203 down_data = data['down']
1204
1204
1205 down_values = []
1205 down_values = []
1206 for data in down_data:
1206 for data in down_data:
1207 for i in range(0,8):
1207 for i in range(0,8):
1208 down_values.append(data[i])
1208 down_values.append(data[i])
1209
1209
1210 return down_values
1210 return down_values
General Comments 0
You need to be logged in to leave comments. Login now