peng 2 роки тому
батько
коміт
880bb7601c
3 змінених файлів з 63 додано та 3 видалено
  1. 3 1
      Ansjer/urls.py
  2. 57 0
      Controller/WeatherControl.py
  3. 3 2
      Object/IPWeatherObject.py

+ 3 - 1
Ansjer/urls.py

@@ -22,7 +22,8 @@ from Controller import FeedBack, EquipmentOTA, EquipmentInfo, AdminManage, AppIn
     OrderTaskController, HistoryUIDController, UIDManageUserController, SerialNumberController, CompanyController, \
     RegionController, VPGController, LanguageController, TestController, DeviceConfirmRegion, S3GetStsController, \
     DetectControllerV2, PcInfo, PctestController, DeviceDebug, PaymentCycle, \
-    DeviceLogController, CouponController, AiController, ShadowController, AppAccountManagement, InitController
+    DeviceLogController, CouponController, AiController, ShadowController, AppAccountManagement, InitController, \
+    WeatherControl
 from Controller.Cron import CronTaskController
 from Controller.MessagePush import EquipmentMessagePush
 from Controller.SensorGateway import SensorGatewayController, EquipmentFamilyController
@@ -263,6 +264,7 @@ urlpatterns = [
     re_path(r'^api/device/share/(?P<operation>.*)$', UserDeviceShareController.UserDeviceShareView.as_view()),
     re_path(r'^server/(?P<apiVersion>[a-zA-Z0-9]+)/open/detect/(?P<operation>.*)$',
             DetectControllerV2.DetectControllerViewV2.as_view()),
+    re_path(r'^weather/(?P<operation>.*)$', WeatherControl.WeatherView.as_view()),
 
     # 后台界面接口 -------------------------------------------------------------------------------------------------------
     # 登录,用户信息,权限

+ 57 - 0
Controller/WeatherControl.py

@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+"""
+@Author : peng
+@Time : 2023-8-18 10:45:46
+@File :WeatherControl.py
+"""
+from Ansjer.config import LOGGER
+import time
+
+from django.views import View
+from Object.IPWeatherObject import WeatherInfo
+from Model.models import UidSetModel, CityInformation
+from Object.ResponseObject import ResponseObject
+
+
+class WeatherView(View):
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        return self.validation(request.GET, operation, request)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        return self.validation(request.POST, operation, request)
+
+    def validation(self, request_dict, operation, request):
+        lang = request_dict.get('lang', 'en')
+        response = ResponseObject(lang)
+        if operation == 'get':  # 获取天气
+            return self.get_weather(request_dict, response)
+        else:
+            return response.json(414)
+
+    @staticmethod
+    def get_weather(request_dict, response):
+        """
+        获取天气
+        @param request_dict: 请求参数
+        @request_dict uid: 设备uid
+        @param response: 响应对象
+        @return: response
+        """
+        uid = request_dict.get('uid')
+        if not uid:
+            return response.json(444, 'uid')
+        uid_set_qs = UidSetModel.objects.filter(uid=uid).values('tb_city_information_id')
+        if not uid_set_qs.exists():
+            return response.json(173)
+        try:
+            city_obj = WeatherInfo(uid_set_qs[0]['tb_city_information_id'])
+            temp, humidity = city_obj.get_city_weather()
+            return response.json(0, {'temp': temp, 'humidity': humidity})
+        except Exception as e:
+            print(e)
+            return response.json(500)
+

+ 3 - 2
Object/IPWeatherObject.py

@@ -28,7 +28,7 @@ class IPQuery:
             res_data = eval(res.content.decode('utf-8'))
             if res_data['ret'] == 200:
                 district = res_data['data']['district']
-                city = res_data['data']['city']+'市'
+                city = res_data['data']['city'] + '市'
                 self.district = district if district else city
 
 
@@ -47,5 +47,6 @@ class WeatherInfo:
             result = response.json()
             if result['code'] == 0:
                 # 返回温湿度
-                return result['data']['condition']['temp'], result['data']['condition']['humidity']
+                return '{} ℃'.format(result['data']['condition']['temp']), '{} %'.format(
+                    result['data']['condition']['humidity'])
         return False