deviceStatus.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. """
  5. import json
  6. import time
  7. import requests
  8. import uuid
  9. import logging
  10. from django.views.generic import TemplateView
  11. from django.shortcuts import render_to_response
  12. from django.http import JsonResponse
  13. import http.client
  14. from urllib.parse import urlencode
  15. from object.ResObject import ResObject
  16. import subprocess
  17. # from gevent.pool import Pool
  18. from model.models import UserModel,UidRtspModel
  19. from object.tkObject import tkObject
  20. from service.CommonService import CommonService
  21. from object.RedisObject import RedisObject
  22. from django.utils.decorators import method_decorator
  23. from django.views.decorators.csrf import csrf_exempt
  24. rtspServer = "rtsp.zositech.org,3.16.66.144"
  25. class deviceStatus(TemplateView):
  26. @method_decorator(csrf_exempt)
  27. def dispatch(self, *args, **kwargs):
  28. return super(deviceStatus, self).dispatch(*args, **kwargs)
  29. def post(self, request, *args, **kwargs):
  30. request.encoding = 'utf-8'
  31. operation = kwargs.get('operation')
  32. if operation == 'test':
  33. return JsonResponse({'res': '11'})
  34. def get(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. request_dict = request.GET
  37. operation = kwargs.get('operation')
  38. if operation == 'test':
  39. api_uri = 'https://api.amazonalexa.com/v3/events'
  40. messageId = str(uuid.uuid4())
  41. access_token = 'wR2eWfmTTFRLXq5pxSvaGGAI1tfLCfv5'
  42. bearer_access_token = 'Bearer {access_token}'.format(access_token=access_token)
  43. headers = {'content-type': "application/json", 'Authorization': bearer_access_token}
  44. payload = {
  45. "event": {
  46. "header": {
  47. "namespace": "Alexa.Discovery",
  48. "name": "AddOrUpdateReport",
  49. "payloadVersion": "3",
  50. "messageId": messageId
  51. },
  52. "payload": {
  53. "endpoints": [
  54. {
  55. "endpointId": "HVTLKFJM6KDTAF9J111A",
  56. "manufacturerName": "Sample Manufacturer",
  57. "description": "Smart Light by Sample Manufacturer",
  58. "friendlyName": "Kitchen Light",
  59. "displayCategories": [
  60. "CAMERA"
  61. ],
  62. "capabilities": [
  63. {
  64. "type": "AlexaInterface",
  65. "interface": "Alexa.CameraStreamController",
  66. "version": "3",
  67. "cameraStreamConfigurations": [
  68. {
  69. "protocols": ["RTSP"],
  70. "resolutions": [{"width": 1280, "height": 720}],
  71. "authorizationTypes": ["NONE"],
  72. "videoCodecs": ["H264"],
  73. "audioCodecs": ["ACC"]
  74. }]
  75. },
  76. {
  77. "type": "AlexaInterface",
  78. "interface": "Alexa.MediaMetadata",
  79. "version": "3",
  80. "proactivelyReported": True,
  81. # "retrievable": True
  82. }
  83. ]
  84. }
  85. ],
  86. "scope": {
  87. "type": "BearerToken",
  88. "token": access_token
  89. }
  90. }
  91. }
  92. }
  93. response = requests.post(api_uri, data = payload, headers = headers)
  94. request_json = response.json()
  95. return JsonResponse({'res': request_json})
  96. elif operation == 'getAccessToken':
  97. # lwa_params = {
  98. # "grant_type" : "authorization_code",
  99. # "code": 'NHxKR2mgPmNgCp9yxNCJvJ7fgPbCayNa',
  100. # "client_id": 'amzn1.application-oa2-client.efb07b51dd444f848b6f0598635da3cc',
  101. # "client_secret": 'showmeyousecret.zosi'
  102. # }
  103. # header = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
  104. # response = requests.post('https://api.amazon.com/auth/o2/token', data=lwa_params, headers= header, allow_redirects=True)
  105. #
  106. #
  107. # request_json = response.json()
  108. # return JsonResponse({'res': request_json})
  109. user_qs = UserModel.objects.filter(userID='158943604783713800138000').values('userID','refresh_token')
  110. refresh_token = user_qs[0]['refresh_token']
  111. payload = {
  112. 'grant_type': 'refresh_token',
  113. 'refresh_token': refresh_token,
  114. 'client_id': 'amzn1.application-oa2-client.efb07b51dd444f848b6f0598635da3cc',
  115. 'client_secret': '8a49390ebe362bfee153be87587f5673d0c1d8aeb6bc1ef736fda6c9d5d81c8f',
  116. 'redirect_uri': 'http://www.baidu.com'
  117. }
  118. return self.post_to_api(payload)
  119. def post_to_api(self, payload):
  120. connection = http.client.HTTPSConnection("api.amazon.com")
  121. headers = {
  122. 'content-type': "application/x-www-form-urlencoded",
  123. 'cache-control': "no-cache"
  124. }
  125. response = requests.post('https://api.amazon.com/auth/o2/token', urlencode(payload), headers= headers, allow_redirects=True)
  126. request_json = response.json()
  127. return JsonResponse({'res': request_json})