Exemplo n.º 1
0
class Compliance:
    def __init__(self, activation=LoRa.OTAA):
        self.lora = LoRa(mode=LoRa.LORAWAN)
        self.lora.compliance_test(True, 0, False)  # enable testing

        if activation == LoRa.OTAA:
            app_eui = binascii.unhexlify(APP_EUI.replace(' ',''))
            app_key = binascii.unhexlify(APP_KEY.replace(' ',''))
            self.lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)
        else:
            dev_addr = struct.unpack(">l", binascii.unhexlify(DEV_ADDR.replace(' ','')))[0]
            nwk_swkey = binascii.unhexlify(NWK_SWKEY.replace(' ',''))
            app_swkey = binascii.unhexlify(APP_SWKEY.replace(' ',''))
            self.lora.join(activation=LoRa.ABP, auth=(dev_addr, nwk_swkey, app_swkey))

        # wait until the module has joined the network
        while not self.lora.has_joined():
            time.sleep(2.5)
            print("Waiting to join...")

        print("Network joined!")
        self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
        self.s.setsockopt(socket.SOL_LORA, socket.SO_CONFIRMED, False)

        self.tx_payload = bytes([(self.lora.compliance_test().downlink_counter >> 8) & 0xFF,
                                  self.lora.compliance_test().downlink_counter & 0xFF])

    def run(self):
        while True:
            while not self.lora.compliance_test().running:
                time.sleep(5.0)
                print('Sending ready packet')
                self.s.send('Ready')

            print('Test running!')
            self.s.setblocking(False)
            while self.lora.compliance_test().running:
                time.sleep(5.0)

                if self.lora.compliance_test().state < 6: # re-join
                    self.s.send(self.tx_payload)

                    if self.lora.compliance_test().link_check:
                        self.tx_payload = bytes([5, self.lora.compliance_test().demod_margin,
                                                    self.lora.compliance_test().nbr_gateways])
                        # set the state to 1 and clear the link check flag
                        self.lora.compliance_test(True, 1, False)
                    else:
                        if self.lora.compliance_test().state == 4:
                            rx_payload = self.s.recv(255)
                            if rx_payload:
                                self.tx_payload = bytes([rx_payload[0]])
                                for i in range(1, len(rx_payload)):
                                    self.tx_payload += bytes([(rx_payload[i] + 1) & 0xFF])
                            self.lora.compliance_test(True, 1)  # set the state to 1
                        else:
                            self.tx_payload = bytes([(self.lora.compliance_test().downlink_counter >> 8) & 0xFF,
                                                      self.lora.compliance_test().downlink_counter & 0xFF])
class Compliance:
    def __init__(self, activation=LoRa.OTAA):
        self.lora = LoRa(mode=LoRa.LORAWAN)
        self.lora.compliance_test(True, 0, False)  # enable testing
        self.activation = activation

        self._join()

        self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 3)
        self.s.setsockopt(socket.SOL_LORA, socket.SO_CONFIRMED, False)

    def _join(self):
        if self.activation == LoRa.OTAA:
            dev_eui = binascii.unhexlify(DEV_EUI.replace(' ', ''))
            app_eui = binascii.unhexlify(APP_EUI.replace(' ', ''))
            app_key = binascii.unhexlify(APP_KEY.replace(' ', ''))
            self.lora.join(activation=LoRa.OTAA,
                           auth=(dev_eui, app_eui, app_key),
                           timeout=0)
        else:
            dev_addr = struct.unpack(
                '>l', binascii.unhexlify(DEV_ADDR.replace(' ', '')))[0]
            nwk_swkey = binascii.unhexlify(NWK_SWKEY.replace(' ', ''))
            app_swkey = binascii.unhexlify(APP_SWKEY.replace(' ', ''))
            self.lora.join(activation=LoRa.ABP,
                           auth=(dev_addr, nwk_swkey, app_swkey))

        # wait until the module has joined the network
        print('Joining.', end='', flush=True)
        time.sleep(0.1)
        while not self.lora.has_joined():
            time.sleep(5)
            print("Joining...")

        print('')
        print('Network joined!')

        self.s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
        self.s.setsockopt(socket.SOL_LORA, socket.SO_DR, 2)
        self.s.setsockopt(socket.SOL_LORA, socket.SO_CONFIRMED, False)
        self.s.setblocking(True)

        print('Waiting for test activation...')

    def run(self):
        while True:
            while not self.lora.compliance_test().running:
                time.sleep(5)
                self.s.send('Ready')

            print('Test running!')

            self.s.setblocking(True)

            self.tx_payload = bytes([
                (self.lora.compliance_test().downlink_counter >> 8) & 0xFF,
                self.lora.compliance_test().downlink_counter & 0xFF
            ])

            while self.lora.compliance_test().running:
                # re-join
                if self.lora.compliance_test().state < 6:
                    try:
                        self.s.send(self.tx_payload)
                        time.sleep(2)
                    except Exception:
                        time.sleep(1)

                    if self.lora.compliance_test().link_check:
                        self.tx_payload = bytes([
                            5,
                            self.lora.compliance_test().demod_margin,
                            self.lora.compliance_test().nbr_gateways
                        ])
                        # set the state to 1 and clear the link check flag
                        self.lora.compliance_test(True, 1, False)
                    else:
                        if self.lora.compliance_test().state == 4:
                            rx_payload = self.s.recv(255)
                            if rx_payload:
                                self.tx_payload = bytes([rx_payload[0]])
                                for i in range(1, len(rx_payload)):
                                    self.tx_payload += bytes([
                                        (rx_payload[i] + 1) & 0xFF
                                    ])
                            self.lora.compliance_test(True,
                                                      1)  # set the state to 1
                        else:
                            self.tx_payload = bytes([
                                (self.lora.compliance_test().downlink_counter
                                 >> 8) & 0xFF,
                                self.lora.compliance_test().downlink_counter
                                & 0xFF
                            ])
                else:
                    self.rejoined = True
                    time.sleep(3.5)
                    self._join()

            # The test has been disabled, send one more message in order to signal that test mode
            # has been deactivated and then wait a few seconds before trying to join again
            time.sleep(3)
            self.s.send(ACTIVATE_MSG)
            if self.activation == LoRa.OTAA:
                time.sleep(3)
            else:
                time.sleep(5)
            if not self.rejoined:
                machine.deepsleep(1000)