drop_dust_service.py 9.98 KB
Newer Older
lcn's avatar
lcn committed
1 2 3
from unify_api.constants import SLOTS_15MIN, DUST_STATE
from unify_api.modules.common.dao.common_dao import storey_pl_by_cid, \
    storey_wp_by_cid
wang.wenrong's avatar
wang.wenrong committed
4
from unify_api.modules.common.dao.common_dao import \
ZZH's avatar
ZZH committed
5
     load_point_pttl_mean
lcn's avatar
lcn committed
6 7 8 9 10 11 12 13
from unify_api.modules.common.procedures.points import points_by_storeys
from unify_api.modules.tsp_water.components.drop_dust_cps import DdwResp, \
    DdResp, IrmResp, IosResp, ItiResp, WsStatiResp
from unify_api.modules.tsp_water.dao.drop_dust_dao import \
    query_dust_fogcan_run_state, query_dust_water_run_state, \
    dust_water_run_state_by_time, sum_water_runts_group, sum_kwh_runts_group, \
    dust_water_run_day_sum_water, dust_fogcan_run_day_sum_kwh, sum_water_group
from unify_api.modules.tsp_water.service.tsp_service import day_env_service,\
ZZH's avatar
ZZH committed
14
    day_env_service
lcn's avatar
lcn committed
15 16 17
from unify_api.utils.common_utils import round_2
from unify_api.utils.time_format import srv_time, last7_day_range, \
    start_end_date
ZZH's avatar
ZZH committed
18 19
from unify_api.modules.tsp_water.procedures.tsp_pds import  \
    per_hour_wave, per_hour_kwh_wave
lcn's avatar
lcn committed
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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


async def post_drop_dust_service(storeys):
    """降尘措施-卡片信息"""
    now_date, timestamp = srv_time()
    now_ymd = now_date.split(" ")[0]
    # 1.获取storey信息
    storey_list = await points_by_storeys(storeys)
    # 雾炮point_id
    point_list = [i["point_id"] for i in storey_list if
                  i["storey_name"] == "雾炮"]
    # 喷淋water_id
    water_list = [i["point_id"] for i in storey_list if
                  i["storey_name"] == "喷淋"]
    # 2.mysql取雾炮数据
    dust_dic = {}
    if point_list:
        dust_list = await query_dust_fogcan_run_state(point_list, now_ymd)
        dust_dic = {i["pid"]: i for i in dust_list if i}
    # 3.mysql取喷淋数据
    water_dic = {}
    if water_list:
        water_list = await query_dust_water_run_state(water_list, now_ymd)
        water_dic = {i["water_id"]: i for i in water_list if i}
    # 4.构造返回
    return_data = {}
    for point in storey_list:
        storey_name = point.get("storey_name")
        storey_id = point.get("storey_id")
        point_id = point.get("point_id")
        room_name = point.get("room_name")
        # 4.1 雾炮数据
        if point_id in dust_dic:
            # 日运行时间
            day_run_time = dust_dic[point_id]["day_run_times"]
            # 日累计用电量
            day_kwh = dust_dic[point_id]["day_kwh_diff"]
            # 开启状态
            state = DUST_STATE[dust_dic[point_id]["open_state"]]
            # 构造返回
            res_dic = {
                "room_name": room_name,
                "storey_id": storey_id,
                "point_id": point_id,
                "day_run_time": day_run_time,
                "day_kwh": day_kwh,
                "state": state,
            }
            if storey_name in return_data:
                return_data[storey_name].append(res_dic)
            else:
                return_data[storey_name] = [res_dic]
        # 4.1 喷淋数据
        if point_id in water_dic:
            # 日运行时间
            day_run_time = water_dic[point_id]["day_run_times"]
            # 日用水量
            day_water = water_dic[point_id]["day_water_diff"]
            # 开启状态
            state = DUST_STATE[water_dic[point_id]["open_state"]]
            # 构造返回
            res_dic = {
                "room_name": room_name,
                "storey_id": storey_id,
                "point_id": point_id,
                "day_run_time": day_run_time,
                "day_kwh": day_water,
                "state": state,
            }
            if storey_name in return_data:
                return_data[storey_name].append(res_dic)
            else:
                return_data[storey_name] = [res_dic]
    # 5. 转换成list格式返回
    if return_data:
        # 房间排序, 并返回数据转化为list
        return_list = [{"name": key, "storey_id": value[0]["storey_id"],
                        "room_data": sorted(value,
                                            key=lambda i: i["point_id"])}
                       for key, value in return_data.items()]
        # 楼层排序
        return_list = sorted(return_list, key=lambda x: x["storey_id"])
    else:
        return_list = []
    return DdResp(res_data=return_list)


async def post_drop_dust_wave_service(point_id, start, end):
    """降尘措施-雾炮-运行曲线"""
    # 1. 获取聚合信息
    slots_list = SLOTS_15MIN
ZZH's avatar
ZZH committed
111
    sql_re = await load_point_pttl_mean(start, end, point_id)
lcn's avatar
lcn committed
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    if not sql_re:
        return DdwResp(slots=[], value=[])
    es_re_dic = {str(i["create_time"])[-8:-3]: i for i in sql_re}
    p_list = []
    for slot in slots_list:
        if slot in es_re_dic:
            pttl_mean = round_2(es_re_dic[slot]["pttl_mean"])
            p_list.append(pttl_mean)
        else:
            p_list.append("")
    return DdwResp(slots=slots_list, value=p_list)


