equipment_operations_pds.py 4.85 KB
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7 8 9 10 11 12
from uuid import uuid4

import pendulum

from pot_libs.aiomqtt_util.hbmqtt_utils import MqttUtil
from pot_libs.logger import log
from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.constants import EVENT_TYPE_UNIT_MAP


async def alarm_setting_pds(pid, locations):
    """获取报警设置"""
wang.wenrong's avatar
wang.wenrong committed
13
    sql_point = """
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
                SELECT
                    sd.id,
                    sd.etype,
                    et.`NAME`,
                    sd.threshold,
                    sd.duration,
                    sd.importance,
                    sd.`enable` 
                FROM
                    soe_config_record sd
                    INNER JOIN event_type et ON sd.etype = et.e_type 
                WHERE
                    sd.pid = %s
        """
    sql_location = """
                SELECT
                    ln.item,
                    ln.ad_type AS location_ytpe,
                    sd.id,
                    sd.etype,
                    et.`name`,
                    sd.importance,
                    sd.threshold,
                    sd.duration,
                    sd.`enable` 
                FROM
                    soe_config_record sd
                    INNER JOIN event_type et ON sd.etype = et.e_type
                    INNER JOIN location ln ON ln.lid = sd.lid 
                WHERE
                    sd.lid IN %s

        """
lcn's avatar
lcn committed
47 48

    async with MysqlUtil() as conn:
49
        res_point = await conn.fetchall(sql_point, args=(pid,)) if pid else {}
lcn's avatar
lcn committed
50
        res_location = await conn.fetchall(sql_location, args=(
ZZH's avatar
ZZH committed
51
            tuple(locations),)) if locations else {}
lcn's avatar
lcn committed
52 53
    alarm_list = []
    for res in res_point:
54 55 56 57 58
        name = res.get("name", "")
        id_ = res.get("id", "")
        threshold = res.get("threshold", "")
        duration = res.get("duration", "")
        enable = res.get("enable", "")
lcn's avatar
lcn committed
59
        event_type = res.get("etype", "")
lcn's avatar
lcn committed
60 61
        p_dic = {
            "id": id_,
62 63 64 65
            "type": name,
            "threshold": threshold,
            "duration": duration,
            "enable": enable,
lcn's avatar
lcn committed
66 67 68 69 70 71 72
            "unit": EVENT_TYPE_UNIT_MAP[event_type]
        }
        alarm_list.append(p_dic)
    for res in res_location:
        name = (res.get("item") + res.get("name")) if res.get(
            "item") != "default" else res.get("name"),
        id_ = res.get("id")
73 74 75 76
        threshold = res.get("threshold", "")
        duration = res.get("duration", "")
        enable = res.get("enable", "")
        event_type = res.get("type", "")
lcn's avatar
lcn committed
77 78
        lo_dic = {
            "id": id_,
79 80 81 82
            "type": name,
            "threshold": threshold,
            "duration": duration,
            "enable": enable,
lcn's avatar
lcn committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            "unit": EVENT_TYPE_UNIT_MAP[event_type]
        }
        alarm_list.append(lo_dic)
    return alarm_list


async def device_get_v1(sid):
    """v1.0查询配置信息"""
    try:
        request_id = str(uuid4())
        data = {
            "sid": sid,
            "method": "get",
            "request_id": request_id,
            "time": pendulum.now().to_datetime_string()
        }
        async with MqttUtil() as emq:
            result = await emq.device_response(
                sid=sid, request_id=request_id, data=data
            )
    # except TimeoutError:
    except Exception as e:
        log.warning(e)
        log.warning(f"sid:{sid}在1.0协议未查询到数据")
        # 4.2 根据2.0协议查询数据
        result = None
    return result


async def device_get_config(sid, key):
    """v2.0 配置查询"""
    try:
        request_id = str(uuid4())
        data = {
            "sid": sid,
            "method": "get-config",
            "request_id": request_id,
            "key": key,
            "time": pendulum.now().to_datetime_string()
        }
        async with MqttUtil() as emq:
            result = await emq.device_response(
                sid=sid, request_id=request_id, data=data
            )
    # except TimeoutError:
    except Exception as e:
        log.warning(e)
        log.warning(f"sid:{sid}在2.0协议未查询到数据")
        # 4.2 根据2.0协议查询数据
        result = None
    return result


async def device_demolish_pds(sid):
    """v2.0 装置拆除"""
    flag = False
    try:
        request_id = str(uuid4())
        data = {
            "sid": sid,
            "method": "command",
            "request_id": request_id,
            "time": pendulum.now().to_datetime_string(),
            "data": {
                "command": "restart",
                "params": {}
            }
        }
        async with MqttUtil() as emq:
            result = await emq.device_response(
                sid=sid, request_id=request_id, data=data
            )
            if result["status_code"] == 200:
                flag = True
    except TimeoutError:
        log.info(f"sid:{sid}拆除装置timeout")
        # 1.0装置不需要下发拆除, 回复timeout认为成功
        flag = True
    return flag