device_sms_alert.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from smartfarming.models.pest_count import MongoCBDPestWarning
  2. from smartfarming.models.device import MongoDevice
  3. from kedong.decoration import kedong_deco, PortError
  4. @kedong_deco(login_required=True)
  5. def user_cbd_pest_warning_record_list(request):
  6. """
  7. 测报灯预警记录列表
  8. page 非必传(str) 页码数
  9. page_size 非必传(str) 条数
  10. warning_type 必传(str) 1目标种类预警 2、指定害虫数量预警 3 害虫数量总和预警 4综合预警
  11. start_time 非必传(str) 开始时间 时间戳
  12. end_time 非必传(str) 结束时间 时间戳
  13. """
  14. page = request.POST.get("page","1")
  15. page_size = request.POST.get("page_size","10")
  16. warning_type = request.POST.get("warning_type")
  17. start_time = request.POST.get("start_time")
  18. end_time = request.POST.get("end_time")
  19. if page and page_size:
  20. if page.isdigit() and page_size.isdigit():
  21. page = int(page)
  22. page_size = int(page_size)
  23. else:
  24. raise PortError("page&page_size","参数有误")
  25. else:
  26. page = 1
  27. page_size = 10
  28. start = max((page - 1), 0) * page_size
  29. end = page * page_size
  30. cbd_pest_warnig_query = MongoCBDPestWarning.objects.filter().order_by("-id")
  31. if warning_type:
  32. if warning_type not in("1","2","3","4"):
  33. raise PortError(warning_type,"参数超出范围")
  34. cbd_pest_warnig_query = cbd_pest_warnig_query.filter(warning_types=warning_type)
  35. if start_time and end_time:
  36. if not (start_time.isdigit() and end_time.isdigit()):
  37. raise PortError("start_time&end_time","参数有误")
  38. cbd_pest_warnig_query = cbd_pest_warnig_query.filter(upltime__range=(int(start_time),int(end_time)))
  39. total = cbd_pest_warnig_query.count()
  40. data = []
  41. for xx in cbd_pest_warnig_query[start:end]:
  42. data.append({
  43. "id":xx.id,
  44. "device_id":xx.device_id,
  45. "warning_type":xx.warning_types,
  46. "warning_name": xx.warning_name,
  47. "warning_content": xx.warning_content,
  48. "upltime":xx.upltime,
  49. "send_user":xx.send_user,
  50. "status":xx.status,
  51. })
  52. return {"data" : data, "total" : total}
  53. @kedong_deco(login_required=True)
  54. def user_cbd_pest_warning_record_read(request):
  55. """
  56. 测报灯预警已读
  57. 参数
  58. id 必传(str) 预警id
  59. req 非必传(str) 一键已读必传 "all" 单个已读无需传递
  60. """
  61. req = request.POST.get("req")
  62. cbd_pest_warnig_query = MongoCBDPestWarning.objects.all()
  63. try:
  64. if req and req=="all":
  65. cbd_pest_warnig_query.update(status=1)
  66. else:
  67. d_id = request.POST.get("id")
  68. cbd_pest_warnig_query = cbd_pest_warnig_query.get(id=d_id)
  69. cbd_pest_warnig_query.status = 1
  70. cbd_pest_warnig_query.save()
  71. return True
  72. except Exception as e:
  73. raise PortError("start_time&end_time","参数有误")
  74. @kedong_deco(login_required=True)
  75. def user_device_map_location(request):
  76. """
  77. 测报灯、气象站设备地图位置
  78. 参数
  79. device_ids 必传(str) 设备号用多个,进行连接
  80. """
  81. device_ids = request.POST.get("device_ids")
  82. if not device_ids:
  83. raise PortError("device_ids","参数缺失")
  84. device_list = device_ids.split(",")
  85. data = []
  86. device_query = MongoDevice.objects.filter(device_type_id__in=[3,5]).filter(device_id__in=device_list)
  87. for xx in device_query:
  88. data.append({
  89. "device_id" : xx.device_id,
  90. "device_type_id" : xx.device_type_id,
  91. "device_name" : xx.device_name,
  92. "lat" : xx.lat,
  93. "lng" : xx.lng,
  94. "uptime" : xx.uptime
  95. })
  96. return data