|
@@ -12,6 +12,7 @@
|
|
|
@Contact: chanjunkai@163.com
|
|
|
"""
|
|
|
import json
|
|
|
+import threading
|
|
|
import time
|
|
|
import requests
|
|
|
import logging
|
|
@@ -55,7 +56,6 @@ class authView(TemplateView):
|
|
|
'redirect_uri': redirect_uri
|
|
|
}
|
|
|
return render_to_response("login.html", context)
|
|
|
- # return render_template('./login.html', **context)
|
|
|
|
|
|
|
|
|
# Anlapus登录
|
|
@@ -117,12 +117,16 @@ class loginHandleView(TemplateView):
|
|
|
auth_request_url = '{SERVER_PREFIX}/oalexa/auth'.format(SERVER_PREFIX=SERVER_PREFIX)
|
|
|
requests_data = {'userName': user, 'userPwd': pwd}
|
|
|
res = requests.post(url=auth_request_url, data=requests_data)
|
|
|
+ if res.status_code != 200:
|
|
|
+ return response.json(10, res={'错误': '请求响应异常'})
|
|
|
res_json = res.json()
|
|
|
|
|
|
# 添加测试服务器测试
|
|
|
if res_json['result_code'] != 0:
|
|
|
auth_request_url = '{SERVER_PREFIX}/oalexa/auth'.format(SERVER_PREFIX=SERVER_PREFIX_TEST)
|
|
|
res = requests.post(url=auth_request_url, data=requests_data)
|
|
|
+ if res.status_code != 200:
|
|
|
+ return response.json(10, res={'错误': '请求响应异常'})
|
|
|
res_json = res.json()
|
|
|
|
|
|
logger.info('请求服务器url: {}'.format(auth_request_url))
|
|
@@ -132,17 +136,15 @@ class loginHandleView(TemplateView):
|
|
|
return response.json(10, res={'msg': 'error'}, extra={'msg': res_json['reason']})
|
|
|
|
|
|
nowTime = int(time.time())
|
|
|
+ code = CommonService.encrypt_data(32)
|
|
|
userID = res_json['result']['userID']
|
|
|
user_qs = UserModel.objects.filter(userID=userID)
|
|
|
- if not user_qs.exists():
|
|
|
- UserModel.objects.create(
|
|
|
- userID=userID,
|
|
|
- addTime=nowTime,
|
|
|
- updTime=nowTime)
|
|
|
- user_qs = UserModel.objects.filter(userID=userID)
|
|
|
- code = CommonService.encrypt_data(32)
|
|
|
- user_qs.update(code=code)
|
|
|
- redirect_uri = redirect_uri + '?code=' + code + '&state=' + state
|
|
|
+ if user_qs.exists():
|
|
|
+ user_qs.update(code=code)
|
|
|
+ else:
|
|
|
+ UserModel.objects.create(userID=userID, code=code, addTime=nowTime, updTime=nowTime)
|
|
|
+
|
|
|
+ redirect_uri += '?code=' + code + '&state=' + state
|
|
|
return response.json(0, res=redirect_uri)
|
|
|
|
|
|
|
|
@@ -164,7 +166,7 @@ class oa2TokenView(TemplateView):
|
|
|
code = request_dict.get("code", None)
|
|
|
client_id = request_dict.get("client_id", None)
|
|
|
refresh_token = request_dict.get("refresh_token", None)
|
|
|
- from var_dump import var_dump
|
|
|
+
|
|
|
logger = logging.getLogger('django')
|
|
|
logger.info('token-------------begin--------')
|
|
|
logger.info(code)
|
|
@@ -198,13 +200,9 @@ class oa2TokenView(TemplateView):
|
|
|
return JsonResponse({'msg': 'error'})
|
|
|
else:
|
|
|
res_json = {'msg': 'code not exists'}
|
|
|
- logger.info(res_json)
|
|
|
return JsonResponse(res_json)
|
|
|
|
|
|
|
|
|
-import threading
|
|
|
-
|
|
|
-
|
|
|
def runSendRtspMsg_thread(UID, PWD, MSG):
|
|
|
command = "./pushtool {UID} {PWD} {MSG} 1".format(UID=UID, PWD=PWD, MSG=MSG)
|
|
|
print('command=>{command}'.format(command=command))
|
|
@@ -238,11 +236,13 @@ class oa2RtspStartView(TemplateView):
|
|
|
app_name = request_dict.get("app_name", 'zosi smart')
|
|
|
|
|
|
user_qs = UserModel.objects.filter(access_token=access_token)
|
|
|
-
|
|
|
if not user_qs.exists():
|
|
|
- return JsonResponse({'msg': 'wrong'})
|
|
|
+ return JsonResponse({'错误': '用户数据不存在'})
|
|
|
|
|
|
ur_qs = UidRtspModel.objects.filter(uid=uid).values('uid', 'nick', 'rtsp_url', 'password', 'region')
|
|
|
+ if not ur_qs.exists():
|
|
|
+ return JsonResponse({'错误': 'uid数据不存在'})
|
|
|
+
|
|
|
UID = ur_qs[0]['uid']
|
|
|
nick = ur_qs[0]['nick']
|
|
|
PWD = ur_qs[0]['password']
|
|
@@ -254,7 +254,9 @@ class oa2RtspStartView(TemplateView):
|
|
|
format(RTSP_PREFIX=RTSP_PREFIX, RESP_SERVER_DOMAIN=RESP_SERVER_DOMAIN, stream_name=stream_name)
|
|
|
|
|
|
logger = logging.getLogger('django')
|
|
|
+ logger.info('------开始向设备下发推流指令------')
|
|
|
logger.info('技能名称: {}'.format(app_name))
|
|
|
+
|
|
|
# 此处后续应该用异步去发送指令
|
|
|
if int(st) == 1:
|
|
|
send_flag = self.runSendStop(UID, PWD, MSG)
|
|
@@ -263,19 +265,24 @@ class oa2RtspStartView(TemplateView):
|
|
|
return JsonResponse({'msg': 'stop yes', 'code': 0})
|
|
|
else:
|
|
|
return JsonResponse({'msg': 'stop no', 'code': 0})
|
|
|
- # threading.Thread(target=runSendRtspMsg_thread, args=(UID, PWD, MSG)).start()
|
|
|
- logger.info('------开始向设备下发推流指令------uid:{},密码:{},指令:{}'.format(UID, PWD, MSG))
|
|
|
+
|
|
|
+ # 请求MQTT发布消息
|
|
|
url = '{}/iot/requestPublishMessage'.format(SERVER_PREFIX_TEST) # 测试服务器
|
|
|
requests_data = {'UID': UID, 'MSG': MSG}
|
|
|
r = requests.post(url, requests_data)
|
|
|
+ if r.status_code != 200:
|
|
|
+ return JsonResponse({'错误': '请求响应异常'})
|
|
|
+
|
|
|
res = r.json()
|
|
|
logger.info('请求MQTT发布消息返回状态: {}'.format(res['result_code']))
|
|
|
if res['result_code'] == 0:
|
|
|
logger.info('请求MQTT下发指令成功')
|
|
|
elif res['result_code'] == 10043:
|
|
|
- logger.info('使用tutk下发指令')
|
|
|
- send_flag = self.runSendRtspMsg(UID, PWD, MSG)
|
|
|
- logger.info('开始打印---------- send_flag3.0 :{} -----------------'.format(send_flag))
|
|
|
+ command = "./pushtool {UID} {PWD} {MSG} 1".format(UID=UID, PWD=PWD, MSG=MSG)
|
|
|
+ logger.info('------------推流指令: {}---------------'.format(command))
|
|
|
+ self.runSendRtspMsg(logger, region, command)
|
|
|
+ else:
|
|
|
+ return JsonResponse({'错误': '请求MQTT发布消息异常'})
|
|
|
|
|
|
# 拉流地址
|
|
|
rtsp_uri = '{RTSP_PREFIX}://{RESP_SERVER_DOMAIN}:443/{stream_name}'. \
|
|
@@ -284,29 +291,26 @@ class oa2RtspStartView(TemplateView):
|
|
|
expirationTime = time.strftime('%Y-%m-%dT%H:%MZ',time.localtime(stop_time))
|
|
|
|
|
|
res_json = {
|
|
|
+ 'uid': UID,
|
|
|
+ 'pwd': PWD,
|
|
|
+ 'msg': MSG,
|
|
|
+ 'uri': rtsp_uri,
|
|
|
'endpointId': uid,
|
|
|
- 'manufacturerName': app_name,
|
|
|
- 'manufacturerId': 'zosi-ACCC8E5E7513',
|
|
|
- 'modelName': 'P1425-LE',
|
|
|
'friendlyName': nick,
|
|
|
+ 'manufacturerName': app_name,
|
|
|
+ 'expirationTime': expirationTime,
|
|
|
'description': 'Camera connected via {}'.format(app_name),
|
|
|
- "expirationTime": expirationTime,
|
|
|
- "idleTimeoutSeconds": 5,
|
|
|
- #'resolutions': [{'width': 1280, 'height': 720}],
|
|
|
- 'resolutions': {'width': 640, 'height': 360},
|
|
|
- 'videoCodecs': 'H264',
|
|
|
+
|
|
|
'audioCodecs': 'ACC',
|
|
|
- # 'audioCodecs': ['G711'],
|
|
|
+ 'videoCodecs': 'H264',
|
|
|
'protocols': ['RTSP'],
|
|
|
+ 'idleTimeoutSeconds': 5,
|
|
|
+ 'modelName': 'P1425-LE',
|
|
|
'authorizationTypes': ['NONE'],
|
|
|
- 'uri': rtsp_uri,
|
|
|
- # 'msg': send_flag,
|
|
|
- 'uid': UID,
|
|
|
- 'pwd': PWD,
|
|
|
- 'msg': MSG
|
|
|
+ 'manufacturerId': 'zosi-ACCC8E5E7513',
|
|
|
+ 'resolutions': {'width': 640, 'height': 360},
|
|
|
}
|
|
|
- logger.info('开始打印------------返回控制摄像头的结果2.0---------------')
|
|
|
- logger.info(res_json)
|
|
|
+ logger.info('------------返回控制摄像头的信息---------------: {}'.format(res_json))
|
|
|
return JsonResponse(res_json, safe=False)
|
|
|
|
|
|
def runReqRtspMsg(self, UID, PWD, MSG):
|
|
@@ -316,51 +320,24 @@ class oa2RtspStartView(TemplateView):
|
|
|
print(res)
|
|
|
return True
|
|
|
|
|
|
- #触发此方法,让摄像头推流到MSG流地址
|
|
|
- def runSendRtspMsg(self, UID, PWD, MSG):
|
|
|
- region = UidRtspModel.objects.get(uid=UID).region
|
|
|
- # return True
|
|
|
- logger = logging.getLogger('django')
|
|
|
- logger.info('开始打印------------摄像头推流到MSG流地址的结果---------------')
|
|
|
- command = "./pushtool {UID} {PWD} {MSG} 1".format(UID=UID, PWD=PWD, MSG=MSG)
|
|
|
- print('command=>{command}'.format(command=command))
|
|
|
+ def reqSendRtspMsg(self, url):
|
|
|
+ requests.get(url=url)
|
|
|
|
|
|
+ # 触发此方法,让摄像头推流到MSG流地址
|
|
|
+ def runSendRtspMsg(self, logger, region, command):
|
|
|
if region == 'CN':
|
|
|
logger.info('------------国内发送推流指令---------------')
|
|
|
- # command_url = "http://47.115.134.251/index.php?command={command}".format(command=command)
|
|
|
- command_url = "http://52.83.252.41:7880/alexa/command?command={command}".format(command=command)
|
|
|
- logger.info("-----------command_url={command_url}".format(command_url=command_url))
|
|
|
+ url = "http://52.83.252.41:7880/alexa/command?command={command}".format(command=command)
|
|
|
+ # 多线程请求国内服务器调用pushtool
|
|
|
+ threading.Thread(target=self.reqSendRtspMsg, args=(url)).start()
|
|
|
+ else:
|
|
|
+ logger.info('------------国外发送推流指令---------------')
|
|
|
try:
|
|
|
- logger.info("-----------------开始调用接口")
|
|
|
- exec_res = requests.get(url=command_url, timeout=2)
|
|
|
- res = exec_res.json()
|
|
|
- logger.info('------------调用接口执行发送命令---------------')
|
|
|
- logger.info('res: {}'.format(res))
|
|
|
- if res['code'] == 200:
|
|
|
- return True
|
|
|
+ back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
|
|
|
+ communicate(timeout=2)
|
|
|
+ logger.info('back: {}'.format(str(back[0].decode()) + str(back[1].decode())))
|
|
|
except Exception as e:
|
|
|
- logger.info('------------except__e-------------')
|
|
|
- logger.info(e)
|
|
|
- return False
|
|
|
-
|
|
|
- logger.info('------------国外发送推流指令---------------')
|
|
|
- try:
|
|
|
- back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
|
|
|
- communicate(timeout=2)
|
|
|
- except Exception as e:
|
|
|
- logger.info('开始打印------------reprrrrrrrrrr--------except-------')
|
|
|
- logger.info(repr(e))
|
|
|
- return repr(e)
|
|
|
- else:
|
|
|
- print("back0----", back[0].decode()) # 注意需要进行解码操作,默认输出的是字节
|
|
|
- print("back1----", back[1].decode()) # back是一个元祖,可以通过元祖取值的方式获取结果
|
|
|
-
|
|
|
- logger.info(UID)
|
|
|
- logger.info(back[0].decode())
|
|
|
- logger.info(back[1].decode())
|
|
|
-
|
|
|
- return str(back[0].decode()) + str(back[1].decode())
|
|
|
- # return True
|
|
|
+ logger.info('调用pushtool异常: {}'.format(repr(e)))
|
|
|
|
|
|
def runSendStop(self, UID, PWD, MSG):
|
|
|
command = "./pushtool {UID} {PWD} {MSG} 0".format(UID=UID, PWD=PWD, MSG=MSG)
|
|
@@ -398,7 +375,7 @@ class oa2DiscoveryDevice(TemplateView):
|
|
|
|
|
|
user_qs = UserModel.objects.filter(access_token=access_token)
|
|
|
if not user_qs.exists():
|
|
|
- return JsonResponse({'res': '用户数据不存在!'})
|
|
|
+ return JsonResponse({'错误': '用户数据不存在'})
|
|
|
|
|
|
response = ResObject()
|
|
|
user = user_qs[0]
|