Package: weeve-modules
weeve Modules Python PackageThis Python pip package is a specially designed support package for developers to implement their own weeve modules.
The package takes care of all weeve specific intercontainer communication protocols and other aspects of the module's functionality within weeve ecosystem.For the full documentation on building weeve modules, please see our official docs:How to create a weeve module.weeve Modules Python PackagePackage specific environment variablesweeve_modules.listener()weeve_modules.send()weeve_modules.weeve_logger()weeve_modules.add_graceful_termination()Example: Input ModuleExample: Processing ModuleExample: Output ModulePackage specific environment variablesThese environment variables are set by the weeve Agent on the edge-node. In order to avoid conflicts, do not override these variables when pushing modules to production.Environment VariablestypeDescriptionLOG_LEVELstringAllowed log levels:DEBUG,INFO,WARNING,ERROR,CRITICAL. Refer tologging levels package documentation.MODULE_NAMEstringName of the module.INGRESS_HOSTstringHost to which data will be received.INGRESS_PORTstringPort to which data will be received.EGRESS_URLSstringHTTP ReST endpoint for the next modules.weeve_modules.listener()listener()is the most important function as it allows the module to set up a listener server to receive data from other modules within the weeve ecosystem.listener()takes one argument:callback_function(object): The callback function defined by a user and ready to receive data for further processing. Callback functionmustaccept data as an argument.fromweeve_modulesimportlistenerdefmy_module_logic(data):# your module code hereif__name__=="__main__":listener(callback_function=my_module_logic)weeve_modules.send()This function enables passing data to the next module in the weeve Edge Application. It takes only one argument:processed_data(dict): Data in JSON format to send to the next module.It returns empty string ("") on success. Otherwise, error message.On error:
"Failed sending data to urls: https://testing.com - Error 404."Example:fromweeve_modulesimportsend# send data to the next moduleerr=send({"temperature":12,"pressure":1019})iferr:# your error protocolweeve_modules.weeve_logger()Weeve logger is based onlogging package. There are five supported logging levels:DEBUG,INFO,WARNING,ERROR,CRITICAL. Refer tologging levels package documentation. These messages have a custom weeve logging protocol which is in sync with weeve Agent and API. Current schema:{
    "timestamp": <timestamp>,
    "level": <logging_level>,
    "filename": <log_message_source_filename>,
    "message": <log_message>
}Example:fromweeve_modulesimportweeve_logger# initialize logging by providing logger name, we strongly recommend using the filenamelog=weeve_logger("tutorial_module")# logging exampleslog.debug("This is debug log messege.")log.info("This is info log messege.")log.warning("This is warning log messege.")log.error("This is error log messege.")log.critical("This is critical log messege.")weeve_modules.add_graceful_termination()add_graceful_termination()function enables Docker to gracefully terminate the module container. This is optional as developers might want to implement their own graceful termination if their modules use extra resources or files. However, we strongly recommend callingadd_graceful_termination()at the start of your script unless the module uses volumes, external resources, etc. when the custom implementation of graceful termination might be required.Example:fromweeve_modulesimportlistener,add_graceful_terminationdefmy_module_logic(data):# your module code hereif__name__=="__main__":add_graceful_termination()listener(callback_function=my_module_logic)Example: Input ModuleThis is an example of a simple input module that receives data over HTTP ReST and passes it to the next module in the weeve Edge Application.
We needsend()andweeve_logger()functions.fromweeve_modulesimportsend,weeve_loggerfrombottleimportrun,post,requestlog=weeve_logger("my_input_module")@post("/")def_request_handler():# receive datareceived_data=request.json# send data to the next moduleerr=send(received_data)if__name__=="__main__":add_graceful_termination()# start the server on http://0.0.0.0:8080/run(host="0.0.0.0",port=8080,quiet=True,)Example: Processing ModuleThis is an example of a simple processing module that receives data from the previous module and filters outtemperaturelower that 0. Later it sends data to the next module in the weeve Edge Application.
We needsend(),weeve_logger()andlistener()functions.fromweeve_modulesimportsend,weeve_logger,listenerlog=weeve_logger("my_processing_module")# my module logic functionalitydeffilter_temperature(data):# filter temperature dataifdata["temperature"]>=0:# send data to the next moduleerr=send(data)# check the success of sending the dataiferr:log.error("Failed to pass filetered data:%s",err)else:log.info("Successfully passed filetered data.")if__name__=="__main__":add_graceful_termination()# set up a listener to receive data from other modules within the weeve ecosystemlistener(callback_function=filter_temperature)Example: Output ModuleThis is an example of a simple output module that receives data from the previous module and sends it over HTTP ReST to a selected endpoint.
We needweeve_logger()andlistener()functions.fromweeve_modulesimportweeve_logger,listenerfromrequestsimportpostfromjsonimportdumpslog=weeve_logger("my_output_module")# my module logic functionalitydefsend_to_endpoint(data):log.info("Sending data to the endpoint.")post(url="",json=data)if__name__=="__main__":add_graceful_termination()# set up a listener to receive data from other modules within the weeve ecosystemlistener(callback_function=send_to_endpoint)