def pbrmemw(self, address, value): while value: chunk = value[:8] m = self.one('PBRMEMW,%04X,%d%s%s' % (address, len(chunk), ''.join(',%02X' % ord(c) for c in chunk), ',' * (8 - len(chunk))), PBRMEMR_RE) if int(m.group(1), 16) != address: raise ProtocolError('address mismatch') if not ''.join(chr(int(i, 16)) for i in m.group(2).split(',')).startswith(chunk): raise ProtocolError('readback mismatch') address += len(chunk) value = value[len(chunk):]
def iact31(self): self.write('ACT_31_00\r\n') line = self.readline() if line == 'No Data\r\n': return while True: if line == ' Done\r\n': break m = re.match( r'\A(.*?);([NS])\s+(\d+)\'(\d+\.\d+);([EW])\s+(\d+)\'(\d+\.\d+);\s*(\d+);\s*(\d+)\r\n', line) if m: name = m.group(1) lat = int(m.group(3)) + float(m.group(4)) / 60.0 if m.group(2) == 'S': lat = -lat lon = int(m.group(6)) + float(m.group(7)) / 60.0 if m.group(5) == 'W': lon = -lon alt = int(m.group(8)) radius = int(m.group(9)) yield Waypoint(name, lat, lon, alt, radius=radius) else: raise ProtocolError('unexpected response %r' % line) line = self.readline()
def ipbrrts(self): for l in self.ieach('PBRRTS,'): l = l.decode('nmea_sentence') m = PBRRTS_RE1.match(l) if m: index, count, name = int(m.group(1)), int( m.group(2)), m.group(3) if count == 1: yield Route(index, name, []) else: routepoints = [] else: m = PBRRTS_RE2.match(l) if m: index, count, routepoint_index = (int(i) for i in m.groups()[0:3]) routepoint_short_name = m.group(4) routepoint_long_name = m.group(5) routepoints.append( Routepoint(routepoint_short_name, routepoint_long_name)) if routepoint_index == count - 1: yield Route(index, name, routepoints) else: raise ProtocolError(m)
def one(self, command, re=None, timeout=1): result = None for m in self.ieach(command, re, timeout): if not result is None: raise ProtocolError(m) result = m return result
def pbrctrw(self, ctr, warning_distance): n = 2 + len(ctr.ctrpoints) self.none('PBRCTRW,%03d,000,%s,%04d' % (n, ctr.name[:17].ljust(17), ctr.warning_distance or warning_distance)) self.none('PBRCTRW,%03d,001,%s' % (n, ctr.remark[:17].ljust(17))) for i, ctrpoint in enumerate(ctr.ctrpoints): command = 'PBRCTRW,%03d,%03d,%s,%02d%06.3f,%s,%03d%06.3f,%s' % ( n, i + 2, ctrpoint.type, abs(60 * ctrpoint.lat) / 60, abs(60 * ctrpoint.lat) % 60, 'S' if ctrpoint.lat < 0 else 'N', abs(60 * ctrpoint.lon) / 60, abs(60 * ctrpoint.lon) % 60, 'W' if ctrpoint.lon < 0 else 'E') if ctrpoint.type == 'C': command += ',%04d' % (ctrpoint.radius,) elif ctrpoint.type in ('T', 'Z'): command += ',%s' % ('+' if ctrpoint.clockwise else '-',) if i == len(ctr.ctrpoints) - 1: status = int(self.one(command, PBRANS_RE).group(1)) if status != 1: raise ProtocolError('status == %d' % (status,)) else: self.none(command)
def act22(self, index): self.write('ACT_22_00\r\n') line = self.readline() if line == 'ACT_22_00 Done\r\n': return True elif line == 'ACT_22_00 Fail\r\n': return False else: raise ProtocolError('unexpected response %r' % line)
def wfa(self, parameter, value): format = FA_FORMAT[parameter] m = re.match(r'(\d+)s\Z', format) if m: width = int(m.group(1)) value = INVALID_CHARS_RE.sub('', value)[:width].ljust(width) command = 'WFA_%02X_%s\r\n' % (parameter, ''.join( '%02X' % ord(c) for c in struct.pack(format, value))) self.write(command) line = self.readline(0.1) if line == command: pass elif line == 'No Par\r\n': raise ProtocolError( 'there are no data defined for this parameter number') elif line == 'not ready\r\n': raise ProtocolError('missing the action $82') else: raise ProtocolError('Unexpected response %r' % line)
def handle(self): srvIface = Authorization(self.server.users, self._set_authenticated_user) self.transport.start_server(server=srvIface) # Get the session channel chan = self.transport.accept(self.auth_timeout) if chan is None: raise ProtocolError( "session channel not opened (authentication failure?)") self.transport.join()
def ieach(self, command, re=None, timeout=1): self.write(command.encode('nmea_sentence')) while True: line = self.readline(timeout) if re is None: yield line else: m = re.match(line.decode('nmea_sentence')) if m is None: raise ProtocolError(line) yield m
def pbrmemr(self, address, length): result = [] first, last = address, address + length while first < last: m = self.one('PBRMEMR,%04X' % first, PBRMEMR_RE) if int(m.group(1), 16) != first: raise ProtocolError('address mismatch') data = list(int(i, 16) for i in m.group(2).split(',')) result.extend(data) first += len(data) return result[:length]
def handle(self): print('Handling incoming request by thread: %s' % (threading.current_thread().name)) srvIface = Authorization(self.server.users, self._set_authenticated_user) self.transport.start_server(server=srvIface) # Get the session channel chan = self.transport.accept(self.auth_timeout) if chan is None: raise ProtocolError( "session channel not opened (authentication failure?)") self.transport.join()
def rxa(self, x, parameter, format): self.write('R%cA_%02X\r\n' % (x, parameter)) line = self.readline(0.2) m = re.match(r'\AR%cA_%02X_((?:[0-9A-F]{2})*)\r\n\Z' % (x, parameter), line) if m: return struct.unpack( format, ''.join( chr(int(x, 16)) for x in re.findall(r'..', m.group(1)))) elif line == 'No Par\r\n': return None else: raise ProtocolError('unexpected response %r' % line)
def ipbrctr(self): for l in self.ieach('PBRCTR,'): l = l.decode('nmea_sentence') m = PBRCTR_RE1.match(l) if m: count, name, warning_distance = int( m.group(1)), m.group(2), int(m.group(3)) remark = None ctrpoints = [] continue m = PBRCTR_RE2.match(l) if m: if int(m.group(1)) != count: raise ProtocolError('count mismatch') remark = m.group(2) continue m = PBRCTR_RE3.match(l) if m: if int(m.group(1)) != count: raise ProtocolError('count mismatch') index, type = int(m.group(2)), m.group(3) lat = int(m.group(4)) + float(m.group(5)) / 60 if m.group(6) == 'S': lat *= -1 lon = int(m.group(7)) + float(m.group(8)) / 60 if m.group(9) == 'W': lon *= -1 radius, clockwise = None, None if type in ('P', 'X'): m = PBRCTR_PX_RE.match(l) if not m: raise ProtocolError(l) elif type == 'C': m = PBRCTR_C_RE.match(l) if not m: raise ProtocolError(l) radius = int(m.group(1)) elif type in ('T', 'Z'): m = PBRCTR_TZ_RE.match(l) if not m: raise ProtocolError(l) clockwise = m.group(1) == '+' else: raise ProtocolError(l) ctrpoint = CTRPoint(type, lat, lon, radius, clockwise) ctrpoints.append(ctrpoint) if index == count - 1: yield CTR(name, warning_distance, remark, ctrpoints) continue m = PBRANS_RE.match(l) if m: status = int(m.group(1)) if status != 1: raise RuntimeError(l) # FIXME continue logger.warning('unexpected response %r', l)
def act32(self, waypoint): self.write('ACT_32_00\r\n') name = INVALID_CHARS_RE.sub('', waypoint.get_id_name())[:16].ljust(16) lat_hemi = 'N' if waypoint.lat > 0 else 'S' lat_deg, lat_min = divmod(abs(60 * waypoint.lat), 60) lon_hemi = 'E' if waypoint.lon > 0 else 'W' lon_deg, lon_min = divmod(abs(60 * waypoint.lon), 60) self.write('%s;%s %2d\'%6.3f;%s %3d\'%6.3f;%6d;%6d\r\n' % (name, lat_hemi, lat_deg, lat_min, lon_hemi, lon_deg, lon_min, waypoint.alt or 0, waypoint.radius or 400)) line = self.readline() if line == ' Done\r\n': return name elif line == 'full list\r\n': raise RuntimeError # FIXME elif line == 'Syntax Error\r\n': raise ProtocolError('syntax error') elif line == 'already exist\r\n': return name
def ieach(self, command, re=None, timeout=1): try: self.write(command.encode('nmea_sentence')) if self.readline(timeout) != XOFF: raise ProtocolError while True: line = self.readline(timeout) if line == XON: break elif re is None: yield line else: m = re.match(line.decode('nmea_sentence')) if m is None: raise ProtocolError(line) yield m except: self.io.flush() raise
def act82(self): self.write('ACT_82_00\r\n') line = self.readline(0.1) if line != ' Done\r\n': raise ProtocolError('unexpected response %r' % line)
def none(self, command, timeout=1): for m in self.ieach(command, None, timeout): raise ProtocolError(m)