diff --git a/app/models/ruling_weather_cache.rb b/app/models/ruling_weather_cache.rb new file mode 100644 index 0000000..c02d235 --- /dev/null +++ b/app/models/ruling_weather_cache.rb @@ -0,0 +1,7 @@ +class RulingWeatherCache + include Mongoid::Document + include Mongoid::Timestamps + field :cache + field :cache_time , :type=> DateTime, :default=> DateTime.now + belongs_to :ruling_weather_setting +end \ No newline at end of file diff --git a/app/models/ruling_weather_setting.rb b/app/models/ruling_weather_setting.rb index 34eb232..a780107 100644 --- a/app/models/ruling_weather_setting.rb +++ b/app/models/ruling_weather_setting.rb @@ -7,6 +7,7 @@ class RulingWeatherSetting field :location, type: String, default: "" field :dataid, type: String, default: "" field :observatory_name, type: String, default: "" + has_one :ruling_weather_cache def get_now_info(custom_location=nil,custom_dataid=nil,custom_observatory_name=nil) time_now = DateTime.now.utc.new_offset(self.time_offset) today = time_now.strftime("%Y-%m-%d") @@ -23,18 +24,35 @@ class RulingWeatherSetting "locationName" => custom_location} res = Net::HTTP.get_response(URI.parse("#{url}?#{data.to_query}")) content = JSON.parse(res.body) - weather_data = get_weather_data(content) - mint = get_element_value(weather_data["MinT"]).to_i #最低溫度(攝氏度) - maxt = get_element_value(weather_data["MaxT"]).to_i #最高溫度(攝氏度) - avgt = get_element_value(weather_data["T"]).to_i #平均溫度(攝氏度) - rain_rate = get_element_value(weather_data["PoP12h"]).to_i #降雨機率(百分比) - uvi = get_element_value(weather_data["UVI"],0).to_i #紫外線指數(數值) - uvi_text = get_element_value(weather_data["UVI"],1).to_s #紫外線指數(文字) - uvi_text = uvi_text.blank? ? I18n.t("ruling_weather.none") : uvi_text - rh = get_element_value(weather_data["RH"]).to_i #相對溼度 - wx_text = get_element_value(weather_data["Wx"],0).to_s #天氣現象(文字) - wx_code = get_element_value(weather_data["Wx"],1).to_i #天氣現象(編碼) - ws = get_element_value(weather_data["WS"]).to_i #風速(m/s) + cahche_model = self.ruling_weather_cache + if cahche_model.nil? + cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self) + end + cache = cahche_model.cache rescue {} + weather_data = get_weather_data(content) rescue {} + if (weather_data.count == 0 rescue true) + mint = cache["mint"].to_i + maxt = cache["maxt"].to_i + avgt = cache["avgt"].to_i + rain_rate = cache["rain_rate"].to_i + uvi = cache["uvi"].to_i + uvi_text = cache["uvi_text"].to_s + rh = cache["rh"].to_i + wx_text = cache["wx_text"].to_s + wx_code = cache["wx_code"].to_i + ws = cache["ws"].to_i + else + mint = get_element_value(weather_data["MinT"]).to_i #最低溫度(攝氏度) + maxt = get_element_value(weather_data["MaxT"]).to_i #最高溫度(攝氏度) + avgt = get_element_value(weather_data["T"]).to_i #平均溫度(攝氏度) + rain_rate = get_element_value(weather_data["PoP12h"]).to_i #降雨機率(百分比) + uvi = get_element_value(weather_data["UVI"],0).to_i #紫外線指數(數值) + uvi_text = get_element_value(weather_data["UVI"],1).to_s #紫外線指數(文字) + rh = get_element_value(weather_data["RH"]).to_i #相對溼度 + wx_text = get_element_value(weather_data["Wx"],0).to_s #天氣現象(文字) + wx_code = get_element_value(weather_data["Wx"],1).to_i #天氣現象(編碼) + ws = get_element_value(weather_data["WS"]).to_i #風速(m/s) + end if wx_code < 10 wx_code = "0" + wx_code.to_s else @@ -55,7 +73,11 @@ class RulingWeatherSetting res2 = Net::HTTP.get_response(URI.parse("#{url2}?#{data2.to_query}")) content2 = JSON.parse(res2.body) weather_data2 = get_weather_data(content2) rescue {} - rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0 + if (weather_data2.count == 0 rescue true) + rain = cache["rain"] + else + rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0 + end end result = {"mint" => mint, "maxt" => maxt, @@ -68,6 +90,10 @@ class RulingWeatherSetting "wx_svg" => wx_svg, "ws" => ws, "rain" => rain} + cahche_model.update(:cache=>result,:cache_time=>time_now) + if uvi_text.blank? + result["uvi_text"] = I18n.t("ruling_weather.none") + end return result end def test diff --git a/modules/ruling_weather/_ruling_weather_widget1.html.erb b/modules/ruling_weather/_ruling_weather_widget1.html.erb index c63e878..db0d860 100644 --- a/modules/ruling_weather/_ruling_weather_widget1.html.erb +++ b/modules/ruling_weather/_ruling_weather_widget1.html.erb @@ -1,52 +1,98 @@
-
+
{{wx_text}}
{{avgt}}
-
+
{{uv-head}}
{{uvi_text}}
-
+
{{relative_humidity-head}}
{{rh}}%
-
+
{{current_wind_speed-head}}
{{ws}}m/s
-
+
{{accumulated_rainfall-head}}
{{rain}}mm
-
\ No newline at end of file +
diff --git a/modules/ruling_weather/_ruling_weather_widget2.html.erb b/modules/ruling_weather/_ruling_weather_widget2.html.erb new file mode 100644 index 0000000..90a50e4 --- /dev/null +++ b/modules/ruling_weather/_ruling_weather_widget2.html.erb @@ -0,0 +1,52 @@ +
+ +
+
+
+ {{wx_text}} +
+
+
{{wx_text}}
+
{{avgt}}
+
+
+
{{uv-head}}
+
{{uvi_text}}
+
+
+
+
+
{{relative_humidity-head}}
+
{{rh}}%
+
+
+
{{current_wind_speed-head}}
+
{{ws}}m/s
+
+
+
{{accumulated_rainfall-head}}
+
{{rain}}mm
+
+
+
+
\ No newline at end of file diff --git a/modules/ruling_weather/info.json b/modules/ruling_weather/info.json index 2a20a42..126e501 100644 --- a/modules/ruling_weather/info.json +++ b/modules/ruling_weather/info.json @@ -9,6 +9,14 @@ "en" : "1. Weather layout 1" }, "thumbnail" : "weather1_thumbs.png" + }, + { + "filename" : "ruling_weather_widget2", + "name" : { + "zh_tw" : "2. 天氣呈現樣式2", + "en" : "2. Weather layout 2" + }, + "thumbnail" : "weather2_thumbs.png" } ] } \ No newline at end of file diff --git a/modules/ruling_weather/thumbs/weather1_thumbs.png b/modules/ruling_weather/thumbs/weather1_thumbs.png index e4bcf01..676d874 100644 Binary files a/modules/ruling_weather/thumbs/weather1_thumbs.png and b/modules/ruling_weather/thumbs/weather1_thumbs.png differ diff --git a/modules/ruling_weather/thumbs/weather2_thumbs.png b/modules/ruling_weather/thumbs/weather2_thumbs.png new file mode 100644 index 0000000..e4bcf01 Binary files /dev/null and b/modules/ruling_weather/thumbs/weather2_thumbs.png differ diff --git a/ruling_weather.gemspec b/ruling_weather.gemspec index 4144b41..3ee4ad5 100644 --- a/ruling_weather.gemspec +++ b/ruling_weather.gemspec @@ -8,11 +8,67 @@ if bundle_update_flag app_path = File.expand_path(__dir__) template_path = env_pwd + '/app/templates' all_template = Dir.glob(template_path+'/*/') + default_weather_widget_info = JSON.parse(File.read("#{app_path}/modules/ruling_weather/info.json"))["widgets"].sort_by{|h| h["filename"].to_i} rescue [] all_template.each do |folder| if !folder.include?('mobile') moudle_path = "#{folder}modules/ruling_weather/" - if !Dir.exist?(moudle_path) && Dir.exist?(File.dirname(moudle_path)) - Bundler.with_clean_env{system ('cp -r '+ app_path + '/modules/* ' + folder + '/modules/.')} + if Dir.exist?(File.dirname(moudle_path)) + if !Dir.exist?(moudle_path) + Bundler.with_clean_env{system ('cp -r '+ app_path + '/modules/* ' + folder + '/modules/.')} + else + info_json_file = "#{moudle_path}info.json" + if File.exist?(info_json_file) + begin + file_text = File.read(info_json_file) rescue "" + encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???") + next if (encode_file_text.include?("???") rescue true) + info = JSON.parse(encode_file_text) rescue {} + flag = (info.count != 0 rescue false) + if flag + puts "Checking RulingWeather widgets" + widget_info = info["widgets"].sort_by{|h| h["filename"].to_i} rescue [] + update_flag = false + last_index = widget_info[-1]["filename"].match(/\d+/)[0].to_i rescue nil + if !last_index.nil? + idx_regex = /^(\d+[\. \t]*)|[ \t]+$/ + default_weather_widget_info.each do |h| + name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'') + widget_info_index = (widget_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1) + if (widget_info_index == -1 || widget_info_index.nil?) + update_flag = true + copy_h = h.dup + h.delete("force_cover") + last_index = last_index + 1 + copy_h["filename"] = copy_h["filename"].sub(/\d+/){|ff| last_index.to_s} + copy_h["name"].keys.each do |locale| + copy_h["name"][locale] = copy_h["name"][locale].sub(/\d+/){|ff| last_index.to_s} + end + widget_info << copy_h + Bundler.with_clean_env{%x[cp -f #{app_path}/modules/ruling_weather/_#{h["filename"]}.html.erb #{folder}modules/ruling_weather/_#{copy_h["filename"]}.html.erb]} + elsif h["force_cover"] == "true" + Bundler.with_clean_env{%x[cp -f #{app_path}/modules/ruling_weather/_#{h["filename"]}.html.erb #{folder}modules/ruling_weather/_#{widget_info[widget_info_index]["filename"]}.html.erb]} + end + end + if update_flag + info["widgets"] = widget_info + puts "Writing json #{info["widgets"].count} in #{info_json_file}" + begin + info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n") + rescue + info_json = info.to_s.gsub("=>",": \n") + end + File.open(info_json_file,"w+"){|f| f.write(info_json)} + end + Bundler.with_clean_env{%x[cp -rn #{app_path}/modules/ruling_weather/thumbs/* #{folder}modules/ruling_weather/thumbs/.]} + Bundler.with_clean_env{%x[cp -f #{app_path}/modules/ruling_weather/thumbs/weather1_thumbs.png #{folder}modules/ruling_weather/thumbs/.]} + end + end + rescue => e + puts e + puts "There has some error when checking RulingWeather widgets" + end + end + end end end end