elec_statistics_service.py 4.69 KB
Newer Older
lcn's avatar
lcn committed
1
from unify_api.modules.elec_charge.common.utils import \
ZZH's avatar
ZZH committed
2
    aver_price, power_charge_download, load_power_charge, today_yesterday_load
lcn's avatar
lcn committed
3 4 5 6 7 8 9 10 11 12
from unify_api.modules.elec_charge.components.elec_statistics_cps import \
    SlotValue
from unify_api.utils.time_format import last_time_str
from unify_api.constants import CO2_N
from unify_api.modules.elec_charge.components.elec_statistics_cps import \
    PcStatiResp
from pot_libs.utils.pendulum_wrapper import my_pendulum
from unify_api.utils.common_utils import round_2


ZZH's avatar
ZZH committed
13
async def power_charge_stats_srv(cid, point_id, start, end, date_type):
lcn's avatar
lcn committed
14 15 16
    # 2. 如果是日统计,则需要增加今日/昨日负荷曲线, 15min一个点
    if date_type == "day":
        # 电量电费
ZZH's avatar
ZZH committed
17 18
        kwh_sv, charge_sv = await load_power_charge([cid], point_id, start,
                                                    end, date_type)
lcn's avatar
lcn committed
19 20 21 22 23
        # 智电u增加碳足迹 电量*0.754
        co2_slot = kwh_sv.slots
        co2_value = [round_2(i * CO2_N) if i else i for i in kwh_sv.value]
        co2_sv = SlotValue(slots=co2_slot, value=co2_value)
        # 需要增加15min电量电费
ZZH's avatar
ZZH committed
24
        kwh_sv_15min, charge_sv_15min = await power_charge_download(
lcn's avatar
lcn committed
25 26
            cid, point_id, start, end)
        # 今日/昨日负荷曲线
ZZH's avatar
ZZH committed
27
        today_p = await today_yesterday_load(cid, point_id, start, end)
lcn's avatar
lcn committed
28
        ysd_start, ysd_end = last_time_str(start, end, "day")
ZZH's avatar
ZZH committed
29 30
        yesterday_p = await today_yesterday_load(cid, point_id, ysd_start,
                                                 ysd_end)
lcn's avatar
lcn committed
31 32 33 34 35
        return PcStatiResp(kwh=kwh_sv, charge=charge_sv, today_p=today_p,
                           yesterday_p=yesterday_p, kwh_15min=kwh_sv_15min,
                           charge_15min=charge_sv_15min, co2=co2_sv)
    elif date_type == "month":
        # 本月电量电费, 平均电价
ZZH's avatar
ZZH committed
36 37
        kwh_sv, charge_sv = await load_power_charge([cid], point_id, start,
                                                    end, date_type)
lcn's avatar
lcn committed
38 39 40 41 42 43 44 45
        this_aver_price = aver_price(kwh_sv, charge_sv)
        # 智电u增加碳足迹 电量*0.754
        co2_slot = kwh_sv.slots
        co2_value = [round_2(i * CO2_N) if i else i for i in kwh_sv.value]
        co2_sv = SlotValue(slots=co2_slot, value=co2_value)
        # 上月电量电费, 平均电价
        last_start, last_end = last_time_str(start, end, "month")
        # 需要增加15min电量电费
ZZH's avatar
ZZH committed
46 47 48 49
        last_kwh_sv, last_charge_sv = await load_power_charge([cid], point_id,
                                                              last_start,
                                                              last_end,
                                                              date_type)
lcn's avatar
lcn committed
50 51 52 53 54 55
        last_aver_price = aver_price(last_kwh_sv, last_charge_sv)
        return PcStatiResp(kwh=kwh_sv, charge=charge_sv,
                           this_aver_price=this_aver_price,
                           last_aver_price=last_aver_price, co2=co2_sv)
    elif date_type == "year":
        # 本月电量电费
ZZH's avatar
ZZH committed
56 57
        kwh_sv, charge_sv = await load_power_charge([cid], point_id, start,
                                                    end, date_type)
lcn's avatar
lcn committed
58 59 60 61 62 63 64 65 66 67 68
        this_aver_price = aver_price(kwh_sv, charge_sv)
        return PcStatiResp(kwh=kwh_sv, charge=charge_sv,
                           this_aver_price=this_aver_price)
    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
69 70
            kwh_sv, charge_sv = await load_power_charge([cid], point_id,
                                                        start, end, date_type)
lcn's avatar
lcn committed
71
            # 负荷曲线
ZZH's avatar
ZZH committed
72 73
            this_p = await today_yesterday_load(cid, point_id, start, end,
                                                is_range=1)
lcn's avatar
lcn committed
74
            # 需要增加15min电量电费
ZZH's avatar
ZZH committed
75
            kwh_sv_15min, charge_sv_15min = await power_charge_download(
lcn's avatar
lcn committed
76 77 78 79 80 81
                cid, point_id, start, end)
            return PcStatiResp(kwh=kwh_sv, charge=charge_sv, today_p=this_p,
                               kwh_15min=kwh_sv_15min,
                               charge_15min=charge_sv_15min)
        else:
            # 电量电费
ZZH's avatar
ZZH committed
82 83
            kwh_sv, charge_sv = await load_power_charge([cid], point_id,
                                                        start, end, date_type)
lcn's avatar
lcn committed
84 85 86 87
            # 平均电价
            this_aver_price = aver_price(kwh_sv, charge_sv)
            return PcStatiResp(kwh=kwh_sv, charge=charge_sv,
                               this_aver_price=this_aver_price)