Paho's MQTT Dev Toolkit
pahotoolkit的Python项目详细描述
泛美卫生组织的MQTT工具包
欢迎使用泛美卫生组织的MQTT工具包。
为了谁?为什么呢?
如果对于那些已经阅读过Paho's MQTT library and its examples文档的人来说,并且像我一样,已经感觉到api在某种程度上对板载应用程序来说是离线和复杂的,那么这个工具包就是这样的。
我个人喜欢泛美卫生组织的mqtt库,无论如何我都不想替换它。但是,我希望编写的应用程序在某种程度上熟悉我使用的框架,例如flask或sanic。 因此,通过使用具有简化api的可靠配置库来抽象底层实现。
基本配置是什么?
此工具包的基本配置使用paho的事件循环实现,方法是使用start_loop
和stop_loop
方法,并创建一个接受不同配置参数start_async
的单入口点方法。
简单用法
有关更多示例,请参阅examples目录。
importtimefrompaho.mqtt.clientimportClientfrompahotoolkitimport(init,start_async,stop_async,subscribe,json_message,)frommy_settingsimport(HOST,PORT,USERNAME,PASSWORD,LOG_CONFIG)@subscribe('/temperature')defhandle_temperature(mqtt_client:Client,userdata,message):print(f'Got {message}')@subscribe('/my/topic')@json_message()# by default, non-json messages in the topic are ignoreddeflisten_my_topic(payload:dict,*args,**kwargs):# todo: do something with the payloadpass@on_connect()defclient_connected(mqtt_client:Client,*args,**kwargs):"""Publish something on_connect."""mqtt_client.publish('/clients','Paho\'s MQTT toolkit message!')# log_config => https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig# the default MQTT Client uses the default's MQTT Client constructor# https://github.com/eclipse/paho.mqtt.python#client-1defmain():client:Client=start_async(host=HOST,port=PORT,# port default to 1883 if not givenusername=USERNAME,password=PASSWORD,# optional fieldsinitialize=False,log_config=LOG_CONFIG)# optional fieldsinit(client)# only required if initialize=False, else by default is automatically calledwhileTrue:time.sleep(1)# or do something in this thread...if__name__=='__main__':try:main()finally:stop_async()
联系人
阿努福索利斯