DeviceLog.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import simplejson as json
  16. from django.views.decorators.csrf import csrf_exempt
  17. from Model.models import Device_Info
  18. from Object.ResponseObject import ResponseObject
  19. from Object.TokenObject import TokenObject
  20. from Object.mongodb import mongodb
  21. from Service.CommonService import CommonService
  22. from Service.DeviceOperation import DeviceOperation
  23. from Service.ModelService import ModelService
  24. '''
  25. 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\"}"
  26. http://192.168.136.40:8077/devices/2N1K3LE78TYJ38CE111A/logs?type=All&starttime=1529462169&endtime=1529462169
  27. '''
  28. @csrf_exempt
  29. def DeviceLog(request, uid):
  30. # 存储设备日志
  31. response = ResponseObject()
  32. if request.method == 'POST':
  33. request.encoding = 'utf-8'
  34. deviceData = request.POST.get('deviceData', None)
  35. device_info = Device_Info.objects.filter(UID=uid)
  36. if device_info.exists():
  37. try:
  38. data = json.loads(deviceData)
  39. mdb = mongodb()
  40. data['et'] = datetime.datetime.utcnow()
  41. col = "log_device_operation"
  42. mdb.insert_one(col=col, data=data)
  43. return response.json(0)
  44. except Exception as e:
  45. print(repr(e))
  46. return response.json(48,repr(e))
  47. else:
  48. return response.json(13)
  49. # 查找设备日志
  50. if request.method == 'GET':
  51. request.encoding = 'utf-8'
  52. token = request.GET.get('token', None)
  53. page = request.GET.get('page', None)
  54. line = request.GET.get('line', None)
  55. type = request.GET.get('type', None)
  56. search_class = request.GET.get('class', None)
  57. starttime = request.GET.get('starttime', None)
  58. endtime = request.GET.get('endtime', None)
  59. if token is not None:
  60. tko = TokenObject(token)
  61. tko.valid()
  62. response.lang = tko.lang
  63. if tko.code == 0:
  64. userID = tko.userID
  65. if page is None and line is None:
  66. page = 1
  67. line = 10000
  68. param_flag = CommonService.get_param_flag(data=[userID, page, line, uid])
  69. own_dev = ModelService.check_own_device(userID=userID, UID=uid)
  70. own_per = ModelService.check_permission(userID=userID, permID=30)
  71. if own_dev is True or own_per is True:
  72. if param_flag is True:
  73. query = {'uid': uid}
  74. if search_class == 'All' or type == 'All':
  75. pass
  76. else:
  77. try:
  78. class_data= DeviceOperation.getODla(search_class=search_class)
  79. query['type'] = {'$gte': class_data[0], '$lte': class_data[1]}
  80. except Exception as e:
  81. if type is not None:
  82. query['type'] = type
  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. qs = mdb.findAll(col=col, page=int(page), line=int(line), query=query)
  95. if qs is not False:
  96. data_list = []
  97. for i in qs['data']:
  98. i['type'] = DeviceOperation.getOperation(type=i['type'])
  99. data_list.append(i)
  100. qs['data'] = data_list
  101. return response.json(0,qs)
  102. else:
  103. return response.json(404)
  104. return response.json(444)
  105. else:
  106. return response.json(tko.code)
  107. else:
  108. return response.json(309)