elec_statistics.py 4.64 KB
Newer Older
lcn's avatar
lcn committed
1
from unify_api.modules.elec_charge.common.utils import \
ZZH's avatar
ZZH committed
2
    max_min_time, load_power_charge
lcn's avatar
lcn committed
3 4
from pot_libs.sanic_api import summary
from pot_libs.utils.pendulum_wrapper import my_pendulum
ZZH's avatar
ZZH committed
5
from unify_api.modules.common.procedures.pttl_max import load_pttl_max
lcn's avatar
lcn committed
6 7 8 9
from unify_api.modules.elec_charge.components.elec_statistics_cps import \
    PcStatiReq, PcStatiResp, MaxpReq, MaxpResp, PcmResp
from unify_api.utils.common_utils import round_2
from unify_api.modules.elec_charge.service.elec_statistics_service import \
ZZH's avatar
ZZH committed
10
    power_charge_stats_srv
lcn's avatar
lcn committed
11 12 13 14 15 16 17 18 19 20


@summary('电量电费统计曲线')
async def post_power_charge_stati(req, body: PcStatiReq) -> PcStatiResp:
    # 1.获取参数
    cid = body.cid
    point_id = body.point_id
    start = body.start
    end = body.end
    date_type = body.date_type
ZZH's avatar
ZZH committed
21
    return await power_charge_stats_srv(cid, point_id, start, end, date_type)
lcn's avatar
lcn committed
22 23 24 25 26


@summary("小程序最高负荷/web最大需量")
async def post_max_p(req, body: MaxpReq) -> MaxpResp:
    cid = body.cid
ZZH's avatar
ZZH committed
27
    pid = body.point_id
lcn's avatar
lcn committed
28 29
    start = body.start
    end = body.end
ZZH's avatar
ZZH committed
30
    max_val, max_val_time = await load_pttl_max(cid, start, end, point_id=pid)
lcn's avatar
lcn committed
31 32 33 34 35 36 37 38 39 40 41
    return MaxpResp(maxp=max_val, date_time=max_val_time)


@summary('电量电费/最多最少最高负荷')
async def post_power_charge_min_max(req, body: PcStatiReq) -> PcmResp:
    # 1.获取参数
    cid = body.cid
    pid = body.point_id
    start = body.start
    end = body.end
    date_type = body.date_type
ZZH's avatar
ZZH committed
42
    return await power_charge_min_max_srv(cid, pid, start, end, date_type)
lcn's avatar
lcn committed
43 44


ZZH's avatar
ZZH committed
45
async def power_charge_min_max_srv(cid, pid, start, end, date_type):
lcn's avatar
lcn committed
46 47 48 49 50 51
    # 初始化返回值
    max_p = {"value": "", "time": ""}
    max_kwh = {"value": "", "time": ""}
    min_kwh = {"value": "", "time": ""}
    max_charge = {"value": "", "time": ""}
    min_charge = {"value": "", "time": ""}
ZZH's avatar
ZZH committed
52
    max_val, max_val_time = await load_pttl_max(cid, start, end, point_id=pid)
lcn's avatar
lcn committed
53 54 55 56
    max_p["value"] = round_2(max_val)
    max_p["time"] = max_val_time
    # 2. 如果是日统计,则需要增加今日/昨日负荷曲线, 15min一个点
    if date_type == "day":
ZZH's avatar
ZZH committed
57 58
        kwh_sv, charge_sv = await load_power_charge([cid], pid, start, end,
                                                    date_type)
lcn's avatar
lcn committed
59 60 61 62 63 64
        k_max_v, k_max_time, k_min_v, k_min_time = \
            max_min_time(kwh_sv, add_one_index=True)
        c_max_v, c_max_time, c_min_v, c_min_time = \
            max_min_time(charge_sv, add_one_index=True)
    elif date_type == "month":
        # 本月电量电费, 平均电价
ZZH's avatar
ZZH committed
65 66
        kwh_sv, charge_sv = await load_power_charge([cid], pid, start, end,
                                                    date_type)
lcn's avatar
lcn committed
67 68 69 70
        k_max_v, k_max_time, k_min_v, k_min_time = max_min_time(kwh_sv)
        c_max_v, c_max_time, c_min_v, c_min_time = max_min_time(charge_sv)
    elif date_type == "year":
        # 本月电量电费
ZZH's avatar
ZZH committed
71 72
        kwh_sv, charge_sv = await load_power_charge([cid], pid, start, end,
                                                    date_type)
lcn's avatar
lcn committed
73 74 75 76 77 78 79 80 81 82
        k_max_v, k_max_time, k_min_v, k_min_time = max_min_time(kwh_sv)
        c_max_v, c_max_time, c_min_v, c_min_time = max_min_time(charge_sv)
    else:
        start_f = my_pendulum.from_format(start, 'YYYY-MM-DD HH:mm:ss')
        end_f = my_pendulum.from_format(end, 'YYYY-MM-DD HH:mm:ss')
        diff_mm = (end_f - start_f).in_minutes()
        if diff_mm <= 48 * 60:
            # 自定义选时范围,不需要最后时间的数据,解决bug
            end = end_f.subtract(minutes=1).format("YYYY-MM-DD HH:mm:ss")
            # 电量电费
ZZH's avatar
ZZH committed
83 84
            kwh_sv, charge_sv = await load_power_charge([cid], pid, start,
                                                        end, date_type)
lcn's avatar
lcn committed
85 86 87 88 89 90
            k_max_v, k_max_time, k_min_v, k_min_time = \
                max_min_time(kwh_sv, add_one_index=True, in_2_day=True)
            c_max_v, c_max_time, c_min_v, c_min_time = \
                max_min_time(charge_sv, add_one_index=True, in_2_day=True)
        else:
            # 电量电费
ZZH's avatar
ZZH committed
91 92
            kwh_sv, charge_sv = await load_power_charge([cid], pid, start,
                                                        end, date_type)
lcn's avatar
lcn committed
93 94 95 96 97 98 99 100 101 102 103 104
            k_max_v, k_max_time, k_min_v, k_min_time = max_min_time(kwh_sv)
            c_max_v, c_max_time, c_min_v, c_min_time = max_min_time(charge_sv)
    max_kwh["value"] = round_2(k_max_v)
    max_kwh["time"] = k_max_time
    min_kwh["value"] = round_2(k_min_v)
    min_kwh["time"] = k_min_time
    max_charge["value"] = round_2(c_max_v)
    max_charge["time"] = c_max_time
    min_charge["value"] = round_2(c_min_v)
    min_charge["time"] = c_min_time
    return PcmResp(max_p=max_p, max_kwh=max_kwh, min_kwh=min_kwh,
                   max_charge=max_charge, min_charge=min_charge)