2021-08-04 03:11:10 +00:00
|
|
|
class RulingBusesController < ApplicationController
|
|
|
|
before_action :set_time_thresh_and_locale
|
|
|
|
def widget
|
|
|
|
set_time_thresh_and_locale
|
|
|
|
subpart = OrbitHelper.get_current_widget
|
|
|
|
begin
|
|
|
|
cats = OrbitHelper.widget_categories || ['all']
|
|
|
|
@ruling_buses = RulingBusInfo.filter_by_widget_categories(cats)
|
|
|
|
bus_trans = t('ruling_bus.ruling_bus')
|
|
|
|
bus_infos = []
|
|
|
|
@ruling_buses.each do |bus_info|
|
|
|
|
bus_title = bus_info.destination.blank? ? ("#{bus_trans}(#{bus_info.monitor_point_show}" + (bus_info.hint.blank? ? (": "+I18n.t("ruling_bus.directions.#{bus_info.direction}")) : bus_info.hint) + ")") : "#{bus_trans}(#{bus_info.monitor_point_show}➠#{bus_info.destination_show})"
|
|
|
|
bus_data = get_bus_data(bus_info)
|
|
|
|
bus_infos << {"bus_title" => bus_title,
|
|
|
|
"bus_id" => bus_info.id.to_s,
|
|
|
|
"bus_data" => bus_data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
{
|
|
|
|
"bus_infos" => bus_infos,
|
|
|
|
"extras" => {
|
|
|
|
"update_url" => "/#{I18n.locale}/xhr/get_bus",
|
|
|
|
"update_method" => "get",
|
|
|
|
"update_interval" => RulingBusesHelper::UpdateFrontInterval*1000,
|
|
|
|
"update_id" => subpart.id.to_s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rescue => e
|
|
|
|
puts [e,e.backtrace]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def get_bus
|
|
|
|
bus_infos = RulingBusInfo.where(:id.in => params[:bus_info_ids])
|
|
|
|
data = {}
|
|
|
|
bus_infos.each do |bus_info|
|
|
|
|
get_bus_data(bus_info,data)
|
|
|
|
end
|
|
|
|
render :json => data
|
|
|
|
end
|
|
|
|
private
|
|
|
|
def set_time_thresh_and_locale
|
|
|
|
@time_now = DateTime.now
|
|
|
|
@bus_locale = I18n.locale.to_s=='zh_tw' ? "Zh_tw" : 'En'
|
|
|
|
@time_thresh = @time_now - RulingBusesHelper::UpdateInterval.seconds
|
|
|
|
end
|
|
|
|
def get_bus_data(bus_info,data=nil)
|
|
|
|
bus_info_id = bus_info.id.to_s
|
|
|
|
cache = RulingBusCache.where(bus_info_id: bus_info_id).first
|
|
|
|
if (cache.nil? || cache.updated_at < @time_thresh)
|
|
|
|
tmp = RulingBusesHelper.get_stop_time(bus_info,@bus_locale)
|
2022-01-03 13:53:41 +00:00
|
|
|
if !tmp.nil?
|
|
|
|
if cache.nil?
|
|
|
|
RulingBusCache.create(dict: tmp, bus_info_id: bus_info_id)
|
|
|
|
else
|
|
|
|
cache.update_attributes(dict: tmp, updated_at: Time.now)
|
|
|
|
end
|
2021-08-04 03:11:10 +00:00
|
|
|
else
|
2022-01-03 13:53:41 +00:00
|
|
|
tmp = cache.dict
|
2021-08-04 03:11:10 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
tmp = cache.dict
|
|
|
|
end
|
|
|
|
tmp = tmp.map do |v|
|
|
|
|
{"route_name"=> v["RouteName"][@bus_locale],
|
|
|
|
"time_to_arrive" => parse_time_diff(v,@time_now)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
data[bus_info_id] = tmp if !data.nil?
|
|
|
|
tmp
|
|
|
|
end
|
|
|
|
def parse_time_diff(bus_data,now=nil)
|
|
|
|
arival = bus_data["NextBusTime"]
|
|
|
|
if !arival.blank?
|
|
|
|
arival = DateTime.parse(arival)
|
|
|
|
time_diff = ((arival-(now||DateTime.now))*1.days).to_i
|
|
|
|
h = time_diff/3600
|
|
|
|
tmp = time_diff%3600
|
|
|
|
m = tmp/60
|
|
|
|
s = tmp%60
|
|
|
|
if time_diff>=3600
|
|
|
|
"#{h}#{I18n.t("ruling_bus.time.hour")} #{m}#{I18n.t("ruling_bus.time.minute")} #{s}#{I18n.t("ruling_bus.time.second")}"
|
|
|
|
elsif time_diff>=60
|
|
|
|
"#{m}#{I18n.t("ruling_bus.time.minute")} #{s}#{I18n.t("ruling_bus.time.second")}"
|
|
|
|
elsif time_diff>0
|
|
|
|
"#{s}#{I18n.t("ruling_bus.time.second")}"
|
|
|
|
else
|
|
|
|
I18n.t("ruling_bus.leaved")
|
|
|
|
end+(bus_data["StopStatus"]==0? "" : " (#{I18n.t("ruling_bus.bus_status.#{bus_data["StopStatus"]}")})")
|
|
|
|
else
|
|
|
|
I18n.t("ruling_bus.bus_status.#{bus_data["StopStatus"]}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|