# Path: libfreeiot/core/resources/device.py
# class Device(Resource):
#     """
#         The RESTFul resource class of device
#     """
#     @jwt_required
#     def get(self, device_id=None):
#         """
#             RESTFul GET Method
#         """
#         if device_id!=None:
#             res = mongo.db.devices.find_one_or_404({'_id': ObjectId(device_id)})
#         else:
#             res = mongo.db.devices.find()
#         return Response(
#             json_util.dumps(res),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def post(self, device_id=None):
#         """
#             RESTFul POST Method
#         """
#         parser = reqparse.RequestParser()
#         parser.add_argument('remark', type=str, help='Remark of the device')
#         parser.add_argument('status', type=int, help='Status of the device')
#         parser.add_argument('version', type=str, help='Version of the device')
#         args = parser.parse_args()
#         if device_id is None:
#             data = args
#             res = mongo.db.devices.insert_one(data)
#         else:
#             data = {}
#             if not args["remark"] is None:
#                 data["remark"] = args["remark"]
#             if not args["status"] is None:
#                 data["status"] = args["status"]
#             if not args["version"] is None:
#                 data["version"] = args["version"]
#             if not ObjectId.is_valid(device_id):
#                 return abort(400)
#             if(len(data) < 1):
#                 return abort(400)
#             mongo.db.devices.update_one({"_id": ObjectId(device_id)},{
#                 "$set": data
#             })
#         return Response(
#             json_util.dumps(data),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def delete(self, device_id):
#         """
#             RESTFul DELETE Method
#         """
#         res = mongo.db.devices.delete_one({'_id': ObjectId(device_id)})
#         return Response(
#             json_util.dumps(res.raw_result),
#             mimetype='application/json'
#         )
# 
# Path: libfreeiot/core/resources/data.py
# class Data(Resource):
#     """
#         The RESTFul resource class of data
#     """
# 
#     def parse_args(self):
#         """ Message Parse Method """
#         parser = reqparse.RequestParser()
#         parser.add_argument('device', type=str, help='ObjectId(str) of the message\'s original device')
#         parser.add_argument('topic', type=str, help='The message\'s topic')
#         parser.add_argument('flag', type=str, help='The message\'s flag')
#         parser.add_argument('content', type=json.loads, help='The message\'s content of JSON Encode')
#         args = parser.parse_args()
#         args["device"] = DBRef("devices", args["device"])
#         return args
# 
#     @jwt_required
#     def get(self, data_id=None):
#         """
#             RESTFul GET Method
#         """
#         if data_id!=None:
#             res = mongo.db.datas.find_one_or_404({'_id': ObjectId(data_id)})
#         else:
#             res = mongo.db.datas.find()
#         return Response(
#             json_util.dumps(res),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def post(self):
#         """
#             RESTFul POST Method
#         """
#         args = self.parse_args()
#         res = mongo.db.datas.insert_one(args)
#         return Response(
#             json_util.dumps(res.inserted_id),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def put(self, data_id):
#         """
#             RESTFul PUT Method
#         """
#         args = self.parse_args()
#         res = mongo.db.datas.update_one({"_id": ObjectId(data_id)}, {"$set": args})
#         return Response(
#             json_util.dumps(res.raw_result),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def delete(self, data_id):
#         """
#             RESTFul DELETE Method
#         """
#         res = mongo.db.datas.delete_one({'_id': ObjectId(data_id)})
#         return Response(
#             json_util.dumps(res.raw_result),
#             mimetype='application/json'
#         )


