@@ -1,4 +1,5 | |||||
1 |
|
1 | |||
|
2 | import requests | |||
2 | from datetime import datetime |
|
3 | from datetime import datetime | |
3 | from django.template.base import kwarg_re |
|
4 | from django.template.base import kwarg_re | |
4 |
|
5 | |||
@@ -138,23 +139,39 class Device(models.Model): | |||||
138 | def change_ip(self, ip_address, mask, gateway, **kwargs): |
|
139 | def change_ip(self, ip_address, mask, gateway, **kwargs): | |
139 |
|
140 | |||
140 | if self.device_type.name=='dds': |
|
141 | if self.device_type.name=='dds': | |
141 | try: |
|
142 | try: | |
142 | #if True: |
|
|||
143 | answer = dds_api.change_ip(ip = self.ip_address, |
|
143 | answer = dds_api.change_ip(ip = self.ip_address, | |
144 | port = self.port_address, |
|
144 | port = self.port_address, | |
145 | new_ip = ip_address, |
|
145 | new_ip = ip_address, | |
146 | mask = mask, |
|
146 | mask = mask, | |
147 | gateway = gateway) |
|
147 | gateway = gateway) | |
148 | if answer[0]=='1': |
|
148 | if answer[0]=='1': | |
149 | self.message = 'DDS - {}'.format(answer) |
|
149 | self.message = '25|DDS - {}'.format(answer) | |
150 | self.ip_address = ip_address |
|
150 | self.ip_address = ip_address | |
151 | self.save() |
|
151 | self.save() | |
152 | else: |
|
152 | else: | |
153 | self.message = 'DDS - {}'.format(answer) |
|
153 | self.message = '30|DDS - {}'.format(answer) | |
154 | return False |
|
154 | return False | |
155 | except Exception as e: |
|
155 | except Exception as e: | |
156 | self.message = str(e) |
|
156 | self.message = '40|{}'.format(str(e)) | |
157 | return False |
|
157 | return False | |
|
158 | ||||
|
159 | elif self.device_type.name=='rc': | |||
|
160 | payload = {'ip': ip_address, | |||
|
161 | 'dns': kwargs.get('dns', '8.8.8.8'), | |||
|
162 | 'gateway': gateway, | |||
|
163 | 'subnet': mask} | |||
|
164 | req = requests.post(self.url('changeip'), data=payload) | |||
|
165 | try: | |||
|
166 | answer = req.json() | |||
|
167 | if answer['changeip']=='ok': | |||
|
168 | self.message = '25|IP succesfully changed' | |||
|
169 | self.ip_address = ip_address | |||
|
170 | self.save() | |||
|
171 | else: | |||
|
172 | self.message = '30|An error ocuur when changing IP' | |||
|
173 | except Exception as e: | |||
|
174 | self.message = '40|{}'.format(str(e)) | |||
158 | else: |
|
175 | else: | |
159 | self.message = 'Not implemented' |
|
176 | self.message = 'Not implemented' | |
160 | return False |
|
177 | return False |
@@ -284,7 +284,8 def device_change_ip(request, id_dev): | |||||
284 |
|
284 | |||
285 | if request.user.is_staff: |
|
285 | if request.user.is_staff: | |
286 | device.change_ip(**request.POST.dict()) |
|
286 | device.change_ip(**request.POST.dict()) | |
287 |
|
|
287 | level, message = device.message.split('|') | |
|
288 | messages.add_message(request, level, message) | |||
288 | else: |
|
289 | else: | |
289 | messages.error(request, 'Not enough permission to delete this object') |
|
290 | messages.error(request, 'Not enough permission to delete this object') | |
290 | return redirect(device.get_absolute_url()) |
|
291 | return redirect(device.get_absolute_url()) |
@@ -236,21 +236,36 class RCConfiguration(Configuration): | |||||
236 |
|
236 | |||
237 | def get_pulses(self, binary=True): |
|
237 | def get_pulses(self, binary=True): | |
238 |
|
238 | |||
239 | pulses = [line.pulses_as_points() for line in self.get_lines()] |
|
239 | ||
240 | points = [tup for tups in pulses for tup in tups] |
|
240 | pulses = [line.pulses_as_points() for line in self.get_lines()] | |
241 | points = set([x for tup in points for x in tup]) |
|
241 | tuples = [tup for tups in pulses for tup in tups] | |
|
242 | points = set([x for tup in tuples for x in tup]) | |||
242 | points = list(points) |
|
243 | points = list(points) | |
243 | points.sort() |
|
244 | points.sort() | |
244 |
|
245 | states = [] | ||
245 | line_points = [line.pulses_as_points() for line in self.get_lines()] |
|
246 | last = [0 for x in pulses] | |
246 | #line_points = [[(x, x+y) for x,y in tups] for tups in line_points] |
|
247 | ||
247 | line_points = [[t for x in tups for t in x] for tups in line_points] |
|
248 | for x in points: | |
248 | states = [[1 if x in tups else 0 for tups in line_points] for x in points] |
|
249 | dum = [] | |
|
250 | for i,tups in enumerate(pulses): | |||
|
251 | ups = [tup[0] for tup in tups] | |||
|
252 | dws = [tup[1] for tup in tups] | |||
|
253 | if x in ups: | |||
|
254 | dum.append(1) | |||
|
255 | elif x in dws: | |||
|
256 | dum.append(0) | |||
|
257 | else: | |||
|
258 | dum.append(last[i]) | |||
|
259 | states.append(dum) | |||
|
260 | last = dum | |||
249 |
|
261 | |||
250 | if binary: |
|
262 | if binary: | |
251 | states.reverse() |
|
263 | ret = [] | |
252 | states = [int(''.join([str(x) for x in flips]), 2) for flips in states] |
|
264 | for flips in states: | |
253 |
|
265 | flips.reverse() | ||
|
266 | ret.append(int(''.join([str(x) for x in flips]), 2)) | |||
|
267 | states = ret | |||
|
268 | ||||
254 | return states[:-1] |
|
269 | return states[:-1] | |
255 |
|
270 | |||
256 | def add_cmd(self, cmd): |
|
271 | def add_cmd(self, cmd): | |
@@ -459,7 +474,9 class RCConfiguration(Configuration): | |||||
459 | self.device.status = 0 |
|
474 | self.device.status = 0 | |
460 | req = requests.get(self.device.url('status')) |
|
475 | req = requests.get(self.device.url('status')) | |
461 | payload = req.json() |
|
476 | payload = req.json() | |
462 |
if payload['status']==' |
|
477 | if payload['status']=='enabled': | |
|
478 | self.device.status = 3 | |||
|
479 | elif payload['status']=='disabled': | |||
463 | self.device.status = 2 |
|
480 | self.device.status = 2 | |
464 | else: |
|
481 | else: | |
465 | self.device.status = 1 |
|
482 | self.device.status = 1 | |
@@ -545,35 +562,35 class RCConfiguration(Configuration): | |||||
545 |
|
562 | |||
546 | values = zip(self.get_pulses(), |
|
563 | values = zip(self.get_pulses(), | |
547 | [x-1 for x in self.get_delays()]) |
|
564 | [x-1 for x in self.get_delays()]) | |
548 | payload = '' |
|
|||
549 |
|
565 | |||
550 | for tup in values: |
|
566 | data = bytearray() | |
551 | vals = pack('<HH', *tup) |
|
567 | #reset | |
552 | payload += '\x05'+vals[0]+'\x04'+vals[1]+'\x05'+vals[2]+'\x05'+vals[3] |
|
568 | data.extend((128, 0)) | |
|
569 | #disable | |||
|
570 | data.extend((129, 0)) | |||
|
571 | #divider | |||
|
572 | data.extend((131, self.clock_divider-1)) | |||
|
573 | #enable writing | |||
|
574 | data.extend((139, 62)) | |||
|
575 | ||||
|
576 | last = 0 | |||
|
577 | for tup in values: | |||
|
578 | vals = pack('<HH', last^tup[0], tup[1]) | |||
|
579 | last = tup[0] | |||
|
580 | data.extend((133, vals[1], 132, vals[0], 133, vals[3], 132, vals[2])) | |||
|
581 | ||||
|
582 | #enable | |||
|
583 | data.extend((129, 1)) | |||
553 |
|
584 | |||
554 | try: |
|
585 | try: | |
555 | ## reset |
|
586 | req = requests.post(self.device.url('write'), data=b64encode(data)) | |
556 | if not self.reset_device(): |
|
|||
557 | return False |
|
|||
558 | ## stop |
|
|||
559 | if not self.stop_device(): |
|
|||
560 | return False |
|
|||
561 | ## write clock divider |
|
|||
562 | req = requests.post(self.device.url('divisor'), |
|
|||
563 | {'divisor': '{:d}'.format(self.clock_divider-1)}) |
|
|||
564 | payload = req.json() |
|
|||
565 | if payload['divisor']=='ok': |
|
|||
566 | self.message = 'Error configuring RC clock divider' |
|
|||
567 | return False |
|
|||
568 | ## write pulses & delays |
|
|||
569 | req = requests.post(self.device.url('write'), data=b64encode(payload)) |
|
|||
570 | payload = req.json() |
|
587 | payload = req.json() | |
571 | if payload['write']=='ok': |
|
588 | if payload['write']=='ok': | |
572 | self.device.status = 2 |
|
589 | self.device.status = 2 | |
573 | self.device.save() |
|
590 | self.device.save() | |
574 | self.message = 'RC configured' |
|
591 | self.message = 'RC configured' | |
575 | else: |
|
592 | else: | |
576 |
self.device.status = |
|
593 | self.device.status = 1 | |
577 | self.device.save() |
|
594 | self.device.save() | |
578 | self.message = 'RC write not ok' |
|
595 | self.message = 'RC write not ok' | |
579 | return False |
|
596 | return False | |
@@ -700,11 +717,11 class RCLine(models.Model): | |||||
700 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit/len(json.loads(codes[0].params)['codes'][0]) |
|
717 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit/len(json.loads(codes[0].params)['codes'][0]) | |
701 | else: |
|
718 | else: | |
702 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit |
|
719 | tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit | |
703 |
|
720 | |||
704 | if ref=='first_baud': |
|
721 | if ref=='first_baud': | |
705 | return int(1 + (tx_width + 1)/2 + params['first_height']*km2unit - params['resolution']*km2unit) |
|
722 | return int(1 + round((tx_width + 1)/2 + params['first_height']*km2unit - params['resolution']*km2unit)) | |
706 | elif ref=='sub_baud': |
|
723 | elif ref=='sub_baud': | |
707 | return int(1 + params['first_height']*km2unit - params['resolution']*km2unit/2) |
|
724 | return int(1 + round(params['first_height']*km2unit - params['resolution']*km2unit/2)) | |
708 | else: |
|
725 | else: | |
709 | return 0 |
|
726 | return 0 | |
710 |
|
727 | |||
@@ -837,8 +854,8 class RCLine(models.Model): | |||||
837 | for p in params['params']: |
|
854 | for p in params['params']: | |
838 | y_win = self.points(ntx, ipp_u, |
|
855 | y_win = self.points(ntx, ipp_u, | |
839 | p['resolution']*p['number_of_samples']*km2unit, |
|
856 | p['resolution']*p['number_of_samples']*km2unit, | |
840 |
before=int(self.rc_configuration.time_before*us2unit) |
|
857 | before=int(self.rc_configuration.time_before*us2unit), | |
841 |
sync=self. |
|
858 | sync=self.get_win_ref(p, params['TX_ref'], km2unit)) | |
842 |
|
859 | |||
843 | if len(tr_ranges)>0 and tr_ranges[0]!='0': |
|
860 | if len(tr_ranges)>0 and tr_ranges[0]!='0': | |
844 | y_win = self.mask_ranges(y_win, tr_ranges) |
|
861 | y_win = self.mask_ranges(y_win, tr_ranges) |
@@ -54,10 +54,10 def reset(): | |||||
54 | return cmd, payload |
|
54 | return cmd, payload | |
55 |
|
55 | |||
56 | @eth_device(ID_CLASS) |
|
56 | @eth_device(ID_CLASS) | |
57 | def change_ip(ip, mask="255.255.255.0", gateway="0.0.0.0"): |
|
57 | def change_ip(new_ip, mask="255.255.255.0", gateway="0.0.0.0"): | |
58 |
|
58 | |||
59 | cmd = CMD_CHANGEIP |
|
59 | cmd = CMD_CHANGEIP | |
60 | payload = ip + '/' + mask + '/' + gateway |
|
60 | payload = new_ip + '/' + mask + '/' + gateway | |
61 |
|
61 | |||
62 | return cmd, payload |
|
62 | return cmd, payload | |
63 |
|
63 |
General Comments 0
You need to be logged in to leave comments.
Login now