Bläddra i källkod

注册IOT创建线程调用Alexa接口,ozi域名

locky 3 månader sedan
förälder
incheckning
74232c44a1
3 ändrade filer med 39 tillägg och 24 borttagningar
  1. 32 19
      Controller/IotCoreController.py
  2. 6 5
      Controller/RegionController.py
  3. 1 0
      Model/models.py

+ 32 - 19
Controller/IotCoreController.py

@@ -2,16 +2,18 @@
 # -*- coding: utf-8 -*-
 import hashlib
 import logging
+import threading
 import time
 import uuid
 from collections import OrderedDict
 
+import requests
 from django.views import View
 
 from Ansjer.config import AWS_IOT_GETS3_PULL_CHINA_ID, AWS_IOT_GETS3_PULL_CHINA_SECRET, \
     AWS_IOT_GETS3_PULL_FOREIGN_ID, AWS_IOT_GETS3_PULL_FOREIGN_SECRET, AWS_ARN, AWS_IOT_SES_ACCESS_CHINA_REGION, \
     AWS_IOT_SES_ACCESS_FOREIGN_REGION_ASIA, AWS_IOT_SES_ACCESS_FOREIGN_REGION_EUROPE, \
-    AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA, REGION_ID_LIST
+    AWS_IOT_SES_ACCESS_FOREIGN_REGION_AMERICA, REGION_ID_LIST, ALEXA_DOMAIN
 from Model.models import Device_Info, iotdeviceInfoModel, SerialNumberModel, UidSetModel
 from Object.IOTCore.IotObject import IOTClient
 from Object.ResponseObject import ResponseObject
@@ -103,7 +105,7 @@ class IotCoreView(View):
                 try:
                     SerialNumberModel.objects.get(serial_number=serial)
                 except:
-                    return response.json(444)
+                    return response.json(173)
 
                 thing_name_suffix = serial_number  # 物品名后缀
                 iot_device_info_qs = iotdeviceInfoModel.objects.filter(serial_number=serial)
@@ -140,17 +142,23 @@ class IotCoreView(View):
                 res = iotClient.register_to_iot_core(thingName, thingGroup, response)
 
                 token_iot_number = hashlib.md5((str(uuid.uuid1()) + str(int(time.time()))).encode('utf-8')).hexdigest()
