feat:tkx 计量设备临期预警
This commit is contained in:
parent
a08e66a06a
commit
55b8564f0d
|
@ -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='计量设备状态'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -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='临期检验预警(天)'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -40,6 +40,11 @@ class Equipment(CommonBModel):
|
||||||
state_choices = ((EQUIP_STATE_OK, "完好"), (EQUIP_STATE_LIMIT, "限用"), (EQUIP_STATE_FIX, "在修"), (EQUIP_STATE_DISABLE, "禁用"), (EQUIP_STATE_SCRAP, "报废"))
|
state_choices = ((EQUIP_STATE_OK, "完好"), (EQUIP_STATE_LIMIT, "限用"), (EQUIP_STATE_FIX, "在修"), (EQUIP_STATE_DISABLE, "禁用"), (EQUIP_STATE_SCRAP, "报废"))
|
||||||
EQUIP_TYPE_PRO = 10
|
EQUIP_TYPE_PRO = 10
|
||||||
EQUIP_TYPE_MEA = 20
|
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 = (
|
# mgmtype_choices = (
|
||||||
# (1, 'A'),
|
# (1, 'A'),
|
||||||
# (2, 'B'),
|
# (2, 'B'),
|
||||||
|
@ -104,6 +109,8 @@ class Equipment(CommonBModel):
|
||||||
cycle = models.PositiveSmallIntegerField("校准或检定周期(月)", null=True, blank=True)
|
cycle = models.PositiveSmallIntegerField("校准或检定周期(月)", null=True, blank=True)
|
||||||
check_date = models.DateField("最近校准检查日期", blank=True, null=True)
|
check_date = models.DateField("最近校准检查日期", blank=True, null=True)
|
||||||
next_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)
|
power_kw = models.FloatField("功率", null=True, blank=True)
|
||||||
|
|
||||||
coordinates = models.JSONField("坐标", default=dict, blank=True)
|
coordinates = models.JSONField("坐标", default=dict, blank=True)
|
||||||
|
|
|
@ -6,7 +6,7 @@ from django.utils.timezone import localtime
|
||||||
from apps.em.models import Equipment
|
from apps.em.models import Equipment
|
||||||
from apps.em.services import set_eq_rs, get_eq_rs, shutdown_or_startup
|
from apps.em.services import set_eq_rs, get_eq_rs, shutdown_or_startup
|
||||||
from apps.mtm.models import Mgroup
|
from apps.mtm.models import Mgroup
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
@shared_task(base=CustomTask)
|
@shared_task(base=CustomTask)
|
||||||
def check_equipment_offline(seconds=30):
|
def check_equipment_offline(seconds=30):
|
||||||
|
@ -36,3 +36,36 @@ def check_mgroup_running():
|
||||||
# pass
|
# pass
|
||||||
# else:
|
# else:
|
||||||
# shutdown_or_startup(equip.id, rs["running_state_timex"], rs["running_state"])
|
# 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()
|
|
@ -5,6 +5,7 @@ from celery import shared_task
|
||||||
from apps.enm.models import MpLogx, Mpoint, MpointStat, EnStat, EnStat2, Xscript
|
from apps.enm.models import MpLogx, Mpoint, MpointStat, EnStat, EnStat2, Xscript
|
||||||
from apps.wpm.models import SfLog
|
from apps.wpm.models import SfLog
|
||||||
import datetime
|
import datetime
|
||||||
|
from django.db.models import Q
|
||||||
from django.db.models import Sum, Avg
|
from django.db.models import Sum, Avg
|
||||||
from dateutil import tz
|
from dateutil import tz
|
||||||
from django.conf import settings
|
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
|
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]:
|
if hour in [8, 9, 10, 19, 20, 21, 22, 23]:
|
||||||
high_time_ranges = [
|
high_time_ranges = [
|
||||||
(time(8, 0), time(11, 0)), # 上午高峰
|
(8, 11), # 上午高峰(小时数范围)
|
||||||
(time(19, 0), time(23, 59)), # 晚上高峰
|
(19, 23), # 晚上高峰(小时数范围)
|
||||||
]
|
]
|
||||||
# 构建查询条件
|
# 构建查询条件
|
||||||
time_conditions = Q()
|
time_conditions = Q()
|
||||||
|
@ -149,9 +148,9 @@ def update_mpoint_val(month, hour):
|
||||||
"""
|
"""
|
||||||
if hour in [11, 12, 17, 18, 0, 1, 2, 3]:
|
if hour in [11, 12, 17, 18, 0, 1, 2, 3]:
|
||||||
flat_time_ranges = [
|
flat_time_ranges = [
|
||||||
(time(11, 0), time(13, 0)), # 上午平谷
|
(11, 13), # 上午平谷
|
||||||
(time(17, 0), time(19, 0)),
|
(17, 19), # 晚上平谷
|
||||||
(time(0, 0), time(4, 0)), # 晚上平谷
|
(0, 4),
|
||||||
]
|
]
|
||||||
# 构建查询条件
|
# 构建查询条件
|
||||||
time_conditions = Q()
|
time_conditions = Q()
|
||||||
|
@ -165,8 +164,8 @@ def update_mpoint_val(month, hour):
|
||||||
"""
|
"""
|
||||||
if hour in [4, 5, 6, 7, 13, 14, 15, 16]:
|
if hour in [4, 5, 6, 7, 13, 14, 15, 16]:
|
||||||
low_time_ranges = [
|
low_time_ranges = [
|
||||||
(time(4, 0), time(8, 0)), # 上午低谷
|
(4, 8), # 上午低谷
|
||||||
(time(13, 0), time(17, 0)), # 晚上低谷
|
(13, 17), # 晚上低谷
|
||||||
]
|
]
|
||||||
# 构建查询条件
|
# 构建查询条件
|
||||||
time_conditions = Q()
|
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]:
|
if month in [5, 6, 7, 8] and hour in [14, 15]:
|
||||||
deep_months = [5, 6, 7, 8]
|
deep_months = [5, 6, 7, 8]
|
||||||
deep_time_ranges = [
|
deep_time_ranges = [
|
||||||
(time(14, 0), time(16, 0)), # 14:00--16:00
|
(14, 16), # 14:00--16:00
|
||||||
]
|
]
|
||||||
# 构建查询条件
|
# 构建查询条件
|
||||||
time_conditions = Q()
|
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
|
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]):
|
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)),
|
peak_months = {
|
||||||
(7,): (time(21, 0), time(23, 0)),}
|
(1, 11, 12): (19, 21),
|
||||||
|
(7,): (21, 23)
|
||||||
|
}
|
||||||
for months, time_range in peak_months.items():
|
for months, time_range in peak_months.items():
|
||||||
start_time, end_time = time_range
|
start_time, end_time = time_range
|
||||||
MpointStat.objects.filter(
|
MpointStat.objects.filter(
|
||||||
|
@ -208,7 +209,7 @@ def update_mpoint_val(month, hour):
|
||||||
hour__gte = start_time,
|
hour__gte = start_time,
|
||||||
hour__lt = end_time,
|
hour__lt = end_time,
|
||||||
).update(val_level="peak")
|
).update(val_level="peak")
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
@shared_task(base=CustomTask)
|
@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():
|
if mrs0.exists() and mrs.exists():
|
||||||
last_val = getattr(mrs.last(), f'val_{val_type}')
|
last_val = getattr(mrs.last(), f'val_{val_type}')
|
||||||
first_val = getattr(mrs0.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
|
val = last_val - first_val
|
||||||
elif first_val - last_val > 0 and (first_val - last_val)/first_val < 0.01:
|
elif first_val - last_val > 0 and (first_val - last_val)/first_val < 0.01:
|
||||||
val = 0
|
val = 0
|
||||||
|
@ -245,6 +246,7 @@ def cal_mpointstat_hour(mpointId: str, year: int, month: int, day: int, hour: in
|
||||||
else:
|
else:
|
||||||
# 这里判断有可能清零了
|
# 这里判断有可能清零了
|
||||||
max_val = max(mrs.aggregate(max=Max(f'val_{val_type}'))["max"], first_val)
|
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
|
val = max_val - first_val + last_val
|
||||||
# if mpoint.code == 'x水泥+P.O42.5 散装':
|
# 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
|
return
|
||||||
ms, _ = MpointStat.objects.get_or_create(**params, defaults=params)
|
ms, _ = MpointStat.objects.get_or_create(**params, defaults=params)
|
||||||
ms.val = ms.val_correct if ms.val_correct is not None else val
|
ms.val = ms.val_correct if ms.val_correct is not None else val
|
||||||
|
update_mpoint_val(month, hour)
|
||||||
# ms.val_level
|
# ms.val_level
|
||||||
ms.save()
|
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 "pcoal" in this_cal_attrs:
|
||||||
if type in ["hour_s", "sflog", "day_s"]:
|
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_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":
|
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(
|
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")
|
sum=Sum("pcoal_coal_consume")
|
||||||
|
@ -909,7 +912,7 @@ def cal_enstat_pcoal_change(enstat: EnStat, new_pcoal_heat):
|
||||||
type = enstat.type
|
type = enstat.type
|
||||||
if type in ["hour_s", "sflog", "day_s"]:
|
if type in ["hour_s", "sflog", "day_s"]:
|
||||||
enstat.pcoal_heat = new_pcoal_heat
|
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":
|
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(
|
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")
|
sum=Sum("pcoal_coal_consume")
|
||||||
|
|
Loading…
Reference in New Issue