def testLEDCommandDecoding(self): commandHeaderBytes = b'\x04\x08\x3a\x02' commandDataBytes= b'\x01\x01\x01\x01\x01\x01\x01\x01' commandBytes = commandHeaderBytes+commandDataBytes packet = NebResponsePacket(commandBytes) self.assertEqual(len(packet.data.ledState), 8) self.assertEqual(packet.data.ledState[0], 1) self.assertEqual(packet.data.ledState[1], 1) self.assertEqual(packet.data.ledState[2], 1) self.assertEqual(packet.data.ledState[3], 1) self.assertEqual(packet.data.ledState[4], 1) self.assertEqual(packet.data.ledState[5], 1) self.assertEqual(packet.data.ledState[6], 1) self.assertEqual(packet.data.ledState[7], 1)
def testDebugCommandDecoding(self): commandHeaderBytes = b'\x00\x10\xbc\x02' commandDataBytes= b'\xde\xea\xbe\xef\xa5\x01\x11\x01\x02\xba\xbe\x00\x01\x02\x03\x04' commandBytes = commandHeaderBytes+commandDataBytes packet = NebResponsePacket(commandBytes) self.assertEqual(packet.data.motionStatus.distance, True ) self.assertEqual(packet.data.motionStatus.force, False) self.assertEqual(packet.data.motionStatus.euler, True ) self.assertEqual(packet.data.motionStatus.quaternion, False) self.assertEqual(packet.data.motionStatus.imuData, False) self.assertEqual(packet.data.motionStatus.motion, True ) self.assertEqual(packet.data.motionStatus.steps, False) self.assertEqual(packet.data.motionStatus.magData, True ) self.assertEqual(packet.data.motionStatus.sitStand, True ) self.assertEqual(packet.data.recorderStatus.status, 2)
def testCreatePedometerPackets(self): #print("\n*** Testing Encoding and Decoding of Pedometer Packets ***") responsePackets = [] packets = nebsim.createWalkingPathPacketList(1000, 61.0, 5.0, 10) for packet in packets: packetString = packet.stringEncode() responsePackets.append(NebResponsePacket(packetString)) for idx,packet in enumerate(responsePackets): self.assertEqual(packets[idx].header.subSystem, SubSystem.Motion) self.assertEqual(packets[idx].header.command, Commands.Motion.Pedometer) self.assertEqual(packets[idx].data.timestamp, packet.data.timestamp) self.assertEqual(packets[idx].data.stepCount, packet.data.stepCount) self.assertEqual(packets[idx].data.stepsPerMinute, packet.data.stepsPerMinute) self.assertEqual(packets[idx].data.walkingDirection, packet.data.walkingDirection) self.assertGreaterEqual(packets[idx].data.walkingDirection, -180.0) self.assertLessEqual(packets[idx].data.walkingDirection, 180.0)
def testCreateEulerPackets(self): #print("\n*** Testing Encoding and Decoding of Euler Angle Packets ***") responsePackets = [] packets = nebsim.createSpinningObjectPacketList(50.0, 1.0, 2.0, 6.0) for packet in packets: packetString = packet.stringEncode() responsePackets.append(NebResponsePacket(packetString)) for idx,packet in enumerate(responsePackets): self.assertEqual(packets[idx].header.subSystem, SubSystem.Motion) self.assertEqual(packets[idx].header.command, Commands.Motion.EulerAngle) self.assertEqual(packets[idx].data.timestamp, packet.data.timestamp) self.assertEqual(packets[idx].data.yaw, packet.data.yaw) self.assertEqual(packets[idx].data.pitch, packet.data.pitch) self.assertEqual(packets[idx].data.roll, packet.data.roll) self.assertEqual(packets[idx].data.demoHeading, packet.data.demoHeading)
def waitForPacket(self, packetType, subSystem, command, timeout=3): packet = None currentTime = time.time() while not packet or \ (not packet.isPacketValid(packetType, subSystem, command) and not packet.isPacketError()): if time.time() - currentTime > timeout: raise TimeoutError try: bytes = self.device.receivePacket() if bytes: packet = NebResponsePacket(bytes) else: packet = None except NotImplementedError as e: logging.error("Dropped bad packet.") packet = None continue except InvalidPacketFormatError as e: logging.error("InvalidPacketFormatError") packet = None continue except CRCError as e: logging.error("CRCError : " + str(e)) packet = None continue except KeyError as e: logging.error( "Tried creating a packet with an invalid subsystem or command : " + str(e)) packet = None continue except TimeoutError as e: logging.error('Read timed out.') return NebResponsePacket.createEmptyResponsePacket( subSystem, command) except KeyboardInterrupt as e: logging.error("KeyboardInterrupt.") return NebResponsePacket.createEmptyResponsePacket( subSystem, command) except: packet = None logging.error("Unexpected error : ", exc_info=True) return NebResponsePacket.createEmptyResponsePacket( subSystem, command) return packet
def testCreateMAGPackets(self): #print("\n*** Testing Encoding and Decoding of MAG Packets ***") responsePackets = [] packets = nebsim.createRandomMAGDataPacketList(50.0, 300, 1.0) for packet in packets: packetString = packet.stringEncode() responsePackets.append(NebResponsePacket(packetString)) for idx,packet in enumerate(responsePackets): self.assertEqual( packets[idx].header.subSystem, SubSystem.Motion) self.assertEqual( packets[idx].header.command, Commands.Motion.MAG) self.assertEqual( packets[idx].data.timestamp, packet.data.timestamp) self.assertEqual( packets[idx].data.mag[0], packet.data.mag[0]) self.assertEqual( packets[idx].data.mag[1], packet.data.mag[1]) self.assertEqual( packets[idx].data.mag[2], packet.data.mag[2]) self.assertEqual( packets[idx].data.accel[0], packet.data.accel[0]) self.assertEqual( packets[idx].data.accel[1], packet.data.accel[1]) self.assertEqual( packets[idx].data.accel[2], packet.data.accel[2])
def storePacketsUntil(self, packetType, subSystem, command): packetList = [] packet = None while not packet or \ (not packet.isPacketValid(packetType, subSystem, command) and not packet.isPacketError()): try: if packet and packet.header.subSystem != SubSystem.Debug: packetList.append(packet) print('Received {0} packets'.format(len(packetList)), end="\r", flush=True) bytes = self.device.receivePacket() if bytes: packet = NebResponsePacket(bytes) else: packet = None except NotImplementedError as e: logging.error("Dropped bad packet.") packet = None continue except InvalidPacketFormatError as e: logging.error("InvalidPacketFormatError.") packet = None continue except CRCError as e: logging.error("CRCError : " + str(e)) packet = None continue except KeyError as e: logging.error( "Tried creating a packet with an invalid subsystem or command : " + str(e)) packet = None continue except TimeoutError as e: logging.error('Read timed out.') return None except KeyboardInterrupt as e: logging.error("KeyboardInterrupt.") return None except: packet = None logging.error("Unexpected error : ", exc_info=True) continue return packetList
def buildPacketList(self, packetList): packets = [] errorList = [] for idx,packetString in enumerate(packetList): try: # print(binascii.hexlify(bytearray(packetString))) nebPacket = NebResponsePacket(packetString) # print('Appending {0}'.format(nebPacket)) packets.append(nebPacket) except KeyError as keyError: # print('Invalid Subsystem or Command Code') errorList.append(keyError) except NotImplementedError as notImplError: # print('Got a non-standard packet at #{0}'\ # .format(idx)) errorList.append(notImplError) except CRCError as crcError: errorList.append(crcError) # print('Got a CRCError at packet #{0}'\ # .format(idx)) # print(crcError) except InvalidPacketFormatError as invPacketError: errorList.append(invPacketError) return (packets, errorList)