-                iotdeviceInfoModel.objects.create(uid=uid,
-                                                  serial_number=serial,
-                                                  endpoint=res[0]['endpoint'],
-                                                  certificate_id=res[0]['certificateId'],
-                                                  certificate_pem=res[0]['certificatePem'],
-                                                  public_key=res[0]['publicKey'],
-                                                  private_key=res[0]['privateKey'],
-                                                  thing_name=res[1]['ThingName'],
-                                                  thing_groups=res[1]['thingGroupName'],
-                                                  token_iot_number=token_iot_number
-                                                  )
+
+                data = {
+                    'uid': uid,
+                    'serial_number': serial,
+                    'endpoint': res[0]['endpoint'],
+                    'certificate_id': res[0]['certificateId'],
+                    'certificate_pem': res[0]['certificatePem'],
+                    'public_key': res[0]['publicKey'],
+                    'private_key': res[0]['privateKey'],
+                    'thing_name': res[1]['ThingName'],
+                    'thing_groups': res[1]['thingGroupName'],
+                    'token_iot_number': token_iot_number
+                }
+                # 创建线程调用Alexa接口
+                threading.Thread(target=call_alexa_api, kwargs=data).start()
+
+                iotdeviceInfoModel.objects.create(**data)
                 res = {
                     'certificateId': res[0]['certificateId'],
                     'certificatePem': res[0]['certificatePem'],
@@ -161,7 +169,7 @@ class IotCoreView(View):
                 return response.json(0, {'res': res})
         except Exception as e:
             print(e)
-            return response.json(500, repr(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @staticmethod
     def thing_regroup(request_dict, response):
@@ -245,7 +253,7 @@ class IotCoreView(View):
             return response.json(0)
         except Exception as e:
             print(e)
-            return response.json(500, repr(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     def clear_Iot_Cerm(self, request_dict, response):
         serial_number = request_dict.get('serial_number', None)
@@ -282,7 +290,7 @@ class IotCoreView(View):
                 return response.json(10044)
             return response.json(0)
         except Exception as e:
-            return response.json(500, repr(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     def get_s3_pull_key(self, request_dict, response, request):
         # 通用发布主题通知
@@ -311,7 +319,7 @@ class IotCoreView(View):
             return response.json(0, MSG)
         except Exception as e:
             # print(e)
-            return response.json(500, repr(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     def get_s3_key_return_msg(self, endpoint):
         MSG = {}
@@ -362,7 +370,7 @@ class IotCoreView(View):
             res = {'endpoint': endpoint, 'token_iot_number': token_iot_number}
             return response.json(0, res)
         except Exception as e:
-            return response.json(500, repr(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     def pcGetIotInfo(self, request_dict, response):
         # PC工具获取IoT数据
@@ -388,4 +396,9 @@ class IotCoreView(View):
             res = {'endpoint': endpoint, 'token_iot_number': token_iot_number}
             return response.json(0, res)
         except Exception as e:
-            return response.json(500, repr(e))
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
+
+
+def call_alexa_api(**kwargs):
+    url = ALEXA_DOMAIN + 'device/creatOrUpdateIotInfo'
+    requests.post(url=url, data=kwargs, timeout=30)

+ 6 - 5
Controller/RegionController.py

@@ -314,11 +314,6 @@ class RegionView(View):
             if not CommonService.check_time_stamp_token(time_stamp_token, time_stamp):
                 return response.json(13)
 
-            # 查询app信息
-            app_inf_qs = App_Info.objects.filter(appBundleId=app_bundle_id)
-            if not app_inf_qs.exists():
-                return response.json(173)
-
             lang_qs = LanguageModel.objects.filter(lang=lang)
             if not lang_qs.exists():
                 lang_qs = LanguageModel.objects.filter(lang='en')
@@ -330,6 +325,8 @@ class RegionView(View):
                 app_name = 'Loocam'
             elif app_bundle_id in ['com.cloudlife.commissionf', 'com.cloudlife.commissionf_a']:
                 app_name = 'VSees'
+            elif app_bundle_id in ['com.livecamera.cooperationh', 'com.livecamera.cooperationh_a']:
+                app_name = 'ozi'
 
             country_qs = CountryLanguageModel.objects.filter(language_id=lang.id). \
                 annotate(push_api=F('country__region__push_api'))
@@ -341,6 +338,8 @@ class RegionView(View):
                 country_qs = country_qs.annotate(api=F('country__region__loocam_api'))
             elif app_name == 'VSees':
                 country_qs = country_qs.annotate(api=F('country__region__neutral_api'))
+            elif app_name == 'ozi':
+                country_qs = country_qs.annotate(api=F('country__region__ozi_api'))
             else:
                 country_qs = country_qs.annotate(api=F('country__region__api'))
             country_qs = country_qs.values('country_id', 'country_name', 'api', 'push_api').order_by('country_id')
@@ -355,6 +354,8 @@ class RegionView(View):
                                 country['api'] = 'https://api.zositech2.com/'
                             elif app_name == 'Loocam':
                                 country['api'] = 'https://api.loocam2.com/'
+                            elif app_name == 'ozi':
+                                country['api'] = 'https://api.zositech2.com/'
                             else:
                                 country['api'] = 'https://www.dvema.com/'
                             break

+ 1 - 0
Model/models.py

@@ -2980,6 +2980,7 @@ class RegionModel(models.Model):
     loocam_api = models.CharField(max_length=50, default='', verbose_name='录看域名')
     neutral_api = models.CharField(max_length=50, default='', verbose_name='中性域名')
     push_api = models.CharField(max_length=50, default='', verbose_name='推送域名')
+    ozi_api = models.CharField(max_length=50, default='', verbose_name='ozi域名')
 
     class Meta:
         db_table = 'tb_region'