feat: 完善2代设备在线判断逻辑
This commit is contained in:
parent
8afaeec70e
commit
2aa2f43237
|
@ -1,5 +1,6 @@
|
|||
import pytz
|
||||
from typing import Optional
|
||||
from datetime import datetime, timedelta
|
||||
from django.db.models import Q, Min, Max
|
||||
from django.core.cache import cache
|
||||
from rest_framework import serializers
|
||||
|
@ -194,9 +195,6 @@ class DeviceCountWithInfoSerializer(DeviceCountSerializer):
|
|||
return online
|
||||
|
||||
|
||||
device_status_map = {'ON': 1, 'OFF': 0}
|
||||
|
||||
|
||||
class DeviceInfoSerializer(serializers.ModelSerializer):
|
||||
device_name = serializers.SerializerMethodField()
|
||||
status = serializers.SerializerMethodField()
|
||||
|
@ -238,9 +236,14 @@ class DeviceInfoSerializer(serializers.ModelSerializer):
|
|||
# 区分一代和二代设备
|
||||
if obj.chip_type == 'AIR-V2':
|
||||
mosq_device_info = MosquitoDeviceInfo.objects.filter(device_id=obj.device_id).first()
|
||||
if mosq_device_info and mosq_device_info.led_status:
|
||||
if mosq_device_info and mosq_device_info.last_connect:
|
||||
self.mosq_device_info = mosq_device_info
|
||||
return device_status_map[mosq_device_info.led_status]
|
||||
now = datetime.now(tz=pytz.timezone('Asia/Shanghai'))
|
||||
dt = now - mosq_device_info.last_connect
|
||||
if dt.seconds > 300:
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
return obj.online
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.4 on 2024-04-01 15:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('mosquito', '0031_auto_20240326_1106'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='deviceinfo',
|
||||
name='last_connect',
|
||||
field=models.DateTimeField(blank=True, null=True, verbose_name='最近连接时间'),
|
||||
),
|
||||
]
|
|
@ -83,6 +83,7 @@ class DeviceInfo(models.Model):
|
|||
location = models.CharField(max_length=100, blank=True, null=True, verbose_name='地理位置')
|
||||
led_lifetime = models.PositiveIntegerField(default=30000, verbose_name='LED 寿命')
|
||||
launch_time = models.DateTimeField(blank=True, null=True, verbose_name='第一次启动时间')
|
||||
last_connect = models.DateTimeField(blank=True, null=True, verbose_name='最近连接时间')
|
||||
|
||||
# mqtt 数据
|
||||
count = models.PositiveIntegerField(blank=True, null=True, verbose_name='蚊子计数')
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import sys
|
||||
import json
|
||||
import pytz
|
||||
import django
|
||||
from typing import Dict
|
||||
from pathlib import Path
|
||||
|
@ -43,11 +44,20 @@ def on_message(client, userdata, message):
|
|||
# 从主题中解析 device_id
|
||||
topic_parts = topic.split('/')
|
||||
device_id = topic_parts[2]
|
||||
topic_postfix = topic_parts[3]
|
||||
|
||||
payload = message.payload.decode('utf-8')
|
||||
|
||||
# update and create
|
||||
post_data = json.loads(payload)
|
||||
|
||||
if topic_postfix == 'state':
|
||||
device = DeviceInfo.objects.filter(device_id=device_id).first()
|
||||
if device:
|
||||
ts = post_data['Time Stamp']
|
||||
tz = pytz.timezone("Asia/Shanghai")
|
||||
device.last_connect = tz.localize(datetime.fromtimestamp(ts))
|
||||
device.save()
|
||||
print(f"Device: {device_id} state post success")
|
||||
return
|
||||
try:
|
||||
update_device_info(device_id, post_data=post_data)
|
||||
create_mosq_post(device_id, post_data=post_data)
|
||||
|
@ -165,7 +175,7 @@ client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
|||
|
||||
# 订阅所有设备的主题
|
||||
client.subscribe(DATA_TOPIC)
|
||||
# client.subscribe(STATE_TOPIC)
|
||||
client.subscribe(STATE_TOPIC)
|
||||
|
||||
# 开始MQTT客户端
|
||||
client.loop_forever()
|
||||
|
|
Loading…
Reference in New Issue