DeviceLog.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/6/15 16:47
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: DeviceLog.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import datetime
  15. from django.views.decorators.csrf import csrf_exempt
  16. from Model.models import Device_Info
  17. from Object.mongodb import mongodb
  18. from Service.TokenManager import JSONTokenManager
  19. from Service.CommonService import CommonService
  20. from Service.ModelService import ModelService
  21. from Service.DeviceOperation import DeviceOperation
  22. from Service.ResponseService import *
  23. '''
  24. curl http://192.168.136.45:8077/devices/C2887N9EBS87ZAT1111A/logs -d "deviceData={\"UID\":\"C2887N9EBS87ZAT1111A\",\"type\":\"0x10014\",\"UserIP\":\"127.0.0.1\",\"time\":1529474865,\"name\":\"admin\"}"
  25. http://192.168.136.40:8077/devices/2N1K3LE78TYJ38CE111A/logs?type=All&starttime=1529462169&endtime=1529462169
  26. '''
  27. @csrf_exempt
  28. def DeviceLog(request, uid):
  29. # 存储设备日志
  30. if request.method == 'POST':
  31. request.encoding = 'utf-8'
  32. deviceData = request.POST.get('deviceData', None)
  33. device_info = Device_Info.objects.filter(UID=uid)
  34. if device_info.exists():
  35. try:
  36. data = json.loads(deviceData)
  37. mdb = mongodb()
  38. data['et'] = datetime.datetime.utcnow()
  39. col = "log_device_operation"
  40. id = mdb.insert_one(col=col, data=data)
  41. return ResponseJSON(0)
  42. # qs = mdb.findAll(col=col, page=1, line=10)
  43. except Exception as e:
  44. print(repr(e))
  45. return ResponseJSON(48,repr(e))
  46. else:
  47. return ResponseJSON(13)
  48. # 查找设备日志
  49. if request.method == 'GET':
  50. request.encoding = 'utf-8'
  51. token = request.GET.get('token', None)
  52. page = request.GET.get('page', None)
  53. line = request.GET.get('line', None)
  54. type = request.GET.get('type', None)
  55. search_class = request.GET.get('class', None)
  56. starttime = request.GET.get('starttime', None)
  57. endtime = request.GET.get('endtime', None)
  58. if token is not None:
  59. tokenManager = JSONTokenManager()
  60. error_code = tokenManager.verify_AToken(token)
  61. if error_code == 0:
  62. userID = tokenManager.accessDict.get('userID', None)
  63. if page is None and line is None:
  64. page = 1
  65. line = 10000
  66. param_flag = CommonService.get_param_flag(data=[userID, page, line, uid])
  67. own_dev = ModelService.check_own_device(userID=userID, UID=uid)
  68. own_per = ModelService.check_permission(userID=userID, permID=30)
  69. if own_dev is True or own_per is True:
  70. if param_flag is True:
  71. query = {'uid': uid}
  72. if search_class == 'All' or type == 'All':
  73. pass
  74. else:
  75. try:
  76. class_data= DeviceOperation.getODla(search_class=search_class)
  77. query['type'] = {'$gte': class_data[0], '$lte': class_data[1]}
  78. except Exception as e:
  79. if type is not None:
  80. query['type'] = type
  81. # if starttime is not None and endtime is not None and starttime != '' and endtime != '':
  82. # query['time'] = {'$gte': int(starttime), '$lte': int(endtime)}
  83. if starttime is not None and starttime != '' and endtime is not None and endtime != '':
  84. query['time'] = {'$gte': int(starttime), '$lte': int(endtime)}
  85. elif starttime is not None and starttime != '':
  86. query['time'] = {'$gte': int(starttime)}
  87. elif endtime is not None and endtime != '':
  88. query['time'] = {'$lte': int(endtime)}
  89. print('___________query____________')
  90. print(query)
  91. print('___________query____________')
  92. mdb = mongodb()
  93. col = "log_device_operation"
  94. # if int(page) < 0:
  95. # count = mdb.cout(col=col,query=query)
  96. # total = math.ceil(count/int(line))
  97. # page = int(total)+int(page)+1
  98. qs = mdb.findAll(col=col, page=int(page), line=int(line), query=query)
  99. if qs is not False:
  100. data_list = []
  101. for i in qs['data']:
  102. i['type'] = DeviceOperation.getOperation(type=i['type'])
  103. data_list.append(i)
  104. qs['data'] = data_list
  105. return ResponseJSON(0,qs)
  106. else:
  107. return ResponseJSON(404)
  108. return ResponseJSON(444)
  109. else:
  110. return HttpResponse(tokenManager.errorCodeInfo(error_code))
  111. else:
  112. return ResponseJSON(311)