Commit 5be6d7c4 authored by ZZH's avatar ZZH

change real time electric load from redis 2023-6-25

parent 27de7a5d
import json
import pandas as pd import pandas as pd
import pendulum
from pot_libs.settings import SETTING from pot_libs.settings import SETTING
from pot_libs.logger import log
from pot_libs.utils.exc_util import BusinessException from pot_libs.utils.exc_util import BusinessException
from pot_libs.aredis_util.aredis_utils import RedisUtils
from unify_api.constants import ( from unify_api.constants import (
POINT_LEVEL_MAP, U_THRESHOLD, COSTTL_THRESHOLD, LF_THRESHOLD, POINT_LEVEL_MAP, U_THRESHOLD, COSTTL_THRESHOLD, LF_THRESHOLD,
THDU_THRESHOLD, BL_THRESHOLD, THDI_THRESHOLD THDU_THRESHOLD, BL_THRESHOLD, THDI_THRESHOLD
) )
from unify_api.modules.common.procedures.points import points_by_storeys, \ from unify_api.modules.common.procedures.points import points_by_storeys, \
get_meter_by_point get_meter_by_point
from unify_api.modules.common.service.td_engine_service import \
get_td_engine_data
from unify_api.modules.electric.dao.electric_dao import \ from unify_api.modules.electric.dao.electric_dao import \
monitor_point_join_by_points, get_electric_datas_dao monitor_point_join_by_points, get_electric_datas_dao
from unify_api.modules.electric.procedures.electric_pds import \ from unify_api.modules.electric.procedures.electric_pds import \
...@@ -18,13 +20,13 @@ from unify_api.utils.common_utils import round_2, round_4, multiplication_two ...@@ -18,13 +20,13 @@ from unify_api.utils.common_utils import round_2, round_4, multiplication_two
from unify_api.modules.electric.procedures.electric_util import \ from unify_api.modules.electric.procedures.electric_util import \
get_wiring_type, load_point_ctnum get_wiring_type, load_point_ctnum
from datetime import datetime from datetime import datetime
from unify_api.constants import REAL_EXP_TIME
from unify_api.modules.common.procedures.location_temp_rcurrent import \ from unify_api.modules.common.procedures.location_temp_rcurrent import \
location_stats_statics location_stats_statics
from unify_api.modules.electric.components.electric import ( from unify_api.modules.electric.components.electric import (
ElecIndexResponse, ElecIndex, EscResp, QcsResp, EclResp, QclResp, ElecIndexResponse, ElecIndex, EscResp, QcsResp, EclResp, QclResp,
) )
from unify_api.utils.taos_new import parse_td_columns from unify_api.utils.time_format import CST, YMD_Hms, timestamp2dts
from unify_api.utils.time_format import get_15min_ago
async def elec_current_storeys_service(storeys): async def elec_current_storeys_service(storeys):
...@@ -33,7 +35,7 @@ async def elec_current_storeys_service(storeys): ...@@ -33,7 +35,7 @@ async def elec_current_storeys_service(storeys):
point_list = await points_by_storeys(storeys) point_list = await points_by_storeys(storeys)
# mtids # mtids
mtids = [i.get("mtid") for i in point_list] mtids = [i.get("mtid") for i in point_list]
cid = point_list[0]['cid'] if len(point_list) > 0 else 0 cid = point_list[0]["cid"] if len(point_list) > 0 else 0
# 2.获取mid, ctnum # 2.获取mid, ctnum
# point_mid = await batch_get_wiring_type(points) # point_mid = await batch_get_wiring_type(points)
# # 3. 获取redis数据 # # 3. 获取redis数据
...@@ -70,8 +72,8 @@ async def elec_current_storeys_service(storeys): ...@@ -70,8 +72,8 @@ async def elec_current_storeys_service(storeys):
"freq": round_2(res[mtid].get("freq")), "freq": round_2(res[mtid].get("freq")),
"costtl": round_2(res[mtid].get("costtl")), "costtl": round_2(res[mtid].get("costtl")),
"lf": round_2(res[mtid].get("lf")), "lf": round_2(res[mtid].get("lf")),
"sdu_i": res[mtid].get('sdu_i'), "sdu_i": res[mtid].get("sdu_i"),
"sdu_u": res[mtid].get('sdu_u'), "sdu_u": res[mtid].get("sdu_u"),
} }
# redis数据过期,或者没有数据 # redis数据过期,或者没有数据
else: else:
...@@ -124,7 +126,7 @@ async def qual_current_storeys_service(storeys): ...@@ -124,7 +126,7 @@ async def qual_current_storeys_service(storeys):
# # 3. 获取redis数据 # # 3. 获取redis数据
# res = await qual_current_data(point_mid) # res = await qual_current_data(point_mid)
mtids = [point["mtid"] for point in point_list if point["mtid"]] mtids = [point["mtid"] for point in point_list if point["mtid"]]
cid = point_list[0]['cid'] if len(point_list) > 0 else 0 cid = point_list[0]["cid"] if len(point_list) > 0 else 0
res = await elec_current_data(mtids, cid) res = await elec_current_data(mtids, cid)
# 4. 返回数据 # 4. 返回数据
qual_data = {} qual_data = {}
...@@ -208,7 +210,7 @@ async def elec_card_level_service(point_list): ...@@ -208,7 +210,7 @@ async def elec_card_level_service(point_list):
# res_redis = await elec_current_data(point_mid) # res_redis = await elec_current_data(point_mid)
mtids = [monitor["mtid"] for monitor in monitor_point_list if mtids = [monitor["mtid"] for monitor in monitor_point_list if
monitor["mtid"]] monitor["mtid"]]
cid = monitor_point_list[0]['cid'] if len(monitor_point_list) > 0 else 0 cid = monitor_point_list[0]["cid"] if len(monitor_point_list) > 0 else 0
results = await elec_current_data(mtids, cid) results = await elec_current_data(mtids, cid)
# 4. 返回数据 # 4. 返回数据
ret_data = { ret_data = {
...@@ -302,7 +304,7 @@ async def qual_current_level_service(point_list): ...@@ -302,7 +304,7 @@ async def qual_current_level_service(point_list):
# res_redis = await qual_current_data(point_mid) # res_redis = await qual_current_data(point_mid)
mtids = [monitor["mtid"] for monitor in monitor_point_list if mtids = [monitor["mtid"] for monitor in monitor_point_list if
monitor["mtid"]] monitor["mtid"]]
cid = monitor_point_list[0]['cid'] if len(monitor_point_list) > 0 else 0 cid = monitor_point_list[0]["cid"] if len(monitor_point_list) > 0 else 0
res = await elec_current_data(mtids, cid) res = await elec_current_data(mtids, cid)
# 4. 返回数据 # 4. 返回数据
ret_data = { ret_data = {
...@@ -583,32 +585,49 @@ async def elec_index_service(cid, point_id, start, end): ...@@ -583,32 +585,49 @@ async def elec_index_service(cid, point_id, start, end):
async def elec_current_service(point_id): async def elec_current_service(point_id):
# 获取mtid # 获取mtid
meter_info = await get_meter_by_point(point_id) p_info = await get_meter_by_point(point_id)
if not meter_info: if not p_info or not p_info["mtid"]:
raise BusinessException( msg = f"没有监测点:{point_id} monitor信息,请联系运维人员!"
message="没有该监测点的monitor信息,请联系运维人员!") raise BusinessException(message=msg)
mtid = meter_info["mtid"]
last_15min_time = get_15min_ago()
# 获取子表中的实时数据
url = f"{SETTING.stb_url}db_electric?tz=Asia/Shanghai"
sql = f"select last_row(*) from mt{mtid}_ele where ts>='{last_15min_time}'"
is_succ, results = await get_td_engine_data(url, sql)
if not is_succ or not results or results.get("code") > 0:
return '', {}
head = parse_td_columns(results)
if not results["data"]:
results["data"] = ['' for i in range(len(head))]
res = dict(zip(head, results["data"][0]))
# 识电U只有一项有数据,返回具体的项 now_ts = pendulum.now(tz=CST).int_timestamp
ctnum = res.get("ctnum") or 3 d_ele_rt, ts = None, now_ts
res = get_sdu_i_and_u(res, ctnum) try:
mtid = p_info["mtid"]
key = f"real_time:electric:{SETTING.mysql_db}:{mtid}"
key_hr = f"real_time:electric_hr:{SETTING.mysql_db}:{mtid}"
rt_ele, rt_ele_hr = await RedisUtils().mget([key, key_hr])
if rt_ele and rt_ele_hr:
rt_ele, rt_ele_hr = json.loads(rt_ele), json.loads(rt_ele_hr)
if now_ts - rt_ele["ts"] <= REAL_EXP_TIME:
if now_ts - rt_ele_hr["ts"] <= REAL_EXP_TIME:
for k in rt_ele_hr.keys():
if k not in rt_ele.keys():
rt_ele[k] = rt_ele_hr[k]
if res.get("ts"): d_ele_rt, ts = rt_ele, rt_ele["ts"]
time_str = str(res["ts"])[0:19]
else: elif rt_ele:
time_str = time_format.get_datetime_str(0) rt_ele = json.loads(rt_ele)
return time_str, res if now_ts - rt_ele["ts"] <= REAL_EXP_TIME:
d_ele_rt, ts = rt_ele, rt_ele["ts"]
elif rt_ele_hr:
rt_ele_hr = json.loads(rt_ele_hr)
if now_ts - rt_ele_hr["ts"] <= REAL_EXP_TIME:
d_ele_rt, ts = rt_ele_hr, rt_ele_hr["ts"]
except Exception as e:
log.error(f"parse real time electric error, pid:{point_id}")
log.exception(e)
time_str = timestamp2dts(ts, YMD_Hms)
if d_ele_rt is None:
return time_str, None
# 识电U只有一项有数据,返回具体的项
ctnum = d_ele_rt.get("ctnum") or 3
return time_str, get_sdu_i_and_u(d_ele_rt, ctnum)
def get_sdu_i_and_u(res, ctnum): def get_sdu_i_and_u(res, ctnum):
...@@ -617,15 +636,15 @@ def get_sdu_i_and_u(res, ctnum): ...@@ -617,15 +636,15 @@ def get_sdu_i_and_u(res, ctnum):
''' '''
res["sdu_i"] = None res["sdu_i"] = None
res["sdu_u"] = None res["sdu_u"] = None
meter_n = res.get('meter_sn', '').lower() meter_sn = res.get("meter_sn", "").lower()
if meter_n == 'a': if meter_sn == "a":
res["sdu_i"] = "ia" res["sdu_i"] = "ia"
res["sdu_u"] = "ua" if ctnum == 3 else "uab" res["sdu_u"] = "ua" if ctnum == 3 else "uab"
if meter_n == 'b': if meter_sn == "b":
res["sdu_i"] = "ib" res["sdu_i"] = "ib"
if ctnum == 3: if ctnum == 3:
res["sdu_u"] = "ub" res["sdu_u"] = "ub"
if meter_n == 'c': if meter_sn == "c":
res["sdu_i"] = "ic" res["sdu_i"] = "ic"
res["sdu_u"] = "uc" if ctnum == 3 else "ucb" res["sdu_u"] = "uc" if ctnum == 3 else "ucb"
return res return res
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment