WeatherControl.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Author : peng
  4. @Time : 2023-8-18 10:45:46
  5. @File :WeatherControl.py
  6. """
  7. import datetime
  8. from Ansjer.config import LOGGER
  9. import time
  10. from django.views import View
  11. from Object.IPWeatherObject import WeatherInfo
  12. from Model.models import UidSetModel, CityInformation
  13. from Object.ResponseObject import ResponseObject
  14. from Object.RedisObject import RedisObject
  15. from Service.CommonService import CommonService
  16. class WeatherView(View):
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.GET, operation, request)
  21. def post(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.POST, operation, request)
  25. def validation(self, request_dict, operation, request):
  26. lang = request_dict.get('lang', 'en')
  27. response = ResponseObject(lang)
  28. if operation == 'get': # 获取天气
  29. return self.get_weather(request_dict, response)
  30. else:
  31. return response.json(414)
  32. @staticmethod
  33. def get_weather(request_dict, response):
  34. """
  35. 获取天气
  36. @param request_dict: 请求参数
  37. @request_dict uid: 设备uid
  38. @param response: 响应对象
  39. @return: response
  40. """
  41. uid = request_dict.get('uid')
  42. if not uid:
  43. return response.json(444, 'uid')
  44. uid_set_qs = UidSetModel.objects.filter(uid=uid).values('tb_city_information_id')
  45. if not uid_set_qs.exists():
  46. return response.json(173)
  47. redis_obj = RedisObject()
  48. city_id = uid_set_qs[0]['tb_city_information_id']
  49. if city_id == 0:
  50. return response.json(10, '请更新设备影子地区信息')
  51. today = datetime.datetime.today()
  52. now_time = datetime.datetime(today.year, today.month, today.day, today.hour)
  53. time_stamp = CommonService.str_to_timestamp(now_time.strftime('%Y-%m-%d %H:%M:%S'))
  54. temp, humidity = None, None
  55. try:
  56. weather = redis_obj.get_hash_data('city_id_{}_weather'.format(city_id), time_stamp)
  57. if weather:
  58. temp, humidity = weather.split('/')
  59. else:
  60. city_obj = WeatherInfo(city_id)
  61. weather_list = city_obj.get_city_24_weather()
  62. if weather_list:
  63. temp = weather_list[0]['temp']
  64. humidity = weather_list[0]['humidity']
  65. for item in weather_list:
  66. temp_time = CommonService.str_to_timestamp(item['date'] + ' ' + item['hour'] + ':00:00')
  67. redis_obj.set_hash_data('city_id_{}_weather'.format(city_id),
  68. {temp_time: '{}/{}'.format(item['temp'], item['humidity'])})
  69. redis_obj.set_expire('city_id_{}_weather'.format(city_id), 86400)
  70. if not all([temp, humidity]):
  71. return response.json(10, '获取天气失败')
  72. return response.json(0, {'temp': temp, 'humidity': humidity})
  73. except Exception as e:
  74. print(e)
  75. return response.json(500)