from messaging import EventDrivenMessageProcessor from retrying import retry log = logging.getLogger(__name__) context = { "mixer": { "settings": None, "initialized": False } } # Message processor edmp = EventDrivenMessageProcessor("audio", context=context, default_hooks={"handler": "play"}) @retry(stop_max_attempt_number=5, wait_fixed=1000) def _ensure_mixer(): ctx = context["mixer"] if ctx.get("initialized", False): return try: settings = ctx["settings"] globals()["pygame"] = importlib.import_module("pygame") log.info("Initializing mixer using settings: {:}".format(settings))
import datetime import logging import pynmea2 import salt.loader from messaging import EventDrivenMessageProcessor from salt_more import SuperiorCommandExecutionError from serial_conn import SerialConn log = logging.getLogger(__name__) # Message processor edmp = EventDrivenMessageProcessor("tracking", default_hooks={"workflow": "extended"}) # Serial connection conn = SerialConn() context = { "position": { "state": None, # Available states: unknown|standstill|moving "last_recorded": None, "last_reported": None, } } @edmp.register_hook(synchronize=False) def status_handler(): """ Get current connection status and more.
import copy import logging import salt.loader from common_util import dict_get, dict_find, dict_filter from messaging import EventDrivenMessageProcessor, keyword_resolve log = logging.getLogger(__name__) # Message processor edmp = EventDrivenMessageProcessor("reactor") context = { "cache.get": lambda *args, **kwargs: dict_get(context.get("cache", None), *args, ** kwargs), "cache.find": lambda *args, **kwargs: dict_find( context.get("cache", {}).values(), *args, **kwargs) } @edmp.register_hook() def module_handler(name, *args, **kwargs): """ Calls a Salt execution module from within minion process. """ return __salt__["minionutil.run_job"](name, *args, **kwargs)
import logging from cloud_cache import NextCloudCache from messaging import EventDrivenMessageProcessor log = logging.getLogger(__name__) # Message processor edmp = EventDrivenMessageProcessor("cloud") cache = NextCloudCache() context = { "upload": { "count": 0, "complete": {}, } } @edmp.register_hook(synchronize=False) def cache_handler(cmd, *args, **kwargs): """ Queries/calls a given cache function. """ ret = { "_type": cmd, } # Private calls not allowed
import logging import re import time from datetime import datetime from messaging import EventDrivenMessageProcessor from serial_conn import SerialConn log = logging.getLogger(__name__) # Message processor edmp = EventDrivenMessageProcessor("ec2x", default_hooks={"handler": "exec"}) # Serial connection conn = SerialConn() error_regex = re.compile("ERROR|\+(?P<type>.+) ERROR: (?P<reason>.+)") rtc_time_regex = re.compile( '^\+CCLK: "(?P<year>\d{2})/(?P<month>\d{2})/(?P<day>\d{2}),(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(?P<tz>[+|-]\d+)"$' ) # Example: +CCLK: "08/01/04,00:19:43+00" network_time_regex = re.compile( '^\+QLTS: "(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2}),(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2})(?P<tz>[+|-]\d+),(?P<dst>\d+)"$' ) # Example: +QLTS: "2017/01/13,03:40:48+32,0" context = {"time": {"state": None}}
import logging from my_mqtt_cache import NextMqttCache from messaging import EventDrivenMessageProcessor log = logging.getLogger(__name__) context = { "upload": { "count": 0, "complete": {}, } } # Message processor edmp = EventDrivenMessageProcessor("my_mqtt", context=context) cache = NextMqttCache() @edmp.register_hook(synchronize=False) def cache_handler(cmd, *args, **kwargs): """ Queries/calls a given cache function. """ ret = { "_type": cmd, } # Private calls not allowed
import salt.loader from binascii import unhexlify from common_util import abs_file_path, add_rotating_file_handler_to, factory_rendering from obd.utils import OBDError from obd_conn import OBDConn from messaging import EventDrivenMessageProcessor, extract_error_from, filter_out_unchanged from timeit import default_timer as timer log = logging.getLogger(__name__) home_dir = "/opt/autopi/obd" # Message processor edmp = EventDrivenMessageProcessor("obd", default_hooks={"workflow": "extended", "handler": "query"}) # OBD connection conn = OBDConn() # ELM327 proxy instance proxy = elm327_proxy.ELM327Proxy() # Loaded CAN databases indexed by procotol ID can_db_cache = {} context = { "battery": { "state": "", "count": 0, "timer": 0.0,
import gpio_pin import logging import os import psutil import RPi.GPIO as gpio import time from messaging import EventDrivenMessageProcessor from spm_conn import SPMConn log = logging.getLogger(__name__) # Message processor edmp = EventDrivenMessageProcessor("spm", default_hooks={"handler": "query"}) # SPM connection conn = SPMConn() context = { "state": None } @edmp.register_hook() def query_handler(cmd, **kwargs): """ Queries a given SPM command. Arguments: - cmd (str): The SPM command to query.
import gpio_pin import logging import mma8x5x_helper as mma8x5x import RPi.GPIO as gpio import threading import time from messaging import EventDrivenMessageProcessor from i2c_conn import I2CConn log = logging.getLogger(__name__) # Message processor edmp = EventDrivenMessageProcessor("mma8x5x", default_hooks={ "handler": "read", "workflow": "extended" }) # I2C connection conn = I2CConn() interrupt_gpio_pin = gpio_pin.ACC_INT1 interrupt_event = threading.Event() # Cached values used when calculating G-forces curr_g_range = mma8x5x.range_as_g(mma8x5x.RANGE_DEFAULT) data_bits = 10 # TODO: Set from settings or something # Callback function triggered for each write on I2C connection
import logging from cloud_cache import NextCloudCache from messaging import EventDrivenMessageProcessor log = logging.getLogger(__name__) context = { "upload": { "count": 0, "complete": {}, } } # Message processor edmp = EventDrivenMessageProcessor("cloud", context=context) cache = NextCloudCache() @edmp.register_hook(synchronize=False) def cache_handler(cmd, *args, **kwargs): """ Queries/calls a given cache function. """ ret = { "_type": cmd, } # Private calls not allowed
log = logging.getLogger(__name__) context = { "cache.get": lambda *args, **kwargs: dict_get(context.get("cache", None), *args, ** kwargs), "cache.find": lambda *args, **kwargs: dict_find( context.get("cache", {}).values(), *args, **kwargs), "result_cache.get": lambda *args, **kwargs: dict_get(context.get("result_cache", None), *args, **kwargs), } # Message processor edmp = EventDrivenMessageProcessor("reactor", context=context) @edmp.register_hook() def module_result_cache_returner(message, result): """ Stores/caches a module result in context. """ if not message["handler"].startswith("module"): log.warn("Skipping result for unsupported handler '{:}': {:}".format( message["handler"], result)) return ctx = context.setdefault("result_cache", {})
"green": 23, "white": 24, "grey": 27, "red": 1, "black": 0 } log = logging.getLogger(__name__) context = { "pin_wires": {} } # Message processor edmp = EventDrivenMessageProcessor("key_fob", context=context) @edmp.register_hook(synchronize=False) def context_handler(): """ Gets current context. """ return context @edmp.register_hook() def power_handler(value=None): """ Powers on/off key fob.