paparazzi / pprzlink

Message and communication library for the Paparazzi UAV system
Other
24 stars 55 forks source link

ENH: Adding python decorators for subscribing to messages #119

Open MJafarMashhadi opened 4 years ago

MJafarMashhadi commented 4 years ago

I'm working on a project which I'm using pprzlink in. I created a couple of decorator classes for subscribing to messages and I was wondering if they are a good addition to pprzlink.

There is IvySubscribe that will bind the function to a list of message types:

# To one message type
@IvySubscribe(ivy_link=ivy, message_types=[("ground", "NEW_AIRCRAFT")])
def new_aircraft_callback(ac_id, msg):
    pass

# RegEx is also supported
@IvySubscribe(ivy_link=ivy, message_types=["(.*)")])
def new_aircraft_callback(ac_id, msg):
    pass

# Subscribe to all 
@IvySubscribe(ivy_link=ivy)
def get_all_messages(ac_id, msg):
    pass

I created IvySubscribeOnce that stop listening for that type of message as soon as it receives one (useful for the new request messages):


@IvySubscribeOnce(
    ivy_link=ivy_link,
    message_types=[r"^((\S*\s*)?\d+_\d+ ground %s_REQ( .*|$))" % request_name]
)
def request_callback(ac_id, msg):
    pass

And if you needed to unbind manually you can use get_all_messages.__ivy_subs__.unsubscribe().

They are useful in my project, but whether it's a good idea to have them as a part of pprz_link itself, I don't know. What do you think?