# -*- coding: utf-8 -*- import datetime import os import urllib2 import json settings_phaseId = 6 if __name__ == '__main__': # 定义 API 地址 url = 'http://10.14.65.106:30087/event_rsu_rsu2cloud_spat_2024-4/_search' # 定义请求体 data = { "query": { "bool": { "must": [ # { # "term": { # "rsuId.keyword": { # "value": "R-HK0511" # } # } # }, { "range": { "timestamp": { "gte": 1713861112000, "lte": 1713861273530 } } } ] } }, "size": 1000, "sort": [ { "timestamp": { "order": "asc" } } ] } # 将请求体转换为 JSON 格式 json_data = json.dumps(data) # 发起 HTTP POST 请求 request = urllib2.Request(url, json_data) request.add_header('Content-Type', 'application/json; charset=UTF-8') # 接收响应 response = urllib2.urlopen(request) # 读取响应内容 response_data = response.read() print(response_data) # 将响应内容转换为 JSON 对象 json_response = json.loads(response_data) hits_array = list(json_response.get('hits').get('hits')) data_array = [] for hits in hits_array: _source = hits.get('_source') intersections = list(_source.get('intersections')) for intersection in intersections: intersectionTimestamp = intersection.get('intersectionTimestamp') phases = list(intersection.get('phases')) for phase in phases: if phase.get('phaseId') == settings_phaseId: phaseStates = list(phase.get('phaseStates')) for phaseState in phaseStates: startTime = phaseState.get('startTime') if startTime == 0: data_array.append( {'intersectionTimestamp': intersectionTimestamp, 'light': phaseState.get('light')}) currentIntersectionTimestamp = data_array[0].get('intersectionTimestamp') currentLight = data_array[0].get('light') result = [] # 0 绿灯 1 红灯 for e in data_array: intersectionTimestamp = e.get('intersectionTimestamp') light = e.get('light') if light == currentLight: continue if light != currentLight: if currentLight == 2 or currentLight == 3: result.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp, 'light': 1}) else: result.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp, 'light': 0}) currentIntersectionTimestamp = intersectionTimestamp currentLight = light if currentLight == 2 or currentLight == 3: result.append( {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000, 'light': 1}) else: result.append( {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000, 'light': 0}) # 打印JSON文件的内容 print(data_array)