예제 #1
0
def h(parent):
    mode = deep_get(parent, "control.mode.intent")
    objects = deep_get(parent, "obs.objects", {})
    if mode == "away" and "human" in objects:
        # TBD: raise a warning
        # TBD: edge trigger by looking at old_view
        ...
예제 #2
0
def h(parent, mounts):
    home = parent

    mode = deep_get(home, "control.mode.intent")
    if mode is None:
        return

    objects = dict()

    # handle rooms
    rooms = mounts.get(room_gvr, {})

    # check all room's mode
    _path = "control.mode.status"
    if len(rooms) == 0:
        deep_get(home, _path)
    elif all(
            deep_get(r, "spec." + _path) == mode_config[mode]["rooms"]["mode"]
            for _, r in rooms.items()):
        deep_set(home, _path, mode)
    elif mode == "normal":
        deep_set(home, _path, "normal")
    else:
        deep_set(home, _path, "undef")

    # check objects in room
    for n, r in rooms.items():
        for o, _ in deep_get(r, "obs.objects", {}).items():
            objects[n] = {"location": n}

    ...

    deep_set(home, "obs.objects", objects)
예제 #3
0
def h1(sv, pv):
    global _dev
    if _dev is None:
        return

    # benchmark
    global _forward_set
    if deep_get(sv, "brightness.intent") == 0.1 and not _forward_set:
        resp, e = patch_spec(*_measure, {"obs": {"forward_leaf": time.time()}})
        if e is None:
            _forward_set = True

    status = lifx.get(_dev)
    power = status.get("power", 0)
    color = list(status.get("color", [1, 1, 1, 1]))

    p, b = deep_get(sv, "power.intent"), deep_get(sv, "brightness.intent")

    if p is not None:
        power = convert["power"]["to"](p)
    if b is not None:
        color[2] = convert["brightness"]["to"](b)

    # benchmark
    lifx.put(_dev, power, color)
예제 #4
0
def do_bright(parent, mounts):
    room, devices = parent, mounts

    bright = deep_get(room, "control.brightness.intent")
    if bright is None:
        return

    num_active_lamp = \
        mount_size(mounts, {ul_gvr, cl_gvr}, has_spec=True,
                   cond=lambda m: deep_get(m, "spec.control.power.status") == "on")

    if num_active_lamp < 1:
        return

    bright_div = bright / num_active_lamp
    _set_bright(devices, bright_div)
예제 #5
0
def h(parent, mounts):
    mode = deep_get(parent, "control.mode.intent")
    if mode is None or mode == "normal":
        return

    # handle rooms
    rooms = mounts.get(room_gvr, {})

    deep_set_all(rooms, "spec.control.mode.intent",
                 mode_config[mode]["rooms"]["mode"])
예제 #6
0
def do_mode_lamps(parent, mounts):
    room, devices = parent, mounts

    mode = deep_get(room, "control.mode.intent")
    if mode is None:
        return

    _config = mode_config[mode]["lamps"]

    _bright = list()
    for lt in [ul_gvr, cl_gvr]:
        _pc = lamp_converters[lt]["power"]["to"]
        _bc = lamp_converters[lt]["brightness"]["to"]

        # iterate over individual lamp
        for n, _l in devices.get(lt, {}).items():

            _p = deep_get(_l, "spec.control.power.intent")
            _b = deep_get(_l, "spec.control.brightness.intent", 0)

            # set power
            if "power" in _config:
                deep_set(_l, "spec.control.power.intent",
                         _pc(_config["power"]))

            # add brightness
            if _pc(_p) == "on":
                _bright.append(_bc(_b))

    if "brightness" in _config:
        _max = _config["brightness"].get("max", 1)
        _min = _config["brightness"].get("min", 0)

        # reset the lamps' brightness only when they
        # don't fit the mode
        if not (_min <= sum(_bright) <= _max) and len(_bright) > 0:
            _bright = deep_get(room, "control.brightness.intent")
            if _min <= _bright <= _max:
                _bright_div = _bright
            else:
                _bright_div = (_max + _min) / 2 / len(_bright)
            _set_bright(devices, _bright_div)
예제 #7
0
def h(parent, mounts):
    room, devices = parent, mounts
    mode = deep_get(room, "control.mode.intent")
    _config = mode_config[mode]["lamps"]

    # handle mode and brightness
    matched = True
    _bright = 0

    for lt in [ul_gvr, cl_gvr]:
        _pc = lamp_converters[lt]["power"]["from"]
        _bc = lamp_converters[lt]["brightness"]["from"]

        # iterate over individual lamp
        for n, _l in devices.get(lt, {}).items():
            if "spec" not in _l:
                continue
            _l = _l["spec"]

            _p = deep_get(_l, "control.power.status", None)
            _b = deep_get(_l, "control.brightness.status", 0)

            # check power
            if "power" in _config and _config["power"] != _pc(_p):
                matched = False

            # add brightness
            if _pc(_p) == "on":
                _bright += _bc(_b)

    deep_set(room, f"control.brightness.status", _bright)

    if "brightness" in _config:
        _max = _config["brightness"].get("max", 1)
        _min = _config["brightness"].get("min", 0)
        if not (_min <= _bright <= _max):
            matched = False

    # other devices
    ...

    deep_set(room, f"control.mode.status", mode if matched else "undef")
예제 #8
0
    def _update_handler_info(self, spec, diff):
        # check whether there is a reflex change
        _changed = not all(
            len(_path) < 2 or _path[1] != "reflex" for _, _path, _, _ in diff)

        if _changed:
            reflexes = util.deep_get(spec, "reflex", {})

            # trim reflexes
            to_remove = list()
            for n, info in self._handler_info.items():
                if info["type"] == HandlerType.BUILTIN:
                    continue
                if n not in reflexes:
                    to_remove.append(n)

            for n in to_remove:
                self._handler_info.pop(n, {})

            # update handlers
            for n, r in reflexes.items():
                info = self._handler_info.get(
                    n,
                    {
                        "fn": do_nothing,
                        "condition": filter_.always,  # TBD conditioned reflex
                        "view_path": ".",
                        "priority": 0,
                        "type": HandlerType.REFLEX,
                    })

                patch = dict()
                if "policy" in r:
                    patch.update({
                        "fn":
                        self._new_reflex(r["policy"], r.get("processor", "py"))
                    })

                if "priority" in r:
                    patch.update({"priority": r["priority"]})

                info.update(patch)
                self._handler_info[n] = info

            self._handler_info_updated = True
예제 #9
0
def h(sv, pv):
    for _attr, _ in sv.items():
        deep_set(pv, f"control.{_attr}.status",
                 deep_get(pv, f"control.{_attr}.intent"))
예제 #10
0
def h(parent):
    mode = deep_get(parent, "control.mode.intent")
    if mode is None:
        return
    assert mode in mode_config, f"{mode} undefined"