import os
import datetime
from bson import json_util
from flask import request, jsonify, Response
from flask_restful import Api
from flask_jwt_simple import JWTManager, create_jwt
from .resources.device import Device
from .resources.data import Data
        '''
        return jsonify({"msg": "Hello World!"})

    @app.route('/api/auth', methods=['POST'])
    def auth():
        '''
          JWT Auth Route
        '''
        username = request.json.get('username', None)
        password = request.json.get('password', None)
        if not username:
            return jsonify({"msg": "Missing username parameter"}), 400
        if not password:
            return jsonify({"msg": "Missing password parameter"}), 400
        if 'auth'in scope.keys():
            authr = scope["auth"].login(username, password)
            if not authr:
                return jsonify({"msg": "Bad username or password"}), 401
        else:
            if username != 'admin' or password == 'admin':
                return jsonify({"msg": "Bad username or password"}), 401
        res = authr or {}
        res["jwt"] = create_jwt(identity=username)
        return Response(
            json_util.dumps(res),
            mimetype='application/json'
        ), 200

    # RESTFul API Routes definition
    api = Api(app)

# Path: libfreeiot/core/resources/device.py
# class Device(Resource):
#     """
#         The RESTFul resource class of device
#     """
#     @jwt_required
#     def get(self, device_id=None):
#         """
#             RESTFul GET Method
#         """
#         if device_id!=None:
#             res = mongo.db.devices.find_one_or_404({'_id': ObjectId(device_id)})
#         else:
#             res = mongo.db.devices.find()
#         return Response(
#             json_util.dumps(res),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def post(self, device_id=None):
#         """
#             RESTFul POST Method
#         """
#         parser = reqparse.RequestParser()
#         parser.add_argument('remark', type=str, help='Remark of the device')
#         parser.add_argument('status', type=int, help='Status of the device')
#         parser.add_argument('version', type=str, help='Version of the device')
#         args = parser.parse_args()
#         if device_id is None:
#             data = args
#             res = mongo.db.devices.insert_one(data)
#         else:
#             data = {}
#             if not args["remark"] is None:
#                 data["remark"] = args["remark"]
#             if not args["status"] is None:
#                 data["status"] = args["status"]
#             if not args["version"] is None:
#                 data["version"] = args["version"]
#             if not ObjectId.is_valid(device_id):
#                 return abort(400)
#             if(len(data) < 1):
#                 return abort(400)
#             mongo.db.devices.update_one({"_id": ObjectId(device_id)},{
#                 "$set": data
#             })
#         return Response(
#             json_util.dumps(data),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def delete(self, device_id):
#         """
#             RESTFul DELETE Method
#         """
#         res = mongo.db.devices.delete_one({'_id': ObjectId(device_id)})
#         return Response(
#             json_util.dumps(res.raw_result),
#             mimetype='application/json'
#         )
# 
# Path: libfreeiot/core/resources/data.py
# class Data(Resource):
#     """
#         The RESTFul resource class of data
#     """
# 
#     def parse_args(self):
#         """ Message Parse Method """
#         parser = reqparse.RequestParser()
#         parser.add_argument('device', type=str, help='ObjectId(str) of the message\'s original device')
#         parser.add_argument('topic', type=str, help='The message\'s topic')
#         parser.add_argument('flag', type=str, help='The message\'s flag')
#         parser.add_argument('content', type=json.loads, help='The message\'s content of JSON Encode')
#         args = parser.parse_args()
#         args["device"] = DBRef("devices", args["device"])
#         return args
# 
#     @jwt_required
#     def get(self, data_id=None):
#         """
#             RESTFul GET Method
#         """
#         if data_id!=None:
#             res = mongo.db.datas.find_one_or_404({'_id': ObjectId(data_id)})
#         else:
#             res = mongo.db.datas.find()
#         return Response(
#             json_util.dumps(res),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def post(self):
#         """
#             RESTFul POST Method
#         """
#         args = self.parse_args()
#         res = mongo.db.datas.insert_one(args)
#         return Response(
#             json_util.dumps(res.inserted_id),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def put(self, data_id):
#         """
#             RESTFul PUT Method
#         """
#         args = self.parse_args()
#         res = mongo.db.datas.update_one({"_id": ObjectId(data_id)}, {"$set": args})
#         return Response(
#             json_util.dumps(res.raw_result),
#             mimetype='application/json'
#         )
# 
#     @jwt_required
#     def delete(self, data_id):
#         """
#             RESTFul DELETE Method
#         """
#         res = mongo.db.datas.delete_one({'_id': ObjectId(data_id)})
#         return Response(
#             json_util.dumps(res.raw_result),
#             mimetype='application/json'
#         )


# Path: libfreeiot/core/routes.py
import os
import datetime
from bson import json_util
from flask import request, jsonify, Response
from flask_restful import Api
from flask_jwt_simple import JWTManager, create_jwt
from .resources.device import Device
from .resources.data import Data

        '''
        return jsonify({"msg": "Hello World!"})

    @app.route('/api/auth', methods=['POST'])
    def auth():
        '''
          JWT Auth Route
        '''
        username = request.json.get('username', None)
        password = request.json.get('password', None)
        if not username:
            return jsonify({"msg": "Missing username parameter"}), 400
        if not password:
            return jsonify({"msg": "Missing password parameter"}), 400
        if 'auth'in scope.keys():
            authr = scope["auth"].login(username, password)
            if not authr:
                return jsonify({"msg": "Bad username or password"}), 401
        else:
            if username != 'admin' or password == 'admin':
                return jsonify({"msg": "Bad username or password"}), 401
        res = authr or {}
        res["jwt"] = create_jwt(identity=username)
        return Response(
            json_util.dumps(res),
            mimetype='application/json'
        ), 200

    # RESTFul API Routes definition
    api = Api(app)

    api.add_resource(Device, '/api/device', '/api/device/<string:device_id>')