Esempio n. 1
0
    def parse(self, dataPacket):
        opCode = dataPacket.opCode
        parsedData = None
#        print("UART - opCode:", opCode, "payload:", dataPacket.payload)

        if opCode == UartRxType.MESH_STATE_0 or opCode == UartRxType.MESH_STATE_1:
            # unpack the mesh packet
            meshPacket = MeshStatePacket(dataPacket.payload)
            
            # have each stone in the meshPacket broadcast it's state
            for stoneState in meshPacket.stoneStates:
                stoneState.broadcastState()
                
        elif opCode == UartRxType.SERVICE_DATA:
            serviceData = ServiceData(dataPacket.payload)
            if serviceData.validData:
                BluenetEventBus.emit(DevTopics.newServiceData, serviceData.getDictionary())

        elif opCode == UartRxType.CROWNSTONE_ID:
            id = Conversion.int8_to_uint8(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.ownCrownstoneId, id)

        elif opCode == UartRxType.MAC_ADDRESS:
            if (len(dataPacket.payload) == 7):
                # Bug in old firmware (2.1.4 and lower) sends an extra byte.
                addr = Conversion.uint8_array_to_address(dataPacket.payload[0:-1])
            else:
                addr = Conversion.uint8_array_to_address(dataPacket.payload)
            if (addr is not ""):
                BluenetEventBus.emit(DevTopics.ownMacAddress, addr)
            else:
                print("invalid address:", dataPacket.payload)

        elif opCode == UartRxType.POWER_LOG_CURRENT:
            # type is CurrentSamples
            parsedData = CurrentSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newCurrentData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_VOLTAGE:
            # type is VoltageSamplesPacket
            parsedData = VoltageSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newVoltageData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_FILTERED_CURRENT:
            # type is CurrentSamples
            parsedData = CurrentSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newFilteredCurrentData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_FILTERED_VOLTAGE:
            # type is VoltageSamplesPacket
            parsedData = VoltageSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newFilteredVoltageData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_POWER:
            # type is PowerCalculationsPacket
            parsedData = PowerCalculationPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newCalculatedPowerData, parsedData.getDict())
            
        elif opCode == UartRxType.ADC_CONFIG:
            # type is PowerCalculationsPacket
            parsedData = AdcConfigPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newAdcConfigPacket, parsedData.getDict())

        elif opCode == UartRxType.ADC_RESTART:
            BluenetEventBus.emit(DevTopics.adcRestarted, None)

        elif opCode == UartRxType.ASCII_LOG:
            stringResult = ""
            for byte in dataPacket.payload:
                stringResult += chr(byte)
            logStr = "LOG: %15.3f - %s" % (time.time(), stringResult)
            sys.stdout.write(logStr)
        elif opCode == UartRxType.UART_MESSAGE:
            stringResult = ""
            for byte in dataPacket.payload:
                stringResult += chr(byte)
            # logStr = "LOG: %15.3f - %s" % (time.time(), stringResult)
            # print(logStr)
            BluenetEventBus.emit(UsbTopics.uartMessage, {"string":stringResult, "data": dataPacket.payload})
        else:
            print("Unknown OpCode", opCode)

        
        parsedData = None
Esempio n. 2
0
    def parse(self, dataPacket):
        opCode = dataPacket.opCode
        parsedData = None
        # print("UART - opCode:", opCode, "payload:", dataPacket.payload)

        if opCode == UartRxType.MESH_SERVICE_DATA:
            # data type + service data (15b)
            serviceData = ServiceData(dataPacket.payload, unencrypted=True)
            statePacket = StoneStatePacket(serviceData)
            statePacket.broadcastState()
            # if serviceData.validData:
            #     BluenetEventBus.emit(DevTopics.newServiceData, serviceData.getDictionary())

        elif opCode == UartRxType.SERVICE_DATA:
            # service data type + device type + data type + service data (15b)
            serviceData = ServiceData(dataPacket.payload)
            if serviceData.validData:
                BluenetEventBus.emit(DevTopics.newServiceData, serviceData.getDictionary())

        elif opCode == UartRxType.CROWNSTONE_ID:
            id = Conversion.int8_to_uint8(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.ownCrownstoneId, id)

        elif opCode == UartRxType.MAC_ADDRESS:
            if len(dataPacket.payload) == 7:
                # Bug in old firmware (2.1.4 and lower) sends an extra byte.
                addr = Conversion.uint8_array_to_address(dataPacket.payload[0:-1])
            else:
                addr = Conversion.uint8_array_to_address(dataPacket.payload)
            if addr is not "":
                BluenetEventBus.emit(DevTopics.ownMacAddress, addr)
            else:
                print("invalid address:", dataPacket.payload)

        elif opCode == UartRxType.POWER_LOG_CURRENT:
            # type is CurrentSamples
            parsedData = CurrentSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newCurrentData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_VOLTAGE:
            # type is VoltageSamplesPacket
            parsedData = VoltageSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newVoltageData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_FILTERED_CURRENT:
            # type is CurrentSamples
            parsedData = CurrentSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newFilteredCurrentData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_FILTERED_VOLTAGE:
            # type is VoltageSamplesPacket
            parsedData = VoltageSamplesPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newFilteredVoltageData, parsedData.getDict())
            
        elif opCode == UartRxType.POWER_LOG_POWER:
            # type is PowerCalculationsPacket
            parsedData = PowerCalculationPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newCalculatedPowerData, parsedData.getDict())
            
        elif opCode == UartRxType.ADC_CONFIG:
            # type is PowerCalculationsPacket
            parsedData = AdcConfigPacket(dataPacket.payload)
            BluenetEventBus.emit(DevTopics.newAdcConfigPacket, parsedData.getDict())

        elif opCode == UartRxType.ADC_RESTART:
            BluenetEventBus.emit(DevTopics.adcRestarted, None)

        elif opCode == UartRxType.ASCII_LOG:
            stringResult = ""
            for byte in dataPacket.payload:
                if byte < 128:
                    stringResult += chr(byte)
            logStr = "LOG: %15.3f - %s" % (time.time(), stringResult)
            sys.stdout.write(logStr)
        elif opCode == UartRxType.UART_MESSAGE:
            stringResult = ""
            for byte in dataPacket.payload:
                stringResult += chr(byte)
            # logStr = "LOG: %15.3f - %s" % (time.time(), stringResult)
            # print(logStr)
            BluenetEventBus.emit(UsbTopics.uartMessage, {"string":stringResult, "data": dataPacket.payload})
        elif opCode == UartRxType.FIRMWARESTATE:
            # no need to process this, that's in the test suite.
            pass
        else:
            print("Unknown OpCode", opCode)

        
        parsedData = None