浏览代码

添加alexa触发pushtool发送指令接口

locky 4 年之前
父节点
当前提交
038df7248d
共有 2 个文件被更改,包括 42 次插入1 次删除
  1. 2 1
      AnsjerUIDManage/urls.py
  2. 40 0
      Controller/AlexaController.py

+ 2 - 1
AnsjerUIDManage/urls.py

@@ -17,7 +17,7 @@ from django.contrib import admin
 from django.urls import path, re_path
 
 from Controller import UserController, FileController, UIDController, LogController, SalesController, \
-    OrderTaskController, HistoryUIDController, SerialNumberController
+    OrderTaskController, HistoryUIDController, SerialNumberController, AlexaController
 
 urlpatterns = [
     path('admin/', admin.site.urls),
@@ -36,4 +36,5 @@ urlpatterns = [
     path('device/online', SalesController.DeviceOnlineView.as_view()),
     re_path('serialNumber/(?P<operation>.*)', SerialNumberController.SerialNumberView.as_view()),
 
+    path('alexa/command', AlexaController.PushCommandView.as_view()),
 ]

+ 40 - 0
Controller/AlexaController.py

@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+import datetime
+import json
+import random
+import subprocess
+from django.views import View
+from django.utils.decorators import method_decorator
+from django.views.decorators.csrf import csrf_exempt
+
+from AnsjerUIDManage.config import SALES, ONLINE_DEVICE
+from Object.ResponseObject import ResponseObject
+
+
+class PushCommandView(View):
+
+    @method_decorator(csrf_exempt)
+    def dispatch(self, request, *args, **kwargs):
+        return super(PushCommandView, self).dispatch(request, *args, **kwargs)
+
+    def get(self, request, *args, **kwargs):
+        request_dict = request.GET
+        return self.PushCommand(request_dict)
+
+    def post(self, request, *args, **kwargs):
+        request_dict = request.POST
+        return self.PushCommand(request_dict)
+
+    def PushCommand(self, request_dict):
+        response = ResponseObject()
+        command = request_dict.get('command', None)
+        if not command:
+            return response(444)
+        try:
+            back = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE). \
+                                    communicate(timeout=2)
+            return response.json(0, back)
+        except Exception as e:
+            return response(500, repr(e))
+