From 55b8564f0da23f327135d54f2fb36429ae0ecab9 Mon Sep 17 00:00:00 2001 From: zty Date: Tue, 3 Dec 2024 18:02:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:tkx=20=E8=AE=A1=E9=87=8F=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E4=B8=B4=E6=9C=9F=E9=A2=84=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/em/migrations/0021_auto_20241203_1531.py | 23 ++++++++++++ .../0022_alter_equipment_check_days_number.py | 18 +++++++++ apps/em/models.py | 7 ++++ apps/em/tasks.py | 37 ++++++++++++++++++- apps/enm/tasks.py | 35 ++++++++++-------- 5 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 apps/em/migrations/0021_auto_20241203_1531.py create mode 100644 apps/em/migrations/0022_alter_equipment_check_days_number.py diff --git a/apps/em/migrations/0021_auto_20241203_1531.py b/apps/em/migrations/0021_auto_20241203_1531.py new file mode 100644 index 00000000..9a5b855b --- /dev/null +++ b/apps/em/migrations/0021_auto_20241203_1531.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.12 on 2024-12-03 07:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('em', '0020_equipment_device_people'), + ] + + operations = [ + migrations.AddField( + model_name='equipment', + name='check_days_number', + field=models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='临期检验预警(天)'), + ), + migrations.AddField( + model_name='equipment', + name='state_measure', + field=models.PositiveIntegerField(choices=[(10, '正常'), (20, '告警'), (30, '过期'), (40, '等待')], default=40, verbose_name='计量设备状态'), + ), + ] diff --git a/apps/em/migrations/0022_alter_equipment_check_days_number.py b/apps/em/migrations/0022_alter_equipment_check_days_number.py new file mode 100644 index 00000000..7b10553e --- /dev/null +++ b/apps/em/migrations/0022_alter_equipment_check_days_number.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.12 on 2024-12-03 09:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('em', '0021_auto_20241203_1531'), + ] + + operations = [ + migrations.AlterField( + model_name='equipment', + name='check_days_number', + field=models.PositiveSmallIntegerField(default=7, verbose_name='临期检验预警(天)'), + ), + ] diff --git a/apps/em/models.py b/apps/em/models.py index 99efdb04..6d56c186 100644 --- a/apps/em/models.py +++ b/apps/em/models.py @@ -40,6 +40,11 @@ class Equipment(CommonBModel): state_choices = ((EQUIP_STATE_OK, "完好"), (EQUIP_STATE_LIMIT, "限用"), (EQUIP_STATE_FIX, "在修"), (EQUIP_STATE_DISABLE, "禁用"), (EQUIP_STATE_SCRAP, "报废")) EQUIP_TYPE_PRO = 10 EQUIP_TYPE_MEA = 20 + EQ_NORMAL = 10 + EQ_WARNING = 20 + EQ_EXPIRED = 30 + EQ_WAIT = 40 + measure_state_choices = ((EQ_NORMAL, "正常"), (EQ_WARNING, "告警"), (EQ_EXPIRED, "过期"), (EQ_WAIT, "等待")) # mgmtype_choices = ( # (1, 'A'), # (2, 'B'), @@ -104,6 +109,8 @@ class Equipment(CommonBModel): cycle = models.PositiveSmallIntegerField("校准或检定周期(月)", null=True, blank=True) check_date = models.DateField("最近校准检查日期", blank=True, null=True) next_check_date = models.DateField("预计下次校准检查日期", blank=True, null=True) + state_measure = models.PositiveIntegerField("计量设备状态", choices=measure_state_choices, default=EQ_WAIT) + check_days_number = models.PositiveSmallIntegerField("临期检验预警(天)", default=7) power_kw = models.FloatField("功率", null=True, blank=True) coordinates = models.JSONField("坐标", default=dict, blank=True) diff --git a/apps/em/tasks.py b/apps/em/tasks.py index 0587617b..1c03bf9c 100644 --- a/apps/em/tasks.py +++ b/apps/em/tasks.py @@ -6,7 +6,7 @@ from django.utils.timezone import localtime from apps.em.models import Equipment from apps.em.services import set_eq_rs, get_eq_rs, shutdown_or_startup from apps.mtm.models import Mgroup - +from django.utils import timezone @shared_task(base=CustomTask) def check_equipment_offline(seconds=30): @@ -35,4 +35,37 @@ def check_mgroup_running(): # if rs['running_state'] == Equipment.OFFLINE: # 如果掉线了不关心 # pass # else: - # shutdown_or_startup(equip.id, rs["running_state_timex"], rs["running_state"]) \ No newline at end of file + # shutdown_or_startup(equip.id, rs["running_state_timex"], rs["running_state"]) + +@shared_task(base=CustomTask) +def check_equipment_measure_time(): + """ + 监测设备检定状态 + """ + all_equips = Equipment.objects.values('id', 'check_days_number', 'next_check_date', 'state_measure') + for equip in all_equips: + if equip['check_days_number'] and equip['next_check_date']: + # 更新检定状态 + now = localtime().date() + if equip['next_check_date'] <= now : + Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_EXPIRED) + elif equip['next_check_date'] - now <= timezone.timedelta(days=equip['check_days_number']): + Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_WARNING) + else: + Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_NORMAL) + else: + Equipment.objects.filter(id=equip['id']).update(state_measure=Equipment.EQ_WAIT) + + + # if instance.check_days_number and instance.next_check_date: + # # 更新检定状态 + # now = timezone.localtime().date() + # if instance.next_check_date <= now : + # instance.state_measure = Equipment.EQ_EXPIRED + # elif instance.next_check_date - now <= timezone.timedelta(days=instance.check_days_number): + # instance.state_measure = Equipment.EQ_WARNING + # else: + # instance.state_measure = Equipment.EQ_NORMAL + # else: + # instance.state_measure = Equipment.EQ_WAIT + # instance.save() \ No newline at end of file diff --git a/apps/enm/tasks.py b/apps/enm/tasks.py index edd31b8f..9b347910 100644 --- a/apps/enm/tasks.py +++ b/apps/enm/tasks.py @@ -5,6 +5,7 @@ from celery import shared_task from apps.enm.models import MpLogx, Mpoint, MpointStat, EnStat, EnStat2, Xscript from apps.wpm.models import SfLog import datetime +from django.db.models import Q from django.db.models import Sum, Avg from dateutil import tz from django.conf import settings @@ -128,12 +129,10 @@ def update_mpoint_val(month, hour): """ high:用电高峰时段8小时:8:00--11:00, 19:00--24:00 更新val_level 为 high """ - from datetime import time - from django.db.models import Q if hour in [8, 9, 10, 19, 20, 21, 22, 23]: high_time_ranges = [ - (time(8, 0), time(11, 0)), # 上午高峰 - (time(19, 0), time(23, 59)), # 晚上高峰 + (8, 11), # 上午高峰(小时数范围) + (19, 23), # 晚上高峰(小时数范围) ] # 构建查询条件 time_conditions = Q() @@ -149,9 +148,9 @@ def update_mpoint_val(month, hour): """ if hour in [11, 12, 17, 18, 0, 1, 2, 3]: flat_time_ranges = [ - (time(11, 0), time(13, 0)), # 上午平谷 - (time(17, 0), time(19, 0)), - (time(0, 0), time(4, 0)), # 晚上平谷 + (11, 13), # 上午平谷 + (17, 19), # 晚上平谷 + (0, 4), ] # 构建查询条件 time_conditions = Q() @@ -165,8 +164,8 @@ def update_mpoint_val(month, hour): """ if hour in [4, 5, 6, 7, 13, 14, 15, 16]: low_time_ranges = [ - (time(4, 0), time(8, 0)), # 上午低谷 - (time(13, 0), time(17, 0)), # 晚上低谷 + (4, 8), # 上午低谷 + (13, 17), # 晚上低谷 ] # 构建查询条件 time_conditions = Q() @@ -181,7 +180,7 @@ def update_mpoint_val(month, hour): if month in [5, 6, 7, 8] and hour in [14, 15]: deep_months = [5, 6, 7, 8] deep_time_ranges = [ - (time(14, 0), time(16, 0)), # 14:00--16:00 + (14, 16), # 14:00--16:00 ] # 构建查询条件 time_conditions = Q() @@ -197,8 +196,10 @@ def update_mpoint_val(month, hour): peak:用电高峰 1,11,12月份 19:00-21:00 -- 7 月份 21:00-23:00 更新val_level 为 peak """ if (month in [1, 11, 12] and hour in [19, 20, 21]) or (month == 7 and hour in [21, 22]): - peak_months = {(1, 11, 12): (time(19, 0), time(21, 0)), - (7,): (time(21, 0), time(23, 0)),} + peak_months = { + (1, 11, 12): (19, 21), + (7,): (21, 23) + } for months, time_range in peak_months.items(): start_time, end_time = time_range MpointStat.objects.filter( @@ -208,7 +209,7 @@ def update_mpoint_val(month, hour): hour__gte = start_time, hour__lt = end_time, ).update(val_level="peak") - + return '' @shared_task(base=CustomTask) @@ -237,7 +238,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in if mrs0.exists() and mrs.exists(): last_val = getattr(mrs.last(), f'val_{val_type}') first_val = getattr(mrs0.last(), f'val_{val_type}') - if last_val >= first_val: + if last_val >= first_val or first_val == 0: val = last_val - first_val elif first_val - last_val > 0 and (first_val - last_val)/first_val < 0.01: val = 0 @@ -245,6 +246,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in else: # 这里判断有可能清零了 max_val = max(mrs.aggregate(max=Max(f'val_{val_type}'))["max"], first_val) + myLogger.info(f'{mpoint.code}--{dt}--{last_val}--{first_val}--清零') val = max_val - first_val + last_val # if mpoint.code == 'x水泥+P.O42.5 散装': @@ -255,6 +257,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in return ms, _ = MpointStat.objects.get_or_create(**params, defaults=params) ms.val = ms.val_correct if ms.val_correct is not None else val + update_mpoint_val(month, hour) # ms.val_level ms.save() @@ -678,7 +681,7 @@ def cal_enstat(type, sflogId, mgroupId, year, month, day, hour, year_s, month_s, if "pcoal" in this_cal_attrs: if type in ["hour_s", "sflog", "day_s"]: enstat.pcoal_heat = get_pcoal_heat(enstat.year_s, enstat.month_s, enstat.day_s) - enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat / 7000 + enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat / 29307.6 elif type == "month_st": enstat.pcoal_coal_consume = EnStat.objects.filter(type="sflog", mgroup=enstat.mgroup, year_s=year_s, month_s=month_s, sflog__team=enstat.team).aggregate( sum=Sum("pcoal_coal_consume") @@ -909,7 +912,7 @@ def cal_enstat_pcoal_change(enstat: EnStat, new_pcoal_heat): type = enstat.type if type in ["hour_s", "sflog", "day_s"]: enstat.pcoal_heat = new_pcoal_heat - enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat / 7000 + enstat.pcoal_coal_consume = enstat.pcoal_consume * enstat.pcoal_heat / 29307.6 elif type == "month_st": enstat.pcoal_coal_consume = EnStat.objects.filter(type="sflog", mgroup=enstat.mgroup, year_s=enstat.year_s, month_s=enstat.month_s, sflog__team=enstat.team).aggregate( sum=Sum("pcoal_coal_consume")