test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import os
  4. import urllib2
  5. import json
  6. settings_phaseId = 6
  7. if __name__ == '__main__':
  8. # 定义 API 地址
  9. url = 'http://10.14.65.106:30087/event_rsu_rsu2cloud_spat_2024-4/_search'
  10. # 定义请求体
  11. data = {
  12. "query": {
  13. "bool": {
  14. "must": [
  15. # {
  16. # "term": {
  17. # "rsuId.keyword": {
  18. # "value": "R-HK0511"
  19. # }
  20. # }
  21. # },
  22. {
  23. "range": {
  24. "timestamp": {
  25. "gte": 1713861112000,
  26. "lte": 1713861273530
  27. }
  28. }
  29. }
  30. ]
  31. }
  32. },
  33. "size": 1000,
  34. "sort": [
  35. {
  36. "timestamp": {
  37. "order": "asc"
  38. }
  39. }
  40. ]
  41. }
  42. # 将请求体转换为 JSON 格式
  43. json_data = json.dumps(data)
  44. # 发起 HTTP POST 请求
  45. request = urllib2.Request(url, json_data)
  46. request.add_header('Content-Type', 'application/json; charset=UTF-8')
  47. # 接收响应
  48. response = urllib2.urlopen(request)
  49. # 读取响应内容
  50. response_data = response.read()
  51. print(response_data)
  52. # 将响应内容转换为 JSON 对象
  53. json_response = json.loads(response_data)
  54. hits_array = list(json_response.get('hits').get('hits'))
  55. data_array = []
  56. for hits in hits_array:
  57. _source = hits.get('_source')
  58. intersections = list(_source.get('intersections'))
  59. for intersection in intersections:
  60. intersectionTimestamp = intersection.get('intersectionTimestamp')
  61. phases = list(intersection.get('phases'))
  62. for phase in phases:
  63. if phase.get('phaseId') == settings_phaseId:
  64. phaseStates = list(phase.get('phaseStates'))
  65. for phaseState in phaseStates:
  66. startTime = phaseState.get('startTime')
  67. if startTime == 0:
  68. data_array.append(
  69. {'intersectionTimestamp': intersectionTimestamp, 'light': phaseState.get('light')})
  70. currentIntersectionTimestamp = data_array[0].get('intersectionTimestamp')
  71. currentLight = data_array[0].get('light')
  72. result = []
  73. # 0 绿灯 1 红灯
  74. for e in data_array:
  75. intersectionTimestamp = e.get('intersectionTimestamp')
  76. light = e.get('light')
  77. if light == currentLight:
  78. continue
  79. if light != currentLight:
  80. if currentLight == 2 or currentLight == 3:
  81. result.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp, 'light': 1})
  82. else:
  83. result.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp, 'light': 0})
  84. currentIntersectionTimestamp = intersectionTimestamp
  85. currentLight = light
  86. if currentLight == 2 or currentLight == 3:
  87. result.append(
  88. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000, 'light': 1})
  89. else:
  90. result.append(
  91. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000, 'light': 0})
  92. # 打印JSON文件的内容
  93. print(data_array)