123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- """
- import json
- import time
- import requests
- import uuid
- import logging
- from django.views.generic import TemplateView
- from django.shortcuts import render_to_response
- from django.http import JsonResponse
- import http.client
- from urllib.parse import urlencode
- from object.ResObject import ResObject
- import subprocess
- # from gevent.pool import Pool
- from model.models import UserModel,UidRtspModel
- from object.tkObject import tkObject
- from service.CommonService import CommonService
- from object.RedisObject import RedisObject
- from django.utils.decorators import method_decorator
- from django.views.decorators.csrf import csrf_exempt
- rtspServer = "rtsp.zositech.org,3.16.66.144"
- class deviceStatus(TemplateView):
- @method_decorator(csrf_exempt)
- def dispatch(self, *args, **kwargs):
- return super(deviceStatus, self).dispatch(*args, **kwargs)
- def post(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- operation = kwargs.get('operation')
- if operation == 'test':
- return JsonResponse({'res': '11'})
- def get(self, request, *args, **kwargs):
- request.encoding = 'utf-8'
- request_dict = request.GET
- operation = kwargs.get('operation')
- if operation == 'test':
- api_uri = 'https://api.amazonalexa.com/v3/events'
- messageId = str(uuid.uuid4())
- access_token = 'wR2eWfmTTFRLXq5pxSvaGGAI1tfLCfv5'
- bearer_access_token = 'Bearer {access_token}'.format(access_token=access_token)
- headers = {'content-type': "application/json", 'Authorization': bearer_access_token}
- payload = {
- "event": {
- "header": {
- "namespace": "Alexa.Discovery",
- "name": "AddOrUpdateReport",
- "payloadVersion": "3",
- "messageId": messageId
- },
- "payload": {
- "endpoints": [
- {
- "endpointId": "HVTLKFJM6KDTAF9J111A",
- "manufacturerName": "Sample Manufacturer",
- "description": "Smart Light by Sample Manufacturer",
- "friendlyName": "Kitchen Light",
- "displayCategories": [
- "CAMERA"
- ],
- "capabilities": [
- {
- "type": "AlexaInterface",
- "interface": "Alexa.CameraStreamController",
- "version": "3",
- "cameraStreamConfigurations": [
- {
- "protocols": ["RTSP"],
- "resolutions": [{"width": 1280, "height": 720}],
- "authorizationTypes": ["NONE"],
- "videoCodecs": ["H264"],
- "audioCodecs": ["ACC"]
- }]
- },
- {
- "type": "AlexaInterface",
- "interface": "Alexa.MediaMetadata",
- "version": "3",
- "proactivelyReported": True,
- # "retrievable": True
- }
- ]
- }
- ],
- "scope": {
- "type": "BearerToken",
- "token": access_token
- }
- }
- }
- }
- response = requests.post(api_uri, data = payload, headers = headers)
- request_json = response.json()
- return JsonResponse({'res': request_json})
- elif operation == 'getAccessToken':
- # lwa_params = {
- # "grant_type" : "authorization_code",
- # "code": 'NHxKR2mgPmNgCp9yxNCJvJ7fgPbCayNa',
- # "client_id": 'amzn1.application-oa2-client.efb07b51dd444f848b6f0598635da3cc',
- # "client_secret": 'showmeyousecret.zosi'
- # }
- # header = {"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}
- # response = requests.post('https://api.amazon.com/auth/o2/token', data=lwa_params, headers= header, allow_redirects=True)
- #
- #
- # request_json = response.json()
- # return JsonResponse({'res': request_json})
- user_qs = UserModel.objects.filter(userID='158943604783713800138000').values('userID','refresh_token')
- refresh_token = user_qs[0]['refresh_token']
- payload = {
- 'grant_type': 'refresh_token',
- 'refresh_token': refresh_token,
- 'client_id': 'amzn1.application-oa2-client.efb07b51dd444f848b6f0598635da3cc',
- 'client_secret': '8a49390ebe362bfee153be87587f5673d0c1d8aeb6bc1ef736fda6c9d5d81c8f',
- 'redirect_uri': 'http://www.baidu.com'
- }
- return self.post_to_api(payload)
- def post_to_api(self, payload):
- connection = http.client.HTTPSConnection("api.amazon.com")
- headers = {
- 'content-type': "application/x-www-form-urlencoded",
- 'cache-control': "no-cache"
- }
- response = requests.post('https://api.amazon.com/auth/o2/token', urlencode(payload), headers= headers, allow_redirects=True)
- request_json = response.json()
- return JsonResponse({'res': request_json})
|