123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- # coding=utf-8
- import datetime
- import logging
- import json
- import random
- import time
- from mongoengine import DoesNotExist
- from django.views.decorators.http import require_POST
- from apilib.utils_json import JsonResponse
- from apps.web.api.ft_north.constant import RESPONSE_CODE
- from apps.web.api.ft_north.models import FengTuTechnologyNorther
- from apps.web.api.ft_north.utils import get_coding_info , get_device_status, generate_json_token,parse_json_token
- logger = logging.getLogger(__name__)
- @require_POST
- def getToken(request):
- """
- 通过账号密码获取token
- """
- logger.debug("[queryToken] request body = {}".format(request.body))
- if not request.body:
- return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1001)"})
- try:
- data = json.loads(request.body).get("data")
- except Exception as e:
- logger.exception(e)
- return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1002)"})
- appid = data.get("appid")
- appSecret = data.get("appSecret")
- logger.debug("[queryToken] appid = {}, appSecret = {}".format(appid, appSecret))
- if not all((appid, appSecret)):
- return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
- try:
- norther = FengTuTechnologyNorther.objects.filter(appId=appid, appSecret=appSecret).first() # type: FengTuTechnologyNorther
- except DoesNotExist:
- return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
- except Exception as e:
- return JsonResponse({"success": False,"code": RESPONSE_CODE.SYS_ERROR, "message": u"系统错误"})
- expire = int(time.time() + 60 * 60 * 24) * 1000
- token = generate_json_token(data=norther.get_token_data(), expire=expire)
- resultData = {
- "token": token,
- "expiresTime": expire
- }
- return JsonResponse({
- "success": True,
- "code": 200,
- "message": "请求成功",
- "data": resultData,
- })
- def getOrder(request):
- """
- 查询订单数量
- :param request:
- :return:
- """
- # 验证身份
- token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip()
- logger.info('[getOrder] , token = {}'.format(token))
- # 验证身份
- tokenData = parse_json_token(token)
- if not tokenData:
- return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"})
- try:
- data = json.loads(request.body).get("data")
- except Exception as e:
- logger.exception(e)
- return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
- # 验证参数
- logger.debug("[queryStationsInfo] request body = {}".format(request.body))
- if not request.body:
- return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
- # 查询参数
- deviceNo = str(data.get('deviceNo'))
- startTime = data.get('startTime',"2015-01-01 00:00:00")
- endTime = data.get("endTime",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
- result = {
- "orderNum": random.randint(10, 200)
- }
- resultData = json.dumps(result)
- return JsonResponse({
- "success": True,
- "message": u"请求成功",
- "code": u"200",
- "data": resultData,
- })
- def getCoding(request):
- """
- 查询端口状态
- :param request:
- :return:
- """
- # 验证身份
- token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip()
- logger.info('[getOrder] , token = {}'.format(token))
- # 验证身份
- tokenData = parse_json_token(token)
- if not tokenData:
- return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"})
- try:
- data = json.loads(request.body).get("data")
- except Exception as e:
- logger.exception(e)
- return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
- # 验证参数
- logger.debug("[queryStationsInfo] request body = {}".format(request.body))
- if not request.body:
- return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
- devNo = str(data.get('deviceNo'))
- codingInfo = get_coding_info(devNo)
- if not codingInfo:
- return JsonResponse({
- "success": False,
- "message": u"设备未启动",
- "code": u"303"
- })
- resultData = json.dumps(codingInfo)
- return JsonResponse({
- "success": True,
- "message": u"请求成功",
- "code": u"200",
- "data": resultData,
- })
- def getDeviceInfo(request):
- """
- 查询设备状态
- :param request:
- :return:
- """
- # 验证身份
- token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip()
- logger.info('[getOrder] , token = {}'.format(token))
- # 验证身份
- tokenData = parse_json_token(token)
- if not tokenData:
- return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"})
- try:
- data = json.loads(request.body).get("data")
- except Exception as e:
- logger.exception(e)
- return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
- # 验证参数
- logger.debug("[queryStationsInfo] request body = {}".format(request.body))
- if not request.body:
- return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
- devNo = str(data.get('deviceNo'))
- deviceStatus = get_device_status(devNo)
- result = {
- "deviceStatus": deviceStatus
- }
- resultData = json.dumps(result)
- return JsonResponse({
- "success": True,
- "message": u"请求成功",
- "code": u"200",
- "data": resultData,
- })
|