Example #1
0
async def actiondev(data):
    for item in data:
        if (item["type"] == "device"):
            systemName = item["systemName"]
            device = devicesArrey.get(systemName)
            device = device["device"]
            val = getvalue(item["value"], {
                "device": device,
                "field": item["action"]
            })
            await setValue(device.systemName, item["action"], val)
        elif (item["type"] == "group"):
            systemName = item["systemName"]
            group = Groups.get(systemName)
            val = getvalue(item["value"], {
                "group": group,
                "field": item["action"]
            })
            await setValueGroup(
                DeviceValueSchema(systemName=group.systemName,
                                  type=item["action"],
                                  status=val))
        elif (item["type"] == "script"):
            templates = None
            fullName = item["systemName"] + ".yml"
            with open(os.path.join(SCRIPTS_DIR, fullName)) as f:
                templates = yaml.safe_load(f)
            runscript(templates)
        elif (item["type"] == "delay"):
            time.sleep(int(item.get("value").get("value")))
        else:
            print("oh")
Example #2
0
def getvalue(data, option):
    type = None
    oldValue = None
    if (("device" in option) and ("field" in option)):
        field = None
        for item in option["device"].values:
            if (item.name == option["field"]):
                type = item.type
                oldValue = item.get()
                break
    if (("group" in option) and ("field" in option)):
        field = None
        for item in option["group"].fields:
            if (item.name == option["field"]):
                type = item.type
                break
    if ("type" in option):
        type = option["type"]
    if (type == "binary" and data["type"] == "enum"):
        if data["value"] == "low":
            return 0
        if data["value"] == "high":
            return 1
        if data["value"] == "togle" and oldValue == "0":
            return 1
        if data["value"] == "togle" and oldValue == "1":
            return 0
    if (type == "enum" and data["type"] == "enum"):
        return data["value"]
    if (type == "text"):
        return data["value"]
    if (type == "number" and data["type"] == "number"):
        return data["value"]
    if (type == "number" and data["type"] == "math"):
        v1 = int(getvalue(data["value1"], {"type": "number"}))
        v2 = int(getvalue(data["value2"], {"type": "number"}))
        if ((not v1 and v1 != 0) or (not v2 and v2 != 0)):
            return None
        if (data["action"] == "+"):
            return v1 + v2
        if (data["action"] == "-"):
            return v1 - v2
        if (data["action"] == "*"):
            return v1 * v2
        if (data["action"] == "/"):
            return v1 // v2
    if (data["type"] == "device"):
        systemName = data["systemName"]
        device = devicesArrey.get(systemName)
        device = device["device"]
        values = device.values
        for item in values:
            if (item.name == data["action"]):
                val = item.get()
                return val
Example #3
0
def ifblock(data):
    try:
        if (data["type"] == "device"):
            systemName = data["systemName"]
            device = devicesArrey.get(systemName)
            device = device["device"]
            values = device.values
            for item in values:
                if (item.name == data["action"]):
                    if (data["value"]):
                        val = getvalue(data["value"], {"type": item.type})
                        rval = item.get()
                        if (rval == "on"):
                            rval = "1"
                        elif (rval == "off"):
                            rval = "0"
                        if (data["oper"] == ">"):
                            val = int(val)
                            realval = int(rval)
                            return realval > val
                        if (data["oper"] == ">="):
                            val = int(val)
                            realval = int(rval)
                            return realval >= val
                        if (data["oper"] == "<"):
                            val = int(val)
                            realval = int(rval)
                            return realval < val
                        if (data["oper"] == "<="):
                            val = int(val)
                            realval = int(rval)
                            return realval <= val
                        if (data["oper"] == "=="):
                            return str(rval) == str(val)
                        if (data["oper"] == "!="):
                            return str(rval) != str(val)
                    else:
                        return False
        return False
    except Exception as e:
        logger.error(f"run script error. detail: {e}")
        return False
Example #4
0
def lockforScript(systemName, type):
    if not systemName:
        return []
    device = devicesArrey.get(systemName)
    device = device["device"]
    fileList = os.listdir(SCRIPTS_DIR)
    listscripts = list()
    # if type=="variable":
    #     type="value"
    for item in fileList:
        templates = None
        with open(os.path.join(SCRIPTS_DIR, item)) as f:
            templates = yaml.safe_load(f)
        trig = templates["trigger"]
        for item2 in trig:
            if (templates["status"] and item2["type"] == "device"
                    and item2["systemName"] == systemName
                    and (item2["action"] == "all" or item2["action"] == type)):
                listscripts.append(templates["name"])
    return listscripts
Example #5
0
async def setValueGroup(data: DeviceValueSchema):
    logger.debug(f'setValueGroup input data:{data.dict()}')
    group = Groups.get(data.systemName)
    if not group:
        return FunctionRespons(status=TypeRespons.INVALID,
                               detail=f"group {data.systemName} not found")
    for item in group.devices:
        device = devicesArrey.get(item.name)
        print(device)
        if not device:
            logger.info(f'device {item.name} not found')
            continue
        field = device["device"].get_field(data.type)
        if not field:
            continue
        if field.type == "number" and not is_digit(data.status):
            continue
        if field.type == "binary" and not is_digit(data.status):
            continue
        print(item.name, data.type, data.status)
        await setValue(item.name, data.type, data.status)
    return FunctionRespons(status=TypeRespons.OK, data="ok")
Example #6
0
 def deviceSetStatus(self, systemName, type, value, script=True):
     try:
         if (value == None or type == "background"):
             return None
         dev = devicesArrey.get(systemName)
         dev = dev["device"]
         values = dev.values
         for item in values:
             if item.name == type:
                 if (item.type == "binary"):
                     if (str(value).lower() == str(item.high).lower()):
                         value = "1"
                     elif (str(value).lower() == str(item.low).lower()):
                         value = "0"
                     else:
                         return None
                 item.set(value)
         return value
     except Exception as e:
         logger.error(
             f'set value error. systemName:{systemName}, detail:{e}')
         return None