# frozen_string_literal: true require_relative 'ratp_utils' # Uncomment and define transports and apiKey below # (or alternatively, define them in config/settings.rb) # API Key can be obtained from https://prim.iledefrance-mobilites.fr/fr/mon-jeton-api (need account) # TRANSPORTS = [ # Transport.new(Type::BUS, '22', 'Ranelagh', 'Opera'), # Transport.new(Type::BUS, '52', 'Ranelagh', 'Opera'), # Transport.new(Type::METRO, '9', 'Ranelagh', 'Mairie de Montreuil'), # Transport.new(Type::METRO, '9', 'Ranelagh', 'Pont de Sevres') # ] # RATP_UPDATE_INTERVAL = '10s' # IDFM_API_KEY = 'apiKey' # Do not modify beyond this point. # TODO: error handling (stop not found, terminus wrong) # TODO: a l'approche raise('WARN: RATP: IDFM_API_KEY not defined. Please create an API key and try again.') unless defined? IDFM_API_KEY unless defined? TRANSPORTS TRANSPORTS = [].freeze puts('WARN: RATP: Transports not defined. See README for more info!') end RATP_UPDATE_INTERVAL = '10s' unless defined? RATP_UPDATE_INTERVAL lines = TRANSPORTS.map do |t| Line.new(t.type, t.number) end.uniq line_ids, pictos = get_line_details(lines) lines_by_id = line_ids.invert pairs = [] TRANSPORTS.each do |t| line_id = line_ids[Line.new(t.type, t.number)] stop_name = t.stop pairs.push(StopAtRoute.new(Line.new(t.type, t.number), line_id, stop_name)) end stop_ids, line_and_stop_name_map = get_stop_ids(pairs) requests = {} TRANSPORTS.each do |t| line_id = line_ids[Line.new(t.type, t.number)] cur_stop_ids = line_and_stop_name_map[line_id][t.stop] cur_stop_ids.each do |stop_id| requests[t] = [] if requests[t].nil? requests[t].push(Request.new(line_id, stop_id, t.destination)) end end SCHEDULER.every RATP_UPDATE_INTERVAL, first_in: 0 do |job| all_timings = get_all_timings(stop_ids) all_data = find_results(all_timings, requests) results = [] keys = all_data.keys keys.each do |k| val = all_data[k] line = lines_by_id[val[0][:route]] results.push( key: k, value: { type: line[:type][:api], id: line[:id], picto: pictos[val[0][:route]], d1: val[0][:dest], t1: val[0][:time], d2: val[1].nil? ? '' : val[1][:dest], t2: val[1].nil? ? '' : val[1][:time] } ) end send_event('ratp', results: results) rescue ConfigurationError => e warn("ERROR: RATP: #{e}") job.unschedule end