Package: brewblox-service
Scaffolding for Brewblox service applicationsIn order to reduce code duplication between services, generic functionality is implemented here.For an example on how to implement your own service based onbrewblox-service, seehttps://github.com/brewblox/brewblox-boilerplate.brewblox_serviceSmall generic tools are defined here.brewblox_loggercan be used for creating module-specific loggers. It is not required, but will play a bit nicer with default formatting of the log.Example:frombrewblox_serviceimportbrewblox_loggerLOGGER=brewblox_logger(__name__)LOGGER.info('hello')service.pyParses commandline arguments, creates anaiohttpapplication object, and runs it.The shortest implementation is:parser=service.create_parser('my_service')config=service.create_config(parser)app=service.create_app(config)service.run_app(app)This will get you a working web application, but it will not have any endpoints.Applications can configure their own features, and add new commandline arguments.Example:classExtendedConfig(models.BaseServiceConfig):my_arg:str# Separately creating the parser allows adding arguments to the default setparser=service.create_parser('my_service')parser.add_argument('--my-arg')# Get a config object from the extended argsconfig=service.create_config(parser,model=ExtendedConfig)# Now create the appapp=service.create_app(config)asyncdefsetup():# Add features for this service# You can call async functions heredevice.setup(app)api.setup(app)# Run the app. It will await the setup() coroutineservice.run_app(app,setup())features.pyMany service features are application-scoped. Their lifecycle should span multiple requests, either because they are not request-driven, or because they manage asynchronous I/O operations (such as listening to AMQP messages).TheServiceFeatureclass offers an abstract base class for this behavior. Implementing classes should definestartup(app)andshutdown(app)functions, and those will be automatically called when the application starts up and shuts down.Bothstartup()andshutdown()are called in an async context, making them the async counterparts of__init__()and__del__()functions.Features must be constructed after the app is created, but before it starts running. (service.create_app()andservice.run_app(app)).
It is recommended but not required to add features in an async setup function.Theadd()andget()functions make it easy to centrally declare a feature, and then use it in any function that has a reference to the aiohttp app.repeater.pyA very common use case for features is to have the equivalent of awhile Trueloop.
TheRepeaterFeatureclass handles the boilerplate involved in setting this up in an async context.mqtt.pyBasic MQTT messaging: publish and listen to events.
To publish data, all you need is thepublish(topic, message)function.To listen to incoming messages, you can combinesubscribe(topic)with one or more calls tolisten(topic, callback).
The subscribe/listen functions allow for + and # wildcards to be used.For a detailed explanation of how to use MQTT topics, seehttp://www.steves-internet-guide.com/understanding-mqtt-topics/.For the Brewblox spec on how and where to publish data, seehttps://brewblox.com/dev/reference/events.html.Includes top-level convenience functions forpublish(topic, message),listen(topic, callback)andsubscribe(topic).