From 640d1a1bcef4a9d2e9fb6c9230cdee7c71856f6e Mon Sep 17 00:00:00 2001 From: caoqianming Date: Tue, 16 Apr 2024 14:59:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8E=BB=E9=99=A4is=5Fonline=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=BC=95=E5=8F=91=E7=9A=84=E5=8F=98=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/em/admin.py | 6 ++--- apps/em/views.py | 8 ++----- .../enp/migrations/0002_auto_20240119_1053.py | 2 -- apps/enp/models.py | 22 ++++++++++++++++++- apps/enp/serializers.py | 11 +++++----- scripts/check_equipment_offline.py | 12 +++++----- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/apps/em/admin.py b/apps/em/admin.py index 099b66ea..7304c503 100644 --- a/apps/em/admin.py +++ b/apps/em/admin.py @@ -5,11 +5,9 @@ from apps.em.models import Ecate, Equipment @admin.register(Ecate) class EcateAdmin(admin.ModelAdmin): - list_display = ('id', 'name', 'code', 'type', - 'is_for_safe', 'is_for_enp', 'is_car') + list_display = ("id", "name", "code", "type", "is_for_safe", "is_for_enp", "is_car") @admin.register(Equipment) class EquipmentAdmin(admin.ModelAdmin): - list_display = ('id', 'name', 'type', - 'cate', 'state', 'is_online', 'running_state') + list_display = ("id", "name", "type", "cate", "state", "running_state") diff --git a/apps/em/views.py b/apps/em/views.py index 7f824678..3060bb0e 100644 --- a/apps/em/views.py +++ b/apps/em/views.py @@ -100,23 +100,19 @@ class EquipmentViewSet(CustomModelViewSet): queryset = self.filter_queryset(self.get_queryset()) result = queryset.aggregate( count=Count("id"), - count_online=Count(Case(When(is_online=1, then=1), output_field=IntegerField())), - count_offline=Count(Case(When(is_online=0, then=1), output_field=IntegerField())), count_running=Count(Case(When(running_state=10, then=1), output_field=IntegerField())), count_standby=Count(Case(When(running_state=20, then=1), output_field=IntegerField())), count_stop=Count(Case(When(running_state=30, then=1), output_field=IntegerField())), count_fail=Count(Case(When(running_state=40, then=1), output_field=IntegerField())), - count_unknown=Count(Case(When(running_state=50, then=1), output_field=IntegerField())), + count_offline=Count(Case(When(running_state=50, then=1), output_field=IntegerField())), ) json_result = { "count": result["count"], - "count_online": result["count_online"], - "count_offline": result["count_offline"], "count_running": result["count_running"], "count_standby": result["count_standby"], "count_stop": result["count_stop"], "count_fail": result["count_fail"], - "count_unknown": result["count_unknown"], + "count_offline": result["count_offline"], } return Response(json_result) diff --git a/apps/enp/migrations/0002_auto_20240119_1053.py b/apps/enp/migrations/0002_auto_20240119_1053.py index 2d534e85..36f5ebfa 100644 --- a/apps/enp/migrations/0002_auto_20240119_1053.py +++ b/apps/enp/migrations/0002_auto_20240119_1053.py @@ -17,7 +17,6 @@ class Migration(migrations.Migration): CREATE TABLE public.enp_envdata ( "timex" timestamptz NOT NULL, "equipment_id" text NOT NULL, - "is_online" INT DEFAULT 1, "running_state" INT DEFAULT 10, "dust_rtd" float, "dust_zs" float, @@ -48,7 +47,6 @@ SELECT create_hypertable('enp_envdata', 'timex'); name="EnvData", fields=[ ("time", models.DateTimeField(primary_key=True, serialize=False, verbose_name="采集时间")), - ("is_online", models.PositiveSmallIntegerField(default=1, verbose_name="是否在线")), ("running_state", models.PositiveSmallIntegerField(default=10, verbose_name="运行状态")), ("dust_rtd", models.FloatField(blank=True, null=True, verbose_name="颗粒物实测(mg/m3)")), ("dust_zs", models.FloatField(blank=True, null=True, verbose_name="颗粒物折算(mg/m3)")), diff --git a/apps/enp/models.py b/apps/enp/models.py index 8b1624b0..1ddc8fa3 100644 --- a/apps/enp/models.py +++ b/apps/enp/models.py @@ -53,10 +53,30 @@ class EnvData(models.Model): 环保监测数据 """ + enp_fields = [ + "running_state", + "dust_rtd", + "dust_zs", + "temperature", + "pressure", + "speed", + "humdity", + "flux", + "pm25", + "pm10", + "tsp", + "wind_direction", + "wind_speed", + "so2_rtd", + "so2_zs", + "nox_rtd", + "nox_zs", + "o2", + ] + RUNING_STATE_CHOICES = ((10, "运行"), (20, "待机"), (30, "停机"), (40, "故障"), (50, "未知")) equipment = models.ForeignKey(Equipment, verbose_name="关联设备", on_delete=models.CASCADE) timex = models.DateTimeField("采集时间", primary_key=True) - is_online = models.PositiveSmallIntegerField("是否在线", default=1) running_state = models.PositiveSmallIntegerField("运行状态", default=10) dust_rtd = models.FloatField("颗粒物实测(mg/m3)", null=True, blank=True) dust_zs = models.FloatField("颗粒物折算(mg/m3)", null=True, blank=True) diff --git a/apps/enp/serializers.py b/apps/enp/serializers.py index 2dc692fa..304c488c 100644 --- a/apps/enp/serializers.py +++ b/apps/enp/serializers.py @@ -37,7 +37,7 @@ class Drain2Serializer(CustomModelSerializer): now = datetime.now() today = str(now)[:10] + " 00:00:00" today_last = str(now)[:10] + " 23:59:59" - odata = equips.values("id", "name", "type", "is_online", "running_state") + odata = equips.values("id", "name", "type", "running_state") eids = [f"'{e['id']}'" for e in odata] eids_str = ",".join(eids) sql_str = f""" @@ -56,7 +56,7 @@ FROM running_state, CASE - WHEN is_online = 1 THEN + WHEN running_state != 50 THEN TIME - LAG ( TIME ) OVER ( PARTITION BY equipment_id ORDER BY TIME ) ELSE'0' END AS duration_online, CASE @@ -66,7 +66,7 @@ FROM END AS duration_run, CASE - WHEN running_state = 20 THEN + WHEN running_state in (20, 30, 40) THEN TIME - LAG ( TIME ) OVER ( PARTITION BY equipment_id ORDER BY TIME ) ELSE'0' END AS duration_standby, CASE @@ -77,10 +77,9 @@ FROM FROM enp_envdata WHERE - running_state IN ( 10, 20 ) - AND TIME >= '{today}' -- 替换成想查询的开始时间 + TIMEX >= '{today}' -- 替换成想查询的开始时间 - AND TIME <= '{today_last}' -- 替换成想查询的结束时间 + AND TIMEX <= '{today_last}' -- 替换成想查询的结束时间 AND equipment_id IN ( {eids_str} ) ) AS durations diff --git a/scripts/check_equipment_offline.py b/scripts/check_equipment_offline.py index 90646ee7..355779fe 100644 --- a/scripts/check_equipment_offline.py +++ b/scripts/check_equipment_offline.py @@ -2,7 +2,6 @@ import os import sys import django import logging -from django.conf import settings from django.core.cache import cache from django.utils import timezone import time @@ -22,17 +21,16 @@ def run(): now = timezone.now() equips = Equipment.objects.all() for equip in equips: - is_online = 0 + is_offline = True cache_key = f"equipment_{equip.id}" cache_value = cache.get(cache_key, None) if cache_value: last_timex = cache_value.get("running_state_timex", None) if last_timex and (now - last_timex).total_seconds() <= 20: - is_online = 1 - if is_online == 0: - equip.is_online = 0 - equip.running_state = RuningState.UNKNOWN - equip.save(update_fields=["is_online", "running_state"]) + is_offline = False + if is_offline: + equip.running_state = RuningState.OFFLINE + equip.save(update_fields=["running_state"]) time.sleep(5)