def test_ack(): src = ns.msg.Profile(uid='pippo', pwd='secret') target = ns.ack(src) msg = ns.marshall(src) assert (ns.is_ack(target, msg))
async def main(color, value, topic): """Starter """ sub_topic = 'hb/{}'.format(topic) mqtt = MQTTClient() await mqtt.connect('mqtt://localhost') await mqtt.subscribe([(sub_topic, QOS_1)]) leds = cmd.Leds() setattr(leds, color, value) await mqtt.publish(topic, ns.marshall(leds)) try: while True: message = await mqtt.deliver_message() payload = message.publish_packet.payload.data if ns.is_protobuf(payload): obj = ns.unmarshall(payload) if ns.is_ack(obj): break else: LOG.debug(">> %s", payload) await mqtt.unsubscribe([sub_topic]) await mqtt.disconnect() except ClientException as cli_exc: LOG.error("Client exception: %s", cli_exc)
def to_json(in_packet): """Convert a protobuf into a json message """ LOG.debug("to_json - input: %s, msg: %s", type(in_packet), in_packet) # from protobuf to json is just a matter of unmarshalling if ns.is_protobuf(in_packet): obj = ns.unmarshall(in_packet) if ns.is_ack(obj, command_type=cmd.Leds): mask = obj.sts obj = cmd.Leds() obj.set_status(mask) return obj else: LOG.debug("to_json - |%r| > /dev/null", in_packet) # do not send the message return None
async def test_proto_receive(event_loop, init_tcp_junction, shutdown): port1, port2 = await init_tcp_junction(src_to_dst=ack_response) chan1 = await ns.connect('0.0.0.0', port1) chan2 = await ns.connect('0.0.0.0', port2) cmd = MyCmd() await ns.proto_send(chan1, cmd) recv = await ns.proto_receive(chan2) #test if received packet is ack ns.is_ack(recv) ns.is_ack(recv, command_type=MyCmd) #test that this is not an ack cfg = ns.msg.Profile(uid='uid', pwd='pwd') ns.is_ack(cfg, command_type=MyCmd) chan1.close() await shutdown()