Ejemplo n.º 1
0
def rpm_engine_event_trigger(result, kind="rpm", key="engine"):
    """
    Looks for RPM result and triggers 'vehicle/engine/[not_running|running|stopped]' event based on the value(s) found.
    This trigger supports single value results as well as multiple values results.
    """

    # Check for error result
    error = extract_error_from(result)
    if error:

        if log.isEnabledFor(logging.DEBUG):
            log.debug("RPM {:} event trigger got error result: {:}".format(key, result))

        # Do not return/break flow because this will trigger negative engine event

    else:  # No error found

        # Check for single value in result
        if "value" in result:

            # Skip if not a RPM result
            if result.get("_type", None) != kind:
                log.error("RPM {:} event trigger got unsupported RPM type result: {:}".format(key, result))

                return

            # Log RPM result when debugging
            if log.isEnabledFor(logging.DEBUG):
                log.debug("RPM {:} event trigger got RPM result: {:}".format(key, result))

        # Check for multiple values in result
        elif "values" in result:
            for res in result["values"]:

                # Only interested in RPM results
                if res.get("_type", None) != kind:
                    continue

                rpm_engine_event_trigger(res)

            return

        # Skip if no value(s) found
        else:
            log.error("RPM {:} event trigger could not find any value(s)".format(key))

            return

    # Check if state has chaged since last known state
    ctx = context.setdefault(key, {})
    old_state = ctx.get("state", "")
    new_state = "running" if not error and result.get("value", 0) > 0 else \
                    ("stopped" if old_state in ["stopped", "running"] else "not_running")
    if old_state != new_state:
        ctx["state"] = new_state

        # Trigger event
        edmp.trigger_event({"reason": str(error)} if error else {},
            "vehicle/{:s}/{:s}".format(key, ctx["state"]))
Ejemplo n.º 2
0
def battery_event_trigger(result):
    """
    Looks for battery results and triggers 'vehicle/battery/*' event when voltage changes.
    """

    # Check for error result
    error = extract_error_from(result)
    if error:
        log.error("Battery event trigger got error result: {:}".format(result))

        state = "unknown"
    else:
        if log.isEnabledFor(logging.DEBUG):
            log.debug(
                "Battery event trigger got battery result: {:}".format(result))

        state = result["state"]

    # Check if state has chaged since last time
    ctx = context["battery"]
    if ctx["state"] != state:
        ctx["state"] = state
        ctx["timer"] = timer()
        ctx["count"] = 1
    else:
        ctx["count"] += 1

    # Proceed only when battery state is repeated at least once and timer threshold is reached
    if ctx["count"] > 1 and timer(
    ) - ctx["timer"] >= ctx["event_thresholds"].get(
            state, ctx["event_thresholds"].get("*", 3)):

        # Reset timer
        ctx["timer"] = timer()

        # Prepare event data
        data = {}
        if state in [
                battery_util.DISCHARGING_STATE,
                battery_util.CRITICAL_LEVEL_STATE
        ]:
            data["level"] = result["level"]
        elif state == "unknown" and error:
            data["reason"] = str(error)

        # Trigger only event if battery state (in tag) and/or level (in data) has changed
        edmp.trigger_event(data,
                           "vehicle/battery/{:s}".format(state),
                           skip_duplicates_filter="vehicle/battery/*")
Ejemplo n.º 3
0
def communication_event_trigger(result):
    """
    Looks for error in result and triggers 'vehicle/communication/[inactive|established|disconnected]' event based on the outcome.
    """

    # Check for error result
    error = extract_error_from(result)
    if error and log.isEnabledFor(logging.DEBUG):
        log.debug("Communication event trigger got error result: {:}".format(result))

    # Check if state has chaged since last known state
    ctx = context.setdefault("communication", {})
    old_state = ctx.get("state", "")
    new_state = "established" if not error else ("disconnected" if old_state in ["disconnected", "established"] else "inactive")
    if old_state != new_state:
        ctx["state"] = new_state

        # Trigger event
        edmp.trigger_event({"reason": str(error)} if error else {},
            "vehicle/communication/{:s}".format(ctx["state"]))
Ejemplo n.º 4
0
def position_event_trigger(result):
    """
    Listens for position results and triggers position unknown/standstill/moving events.
    """

    # Check for error result
    error = extract_error_from(result)
    if log.isEnabledFor(logging.DEBUG) and error:
        log.debug(
            "Position event trigger is unable to determine GNSS location: {:}".
            format(error))

    ctx = context["position"]

    old_state = ctx["state"]
    new_state = None

    old_error = ctx["error"]
    new_error = None

    # Determine new state
    if error:
        new_state = "unknown"
        new_error = str(error)
    elif result.get("sog", 0) > 0:
        new_state = "moving"
    else:
        new_state = "standstill"

    # Check if state has chaged since last known state
    if old_state != new_state or old_error != new_error:
        ctx["state"] = new_state
        ctx["error"] = new_error

        # Trigger event
        edmp.trigger_event({"reason": ctx["error"]} if ctx["error"] else {},
                           "vehicle/position/{:s}".format(ctx["state"]))