dzavalishin / mqtt_udp

Simpified version of MQTT over UDP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returning packet value

jericho555 opened this issue · comments

Hi
I was thinking about whether it could be possible to modify mqttudp.engine.listen(recv_packet) to return the packet value other than just printing it out in function.
I feel that way I could dictate the behavior of my system with ease.

what do you think?

Please tell me what file you’re talking about?

If you're talking about Python implementation, then please note that mqttudp.engine.listen argument is a function that is called with packet received.

Pease refer to lang/python3/examples/mqtt_udp_to_broker.py

def recv_packet_from_udp(pkt):
    global last
    if pkt.ptype != mqttudp.engine.PacketType.Publish:
        return
    if last.__contains__(pkt.topic) and last[pkt.topic] == pkt.value:
        return
    last[pkt.topic] = pkt.value
    #print( topic+"="+value )
    log.info( "UDP to broker "+pkt.topic+"="+pkt.value )
    bclient.publish(pkt.topic, pkt.value, qos=0)


if __name__ == "__main__":
    print( "Will resend all the MQTT/UDP traffic to MQTT broker at "+MQTT_BROKER_HOST )

    #global last

    bclient = broker.Client()
    #client.on_connect = on_connect
    #client.on_message = on_message

    bclient.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, 60)
    #print("connected", bclient)
    log.info( "connected " + str(bclient) )

    blt = threading.Thread(target=broker_listen_thread, args=(bclient,))
    blt.start()

    mqttudp.engine.listen(recv_packet_from_udp)
    blt.join()

Here recv_packet_from_udp is called for each incoming packet.