Example #1
0
class BleRpc:
    def __init__(self, config, mesh):
        self.config = config
        self.mesh = mesh
        self.ble_comm = BleServices(
            config.get('ble_name_prefix', PymeshConfig.BLE_NAME_PREFIX) +
            str(config.get('MAC')))

        self.rx_worker = RXWorker(self.ble_comm)
        self.tx_worker = TXWorker(self.ble_comm)

        self.rpc_handler = RPCHandler(self.rx_worker, self.tx_worker,
                                      self.mesh, self.ble_comm)

        # setting hooks for triggering when new message was received and ACK
        self.mesh.meshaging.on_rcv_message = self.on_rcv_message
        self.mesh.meshaging.on_rcv_ack = self.on_rcv_ack

        self.ble_comm.on_disconnect = self.ble_on_disconnect

    def terminate(self):
        ''' kill all, to exit nicely '''
        self.rx_worker.timer_kill()
        self.ble_comm.close()

    def on_rcv_message(self, message):
        ''' hook triggered when a new message arrived '''
        message_data = {
            'mac': message.mac,
            'payload': message.payload,
            'ts': message.ts,
            'id': message.id,
        }

        msg = msgpack.packb(['notify', 'msg', message_data])
        self.rx_worker.put(msg)
        print(message_data['payload'])
        print("%d =================  RECEIVED :) :) :) " % time.ticks_ms())

    def on_rcv_ack(self, message):
        ''' hook triggered when the ACK arrived '''
        message_data = {
            'id': message.id,
        }

        msg = msgpack.packb(['notify', 'msg-ack', message_data])
        self.rx_worker.put(msg)
        print("%d =================  ACK RECEIVED :) :) :) " % time.ticks_ms())

    def ble_on_disconnect(self):
        ''' if BLE disconnected, it's better to re-instantiate RPC handler '''
        self.rpc_handler = RPCHandler(self.rx_worker, self.tx_worker,
                                      self.mesh, self.ble_comm)
Example #2
0
    def __init__(self, config, mesh):
        self.config = config
        self.mesh = mesh
        self.ble_comm = BleServices(config.get('ble_name_prefix', PymeshConfig.BLE_NAME_PREFIX) + str(config.get('MAC')))

        self.rx_worker = RXWorker(self.ble_comm)
        self.tx_worker = TXWorker(self.ble_comm)

        self.rpc_handler = RPCHandler(self.rx_worker, self.tx_worker, self.mesh, self.ble_comm)

        # setting hooks for triggering when new message was received and ACK
        self.mesh.meshaging.on_rcv_message = self.on_rcv_message
        self.mesh.meshaging.on_rcv_ack = self.on_rcv_ack

        self.ble_comm.on_disconnect = self.ble_on_disconnect