device.py 4.33 KB
Newer Older
1 2 3
#!/usr/bin/env python
# -*- coding: utf_8 -*-

wanli's avatar
wanli committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
from flask import current_app, jsonify, request
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from flask_jwt_extended import ( jwt_required, get_jwt_identity )
from application.signal_manager import signalManager
from models.device import  postDeviceSchema, deleteDeviceSchema, getListDeviceSchema, getListDevicesSchema, getDeviceSchema, putDeviceSchema
from webcreator.log import logger
from webcreator.response import ResponseCode, response_result

class DeviceResourceList(Resource):
    def __init__(self):
        pass
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        # self.parser = RequestParser()

    def get(self):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        # self.parser.add_argument("page", type=int, location="args", default=1)
        # self.parser.add_argument("pageSize", type=int, location="args", default=15)
        # args = self.parser.parse_args()

        try:
            json_payload = request.json
            logger.warn(json_payload)
            data = getListDeviceSchema.load(json_payload)
29
            result, message = signalManager.actionGetListDevice.emit(data)
wanli's avatar
wanli committed
30
            json_dumps = getListDeviceSchema.dump(result)
31 32
            if result:
                json_dumps = getListDevicesSchema.dump(result.items)
wanli's avatar
wanli committed
33
                logger.warn(json_dumps)
34 35
                return response_result(message, data=json_dumps, count=result.total)
            return response_result(message)
wanli's avatar
wanli committed
36 37
        except Exception as e:
            current_app.logger.error(e)
38
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
39 40 41 42 43 44

    @jwt_required(locations=["headers"])
    def post(self):
        try:
            json_payload = request.json
            data = postDeviceSchema.load(json_payload)
45 46 47 48
            result, message = signalManager.actionPostDevice.emit(data)
            logger.info(result)
            logger.warn(message)
            return response_result(message)
wanli's avatar
wanli committed
49 50
        except Exception as e:
            current_app.logger.error(e)
51
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70


class DeviceResource(Resource):
    def __init__(self):
        pass
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        # self.parser = RequestParser()

    @jwt_required(locations=["headers"])
    def get(self, uuid):
        # 特殊参数,即不是从json获取参数的接口,可以将这个注释打开
        # self.parser.add_argument("page", type=int, location="args", default=1)
        # self.parser.add_argument("pageSize", type=int, location="args", default=15)
        # args = self.parser.parse_args()

        try:
            json_payload = request.json
            print("========>", uuid, json_payload)
            data = getDeviceSchema.load(json_payload)
71 72 73 74 75
            result, message = signalManager.actionGetDevice.emit(uuid, data)
            if result:
                json_dumps = getDeviceSchema.dump(result)
                return response_result(message, data=json_dumps)
            return response_result(message)
wanli's avatar
wanli committed
76 77
        except Exception as e:
            current_app.logger.error(e)
78
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
79 80 81 82 83 84 85 86


    @jwt_required(locations=["headers"])
    def put(self, uuid):
        try:
            json_payload = request.json
            print("========>", uuid, json_payload)
            data = putDeviceSchema.load(json_payload)
87 88 89 90
            result, message = signalManager.actionPutDevice.emit(uuid, data)
            logger.info(result)
            logger.info(message)
            return response_result(message, data=result)
wanli's avatar
wanli committed
91 92
        except Exception as e:
            current_app.logger.error(e)
93
            return response_result(ResponseCode.HTTP_SERVER_ERROR)
wanli's avatar
wanli committed
94 95 96 97 98 99 100 101


    @jwt_required(locations=["headers"])
    def delete(self, uuid):
        try:
            json_payload = request.json
            print("========>", uuid, json_payload)
            # data = deleteDeviceSchema.load(json_payload)
102 103
            result, message = signalManager.actionDeleteDevice.emit(uuid)
            return response_result(message, data=result)
wanli's avatar
wanli committed
104 105
        except Exception as e:
            current_app.logger.error(e)
106
            return response_result(ResponseCode.HTTP_SERVER_ERROR)