##// END OF EJS Templates
Update RC control methods, add change_ip for RC, fix win_reference for sub-baudio...
Juan C. Espinoza -
r236:7c5422f0dc5b
parent child
Show More
@@ -1,4 +1,5
1 1
2 import requests
2 3 from datetime import datetime
3 4 from django.template.base import kwarg_re
4 5
@@ -138,23 +139,39 class Device(models.Model):
138 139 def change_ip(self, ip_address, mask, gateway, **kwargs):
139 140
140 141 if self.device_type.name=='dds':
141 try:
142 #if True:
142 try:
143 143 answer = dds_api.change_ip(ip = self.ip_address,
144 144 port = self.port_address,
145 145 new_ip = ip_address,
146 146 mask = mask,
147 147 gateway = gateway)
148 148 if answer[0]=='1':
149 self.message = 'DDS - {}'.format(answer)
149 self.message = '25|DDS - {}'.format(answer)
150 150 self.ip_address = ip_address
151 151 self.save()
152 152 else:
153 self.message = 'DDS - {}'.format(answer)
153 self.message = '30|DDS - {}'.format(answer)
154 154 return False
155 155 except Exception as e:
156 self.message = str(e)
156 self.message = '40|{}'.format(str(e))
157 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 175 else:
159 176 self.message = 'Not implemented'
160 177 return False
@@ -284,7 +284,8 def device_change_ip(request, id_dev):
284 284
285 285 if request.user.is_staff:
286 286 device.change_ip(**request.POST.dict())
287 messages.success(request, device.message)
287 level, message = device.message.split('|')
288 messages.add_message(request, level, message)
288 289 else:
289 290 messages.error(request, 'Not enough permission to delete this object')
290 291 return redirect(device.get_absolute_url())
@@ -236,21 +236,36 class RCConfiguration(Configuration):
236 236
237 237 def get_pulses(self, binary=True):
238 238
239 pulses = [line.pulses_as_points() for line in self.get_lines()]
240 points = [tup for tups in pulses for tup in tups]
241 points = set([x for tup in points for x in tup])
239
240 pulses = [line.pulses_as_points() for line in self.get_lines()]
241 tuples = [tup for tups in pulses for tup in tups]
242 points = set([x for tup in tuples for x in tup])
242 243 points = list(points)
243 points.sort()
244
245 line_points = [line.pulses_as_points() for line in self.get_lines()]
246 #line_points = [[(x, x+y) for x,y in tups] for tups in line_points]
247 line_points = [[t for x in tups for t in x] for tups in line_points]
248 states = [[1 if x in tups else 0 for tups in line_points] for x in points]
244 points.sort()
245 states = []
246 last = [0 for x in pulses]
247
248 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 262 if binary:
251 states.reverse()
252 states = [int(''.join([str(x) for x in flips]), 2) for flips in states]
253
263 ret = []
264 for flips in states:
265 flips.reverse()
266 ret.append(int(''.join([str(x) for x in flips]), 2))
267 states = ret
268
254 269 return states[:-1]
255 270
256 271 def add_cmd(self, cmd):
@@ -459,7 +474,9 class RCConfiguration(Configuration):
459 474 self.device.status = 0
460 475 req = requests.get(self.device.url('status'))
461 476 payload = req.json()
462 if payload['status']=='ok':
477 if payload['status']=='enabled':
478 self.device.status = 3
479 elif payload['status']=='disabled':
463 480 self.device.status = 2
464 481 else:
465 482 self.device.status = 1
@@ -545,35 +562,35 class RCConfiguration(Configuration):
545 562
546 563 values = zip(self.get_pulses(),
547 564 [x-1 for x in self.get_delays()])
548 payload = ''
549 565
550 for tup in values:
551 vals = pack('<HH', *tup)
552 payload += '\x05'+vals[0]+'\x04'+vals[1]+'\x05'+vals[2]+'\x05'+vals[3]
566 data = bytearray()
567 #reset
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 585 try:
555 ## reset
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))
586 req = requests.post(self.device.url('write'), data=b64encode(data))
570 587 payload = req.json()
571 588 if payload['write']=='ok':
572 589 self.device.status = 2
573 590 self.device.save()
574 591 self.message = 'RC configured'
575 592 else:
576 self.device.status = 4
593 self.device.status = 1
577 594 self.device.save()
578 595 self.message = 'RC write not ok'
579 596 return False
@@ -700,11 +717,11 class RCLine(models.Model):
700 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 718 else:
702 719 tx_width = float(json.loads(RCLine.objects.get(pk=tx_id).params)['pulse_width'])*km2unit
703
704 if ref=='first_baud':
705 return int(1 + (tx_width + 1)/2 + params['first_height']*km2unit - params['resolution']*km2unit)
706 elif ref=='sub_baud':
707 return int(1 + params['first_height']*km2unit - params['resolution']*km2unit/2)
720
721 if ref=='first_baud':
722 return int(1 + round((tx_width + 1)/2 + params['first_height']*km2unit - params['resolution']*km2unit))
723 elif ref=='sub_baud':
724 return int(1 + round(params['first_height']*km2unit - params['resolution']*km2unit/2))
708 725 else:
709 726 return 0
710 727
@@ -837,8 +854,8 class RCLine(models.Model):
837 854 for p in params['params']:
838 855 y_win = self.points(ntx, ipp_u,
839 856 p['resolution']*p['number_of_samples']*km2unit,
840 before=int(self.rc_configuration.time_before*us2unit)+self.get_win_ref(p, params['TX_ref'], km2unit),
841 sync=self.rc_configuration.sync)
857 before=int(self.rc_configuration.time_before*us2unit),
858 sync=self.get_win_ref(p, params['TX_ref'], km2unit))
842 859
843 860 if len(tr_ranges)>0 and tr_ranges[0]!='0':
844 861 y_win = self.mask_ranges(y_win, tr_ranges)
@@ -54,10 +54,10 def reset():
54 54 return cmd, payload
55 55
56 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 59 cmd = CMD_CHANGEIP
60 payload = ip + '/' + mask + '/' + gateway
60 payload = new_ip + '/' + mask + '/' + gateway
61 61
62 62 return cmd, payload
63 63
General Comments 0
You need to be logged in to leave comments. Login now