adio_card.py 6.75 KB
Newer Older
lcn's avatar
lcn committed
1 2
import asyncio
import hashlib
lcn's avatar
lcn committed
3 4 5 6
import json
import time
import aioredis

lcn's avatar
lcn committed
7
from pot_libs.aredis_util.aredis_utils import RedisClient, RedisUtils
lcn's avatar
lcn committed
8
from pot_libs.settings import SETTING
lcn's avatar
lcn committed
9
from pot_libs.utils import time_format
lcn's avatar
lcn committed
10 11
from unify_api import constants
from unify_api.constants import POINT_LEVEL_MAP
lcn's avatar
lcn committed
12
from unify_api.modules.adio.components.adio import AdioIndex
lcn's avatar
lcn committed
13 14 15
from unify_api.modules.adio.components.adio_card_cps import AcResp
from unify_api.modules.adio.dao.adio_card_dao import \
    monitor_location_join_by_locations, alarm_setting_by_locations
lcn's avatar
lcn committed
16 17
from unify_api.modules.adio.dao.adio_dao import get_location_15min_dao, \
    get_location_dao
lcn's avatar
lcn committed
18 19
from unify_api.modules.common.procedures.list_point_pds import \
    monitor_map_point_location
lcn's avatar
lcn committed
20
from unify_api.utils.common_utils import round_2
lcn's avatar
lcn committed
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

ADIO_CURRENT = "adio_current"


async def post_adio_card_service(location_list, cid):
    """安全监测-卡片信息-level"""
    # 构造cid下每个monitor包含的point和location
    monitor_p_dic, monitor_l_dic = await monitor_map_point_location(cid)
    # 1. 获取每个location_id的详细信息, mysql的in查询,打乱了location_list顺序
    monitor_location_list = await monitor_location_join_by_locations(
        location_list)
    # 2. 查询温度漏电流阈值
    alarm_setting_list = await alarm_setting_by_locations(location_list)
    alarm_dic = {alarm["lid"]: alarm for alarm in alarm_setting_list}
    # 3. 根据location_list查询redis
    # multi_exec查询redis
    redis = await aioredis.create_redis_pool(SETTING.redis_single)
    tr = redis.multi_exec()
    for location_id in location_list:
        tr.hget(ADIO_CURRENT, location_id)
    res_redis = await tr.execute()
    # 3. 构造返回
    ret_data = {
        "inline": {},
        "transformer": {},
        "feeder": {},
        "power_dist": {},
        "device": {}
    }
    i = 0
    for info in monitor_location_list:
        m_name = info.get("name")
        mtid = info.get("mtid")
        m_type = POINT_LEVEL_MAP.get(info.get("m_type"))
        location_id = info.get("lid")
        location_type = info.get("ad_type")
        location_item = info.get("item")
        # redis数据
        adio_value = ""
        redis_data = res_redis[i]
        i += 1
        if redis_data:
            adio_info = json.loads(redis_data)
            time_now = int(time.time())
            real_tt = adio_info.get("timestamp", 0)
            if (time_now - real_tt) <= constants.REAL_EXP_TIME:
                adio_value = round(adio_info.get("value", 0), 2)
lcn's avatar
lcn committed
68
        
lcn's avatar
lcn committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 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
        threshold = alarm_dic[location_id]["threshold"]
        if m_name in ret_data[m_type]:
            if location_type == "temperature":
                ret_data[m_type][m_name].setdefault(location_type, []).append(
                    {"item": location_item, "value": adio_value}
                )
                # 阈值取最小数值
                if threshold < ret_data[m_type][m_name]["t_threshold"]:
                    ret_data[m_type][m_name]["t_threshold"] = threshold
            else:
                ret_data[m_type][m_name].setdefault(location_type, []).append(
                    {"item": "漏电流", "value": adio_value}
                )
                ret_data[m_type][m_name]["r_threshold"] = threshold
            # 卡片增加location_id字段
            ret_data[m_type][m_name]["location_id"].append(location_id)
        else:
            if location_type == "temperature":
                ret_data[m_type][m_name] = {
                    "name": m_name,
                    "t_threshold": threshold,
                    "temperature": [
                        {"item": location_item, "value": adio_value}]
                }
            else:
                ret_data[m_type][m_name] = {
                    "name": m_name,
                    "r_threshold": threshold,
                    "residual_current": [
                        {"item": "漏电流", "value": adio_value}]
                }
            # 卡片增加location_id字段
            ret_data[m_type][m_name]["location_id"] = [location_id]
            # 卡片增加point_id字段
            ret_data[m_type][m_name]["point_id"] = monitor_p_dic[mtid]
    # 返回内容,转换为list
    for key, value in ret_data.items():
        if value:
            ret_data[key] = [j for i, j in value.items()]
        else:
            ret_data[key] = []
    return AcResp(
        inline=ret_data["inline"],
        transformer=ret_data["transformer"],
        feeder=ret_data["feeder"],
        power_dist=ret_data["power_dist"],
        device=ret_data["device"],
    )
lcn's avatar
lcn committed
117 118


lcn's avatar
lcn committed
119
async def adio_index_service(location_group, start, end):
lcn's avatar
lcn committed
120 121 122 123 124 125 126 127
    # # load location表信息
    location_info = await get_location_dao(location_group)
    intervel, _ = time_format.time_pick_transf(start, end)
    tasks = [get_location_15min_service(
        location_info, lid, start, end, intervel) for lid in
        location_group]
    adio_index_result = await asyncio.gather(*tasks)
    datas = list(adio_index_result)
lcn's avatar
lcn committed
128 129 130 131 132 133 134
    adio_indexes = []
    for data in datas:
        adio_index = AdioIndex(**data)
        adio_indexes.append(adio_index)
    return adio_indexes


lcn's avatar
lcn committed
135
async def get_location_15min_service(location_info, lid, start, end, intervel):
lcn's avatar
lcn committed
136 137
    value_max, value_min, value_avg = [], [], []
    value_max_time, value_min_time = [], []
lcn's avatar
lcn committed
138 139 140 141 142
    if intervel == 24 * 3600:
        table_name = "location_1day_aiao"
    else:
        table_name = "location_15min_aiao"
    datas = await get_location_15min_dao(lid, start, end, table_name)
lcn's avatar
lcn committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    for data in datas:
        value_max.append(data.get("value_max"))
        value_min.append(data.get("value_min"))
        value_avg.append(data.get("value_avg"))
        value_max_time.append(data.get("value_max_time"))
        value_min_time.append(data.get("value_min_time"))
    if value_max:
        value_max_max = max([m for m in value_max])
        value_max_max_index = value_max.index(value_max_max)
        value_max_time_data = value_max_time[value_max_max_index]
    else:
        value_max_max, value_max_time_data = "", ""
    if value_min:
        value_min_min = min([m for m in value_min])
        value_min_min_index = value_min.index(value_min_min)
        value_min_time_data = value_min_time[value_min_min_index]
    else:
        value_min_min, value_min_time_data = "", ""
    value_avg_list = [m for m in value_avg]
    value_avg_data = sum(value_avg_list) / len(value_avg_list) \
        if value_avg_list else 0
lcn's avatar
lcn committed
164 165 166 167 168 169 170 171 172
    adio_index = dict({
        "type": location_info[lid]["type"],
        "item": location_info[lid]["item"],
        "max": round_2(value_max_max),
        "max_time": str(value_max_time_data) if value_max_time_data else "",
        "min": round_2(value_min_min),
        "min_time": str(value_min_time_data) if value_min_time_data else "",
        "avg": round_2(value_avg_data),
    })
lcn's avatar
lcn committed
173
    return adio_index