views.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # coding=utf-8
  2. import datetime
  3. import logging
  4. import json
  5. import random
  6. import time
  7. from mongoengine import DoesNotExist
  8. from django.views.decorators.http import require_POST
  9. from apilib.utils_json import JsonResponse
  10. from apps.web.api.ft_north.constant import RESPONSE_CODE
  11. from apps.web.api.ft_north.models import FengTuTechnologyNorther
  12. from apps.web.api.ft_north.utils import get_coding_info , get_device_status, generate_json_token,parse_json_token
  13. logger = logging.getLogger(__name__)
  14. @require_POST
  15. def getToken(request):
  16. """
  17. 通过账号密码获取token
  18. """
  19. logger.debug("[queryToken] request body = {}".format(request.body))
  20. if not request.body:
  21. return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1001)"})
  22. try:
  23. data = json.loads(request.body).get("data")
  24. except Exception as e:
  25. logger.exception(e)
  26. return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1002)"})
  27. appid = data.get("appid")
  28. appSecret = data.get("appSecret")
  29. logger.debug("[queryToken] appid = {}, appSecret = {}".format(appid, appSecret))
  30. if not all((appid, appSecret)):
  31. return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
  32. try:
  33. norther = FengTuTechnologyNorther.objects.filter(appId=appid, appSecret=appSecret).first() # type: FengTuTechnologyNorther
  34. except DoesNotExist:
  35. return JsonResponse({"success": False,"code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
  36. except Exception as e:
  37. return JsonResponse({"success": False,"code": RESPONSE_CODE.SYS_ERROR, "message": u"系统错误"})
  38. expire = int(time.time() + 60 * 60 * 24) * 1000
  39. token = generate_json_token(data=norther.get_token_data(), expire=expire)
  40. resultData = {
  41. "token": token,
  42. "expiresTime": expire
  43. }
  44. return JsonResponse({
  45. "success": True,
  46. "code": 200,
  47. "message": "请求成功",
  48. "data": resultData,
  49. })
  50. def getOrder(request):
  51. """
  52. 查询订单数量
  53. :param request:
  54. :return:
  55. """
  56. # 验证身份
  57. token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip()
  58. logger.info('[getOrder] , token = {}'.format(token))
  59. # 验证身份
  60. tokenData = parse_json_token(token)
  61. if not tokenData:
  62. return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"})
  63. try:
  64. data = json.loads(request.body).get("data")
  65. except Exception as e:
  66. logger.exception(e)
  67. return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
  68. # 验证参数
  69. logger.debug("[queryStationsInfo] request body = {}".format(request.body))
  70. if not request.body:
  71. return JsonResponse({"success":False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
  72. # 查询参数
  73. deviceNo = str(data.get('deviceNo'))
  74. startTime = data.get('startTime',"2015-01-01 00:00:00")
  75. endTime = data.get("endTime",datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
  76. result = {
  77. "orderNum": random.randint(10, 200)
  78. }
  79. resultData = json.dumps(result)
  80. return JsonResponse({
  81. "success": True,
  82. "message": u"请求成功",
  83. "code": u"200",
  84. "data": resultData,
  85. })
  86. def getCoding(request):
  87. """
  88. 查询端口状态
  89. :param request:
  90. :return:
  91. """
  92. # 验证身份
  93. token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip()
  94. logger.info('[getOrder] , token = {}'.format(token))
  95. # 验证身份
  96. tokenData = parse_json_token(token)
  97. if not tokenData:
  98. return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"})
  99. try:
  100. data = json.loads(request.body).get("data")
  101. except Exception as e:
  102. logger.exception(e)
  103. return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
  104. # 验证参数
  105. logger.debug("[queryStationsInfo] request body = {}".format(request.body))
  106. if not request.body:
  107. return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
  108. devNo = str(data.get('deviceNo'))
  109. codingInfo = get_coding_info(devNo)
  110. if not codingInfo:
  111. return JsonResponse({
  112. "success": False,
  113. "message": u"设备未启动",
  114. "code": u"303"
  115. })
  116. resultData = json.dumps(codingInfo)
  117. return JsonResponse({
  118. "success": True,
  119. "message": u"请求成功",
  120. "code": u"200",
  121. "data": resultData,
  122. })
  123. def getDeviceInfo(request):
  124. """
  125. 查询设备状态
  126. :param request:
  127. :return:
  128. """
  129. # 验证身份
  130. token = request.META.get('HTTP_AUTHORIZATION', "").replace("Bearer", "").strip()
  131. logger.info('[getOrder] , token = {}'.format(token))
  132. # 验证身份
  133. tokenData = parse_json_token(token)
  134. if not tokenData:
  135. return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_TOKEN, "message": u"请求参数错误(1001)"})
  136. try:
  137. data = json.loads(request.body).get("data")
  138. except Exception as e:
  139. logger.exception(e)
  140. return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1004)"})
  141. # 验证参数
  142. logger.debug("[queryStationsInfo] request body = {}".format(request.body))
  143. if not request.body:
  144. return JsonResponse({"success": False, "code": RESPONSE_CODE.ERROR_POST, "message": u"请求参数错误(1003)"})
  145. devNo = str(data.get('deviceNo'))
  146. deviceStatus = get_device_status(devNo)
  147. result = {
  148. "deviceStatus": deviceStatus
  149. }
  150. resultData = json.dumps(result)
  151. return JsonResponse({
  152. "success": True,
  153. "message": u"请求成功",
  154. "code": u"200",
  155. "data": resultData,
  156. })