#!/usr/bin/env python3 # -*- coding: utf-8 -*- import logging from typing import List, Optional from m2m.nbiot.quectel import QuectelModule logger = logging.getLogger('m2m.bc66') class ModuleBC66(QuectelModule): """ Implementation for Quectel BC66 NB-IoT modules. """ def __init__(self, serial_port: str, baudrate: int = 9600, **kwargs): super().__init__(serial_port, baudrate, **kwargs) self._setup() def _setup(self): self.set_echo_mode(False) # Disable sleep lock and initialize network URCs self.send_at_command('AT+QSCLK=0') self.send_at_command('AT+CEREG=0') def reboot(self) -> bool: """Reboots the module (AT+QRST=1).""" return self.send_at_command('AT+QRST=1') == b'OK' def lwm2m_configure(self, server_ip: str, port: int = 5683, endpoint: str = "", bootstrap: bool = False, security_mode: int = 3, psk_id: str = "", psk: str = "") -> bool: """Configures LwM2M settings (AT+QLWCONFIG).""" bs = 1 if bootstrap else 0 cmd = f'AT+QLWCONFIG={bs},"{server_ip}",{port},"{endpoint}",60,{security_mode}' if security_mode != 3 and psk_id and psk: cmd += f',"{psk_id}","{psk}"' return self.send_at_command(cmd) == b'OK' def lwm2m_register(self, timeout: int = 60) -> bool: """Registers to LwM2M server (AT+QLWREG).""" resp = self.send_at_command('AT+QLWREG', timeout=timeout) return b'QLWREG: 0' in resp or b'OK' in resp def ping(self, host: str, context_id: int = 1, timeout: int = 4, num: int = 4) -> List[str]: raise NotImplementedError("BC66 Ping parsing not fully standardized.") def dns_query(self, host: str, context_id: int = 1, timeout: int = 30) -> List[str]: raise NotImplementedError("BC66 DNS query not fully standardized.")