##// END OF EJS Templates
ver_3Julio :: Fix IP bug in ABS
jespinoza -
r407:c93b3c968e19
parent child
Show More
@@ -1,461 +1,462
1 from django.shortcuts import render_to_response
1 from django.shortcuts import render_to_response
2 from django.template import RequestContext
2 from django.template import RequestContext
3 from django.shortcuts import redirect, render, get_object_or_404
3 from django.shortcuts import redirect, render, get_object_or_404
4 from django.contrib import messages
4 from django.contrib import messages
5 from django.conf import settings
5 from django.conf import settings
6 from django.http import HttpResponse
6 from django.http import HttpResponse
7 from django.urls import reverse
7 from django.urls import reverse
8 from django.views.decorators.csrf import csrf_exempt
8 from django.views.decorators.csrf import csrf_exempt
9 from django.utils.safestring import mark_safe
9 from django.utils.safestring import mark_safe
10
10
11 from datetime import datetime
11 from datetime import datetime
12 from time import sleep
12 from time import sleep
13 import os
13 import os
14 import io
14 import io
15
15
16 from apps.main.models import Device, Configuration, Experiment
16 from apps.main.models import Device, Configuration, Experiment
17 from apps.main.views import sidebar
17 from apps.main.views import sidebar
18
18
19 from .models import ABSConfiguration, ABSBeam
19 from .models import ABSConfiguration, ABSBeam
20 from .forms import ABSConfigurationForm, ABSBeamEditForm, ABSBeamAddForm, ABSImportForm
20 from .forms import ABSConfigurationForm, ABSBeamEditForm, ABSBeamAddForm, ABSImportForm
21
21
22 from .utils.overJroShow import overJroShow
22 from .utils.overJroShow import overJroShow
23 #from .utils.OverJRO import OverJRO
23 #from .utils.OverJRO import OverJRO
24 #Create your views here.
24 #Create your views here.
25 import json, ast
25 import json, ast
26
26
27
27
28 def get_values_from_form(form_data):
28 def get_values_from_form(form_data):
29
29
30 sublistup = []
30 sublistup = []
31 sublistdown = []
31 sublistdown = []
32 subtxlistup = []
32 subtxlistup = []
33 subtxlistdown = []
33 subtxlistdown = []
34 subrxlistup = []
34 subrxlistup = []
35 subrxlistdown = []
35 subrxlistdown = []
36
36
37 up_values_list = []
37 up_values_list = []
38 down_values_list = []
38 down_values_list = []
39 up_txvalues_list = []
39 up_txvalues_list = []
40 down_txvalues_list = []
40 down_txvalues_list = []
41 up_rxvalues_list = []
41 up_rxvalues_list = []
42 down_rxvalues_list = []
42 down_rxvalues_list = []
43
43
44 values_list = {}
44 values_list = {}
45 cont = 1
45 cont = 1
46
46
47 for i in range(1,65):
47 for i in range(1,65):
48 x = float(form_data['abs_up'+str(i)])
48 x = float(form_data['abs_up'+str(i)])
49 y = float(form_data['abs_down'+str(i)])
49 y = float(form_data['abs_down'+str(i)])
50 sublistup.append(x)
50 sublistup.append(x)
51 sublistdown.append(y)
51 sublistdown.append(y)
52
52
53 if str(i) in form_data.getlist('uptx_checks'):
53 if str(i) in form_data.getlist('uptx_checks'):
54 subtxlistup.append(1)
54 subtxlistup.append(1)
55 else:
55 else:
56 subtxlistup.append(0)
56 subtxlistup.append(0)
57 if str(i) in form_data.getlist('downtx_checks'):
57 if str(i) in form_data.getlist('downtx_checks'):
58 subtxlistdown.append(1)
58 subtxlistdown.append(1)
59 else:
59 else:
60 subtxlistdown.append(0)
60 subtxlistdown.append(0)
61
61
62 if str(i) in form_data.getlist('uprx_checks'):
62 if str(i) in form_data.getlist('uprx_checks'):
63 subrxlistup.append(1)
63 subrxlistup.append(1)
64 else:
64 else:
65 subrxlistup.append(0)
65 subrxlistup.append(0)
66 if str(i) in form_data.getlist('downrx_checks'):
66 if str(i) in form_data.getlist('downrx_checks'):
67 subrxlistdown.append(1)
67 subrxlistdown.append(1)
68 else:
68 else:
69 subrxlistdown.append(0)
69 subrxlistdown.append(0)
70
70
71 cont = cont+1
71 cont = cont+1
72
72
73 if cont == 9:
73 if cont == 9:
74 up_values_list.append(sublistup)
74 up_values_list.append(sublistup)
75 down_values_list.append(sublistdown)
75 down_values_list.append(sublistdown)
76 sublistup = []
76 sublistup = []
77 sublistdown = []
77 sublistdown = []
78
78
79 up_txvalues_list.append(subtxlistup)
79 up_txvalues_list.append(subtxlistup)
80 down_txvalues_list.append(subtxlistdown)
80 down_txvalues_list.append(subtxlistdown)
81 subtxlistup = []
81 subtxlistup = []
82 subtxlistdown = []
82 subtxlistdown = []
83 up_rxvalues_list.append(subrxlistup)
83 up_rxvalues_list.append(subrxlistup)
84 down_rxvalues_list.append(subrxlistdown)
84 down_rxvalues_list.append(subrxlistdown)
85 subrxlistup = []
85 subrxlistup = []
86 subrxlistdown = []
86 subrxlistdown = []
87 cont = 1
87 cont = 1
88
88
89
89
90 list_uesup = []
90 list_uesup = []
91 list_uesdown = []
91 list_uesdown = []
92 for i in range(1,5):
92 for i in range(1,5):
93 if form_data['ues_up'+str(i)] == '':
93 if form_data['ues_up'+str(i)] == '':
94 list_uesup.append(0.0)
94 list_uesup.append(0.0)
95 else:
95 else:
96 list_uesup.append(float(form_data['ues_up'+str(i)]))
96 list_uesup.append(float(form_data['ues_up'+str(i)]))
97
97
98 if form_data['ues_down'+str(i)] == '':
98 if form_data['ues_down'+str(i)] == '':
99 list_uesdown.append(0.0)
99 list_uesdown.append(0.0)
100 else:
100 else:
101 list_uesdown.append(float(form_data['ues_down'+str(i)]))
101 list_uesdown.append(float(form_data['ues_down'+str(i)]))
102
102
103 onlyrx_list = form_data.getlist('onlyrx')
103 onlyrx_list = form_data.getlist('onlyrx')
104 only_rx = {}
104 only_rx = {}
105 if '1' in onlyrx_list:
105 if '1' in onlyrx_list:
106 only_rx['up'] = True
106 only_rx['up'] = True
107 else:
107 else:
108 only_rx['up'] = False
108 only_rx['up'] = False
109 if '2' in onlyrx_list:
109 if '2' in onlyrx_list:
110 only_rx['down'] = True
110 only_rx['down'] = True
111 else:
111 else:
112 only_rx['down'] = False
112 only_rx['down'] = False
113
113
114 antenna = {'antenna_up': up_values_list, 'antenna_down': down_values_list}
114 antenna = {'antenna_up': up_values_list, 'antenna_down': down_values_list}
115 tx = {'up': up_txvalues_list, 'down': down_txvalues_list}
115 tx = {'up': up_txvalues_list, 'down': down_txvalues_list}
116 rx = {'up': up_rxvalues_list, 'down': down_rxvalues_list}
116 rx = {'up': up_rxvalues_list, 'down': down_rxvalues_list}
117 ues = {'up': list_uesup, 'down': list_uesdown}
117 ues = {'up': list_uesup, 'down': list_uesdown}
118 name = str(form_data['beam_name'])
118 name = str(form_data['beam_name'])
119
119
120 beam_data = {'name': name, 'antenna': antenna, 'tx': tx, 'rx': rx, 'ues': ues, 'only_rx': only_rx}
120 beam_data = {'name': name, 'antenna': antenna, 'tx': tx, 'rx': rx, 'ues': ues, 'only_rx': only_rx}
121
121
122 return beam_data
122 return beam_data
123
123
124
124
125 def abs_conf(request, id_conf):
125 def abs_conf(request, id_conf):
126
126
127 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
127 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
128 beams = ABSBeam.objects.filter(abs_conf=conf)
128 beams = ABSBeam.objects.filter(abs_conf=conf)
129 #------------Colors for Active Beam:-------------
129 #------------Colors for Active Beam:-------------
130 all_status = {}
130 all_status = {}
131 module_messages = json.loads(conf.module_messages)
131 module_messages = json.loads(conf.module_messages)
132
132
133 color_status = {}
133 color_status = {}
134 for i, status in enumerate(conf.module_status):
134 for i, status in enumerate(conf.module_status):
135 if status == '3': #Running background-color: #00cc00;
135 if status == '3': #Running background-color: #00cc00;
136 all_status['{}'.format(i+1)] = 2
136 all_status['{}'.format(i+1)] = 2
137 color_status['{}'.format(i+1)] = 'class=text-success'#'bgcolor=#00cc00'
137 color_status['{}'.format(i+1)] = 'class=text-success'#'bgcolor=#00cc00'
138 elif status == '2':
138 elif status == '2':
139 all_status['{}'.format(i+1)] = 1
139 all_status['{}'.format(i+1)] = 1
140 color_status['{}'.format(i+1)] = 'class=text-info'
140 color_status['{}'.format(i+1)] = 'class=text-info'
141 elif status == '1': #Connected background-color: #ee902c;
141 elif status == '1': #Connected background-color: #ee902c;
142 all_status['{}'.format(i+1)] = 1
142 all_status['{}'.format(i+1)] = 1
143 color_status['{}'.format(i+1)] = 'class=text-warning'#'bgcolor=#ee902c'
143 color_status['{}'.format(i+1)] = 'class=text-warning'#'bgcolor=#ee902c'
144 else: #Disconnected background-color: #ff0000;
144 else: #Disconnected background-color: #ff0000;
145 all_status['{}'.format(i+1)] = 0
145 all_status['{}'.format(i+1)] = 0
146 color_status['{}'.format(i+1)] = 'class=text-danger'#'bgcolor=#FF0000'
146 color_status['{}'.format(i+1)] = 'class=text-danger'#'bgcolor=#FF0000'
147 #------------------------------------------------
147 #------------------------------------------------
148
148
149 kwargs = {}
149 kwargs = {}
150 kwargs['connected_modules'] = str(conf.connected_modules())+'/64'
150 kwargs['connected_modules'] = str(conf.connected_modules())+'/64'
151 kwargs['dev_conf'] = conf
151 kwargs['dev_conf'] = conf
152
152
153 if conf.operation_mode == 0:
153 if conf.operation_mode == 0:
154 kwargs['dev_conf_keys'] = ['label', 'operation_mode']
154 kwargs['dev_conf_keys'] = ['label', 'operation_mode']
155 else:
155 else:
156 kwargs['dev_conf_keys'] = ['label', 'operation_mode', 'operation_value']
156 kwargs['dev_conf_keys'] = ['label', 'operation_mode', 'operation_value']
157
157
158 kwargs['title'] = 'ABS Configuration'
158 kwargs['title'] = 'ABS Configuration'
159 kwargs['suptitle'] = 'Details'
159 kwargs['suptitle'] = 'Details'
160 kwargs['button'] = 'Edit Configuration'
160 kwargs['button'] = 'Edit Configuration'
161
161
162 if conf.active_beam != 0:
162 if conf.active_beam != 0:
163 kwargs['active_beam'] = int(conf.active_beam)
163 kwargs['active_beam'] = int(conf.active_beam)
164
164
165 kwargs['beams'] = beams
165 kwargs['beams'] = beams
166 kwargs['modules_status'] = all_status
166 kwargs['modules_status'] = all_status
167 kwargs['color_status'] = color_status
167 kwargs['color_status'] = color_status
168 kwargs['module_messages'] = module_messages
168 kwargs['module_messages'] = module_messages
169 ###### SIDEBAR ######
169 ###### SIDEBAR ######
170 kwargs.update(sidebar(conf=conf))
170 kwargs.update(sidebar(conf=conf))
171
171
172 return render(request, 'abs_conf.html', kwargs)
172 return render(request, 'abs_conf.html', kwargs)
173
173
174
174
175 def abs_conf_edit(request, id_conf):
175 def abs_conf_edit(request, id_conf):
176
176
177 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
177 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
178
178
179 beams = ABSBeam.objects.filter(abs_conf=conf)
179 beams = ABSBeam.objects.filter(abs_conf=conf)
180
180
181 if request.method=='GET':
181 if request.method=='GET':
182 form = ABSConfigurationForm(instance=conf)
182 form = ABSConfigurationForm(instance=conf)
183
183
184 if request.method=='POST':
184 if request.method=='POST':
185 form = ABSConfigurationForm(request.POST, instance=conf)
185 form = ABSConfigurationForm(request.POST, instance=conf)
186
186
187 if form.is_valid():
187 if form.is_valid():
188 conf = form.save(commit=False)
188 conf = form.save(commit=False)
189 conf.save()
189 conf.save()
190 return redirect('url_abs_conf', id_conf=conf.id)
190 return redirect('url_abs_conf', id_conf=conf.id)
191
191
192 ###### SIDEBAR ######
192 ###### SIDEBAR ######
193 kwargs = {}
193 kwargs = {}
194
194
195 kwargs['dev_conf'] = conf
195 kwargs['dev_conf'] = conf
196 #kwargs['id_dev'] = conf.id
196 #kwargs['id_dev'] = conf.id
197 kwargs['id_conf'] = conf.id
197 kwargs['id_conf'] = conf.id
198 kwargs['form'] = form
198 kwargs['form'] = form
199 kwargs['abs_beams'] = beams
199 kwargs['abs_beams'] = beams
200 kwargs['title'] = 'Device Configuration'
200 kwargs['title'] = 'Device Configuration'
201 kwargs['suptitle'] = 'Edit'
201 kwargs['suptitle'] = 'Edit'
202 kwargs['button'] = 'Save'
202 kwargs['button'] = 'Save'
203
203
204 kwargs['edit'] = True
204 kwargs['edit'] = True
205
205
206 return render(request, 'abs_conf_edit.html', kwargs)
206 return render(request, 'abs_conf_edit.html', kwargs)
207
207
208 @csrf_exempt
208 @csrf_exempt
209 def abs_conf_alert(request):
209 def abs_conf_alert(request):
210
210
211 if request.method == 'POST':
211 if request.method == 'POST':
212 print (request.POST)
212 print (request.POST)
213 return HttpResponse(json.dumps({'result':1}), content_type='application/json')
213 return HttpResponse(json.dumps({'result':1}), content_type='application/json')
214 else:
214 else:
215 return redirect('index')
215 return redirect('index')
216
216
217
217
218 def import_file(request, id_conf):
218 def import_file(request, id_conf):
219
219
220 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
220 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
221 if request.method=='POST':
221 if request.method=='POST':
222 form = ABSImportForm(request.POST, request.FILES)
222 form = ABSImportForm(request.POST, request.FILES)
223 if form.is_valid():
223 if form.is_valid():
224 try:
224 try:
225 parms = conf.import_from_file(request.FILES['file_name'])
225 parms = conf.import_from_file(request.FILES['file_name'])
226
226
227 if parms:
227 if parms:
228 conf.update_from_file(parms)
228 conf.update_from_file(parms)
229 messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
229 messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name'])
230 return redirect(conf.get_absolute_url_edit())
230 return redirect(conf.get_absolute_url_edit())
231
231
232 except Exception as e:
232 except Exception as e:
233 messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e))
233 messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], e))
234
234
235 else:
235 else:
236 messages.warning(request, 'Your current configuration will be replaced')
236 messages.warning(request, 'Your current configuration will be replaced')
237 form = ABSImportForm()
237 form = ABSImportForm()
238
238
239 kwargs = {}
239 kwargs = {}
240 kwargs['form'] = form
240 kwargs['form'] = form
241 kwargs['title'] = 'ABS Configuration'
241 kwargs['title'] = 'ABS Configuration'
242 kwargs['suptitle'] = 'Import file'
242 kwargs['suptitle'] = 'Import file'
243 kwargs['button'] = 'Upload'
243 kwargs['button'] = 'Upload'
244 kwargs['previous'] = conf.get_absolute_url()
244 kwargs['previous'] = conf.get_absolute_url()
245
245
246 return render(request, 'abs_import.html', kwargs)
246 return render(request, 'abs_import.html', kwargs)
247
247
248
248
249 def start(request, id_conf, id_beam):
249 def start(request, id_conf, id_beam):
250
250
251 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
251 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
252 beams_list = ABSBeam.objects.filter(abs_conf=conf)
252 beams_list = ABSBeam.objects.filter(abs_conf=conf)
253 if id_beam>=len(beams_list):
253 if id_beam>=len(beams_list):
254 return HttpResponse(json.dumps({'result':0}), content_type='application/json')
254 return HttpResponse(json.dumps({'result':0}), content_type='application/json')
255 conf.start_device(id_beam)
255 conf.start_device(id_beam)
256
256
257 for i, beam in enumerate(beams_list):
257 for i, beam in enumerate(beams_list):
258 if i==int(id_beam):
258 if i==int(id_beam):
259 conf.active_beam = beam.pk
259 conf.active_beam = beam.pk
260 conf.save()
260 break
261 break
261
262
262 return HttpResponse(json.dumps({'result':1}), content_type='application/json')
263 return HttpResponse(json.dumps({'result':1}), content_type='application/json')
263
264
264
265
265 def send_beam(request, id_conf, id_beam):
266 def send_beam(request, id_conf, id_beam):
266
267
267 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
268 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
268
269
269 abs = Configuration.objects.filter(pk=conf.device.conf_active).first()
270 abs = Configuration.objects.filter(pk=conf.device.conf_active).first()
270 if abs!=conf:
271 if abs!=conf:
271 url = '#' if abs is None else abs.get_absolute_url()
272 url = '#' if abs is None else abs.get_absolute_url()
272 label = 'None' if abs is None else abs.label
273 label = 'None' if abs is None else abs.label
273 messages.warning(
274 messages.warning(
274 request,
275 request,
275 mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format(
276 mark_safe('The current configuration has not been written in the modules, the active configuration is <a href="{}">{}</a>'.format(
276 url,
277 url,
277 label
278 label
278 ))
279 ))
279 )
280 )
280 return redirect(conf.get_absolute_url())
281 return redirect(conf.get_absolute_url())
281
282
282 beam = get_object_or_404(ABSBeam, pk=id_beam)
283 beam = get_object_or_404(ABSBeam, pk=id_beam)
283
284
284 if request.method == 'POST':
285 if request.method == 'POST':
285
286
286 beams_list = ABSBeam.objects.filter(abs_conf=conf)
287 beams_list = ABSBeam.objects.filter(abs_conf=conf)
287 conf.active_beam = id_beam
288 conf.active_beam = id_beam
288
289
289 i = 0
290 i = 0
290 for b in beams_list:
291 for b in beams_list:
291 if b.id == int(id_beam):
292 if b.id == int(id_beam):
292 break
293 break
293 else:
294 else:
294 i += 1
295 i += 1
295 beam_pos = i + 1 #Estandarizar
296 beam_pos = i + 1 #Estandarizar
296 print ('%s Position: %s') % (beam.name, str(beam_pos))
297 print ('%s Position: %s') % (beam.name, str(beam_pos))
297 conf.send_beam(beam_pos)
298 conf.send_beam(beam_pos)
298
299
299 return redirect('url_abs_conf', conf.id)
300 return redirect('url_abs_conf', conf.id)
300
301
301 kwargs = {
302 kwargs = {
302 'title': 'ABS',
303 'title': 'ABS',
303 'suptitle': conf.label,
304 'suptitle': conf.label,
304 'message': 'Are you sure you want to change ABS Beam to: {}?'.format(beam.name),
305 'message': 'Are you sure you want to change ABS Beam to: {}?'.format(beam.name),
305 'delete': False
306 'delete': False
306 }
307 }
307 kwargs['menu_configurations'] = 'active'
308 kwargs['menu_configurations'] = 'active'
308
309
309 return render(request, 'confirm.html', kwargs)
310 return render(request, 'confirm.html', kwargs)
310
311
311
312
312 def add_beam(request, id_conf):
313 def add_beam(request, id_conf):
313
314
314 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
315 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
315 confs = Configuration.objects.all()
316 confs = Configuration.objects.all()
316
317
317 if request.method=='GET':
318 if request.method=='GET':
318 form = ABSBeamAddForm()
319 form = ABSBeamAddForm()
319
320
320 if request.method=='POST':
321 if request.method=='POST':
321 form = ABSBeamAddForm(request.POST)
322 form = ABSBeamAddForm(request.POST)
322
323
323 beam_data = get_values_from_form(request.POST)
324 beam_data = get_values_from_form(request.POST)
324
325
325 new_beam = ABSBeam(
326 new_beam = ABSBeam(
326 name = beam_data['name'],
327 name = beam_data['name'],
327 antenna = json.dumps(beam_data['antenna']),
328 antenna = json.dumps(beam_data['antenna']),
328 abs_conf = conf,
329 abs_conf = conf,
329 tx = json.dumps(beam_data['tx']),
330 tx = json.dumps(beam_data['tx']),
330 rx = json.dumps(beam_data['rx']),
331 rx = json.dumps(beam_data['rx']),
331 ues = json.dumps(beam_data['ues']),
332 ues = json.dumps(beam_data['ues']),
332 only_rx = json.dumps(beam_data['only_rx'])
333 only_rx = json.dumps(beam_data['only_rx'])
333 )
334 )
334 new_beam.save()
335 new_beam.save()
335 messages.success(request, 'Beam: "%s" has been added.' % new_beam.name)
336 messages.success(request, 'Beam: "%s" has been added.' % new_beam.name)
336
337
337 return redirect('url_edit_abs_conf', conf.id)
338 return redirect('url_edit_abs_conf', conf.id)
338
339
339 ###### SIDEBAR ######
340 ###### SIDEBAR ######
340 kwargs = {}
341 kwargs = {}
341
342
342 #kwargs['dev_conf'] = conf.device
343 #kwargs['dev_conf'] = conf.device
343 #kwargs['id_dev'] = conf.device
344 #kwargs['id_dev'] = conf.device
344 #kwargs['previous'] = conf.get_absolute_url_edit()
345 #kwargs['previous'] = conf.get_absolute_url_edit()
345 kwargs['id_conf'] = conf.id
346 kwargs['id_conf'] = conf.id
346 kwargs['form'] = form
347 kwargs['form'] = form
347 kwargs['title'] = 'ABS Beams'
348 kwargs['title'] = 'ABS Beams'
348 kwargs['suptitle'] = 'Add Beam'
349 kwargs['suptitle'] = 'Add Beam'
349 kwargs['button'] = 'Add'
350 kwargs['button'] = 'Add'
350 kwargs['no_sidebar'] = True
351 kwargs['no_sidebar'] = True
351 kwargs['edit'] = True
352 kwargs['edit'] = True
352
353
353 return render(request, 'abs_add_beam.html', kwargs)
354 return render(request, 'abs_add_beam.html', kwargs)
354
355
355
356
356 def edit_beam(request, id_conf, id_beam):
357 def edit_beam(request, id_conf, id_beam):
357
358
358 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
359 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
359 beam = get_object_or_404(ABSBeam, pk=id_beam)
360 beam = get_object_or_404(ABSBeam, pk=id_beam)
360
361
361 if request.method=='GET':
362 if request.method=='GET':
362 form = ABSBeamEditForm(initial={'beam': beam})
363 form = ABSBeamEditForm(initial={'beam': beam})
363
364
364 if request.method=='POST':
365 if request.method=='POST':
365 form = ABSBeamEditForm(request.POST)
366 form = ABSBeamEditForm(request.POST)
366
367
367 beam_data = get_values_from_form(request.POST)
368 beam_data = get_values_from_form(request.POST)
368
369
369 beam.dict_to_parms(beam_data)
370 beam.dict_to_parms(beam_data)
370 beam.save()
371 beam.save()
371
372
372 messages.success(request, 'Beam: "%s" has been updated.' % beam.name)
373 messages.success(request, 'Beam: "%s" has been updated.' % beam.name)
373
374
374 return redirect('url_edit_abs_conf', conf.id)
375 return redirect('url_edit_abs_conf', conf.id)
375
376
376 ###### SIDEBAR ######
377 ###### SIDEBAR ######
377 kwargs = {}
378 kwargs = {}
378
379
379 kwargs['id_conf'] = conf.id
380 kwargs['id_conf'] = conf.id
380 kwargs['form'] = form
381 kwargs['form'] = form
381 kwargs['title'] = 'ABS Beams'
382 kwargs['title'] = 'ABS Beams'
382 kwargs['suptitle'] = 'Edit Beam'
383 kwargs['suptitle'] = 'Edit Beam'
383 kwargs['button'] = 'Save'
384 kwargs['button'] = 'Save'
384 kwargs['no_sidebar'] = True
385 kwargs['no_sidebar'] = True
385
386
386 #kwargs['previous'] = conf.get_absolute_url_edit()
387 #kwargs['previous'] = conf.get_absolute_url_edit()
387 kwargs['edit'] = True
388 kwargs['edit'] = True
388
389
389 return render(request, 'abs_edit_beam.html', kwargs)
390 return render(request, 'abs_edit_beam.html', kwargs)
390
391
391
392
392
393
393 def remove_beam(request, id_conf, id_beam):
394 def remove_beam(request, id_conf, id_beam):
394
395
395 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
396 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
396 beam = get_object_or_404(ABSBeam, pk=id_beam)
397 beam = get_object_or_404(ABSBeam, pk=id_beam)
397
398
398 if request.method=='POST':
399 if request.method=='POST':
399 if beam:
400 if beam:
400 try:
401 try:
401 beam.delete()
402 beam.delete()
402 messages.success(request, 'Beam: "%s" has been deleted.' % beam)
403 messages.success(request, 'Beam: "%s" has been deleted.' % beam)
403 except:
404 except:
404 messages.error(request, 'Unable to delete beam: "%s".' % beam)
405 messages.error(request, 'Unable to delete beam: "%s".' % beam)
405
406
406 return redirect('url_edit_abs_conf', conf.id)
407 return redirect('url_edit_abs_conf', conf.id)
407
408
408 ###### SIDEBAR ######
409 ###### SIDEBAR ######
409 kwargs = {}
410 kwargs = {}
410
411
411 kwargs['object'] = beam
412 kwargs['object'] = beam
412 kwargs['delete'] = True
413 kwargs['delete'] = True
413 kwargs['title'] = 'Delete'
414 kwargs['title'] = 'Delete'
414 kwargs['suptitle'] = 'Beam'
415 kwargs['suptitle'] = 'Beam'
415 kwargs['previous'] = conf.get_absolute_url_edit()
416 kwargs['previous'] = conf.get_absolute_url_edit()
416 return render(request, 'confirm.html', kwargs)
417 return render(request, 'confirm.html', kwargs)
417
418
418
419
419
420
420 def plot_patterns(request, id_conf, id_beam=None):
421 def plot_patterns(request, id_conf, id_beam=None):
421
422
422 kwargs = {}
423 kwargs = {}
423 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
424 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
424 beams = ABSBeam.objects.filter(abs_conf=conf)
425 beams = ABSBeam.objects.filter(abs_conf=conf)
425
426
426 if id_beam:
427 if id_beam:
427 beam = get_object_or_404(ABSBeam, pk=id_beam)
428 beam = get_object_or_404(ABSBeam, pk=id_beam)
428 kwargs['beam'] = beam
429 kwargs['beam'] = beam
429
430
430 ###### SIDEBAR ######
431 ###### SIDEBAR ######
431
432
432 kwargs['dev_conf'] = conf.device
433 kwargs['dev_conf'] = conf.device
433 kwargs['id_dev'] = conf.device
434 kwargs['id_dev'] = conf.device
434 kwargs['id_conf'] = conf.id
435 kwargs['id_conf'] = conf.id
435 kwargs['abs_beams'] = beams
436 kwargs['abs_beams'] = beams
436 kwargs['title'] = 'ABS Patterns'
437 kwargs['title'] = 'ABS Patterns'
437 kwargs['suptitle'] = conf.name
438 kwargs['suptitle'] = conf.name
438 kwargs['no_sidebar'] = True
439 kwargs['no_sidebar'] = True
439
440
440 return render(request, 'abs_patterns.html', kwargs)
441 return render(request, 'abs_patterns.html', kwargs)
441
442
442
443
443 def plot_pattern(request, id_conf, id_beam, antenna):
444 def plot_pattern(request, id_conf, id_beam, antenna):
444
445
445 if antenna=='down':
446 if antenna=='down':
446 sleep(3)
447 sleep(3)
447
448
448 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
449 conf = get_object_or_404(ABSConfiguration, pk=id_conf)
449 beam = get_object_or_404(ABSBeam, pk=id_beam)
450 beam = get_object_or_404(ABSBeam, pk=id_beam)
450 just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0
451 just_rx = 1 if json.loads(beam.only_rx)[antenna] else 0
451 phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)]
452 phases = json.loads(beam.antenna)['antenna_{}'.format(antenna)]
452 gain_tx = json.loads(beam.tx)[antenna]
453 gain_tx = json.loads(beam.tx)[antenna]
453 gain_rx = json.loads(beam.rx)[antenna]
454 gain_rx = json.loads(beam.rx)[antenna]
454 ues = json.loads(beam.ues)[antenna]
455 ues = json.loads(beam.ues)[antenna]
455 newOverJro = overJroShow(beam.name)
456 newOverJro = overJroShow(beam.name)
456 fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx)
457 fig = newOverJro.plotPattern2(datetime.today(), phases, gain_tx, gain_rx, ues, just_rx)
457 buf = io.BytesIO()
458 buf = io.BytesIO()
458 fig.savefig(buf, format='png')
459 fig.savefig(buf, format='png')
459 response = HttpResponse(buf.getvalue(), content_type='image/png')
460 response = HttpResponse(buf.getvalue(), content_type='image/png')
460 return response
461 return response
461
462
@@ -1,151 +1,156
1 '''
1 '''
2 Created on Dec 2, 2014
2 Created on Dec 2, 2014
3
3
4 @author: Miguel Urco
4 @author: Miguel Urco
5
5
6 eth_device decorator is used to implement an api to ethernet devices.
6 eth_device decorator is used to implement an api to ethernet devices.
7 When eth_device decorator is used it adds two parameters to any function (ip and port)
7 When eth_device decorator is used it adds two parameters to any function (ip and port)
8
8
9 #Definition
9 #Definition
10
10
11 @eth_device
11 @eth_device
12 def enable_rf()
12 def enable_rf()
13 cmd = "xxxxx"
13 cmd = "xxxxx"
14 payload = "xxxxxx"
14 payload = "xxxxxx"
15
15
16 return cmd, payload
16 return cmd, payload
17
17
18 #How to call this function:
18 #How to call this function:
19 answer = enable_rf(ip, port)
19 answer = enable_rf(ip, port)
20
20
21 '''
21 '''
22 import json
22 import json
23 from devices.dds import data
23 from devices.dds import data
24
24
25 from devices.jro_device import eth_device, IdClass
25 from devices.jro_device import eth_device, eth_devices, IdClass
26
26
27 ID_CLASS = IdClass["dds"]
27 ID_CLASS = IdClass["dds"]
28
28
29 CMD_RESET =0X01
29 CMD_RESET =0X01
30 CMD_ENABLE =0X02
30 CMD_ENABLE =0X02
31 CMD_CHANGEIP =0X03
31 CMD_CHANGEIP =0X03
32 CMD_STATUS =0X04
32 CMD_STATUS =0X04
33 CMD_ECHO =0XFE
33 CMD_ECHO =0XFE
34
34
35 DDS_CMD_RESET =0X10
35 DDS_CMD_RESET =0X10
36 DDS_CMD_ENABLE_RF =0x11
36 DDS_CMD_ENABLE_RF =0x11
37 # DDS_CMD_MULTIPLIER =0X12
37 # DDS_CMD_MULTIPLIER =0X12
38 # DDS_CMD_MODE =0x13
38 # DDS_CMD_MODE =0x13
39 # DDS_CMD_FREQUENCY_A =0X14
39 # DDS_CMD_FREQUENCY_A =0X14
40 # DDS_CMD_FREQUENCY_B =0x15
40 # DDS_CMD_FREQUENCY_B =0x15
41 # DDS_CMD_PHASE_A =0X16
41 # DDS_CMD_PHASE_A =0X16
42 # DDS_CMD_PHASE_B =0x17
42 # DDS_CMD_PHASE_B =0x17
43 # DDS_CMD_AMPLITUDE_1 =0X19 #Se han invertido la posicion de los canales
43 # DDS_CMD_AMPLITUDE_1 =0X19 #Se han invertido la posicion de los canales
44 # DDS_CMD_AMPLITUDE_2 =0x18 #en el PCB
44 # DDS_CMD_AMPLITUDE_2 =0x18 #en el PCB
45
45
46 DDS_CMD_WRITE =0x50
46 DDS_CMD_WRITE =0x50
47 DDS_CMD_READ =0x8000
47 DDS_CMD_READ =0x8000
48
48
49 @eth_device(ID_CLASS)
49 @eth_device(ID_CLASS)
50 def reset():
50 def reset():
51
51
52 cmd = CMD_RESET
52 cmd = CMD_RESET
53 payload = ""
53 payload = ""
54
54
55 return cmd, payload
55 return cmd, payload
56
56
57 @eth_device(ID_CLASS)
57 @eth_devices(ID_CLASS)
58 def change_ip(new_ip, mask="255.255.255.0", gateway="0.0.0.0"):
58 def change_ip(new_ip, mask="255.255.255.0", gateway="0.0.0.0"):
59
59
60 cmd = CMD_CHANGEIP
60 cmd = CMD_CHANGEIP
61 payload = new_ip + '/' + mask + '/' + gateway
61 payload = b''
62 payload += new_ip.encode()
63 payload += b'/'
64 payload += mask.encode()
65 payload += b'/'
66 payload += gateway.encode()
62
67
63 return cmd, payload
68 return cmd, payload
64
69
65 @eth_device(ID_CLASS)
70 @eth_device(ID_CLASS)
66 def status():
71 def status():
67
72
68 cmd = CMD_STATUS
73 cmd = CMD_STATUS
69 payload = b''
74 payload = b''
70 return cmd, payload
75 return cmd, payload
71
76
72 @eth_device(ID_CLASS)
77 @eth_device(ID_CLASS)
73 def echo():
78 def echo():
74
79
75 cmd = CMD_ECHO
80 cmd = CMD_ECHO
76 payload = ""
81 payload = ""
77
82
78 return cmd, payload
83 return cmd, payload
79
84
80 @eth_device(ID_CLASS)
85 @eth_device(ID_CLASS)
81 def enable_rf():
86 def enable_rf():
82
87
83 cmd = DDS_CMD_ENABLE_RF
88 cmd = DDS_CMD_ENABLE_RF
84 payload = chr(0x01).encode()
89 payload = chr(0x01).encode()
85
90
86 return cmd, payload
91 return cmd, payload
87
92
88 @eth_device(ID_CLASS)
93 @eth_device(ID_CLASS)
89 def disable_rf():
94 def disable_rf():
90
95
91 cmd = DDS_CMD_ENABLE_RF
96 cmd = DDS_CMD_ENABLE_RF
92 payload = chr(0x00).encode()
97 payload = chr(0x00).encode()
93
98
94 return cmd, payload
99 return cmd, payload
95
100
96 @eth_device(ID_CLASS)
101 @eth_device(ID_CLASS)
97 def read_all_device():
102 def read_all_device():
98
103
99 payload = ""
104 payload = ""
100
105
101 return DDS_CMD_READ, payload
106 return DDS_CMD_READ, payload
102
107
103 @eth_device(ID_CLASS)
108 @eth_device(ID_CLASS)
104 def write_all_device(payload):
109 def write_all_device(payload):
105
110
106 return DDS_CMD_WRITE, payload
111 return DDS_CMD_WRITE, payload
107
112
108 def read_config(ip, port):
113 def read_config(ip, port):
109 """
114 """
110 Output:
115 Output:
111 parms : Dictionary with keys
116 parms : Dictionary with keys
112 multiplier :
117 multiplier :
113 frequencyA :
118 frequencyA :
114 frequencyB :
119 frequencyB :
115 frequencyA_Mhz :
120 frequencyA_Mhz :
116 frequencyB_Mhz :
121 frequencyB_Mhz :
117 modulation :
122 modulation :
118 phaseA_degrees :
123 phaseA_degrees :
119 phaseB_degrees :
124 phaseB_degrees :
120 amplitudeI :
125 amplitudeI :
121 amplitudeQ :
126 amplitudeQ :
122
127
123 """
128 """
124 payload = read_all_device(ip, port)
129 payload = read_all_device(ip, port)
125
130
126 return data.dds_str_to_dict(payload)
131 return data.dds_str_to_dict(payload)
127
132
128 def write_config(ip, port, parms):
133 def write_config(ip, port, parms):
129 """
134 """
130 Input:
135 Input:
131 ip :
136 ip :
132 port :
137 port :
133 parms : Dictionary with keys
138 parms : Dictionary with keys
134 multiplier : 4 to 20
139 multiplier : 4 to 20
135 frequencyA : 0 to (2**48-1) equivalent to: 0 - "Master clock"
140 frequencyA : 0 to (2**48-1) equivalent to: 0 - "Master clock"
136 frequencyB : 0 to (2**48-1) equivalent to: 0 - "Master clock"
141 frequencyB : 0 to (2**48-1) equivalent to: 0 - "Master clock"
137 modulation : 0 to 3
142 modulation : 0 to 3
138 phaseA_degrees : 0 - 360 degrees
143 phaseA_degrees : 0 - 360 degrees
139 phaseB_degrees : 0 - 360 degrees
144 phaseB_degrees : 0 - 360 degrees
140 amplitudeI : 0 to (2**12-1) equivalent to: 0 - 100%
145 amplitudeI : 0 to (2**12-1) equivalent to: 0 - 100%
141 amplitudeQ : 0 to (2**12-1) equivalent to: 0 - 100%
146 amplitudeQ : 0 to (2**12-1) equivalent to: 0 - 100%
142
147
143 """
148 """
144
149
145 payload = data.dict_to_dds_str(parms)
150 payload = data.dict_to_dds_str(parms)
146 answer = write_all_device(ip, port, payload)
151 answer = write_all_device(ip, port, payload)
147 return answer
152 return answer
148
153
149 if __name__ == '__main__':
154 if __name__ == '__main__':
150
155
151 print(read_config("127.0.0.1", 2000))
156 print(read_config("127.0.0.1", 2000))
@@ -1,414 +1,429
1 '''
1 '''
2 Created on Dec 2, 2014
2 Created on Dec 2, 2014
3
3
4 @author: Miguel Urco
4 @author: Miguel Urco
5 '''
5 '''
6 import time
6 import time
7 import struct
7 import struct
8 import socket
8 import socket
9
9
10 DEBUG = False
10 DEBUG = False
11 CMD_RESET =0X01
11 CMD_RESET =0X01
12 CMD_ENABLE =0X02
12 CMD_ENABLE =0X02
13 CMD_CHANGEIP =0X03
13 CMD_CHANGEIP =0X03
14
14
15 IdClass={
15 IdClass={
16 "rc" : 0x01,
16 "rc" : 0x01,
17 "dds" : 0x02,
17 "dds" : 0x02,
18 "jars" : 0x03,
18 "jars" : 0x03,
19 "usrp" : 0x04,
19 "usrp" : 0x04,
20 "echotek" : 0x05,
20 "echotek" : 0x05,
21 "abs" : 0x06,
21 "abs" : 0x06,
22 "clk_gen" : 0x07
22 "clk_gen" : 0x07
23 }
23 }
24
24
25 def ascii2hex(cadena):
25 def ascii2hex(cadena):
26
26
27 hex_cad = ''
27 hex_cad = ''
28 for c in cadena:
28 for c in cadena:
29 hex_cad += hex(ord(c))[2:].rjust(2,'0') + ' '
29 hex_cad += hex(ord(c))[2:].rjust(2,'0') + ' '
30
30
31 return hex_cad
31 return hex_cad
32
32
33 def ping(host):
33 def ping(host):
34 """
34 """
35 Returns True if host responds to a ping request
35 Returns True if host responds to a ping request
36 """
36 """
37 import os, platform
37 import os, platform
38
38
39 # Ping parameters as function of OS
39 # Ping parameters as function of OS
40 ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1"
40 ping_str = "-n 1" if platform.system().lower()=="windows" else "-c 1"
41
41
42 # Ping
42 # Ping
43 return os.system("ping " + ping_str + " " + host) == 0
43 return os.system("ping " + ping_str + " " + host) == 0
44
44
45 class IPData(object):
45 class IPData(object):
46
46
47 '''
47 '''
48 Clase para manejar la trama de datos provenientes del/hacia un dispositivo Ethernet.
48 Clase para manejar la trama de datos provenientes del/hacia un dispositivo Ethernet.
49 La trama de datos es la siguiente:
49 La trama de datos es la siguiente:
50
50
51 **********************************
51 **********************************
52 ** FORMATO GENERAL DE UNA TRAMA **
52 ** FORMATO GENERAL DE UNA TRAMA **
53 **********************************
53 **********************************
54
54
55 1. Cabecera (5 bytes): Secuencia Fija que deber ser "$JRO$"
55 1. Cabecera (5 bytes): Secuencia Fija que deber ser "$JRO$"
56 2. Longitud (3 bytes): Cantidad de bytes de la Data, contados desde IdClass hasta el Xor
56 2. Longitud (3 bytes): Cantidad de bytes de la Data, contados desde IdClass hasta el Xor
57 3. Id class (1 byte) : Clase de dispositivo a configurar. Por defecto 0
57 3. Id class (1 byte) : Clase de dispositivo a configurar. Por defecto 0
58 4. Id device (1 byte): Identificar del dispositivo a configurar. Por defecto 0
58 4. Id device (1 byte): Identificar del dispositivo a configurar. Por defecto 0
59 5. Cmd (2 bytes): Identificador del comando a ejecutarse.
59 5. Cmd (2 bytes): Identificador del comando a ejecutarse.
60 3. Payload (n bytes): Carga Util conteniendo secuencia,comandos y parametros
60 3. Payload (n bytes): Carga Util conteniendo secuencia,comandos y parametros
61 4. Xor (1 byte): Byte de revision de consistencia de la data al aplicar Xor a todos los bytes,
61 4. Xor (1 byte): Byte de revision de consistencia de la data al aplicar Xor a todos los bytes,
62 desde la longitud hasta el payload.
62 desde la longitud hasta el payload.
63
63
64 '''
64 '''
65 __HEADER = "$JRO$"
65 __HEADER = "$JRO$"
66
66
67 def __init__(self, ip, port, id_class=0, id_dev=0):
67 def __init__(self, ip, port, id_class=0, id_dev=0):
68 '''
68 '''
69 '''
69 '''
70 self.id_class = id_class
70 self.id_class = id_class
71 self.id_dev = id_dev
71 self.id_dev = id_dev
72
72
73 self.address = (str(ip), int(port))
73 self.address = (str(ip), int(port))
74
74
75 self.__iniVariables()
75 self.__iniVariables()
76
76
77 def __iniVariables(self):
77 def __iniVariables(self):
78
78
79 self.tx_buffer = None
79 self.tx_buffer = None
80 self.rx_buffer = None
80 self.rx_buffer = None
81
81
82 #self.header = None
82 #self.header = None
83 self.len = None
83 self.len = None
84 self.cmd = None
84 self.cmd = None
85 self.payload = None
85 self.payload = None
86
86
87 self.invalid = True
87 self.invalid = True
88 self.errormsg = ''
88 self.errormsg = ''
89 self.hasPayload = False
89 self.hasPayload = False
90
90
91 def __getXor(self, cadena):
91 def __getXor(self, cadena):
92 '''
92 '''
93 '''
93 '''
94 #trama = '%03d' %lenght + ipPayload
94 #trama = '%03d' %lenght + ipPayload
95 xor = 0
95 xor = 0
96 for character in cadena:
96 for character in cadena:
97 xor = xor ^ ord(character)
97 xor = xor ^ ord(character)
98
98
99 # xor_hex = hex(xor)
99 # xor_hex = hex(xor)
100 # xor_hex = xor_hex[2:]
100 # xor_hex = xor_hex[2:]
101 # xor_hex = xor_hex.rjust(2,'0')
101 # xor_hex = xor_hex.rjust(2,'0')
102
102
103 return xor
103 return xor
104
104
105 def __verifyXor(self, cadena):
105 def __verifyXor(self, cadena):
106
106
107 xor = self.__getXor(cadena)
107 xor = self.__getXor(cadena)
108
108
109 if xor != 0:
109 if xor != 0:
110 return 0
110 return 0
111
111
112 return 1
112 return 1
113
113
114 def __encoder(self, cmd, payload):
114 def __encoder(self, cmd, payload):
115 '''
115 '''
116 Inputs:
116 Inputs:
117 cmd : Entero que indica el tipo de comando
117 cmd : Entero que indica el tipo de comando
118 payload : Cadena de caracteres con informacion, depende del comando
118 payload : Cadena de caracteres con informacion, depende del comando
119
119
120 '''
120 '''
121
121
122 #seq = '%04d' %(sequence)
122 #seq = '%04d' %(sequence)
123 #conf = '%04d' %(confcode)
123 #conf = '%04d' %(confcode)
124
124
125 #Number to Cad: 2 Bytes <> H, 4 Bytes <> I
125 #Number to Cad: 2 Bytes <> H, 4 Bytes <> I
126 cmd_cad = struct.pack(">H", cmd)
126 cmd_cad = struct.pack(">H", cmd)
127
127
128 data = chr(self.id_class).encode() + chr(self.id_dev).encode() + cmd_cad + payload
128 data = chr(self.id_class).encode() + chr(self.id_dev).encode() + cmd_cad + payload
129
129
130 len_data = len(data) + 1 # + xor
130 len_data = len(data) + 1 # + xor
131 len_cad = struct.pack('>I', len_data)
131 len_cad = struct.pack('>I', len_data)
132
132
133 lenAndData = len_cad + chr(self.id_class).encode() + chr(self.id_dev).encode() + cmd_cad + payload
133 lenAndData = len_cad + chr(self.id_class).encode() + chr(self.id_dev).encode() + cmd_cad + payload
134
134
135 lenAndData = list(lenAndData)
135 lenAndData = list(lenAndData)
136
136
137 new = ''
137 new = ''
138 for u in lenAndData:
138 for u in lenAndData:
139 new += chr(u)
139 new += chr(u)
140
140
141 xor = self.__getXor(new)
141 xor = self.__getXor(new)
142
142
143 trama = self.__HEADER + new + chr(xor)
143 trama = self.__HEADER + new + chr(xor)
144
144
145 self.tx_buffer = trama
145 self.tx_buffer = trama
146
146
147 return trama
147 return trama
148
148
149 def __decoder(self, rx_buffer):
149 def __decoder(self, rx_buffer):
150 '''
150 '''
151 Evalua la trama y la separa en los campos correspondientes
151 Evalua la trama y la separa en los campos correspondientes
152
152
153 4Bytes | 4Bytes | 1Byte | 1 Byte | 2 Bytes | n Bytes | 1 Byte
153 4Bytes | 4Bytes | 1Byte | 1 Byte | 2 Bytes | n Bytes | 1 Byte
154 Header | Len | Id class | Id dev | Cmd | Payload | Xor
154 Header | Len | Id class | Id dev | Cmd | Payload | Xor
155
155
156 '''
156 '''
157 self.invalid = True
157 self.invalid = True
158 self.hasPayload = False
158 self.hasPayload = False
159 self.rx_buffer = rx_buffer
159 self.rx_buffer = rx_buffer
160
160
161 try:
161 try:
162 index = rx_buffer.find(self.__HEADER)
162 index = rx_buffer.find(self.__HEADER)
163 except:
163 except:
164 self.errormsg = "rx_buffer is not a string"
164 self.errormsg = "rx_buffer is not a string"
165 return 0
165 return 0
166
166
167 if index == -1:
167 if index == -1:
168 self.errormsg = "No header found: %s" %ascii2hex(rx_buffer)
168 self.errormsg = "No header found: %s" %ascii2hex(rx_buffer)
169 return 0
169 return 0
170
170
171 #print('???????', flush=True)
171 #print('???????', flush=True)
172
172
173 rx_buffer = rx_buffer[index + len(self.__HEADER):]
173 rx_buffer = rx_buffer[index + len(self.__HEADER):]
174
174
175 len_cad = rx_buffer[0:4].encode()
175 len_cad = rx_buffer[0:4].encode()
176
176
177 len_data = struct.unpack('>I',len_cad)[0]
177 len_data = struct.unpack('>I',len_cad)[0]
178
178
179 lenAndDataAndXor = rx_buffer[0:len_data + 4] #Incluye los 4 bytes de la longitud
179 lenAndDataAndXor = rx_buffer[0:len_data + 4] #Incluye los 4 bytes de la longitud
180
180
181 dataAndXor = lenAndDataAndXor[4:]
181 dataAndXor = lenAndDataAndXor[4:]
182
182
183 if len(dataAndXor) < len_data:
183 if len(dataAndXor) < len_data:
184 self.errormsg = "Data length is lower than %s" %(len_data)
184 self.errormsg = "Data length is lower than %s" %(len_data)
185 return 0
185 return 0
186
186
187 # print self.header, ", ", ascii2hex(lenCad), ", ", ascii2hex(ipDataAndXor), ", ", hex(self.xor)
187 # print self.header, ", ", ascii2hex(lenCad), ", ", ascii2hex(ipDataAndXor), ", ", hex(self.xor)
188
188
189 if not self.__verifyXor(lenAndDataAndXor):
189 if not self.__verifyXor(lenAndDataAndXor):
190 self.errormsg = "Invalid xor: %s" %lenAndDataAndXor[-1]
190 self.errormsg = "Invalid xor: %s" %lenAndDataAndXor[-1]
191 return 0
191 return 0
192
192
193 self.invalid = False
193 self.invalid = False
194
194
195 len_payload = len_data - 5 #Decrementar 1B (id_class), 1B (id_dev), 2B (cmd) y 1B (xor)
195 len_payload = len_data - 5 #Decrementar 1B (id_class), 1B (id_dev), 2B (cmd) y 1B (xor)
196
196
197 id_class = ord(dataAndXor[0])
197 id_class = ord(dataAndXor[0])
198 id_dev = ord(dataAndXor[1])
198 id_dev = ord(dataAndXor[1])
199 cmd_cad = dataAndXor[2:4].encode()
199 cmd_cad = dataAndXor[2:4].encode()
200 payload = dataAndXor[4:4+len_payload]
200 payload = dataAndXor[4:4+len_payload]
201
201
202 cmd = struct.unpack('>H',cmd_cad)[0]
202 cmd = struct.unpack('>H',cmd_cad)[0]
203
203
204 self.id_class = id_class
204 self.id_class = id_class
205 self.id_dev = id_dev
205 self.id_dev = id_dev
206 self.cmd = cmd
206 self.cmd = cmd
207
207
208 if len(payload) < 1:
208 if len(payload) < 1:
209 self.errormsg = "IP data is valid but it hasn't payload"
209 self.errormsg = "IP data is valid but it hasn't payload"
210 return 1
210 return 1
211
211
212 self.hasPayload = True
212 self.hasPayload = True
213 self.payload = payload
213 self.payload = payload
214
214
215 self.errormsg = "Successful"
215 self.errormsg = "Successful"
216
216
217 return 1
217 return 1
218
218
219 def __decoder_api(self, rx_buffer, debug = DEBUG):
219 def __decoder_api(self, rx_buffer, debug = DEBUG):
220 """
220 """
221 Input:
221 Input:
222 rx_buffer : Trama recibida como respuesta a un comando enviada a un dispositivo.
222 rx_buffer : Trama recibida como respuesta a un comando enviada a un dispositivo.
223
223
224 Return:
224 Return:
225 0 : Trama recibida incorrecta. La cadena "rx_buffer" no ha sido decodificada correctamente.
225 0 : Trama recibida incorrecta. La cadena "rx_buffer" no ha sido decodificada correctamente.
226 -1 : Dispositivo no inicializado. El dispositivo, dds o rc, no ha sido inicializado
226 -1 : Dispositivo no inicializado. El dispositivo, dds o rc, no ha sido inicializado
227 correctamente.
227 correctamente.
228 -2 : Trama enviada no reconocida. La cadena recibida es correcta y el dispositivo ha sido
228 -2 : Trama enviada no reconocida. La cadena recibida es correcta y el dispositivo ha sido
229 inicializaado correctamente pero la trama enviada no ha sido reconocida por el
229 inicializaado correctamente pero la trama enviada no ha sido reconocida por el
230 dispositivo o el comando enviado no ha sido implementado.
230 dispositivo o el comando enviado no ha sido implementado.
231 >0 : Trama enviada y recibida correctamente
231 >0 : Trama enviada y recibida correctamente
232 """
232 """
233
233
234 if not self.__decoder(rx_buffer):
234 if not self.__decoder(rx_buffer):
235 return "0:Error decoding eth data: " + ascii2hex(self.rx_buffer)
235 return "0:Error decoding eth data: " + ascii2hex(self.rx_buffer)
236
236
237 # if self.getPayload() == "OK":
237 # if self.getPayload() == "OK":
238 # return 1
238 # return 1
239 #
239 #
240 # if self.getPayload() == "NI":
240 # if self.getPayload() == "NI":
241 # return -1
241 # return -1
242 #
242 #
243 # if self.getPayload() == "KO":
243 # if self.getPayload() == "KO":
244 # return -2
244 # return -2
245
245
246 if debug:
246 if debug:
247 print(ascii2hex(self.rx_buffer))
247 print(ascii2hex(self.rx_buffer))
248
248
249 return self.payload
249 return self.payload
250
250
251 def getRxBufferHex(self):
251 def getRxBufferHex(self):
252
252
253 if self.rx_buffer == None:
253 if self.rx_buffer == None:
254 return ''
254 return ''
255
255
256 cad = ascii2hex(self.rx_buffer)
256 cad = ascii2hex(self.rx_buffer)
257
257
258 return cad
258 return cad
259
259
260 def getTxBufferHex(self):
260 def getTxBufferHex(self):
261
261
262 if self.tx_buffer == None:
262 if self.tx_buffer == None:
263 return ''
263 return ''
264
264
265 cad = ascii2hex(self.tx_buffer)
265 cad = ascii2hex(self.tx_buffer)
266
266
267 return cad
267 return cad
268
268
269 def isInvalid(self):
269 def isInvalid(self):
270
270
271 return self.invalid
271 return self.invalid
272
272
273 def getCmd(self):
273 def getCmd(self):
274 return self.cmd
274 return self.cmd
275
275
276 def getPayload(self):
276 def getPayload(self):
277 return self.payload
277 return self.payload
278
278
279 def getErrorMessage(self):
279 def getErrorMessage(self):
280
280
281 return self.errormsg
281 return self.errormsg
282
282
283 def getTxBuffer(self):
283 def getTxBuffer(self):
284
284
285 return self.tx_buffer
285 return self.tx_buffer
286
286
287 def getRxBuffer(self):
287 def getRxBuffer(self):
288
288
289 return self.rx_buffer
289 return self.rx_buffer
290
290
291 def __encodeIpCmd(self, ip, mask, gateway):
291 def __encodeIpCmd(self, ip, mask, gateway):
292
292
293 payload = ip + '/' + mask + '/' + gateway
293 payload = ip + '/' + mask + '/' + gateway
294 return self.__encoder(CMD_CHANGEIP, payload)
294 return self.__encoder(CMD_CHANGEIP, payload)
295
295
296 def __encodeResetCmd(self):
296 def __encodeResetCmd(self):
297
297
298 payload = ""
298 payload = ""
299 return self.__encoder(CMD_RESET, payload)
299 return self.__encoder(CMD_RESET, payload)
300
300
301 def __sendTCPData(self, cadena=b''):
301 def __sendTCPData(self, cadena=b''):
302
302
303 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
303 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
304 sck.settimeout(3)
304 sck.settimeout(3)
305
305
306
306
307 try:
307 try:
308 sck.connect(self.address)
308 sck.connect(self.address)
309 except:
309 except:
310 return None
310 return None
311
311
312 #print('trama', cadena.encode('raw_unicode_escape'), flush=True)
312 #print('trama', cadena.encode('raw_unicode_escape'), flush=True)
313 sck.send(cadena.encode('raw_unicode_escape'))
313 sck.send(cadena.encode('raw_unicode_escape'))
314
314
315 rx_buffer = b''
315 rx_buffer = b''
316 ini = time.time()
316 ini = time.time()
317
317
318 try:
318 try:
319 while True:
319 while True:
320
320
321 if time.time() - ini > 0.5:
321 if time.time() - ini > 0.5:
322 break
322 break
323
323
324 try:
324 try:
325 tmp = sck.recv(4096)
325 tmp = sck.recv(4096)
326 except:
326 except:
327 break
327 break
328
328
329 if len(tmp) < 1:
329 if len(tmp) < 1:
330 continue
330 continue
331
331
332 ini = time.time()
332 ini = time.time()
333 rx_buffer += tmp
333 rx_buffer += tmp
334
334
335 finally:
335 finally:
336 sck.close()
336 sck.close()
337
337
338
338
339 rx_buffer = list(rx_buffer)
339 rx_buffer = list(rx_buffer)
340
340
341 #print('-**********-')
341 #print('-**********-')
342 #print(rx_buffer, flush=True)
342 #print(rx_buffer, flush=True)
343 #print('-**********-')
343 #print('-**********-')
344
344
345 new = ''
345 new = ''
346 for u in rx_buffer:
346 for u in rx_buffer:
347 new += chr(u)
347 new += chr(u)
348
348
349 return new
349 return new
350
350
351 def changeIP(self, ip, mask, gateway):
351 def changeIP(self, ip, mask, gateway):
352
352
353 tx_buffer = self.__encodeIpCmd(ip, mask, gateway)
353 tx_buffer = self.__encodeIpCmd(ip, mask, gateway)
354 rx_buffer = self.__sendTCPData(tx_buffer)
354 rx_buffer = self.__sendTCPData(tx_buffer)
355
355
356 sts = self.__decoder_api(rx_buffer)
356 sts = self.__decoder_api(rx_buffer)
357
357
358 if sts > 0:
358 if sts > 0:
359 self.address = (ip, self.address[1])
359 self.address = (ip, self.address[1])
360
360
361 return sts
361 return sts
362
362
363 def reset(self):
363 def reset(self):
364
364
365 tx_buffer = self.__encodeResetCmd()
365 tx_buffer = self.__encodeResetCmd()
366 rx_buffer = self.__sendTCPData(tx_buffer)
366 rx_buffer = self.__sendTCPData(tx_buffer)
367
367
368 return self.__decoder_api(rx_buffer)
368 return self.__decoder_api(rx_buffer)
369
369
370 def sendData(self, cmd, payload, server=False):
370 def sendData(self, cmd, payload, server=False):
371
371
372 if server:
372 if server:
373 tx_buffer = self.__encoder(cmd, payload)
373 tx_buffer = self.__encoder(cmd, payload)
374
374
375 print('TX:{}'.format(ascii2hex(tx_buffer)))
375 print('TX:{}'.format(ascii2hex(tx_buffer)))
376
376
377 self.client_connection.sendall(tx_buffer)
377 self.client_connection.sendall(tx_buffer)
378
378
379 else:
379 else:
380
380
381 tx_buffer = self.__encoder(cmd, payload)
381 tx_buffer = self.__encoder(cmd, payload)
382
382
383 print('TX:{}'.format(ascii2hex(tx_buffer)))
383 print('TX:{}'.format(ascii2hex(tx_buffer)))
384
384
385 rx_buffer = self.__sendTCPData(tx_buffer)
385 rx_buffer = self.__sendTCPData(tx_buffer)
386
386
387 if not rx_buffer:
387 if not rx_buffer:
388 msg = "0:Could not connect to Device %s" %str(self.address)
388 msg = "0:Could not connect to Device %s" %str(self.address)
389 return msg
389 return msg
390
390
391 print('RX:{}'.format(ascii2hex(rx_buffer)))
391 print('RX:{}'.format(ascii2hex(rx_buffer)))
392
392
393 return self.__decoder_api(rx_buffer)
393 return self.__decoder_api(rx_buffer)
394
394
395 def receiveData(self, rx_buffer):
395 def receiveData(self, rx_buffer):
396
396
397 print('RX:{}'.format(ascii2hex(rx_buffer)))
397 print('RX:{}'.format(ascii2hex(rx_buffer)))
398
398
399 return self.__decoder(rx_buffer)
399 return self.__decoder(rx_buffer)
400
400
401
401
402 def eth_device(id_class):
402 def eth_device(id_class):
403 def inner_func(func):
403 def inner_func(func):
404 def func_wrapper(ip, port, *args):
404 def func_wrapper(ip, port, *args):
405
405
406 cmd, payload = func(*args)
406 cmd, payload = func(*args)
407
407
408 ipObj = IPData(ip, port, id_class=id_class)
408 ipObj = IPData(ip, port, id_class=id_class)
409 rx = ipObj.sendData(cmd, payload)
409 rx = ipObj.sendData(cmd, payload)
410
410
411 return rx
411 return rx
412
412
413 return func_wrapper
413 return func_wrapper
414 return inner_func
414 return inner_func
415
416 def eth_devices(id_class):
417 def inner_func(func):
418 def func_wrapper(ip, port, **kwargs):
419
420 cmd, payload = func(**kwargs)
421
422 ipObj = IPData(ip, port, id_class=id_class)
423 rx = ipObj.sendData(cmd, payload)
424
425 return rx
426
427 return func_wrapper
428 return inner_func
429
General Comments 0
You need to be logged in to leave comments. Login now