def trainPosition(): to_airport = {} from_airport = {} current_sec = int(time.time()) for i in getWSJsonResponse(OSE_WS_URL): # get values from record id = i["id"] trip_id = i["tripId"] start = i["tripStartEn"] end = i["tripEndEn"] end2 = i["endStationNameEn"] station = i["closestStationId"] speed = i["speed"] lat = float(i["lat"]) lon = float(i["lon"]) distance = distance_from_station(lat, lon, station) # create record rc = {'start': start, 'end': end, 'end2': end2, 'station': getStation(station), 'speed': speed, 'id': id, 'trip_id': trip_id, 'distance': distance} # default direction from airport direction = "d" session_id = id + "_" + direction # airport to liosia, kiato if (start in ['AIRPORT'] and (end in ['A. LIOSSIA', 'KIATO'] or end2 in ['A. LIOSSIA', 'KIATO'])) or (start in ['A. LIOSSIA'] and (end in ['KIATO'] or end2 in ['KIATO'])): rc['next_station'] = getNextStation(station, speed, 1) rc['prev_station'] = getNextStation(station, speed, 2) from_airport[id] = rc from_airport[id].update(checkPrev(session_id, current_sec)) # kiato,liosia to airport elif start in ['A. LIOSSIA', 'KIATO'] and end in ['AIRPORT'] or end2 in ['AIRPORT']: rc['next_station'] = getNextStation(station, speed, 2) rc['prev_station'] = getNextStation(station, speed, 1) to_airport[id] = rc # update direction direction = "r" session_id = id + "_" + direction to_airport[id].update(checkPrev(session_id, current_sec)) # update session if has changed if (not session.has_key(session_id)) or session.get(session_id)[0] != st.get(station, [station, '', ''])[0] or distance != session.get(session_id)[2]: session[session_id] = [st.get(station, [station, '', ''])[0], current_sec, distance] # sort dictionaries based on id return {'stations':stationsToDisplay(), 'stationsFrom':trainsToDisplay(from_airport.items()), 'stationsTo':trainsToDisplay(to_airport.items()), 'from_airport':OrderedDict(sorted(from_airport.items(), key=lambda t: t[0])), 'to_airport':OrderedDict(sorted(to_airport.items(), key=lambda t: t[0]))}
def getStation(station): return st.get(station, [station, '', ''])[0]