feat: mosq 定时任务,更新设备 location
This commit is contained in:
parent
0060d4c8c8
commit
58d456d3a7
|
@ -2,7 +2,6 @@ from typing import Optional
|
|||
from rest_framework import serializers
|
||||
from counter.models import DeviceCount, DeviceInfo
|
||||
from mosquito.models import DeviceInfo as MosquitoDeviceInfo
|
||||
from geopy.geocoders import Nominatim
|
||||
|
||||
|
||||
class DeviceInfoMobileSerializer(serializers.ModelSerializer):
|
||||
|
@ -25,7 +24,6 @@ class DeviceInfoMobileSerializer(serializers.ModelSerializer):
|
|||
super().__init__(*args, **kwargs)
|
||||
self.latest = None
|
||||
self.cur_device = None
|
||||
self.geolocator = Nominatim(user_agent="geoapiExercises")
|
||||
|
||||
def get_device_name(self, obj) -> Optional[str]:
|
||||
qs = MosquitoDeviceInfo.objects.filter(device_id=obj.device_id)
|
||||
|
@ -45,12 +43,5 @@ class DeviceInfoMobileSerializer(serializers.ModelSerializer):
|
|||
return [lon, lat]
|
||||
return None
|
||||
|
||||
# TODO: 作为定时任务
|
||||
# def get_location(self, obj):
|
||||
# if self.cur_device:
|
||||
# lon, lat = [self.cur_device.longitude, self.cur_device.latitude]
|
||||
# if lon and lat:
|
||||
# location = self.geolocator.reverse("{},{}".format(lat, lon))
|
||||
# address = location.raw['address']
|
||||
# return address
|
||||
# return None
|
||||
def get_location(self, obj):
|
||||
return obj.location
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import time as std_time
|
||||
import pytz
|
||||
import requests
|
||||
from functools import reduce
|
||||
# from collections import OrderedDict
|
||||
from datetime import datetime, time, timedelta
|
||||
from django.db.models import Max
|
||||
from celery import task
|
||||
|
@ -144,14 +145,42 @@ def update_daily_statistic():
|
|||
return _date_list
|
||||
|
||||
|
||||
def get_location(lon: int, lat: int) -> str:
|
||||
key = 'f9fc42eb1a45f311beeb832740f67b0f'
|
||||
url = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location={},{}&key={}'.format(lon, lat, key)
|
||||
resp = requests.get(url)
|
||||
if resp.status_code != 200:
|
||||
return ''
|
||||
data = resp.json()
|
||||
if data['status'] == '1' and data['info'] == 'OK':
|
||||
address = data['regeocode']['formatted_address']
|
||||
|
||||
# address == []
|
||||
if isinstance(address, list):
|
||||
return ''
|
||||
return address
|
||||
else:
|
||||
return ''
|
||||
|
||||
|
||||
@task()
|
||||
def update_latest_statistic():
|
||||
pass
|
||||
def update_mosql_device_location():
|
||||
"""
|
||||
更新 mosquito DeviceInfo 表 location 字段
|
||||
高德逆地理编码配额:5000/天,并发30/秒
|
||||
"""
|
||||
device_list = DeviceInfo.objects.all()
|
||||
for device in device_list:
|
||||
if device.longitude and device.latitude:
|
||||
location = get_location(device.longitude, device.latitude)
|
||||
std_time.sleep(0.1)
|
||||
device.location = location
|
||||
device.save()
|
||||
|
||||
|
||||
@task()
|
||||
def update_mosq_device_info():
|
||||
""" 更新mosquito的DeviceInfo表"""
|
||||
""" 更新 mosquito DeviceInfo 表"""
|
||||
counter_devices = CounterDeviceInfor.objects.values_list('device_id', flat=True)
|
||||
mosq_devices = DeviceInfo.objects.values_list('device_id', flat=True)
|
||||
s1 = set(counter_devices)
|
||||
|
@ -183,4 +212,4 @@ if __name__ == '__main__':
|
|||
# a, b = get_daily_statistic()
|
||||
# print(a, b)
|
||||
# update_mosq_device_info()
|
||||
update_daily_statistic()
|
||||
update_mosql_device_location()
|
||||
|
|
|
@ -26,3 +26,5 @@ six==1.11.0
|
|||
sqlparse==0.3.0
|
||||
vine==1.1.4
|
||||
paho-mqtt==1.6.1
|
||||
|
||||
requests~=2.31.0
|
Loading…
Reference in New Issue