fix : em enm wpm 修改更新设备运行状态

This commit is contained in:
zty 2024-09-25 16:03:59 +08:00
parent 73aa186293
commit 008219e9ba
3 changed files with 15 additions and 7 deletions

View File

@ -8,6 +8,9 @@ from django.core.cache import cache
from apps.utils.tasks import ctask_run from apps.utils.tasks import ctask_run
import logging import logging
from apps.mtm.services import mgroup_run_change from apps.mtm.services import mgroup_run_change
import random
import time
myLogger = logging.getLogger("log") myLogger = logging.getLogger("log")
baseTime = datetime.datetime(year=1990, month=4, day=4, hour=0, minute=0, second=0, tzinfo=tz.gettz(settings.TIME_ZONE)) baseTime = datetime.datetime(year=1990, month=4, day=4, hour=0, minute=0, second=0, tzinfo=tz.gettz(settings.TIME_ZONE))
@ -27,20 +30,22 @@ def set_eq_rs(equipId, last_timex: datetime, last_mrs: int):
eq_rs_change = False eq_rs_change = False
ten_minutes = datetime.timedelta(minutes=10) ten_minutes = datetime.timedelta(minutes=10)
now = datetime.datetime.now() now = datetime.datetime.now()
mgroup_sort_trigger = False
if eq_rs_cache["running_state"] != last_mrs: # 如果状态变动了要调用方法否则只需更新缓存 if eq_rs_cache["running_state"] != last_mrs: # 如果状态变动了要调用方法否则只需更新缓存
eq_rs_change = True eq_rs_change = True
update_time = now update_time = now
elif now > eq_rs_cache.get('update_time', baseTime) + ten_minutes: # 如果当前时间大于缓存时间10分钟则更新 elif now > eq_rs_cache.get('update_time', baseTime) + ten_minutes: # 如果当前时间大于缓存时间10分钟则更新
eq_rs_change = True eq_rs_change = True
update_time = now update_time = now
mgroup_sort_trigger = True
else: else:
update_time = eq_rs_cache.get('update_time', baseTime) update_time = eq_rs_cache.get('update_time', baseTime)
cache.set(f"equipment_{equipId}", {"running_state": last_mrs, "running_state_timex": last_timex, "update_time":update_time}, timeout=None) # 更新缓存 cache.set(f"equipment_{equipId}", {"running_state": last_mrs, "running_state_timex": last_timex, "update_time":update_time}, timeout=None) # 更新缓存
if eq_rs_change: if eq_rs_change:
ctask_run.delay("apps.em.services.shutdown_or_startup", equipId, last_timex, last_mrs) ctask_run.delay("apps.em.services.shutdown_or_startup", equipId, last_timex, last_mrs, mgroup_sort_trigger)
def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs, note: str = ""): def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs, note: str = "", mgroup_sort_trigger: bool = False):
""" """
last_mrs 设备运行状态值 last_mrs 设备运行状态值
""" """
@ -70,6 +75,8 @@ def shutdown_or_startup(equipId: str, last_timex: datetime, last_mrs, note: str
new_run = True new_run = True
if new_run is not None: if new_run is not None:
if mgroup_sort_trigger:
time.sleep(mgroup.sort)
mgroup_run_change(mgroup, new_run, last_timex, note) mgroup_run_change(mgroup, new_run, last_timex, note)

View File

@ -82,8 +82,8 @@ class MpointStatSerializer(CustomModelSerializer):
ep_belong_name = serializers.CharField(source="mpoint.ep_belong.name", read_only=True) ep_belong_name = serializers.CharField(source="mpoint.ep_belong.name", read_only=True)
mgroup_name = serializers.CharField(source="mgroup.name", read_only=True) mgroup_name = serializers.CharField(source="mgroup.name", read_only=True)
belong_dept_name = serializers.CharField(source="mgroup.belong_dept.name", read_only=True) belong_dept_name = serializers.CharField(source="mgroup.belong_dept.name", read_only=True)
sflog_start_time = serializers.CharField(source="sflog.start_time", read_only=True) sflog_start_time = serializers.DateTimeField(source="sflog.start_time", read_only=True)
sflog_end_time = serializers.CharField(source="sflog.end_time", read_only=True) sflog_end_time = serializers.DateTimeField(source="sflog.end_time", read_only=True)
class Meta: class Meta:
model = MpointStat model = MpointStat

View File

@ -10,6 +10,8 @@ from apps.wpm.models import SfLog, StLog, SfLogExp
from django.utils import timezone from django.utils import timezone
from django.db.models import F from django.db.models import F
from apps.wpm.services import get_pcoal_heat from apps.wpm.services import get_pcoal_heat
from django.core.cache import cache
import time
@shared_task(base=CustomTask) @shared_task(base=CustomTask)
@ -101,16 +103,15 @@ def cal_exp_duration_sec(stlogId: str='', all=False, now: datetime=None):
ret = SfLogExp.objects.filter( ret = SfLogExp.objects.filter(
sflog=sflog, stlog__is_shutdown=True).aggregate(sum=Sum('duration_sec')) sflog=sflog, stlog__is_shutdown=True).aggregate(sum=Sum('duration_sec'))
if ret.get('sum', 0): if ret.get('sum', 0):
get_total_sec_now(sflog.id)
sflog.shut_sec = ret['sum'] sflog.shut_sec = ret['sum']
sflog.save() sflog.save()
# 更新sflog总时长 # 更新sflog总时长
if sflog.end_time > now:
get_total_sec_now(sflog.id)
# if stlogId: # if stlogId:
# cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None, # cal_enstat('sflog', sflog.id, sflog.mgroup.id, None, None, None,
# None, None, None, None, cascade=True, cal_attrs=['run_hour']) # None, None, None, None, cascade=True, cal_attrs=['run_hour'])
@shared_task(base=CustomTask) @shared_task(base=CustomTask)
def cal_enstat_when_pcoal_heat_change(year_s, month_s, day_s): def cal_enstat_when_pcoal_heat_change(year_s, month_s, day_s):
from apps.enm.models import EnStat from apps.enm.models import EnStat