|
@@ -6,12 +6,10 @@
|
|
"""
|
|
"""
|
|
import datetime
|
|
import datetime
|
|
|
|
|
|
-from Ansjer.config import LOGGER
|
|
|
|
-import time
|
|
|
|
-
|
|
|
|
|
|
+from Ansjer.config import CONFIG_INFO, CONFIG_CN
|
|
from django.views import View
|
|
from django.views import View
|
|
-from Object.IPWeatherObject import WeatherInfo
|
|
|
|
-from Model.models import UidSetModel, CityInformation
|
|
|
|
|
|
+from Object.IPWeatherObject import WeatherInfo, OpenWeatherMap
|
|
|
|
+from Model.models import UidSetModel, IPAddr
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.ResponseObject import ResponseObject
|
|
from Object.RedisObject import RedisObject
|
|
from Object.RedisObject import RedisObject
|
|
from Service.CommonService import CommonService
|
|
from Service.CommonService import CommonService
|
|
@@ -21,14 +19,14 @@ class WeatherView(View):
|
|
def get(self, request, *args, **kwargs):
|
|
def get(self, request, *args, **kwargs):
|
|
request.encoding = 'utf-8'
|
|
request.encoding = 'utf-8'
|
|
operation = kwargs.get('operation')
|
|
operation = kwargs.get('operation')
|
|
- return self.validation(request.GET, operation, request)
|
|
|
|
|
|
+ return self.validation(request.GET, operation)
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
def post(self, request, *args, **kwargs):
|
|
request.encoding = 'utf-8'
|
|
request.encoding = 'utf-8'
|
|
operation = kwargs.get('operation')
|
|
operation = kwargs.get('operation')
|
|
- return self.validation(request.POST, operation, request)
|
|
|
|
|
|
+ return self.validation(request.POST, operation)
|
|
|
|
|
|
- def validation(self, request_dict, operation, request):
|
|
|
|
|
|
+ def validation(self, request_dict, operation):
|
|
lang = request_dict.get('lang', 'en')
|
|
lang = request_dict.get('lang', 'en')
|
|
response = ResponseObject(lang)
|
|
response = ResponseObject(lang)
|
|
if operation == 'get': # 获取天气
|
|
if operation == 'get': # 获取天气
|
|
@@ -48,27 +46,42 @@ class WeatherView(View):
|
|
uid = request_dict.get('uid')
|
|
uid = request_dict.get('uid')
|
|
if not uid:
|
|
if not uid:
|
|
return response.json(444, 'uid')
|
|
return response.json(444, 'uid')
|
|
- uid_set_qs = UidSetModel.objects.filter(uid=uid).values('tb_city_information_id')
|
|
|
|
|
|
+
|
|
|
|
+ uid_set_qs = UidSetModel.objects.filter(uid=uid).values('tb_city_information_id', 'ip')
|
|
if not uid_set_qs.exists():
|
|
if not uid_set_qs.exists():
|
|
return response.json(173)
|
|
return response.json(173)
|
|
- redis_obj = RedisObject()
|
|
|
|
- city_id = uid_set_qs[0]['tb_city_information_id']
|
|
|
|
- if city_id == 0:
|
|
|
|
- return response.json(10, '请更新设备影子地区信息')
|
|
|
|
- today = datetime.datetime.today()
|
|
|
|
- now_time = datetime.datetime(today.year, today.month, today.day, today.hour)
|
|
|
|
- time_stamp = CommonService.str_to_timestamp(now_time.strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
|
|
+
|
|
try:
|
|
try:
|
|
- weather = redis_obj.get_data('city_id_{}_{}_weather'.format(city_id, time_stamp))
|
|
|
|
- if weather:
|
|
|
|
- temp, humidity = weather.split('/')
|
|
|
|
|
|
+ # 根据服务器配置使用不同的服务, 国内:阿里云墨迹天气, 测试,国外:OpenWeatherMap
|
|
|
|
+ if CONFIG_INFO == CONFIG_CN:
|
|
|
|
+ city_id = uid_set_qs[0]['tb_city_information_id']
|
|
|
|
+ if city_id == 0:
|
|
|
|
+ return response.json(10, '请更新设备影子地区信息')
|
|
|
|
+
|
|
|
|
+ today = datetime.datetime.today()
|
|
|
|
+ now_time = datetime.datetime(today.year, today.month, today.day, today.hour)
|
|
|
|
+ time_stamp = CommonService.str_to_timestamp(now_time.strftime('%Y-%m-%d %H:%M:%S'))
|
|
|
|
+ key = 'city_id_{}_{}_weather'.format(city_id, time_stamp)
|
|
|
|
+
|
|
|
|
+ redis_obj = RedisObject()
|
|
|
|
+ weather = redis_obj.get_data(key)
|
|
|
|
+ if weather:
|
|
|
|
+ temp, humidity = weather.split('/')
|
|
|
|
+ else:
|
|
|
|
+ city_obj = WeatherInfo(city_id)
|
|
|
|
+ temp, humidity = city_obj.get_city_weather()
|
|
|
|
+ if temp and humidity:
|
|
|
|
+ redis_obj.set_ex_data(key, '{}/{}'.format(temp, humidity), 3600)
|
|
else:
|
|
else:
|
|
- city_obj = WeatherInfo(city_id)
|
|
|
|
- temp, humidity = city_obj.get_city_weather()
|
|
|
|
- if temp and humidity:
|
|
|
|
- redis_obj.set_ex_data('city_id_{}_{}_weather'.format(city_id, time_stamp),
|
|
|
|
- '{}/{}'.format(temp, humidity), 3600)
|
|
|
|
- if not all([temp, humidity]):
|
|
|
|
|
|
+ ip = uid_set_qs[0]['ip']
|
|
|
|
+ ip_addr_qs = IPAddr.objects.filter(ip=ip, is_geoip2=True).values('lat', 'lon')
|
|
|
|
+ if not ip_addr_qs.exists():
|
|
|
|
+ return response.json(173)
|
|
|
|
+ lat, lon = ip_addr_qs[0]['lat'], ip_addr_qs[0]['lon']
|
|
|
|
+ open_weather_map = OpenWeatherMap(lat, lon)
|
|
|
|
+ temp, humidity = open_weather_map.get_weather()
|
|
|
|
+
|
|
|
|
+ if temp is None and humidity is None:
|
|
return response.json(10, '获取天气失败')
|
|
return response.json(10, '获取天气失败')
|
|
return response.json(0, {'temp': temp, 'humidity': humidity})
|
|
return response.json(0, {'temp': temp, 'humidity': humidity})
|
|
except Exception as e:
|
|
except Exception as e:
|