async def drop_water_wave_service(point_id, start, end):
    """降尘措施-雾炮-运行曲线"""
    # 1. 获取聚合信息
    slots_list = SLOTS_15MIN
    water_list = await dust_water_run_state_by_time(start, end, point_id)
    if not water_list:
        return DdwResp(slots=[], value=[])
    water_dic = {str(i["quarter_time"]).split(" ")[1][:5]: i for i in
                 water_list}
    water_list = []
    for slot in slots_list:
        if slot in water_dic:
            water_data = round_2(water_dic[slot]["qr_water_diff"])
            water_list.append(water_data)
        else:
            water_list.append("")
    return DdwResp(slots=slots_list, value=water_list)


async def index_run_monit_service(cid):
    """首页-运行监测-统计"""
    now_date, timestamp = srv_time()
    now_ymd = now_date.split(" ")[0]
    # 1. 喷淋
    water_start = 0
    water_stop = 0

    water_list = await storey_pl_by_cid(cid)
    water_id_list = [i["point_id"] for i in water_list]
    water_info = await query_dust_water_run_state(water_id_list, now_ymd)
    # 每个water_id保留最新一条记录
    water_dic = {i["water_id"]: i for i in water_info if i}
    for key, value in water_dic.items():
        state = value["open_state"]
        if state == 0:
            water_stop += 1
        else:
            water_start += 1
    # 2. 雾炮
    gun_start = 0
    gun_stop = 0

    gun_list = await storey_wp_by_cid(cid)
    gun_id_list = [i["point_id"] for i in gun_list]
    gun_info = await query_dust_fogcan_run_state(gun_id_list, now_ymd)
    # 每个gun_id保留最新一条记录
    gun_dic = {i["pid"]: i for i in gun_info if i}
    for key, value in gun_dic.items():
        state = value["open_state"]
        if state == 0:
            gun_stop += 1
        else:
            gun_start += 1
    return IrmResp(
        water={"start": water_start, "stop": water_stop},
        fog_gun={"start": gun_start, "stop": gun_stop},
    )


async def index_op_stat_service(cid):
    """首页-运行统计-扬尘"""
    # 1. 获取最近7天起始时间, 不包括今天
    s, e = last7_day_range()
    start = s.split(" ")[0]
    end = e.split(" ")[0]
    # 2. 喷淋
    # water_id和name关系
    water_list = await storey_pl_by_cid(cid)
    water_map = {water["point_id"]: water for water in water_list}
    # 运行时间和用水量
    water_sum = await sum_water_runts_group(start, end)
    if not water_sum:
        return IosResp(water=[], fog_gun=[])
    water_res = []
    for water in water_sum:
        run_time = round_2(water["SUM(run_ts)"])
        use_water = round_2(water["SUM(water_total)"])
        name = water_map[water["water_id"]]["room_name"]
        water_res.append(
            {"name": name, "run_time": run_time, "use_water": use_water}
        )
    water_res = sorted(water_res, key=lambda x: x["use_water"], reverse=True)
    # 3.雾炮
    gun_list = await storey_wp_by_cid(cid)
    gun_map = {gun["point_id"]: gun for gun in gun_list}
    # 运行时间和用电量
    gun_sum = await sum_kwh_runts_group(start, end)
    gun_res = []
    for gun in gun_sum:
        run_time_gun = round_2(gun["SUM(run_ts)"])
        use_kwh = round_2(gun["SUM(kwh_total)"])
        name_gun = gun_map[gun["pid"]]["room_name"]
        gun_res.append(
            {"name": name_gun, "run_time": run_time_gun, "use_kwh": use_kwh}
        )
    gun_res = sorted(gun_res, key=lambda x: x["use_kwh"], reverse=True)
    return IosResp(
        water=water_res,
        fog_gun=gun_res
    )


async def index_today_info_service(cid):
    """首页-今日数据-扬尘"""
    # 1. 环境信息
    # dr = await day_env_service(cid)
ZZH's avatar
ZZH committed
231
    dr = await day_env_service(cid)
lcn's avatar
lcn committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    # 2. 今日用水, 用电
    today_start, today_end, m_start, m_end = start_end_date()
    start = today_start.split(" ")[0]
    end = today_end.split(" ")[0]

    sum_water = await dust_water_run_day_sum_water(start)
    sum_kwh = await dust_fogcan_run_day_sum_kwh(start)
    return ItiResp(
        pm2_5=dr.pm2_5,
        pm10=dr.pm10,
        tsp=dr.tsp,
        today_water=round_2(sum_water["sum(water_total)"]),
        today_kwh=round_2(sum_kwh["sum(kwh_total)"]),
    )


async def index_water_service(start, end, date_type):
    # 用电量 pm2.5 pm10 tsp
    # pm25_list, pm10_list, tsp_list, slots = await per_hour_wave(start, end)
ZZH's avatar
ZZH committed
251
    pm25_list, pm10_list, tsp_list, slots = await per_hour_wave(start, end)
lcn's avatar
lcn committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    water_info = await sum_water_group(start, end, date_type)
    water_list = ['' for _ in range(len(slots))]
    for index, info in enumerate(water_info):
        water_list[index] = round_2(info.get("water"))
    return WsStatiResp(
        pm2_5=pm25_list,
        pm10=pm10_list,
        tsp=tsp_list,
        slots=slots,
        water_or_electric=water_list,
    )


async def index_electric_service(cid, start, end):
    storey_list = await storey_wp_by_cid(cid)
    point_list = [storey["point_id"] for storey in storey_list]
ZZH's avatar
ZZH committed
268 269
    pm25_list, pm10_list, tsp_list, slots = await per_hour_wave(start, end)
    kwh_res, slots = await per_hour_kwh_wave(start, end, point_list)
lcn's avatar
lcn committed
270 271 272 273 274 275 276
    return WsStatiResp(
        pm2_5=pm25_list,
        pm10=pm10_list,
        tsp=tsp_list,
        slots=slots,
        water_or_electric=kwh_res,
    )