-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_weather_graph.rb
145 lines (126 loc) · 4.59 KB
/
web_weather_graph.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require 'sinatra'
require 'open-uri'
require 'json'
require 'erb'
require 'parseconfig'
require 'yaml'
require 'cgi'
require 'rrd'
config = ParseConfig.new('./web_weather_graph.conf')
KEY = config.params['datasource']['key']
DATA_DIR = config.params['weather_graph']['datadir']
COLLECTOR_CONF = config.params['weather_graph']['collector_file']
URLBASE = 'http://www.worldweatheronline.com'
FORMAT = 'json'
class WeatherData
attr_accessor :locality
attr_accessor :observation_time, :cloudcover, :humidity, :precip_mm, :presure, :temp_c, :temp_f, :visibility, :weather_code, :weather_desc,
:weather_icon_url, :winddir_16_point, :winddir_degree, :windspeed_kmph, :windspeed_miles
private
# Define a correct filename from location name
def normalize_locality(locality)
return locality.gsub!(/[^0-9A-Za-z.\-]/, '_')
end
public
def initialize
@observation_time = ''
@cloudcover = ''
@humidity = ''
@precip_mm = ''
@presure = ''
@temp_c = ''
@temp_f = ''
@visibility = ''
@weather_code = ''
@weather_desc = ''
@weather_icon_url = ''
@winddir_16_point = ''
@winddir_degree = ''
@windspeed_kmph = ''
@windspeed_miles = ''
end
# Read all lines with last residual value
def get_data(locality)
File.open(DATA_DIR+normalize_locality(locality),"r").each do |line|
data = line.split("\t")
@observation_time = data[0]
@cloudcover = data[7]
@humidity = data[4]
@precip_mm = data[6]
@presure = data[5]
@temp_c = data[2]
@temp_f = data[3]
@visibility = data[8]
@weather_code = data[1]
@weather_desc = data
@weather_icon_url = data[13][0..-2]
@winddir_16_point = data[9]
@winddir_degree = data[10]
@windspeed_kmph = data[11]
@windspeed_miles = data[12]
end
@locality = normalize_locality(locality)
end
# return hash of localities matches with query
def get_localities(query)
p query_escaped = CGI::escape(query)
@url = "#{URLBASE}/feed/search.ashx?key=#{KEY}&q=#{query_escaped}&format=#{FORMAT}&num_of_days=2"
rawdata = ''
open(@url).each do |line|
rawdata << line
end
data = JSON.parse(rawdata)
return data["search_api"]["result"]
# @city = data[0]["areaName"][0]["value"]
# @country = data[0]["country"][0]["value"]
# @latitude = data[0]["latitude"]
# @longitude = data[0]["longitude"]
# @population = data[0]["population"]
# @weatherUrl = data[0]["weatherUrl"][0]["value"]
end
# Add locality to COLLECTOR_FILE for collector script
def add_to_collector(locality)
p locality.to_yaml
# open(DATA_DIR+COLLECTOR_CONF,'w') {|f| YAML.dump(locality, f)}
end
end
class WebWeatherGraph < Sinatra::Base
set :static, true
set :logging, true
set :public, File.dirname(__FILE__) + '/public'
set :views, File.dirname(__FILE__) + '/views'
# Need I do this? -> I don't know... (to not create a new instance for all time)
before do
unless defined? @weather
@weather = WeatherData.new
end
end
get '/' do
erb :index
end
# post '/get_localities' do
# @localities = @weather.get_localities(params[:query])
# erb :get_localities
# end
#
# post 'select_locality' do
# p locality = params[:locality]
# end
get '/graph' do
# myrrd = RRD::Base.new("data/rrd/Trencin_Slovakia.rrd")
# p RRD::Wrapper.last_update "data/rrd/Trencin_Slovakia.rrd"
RRD.graph "public/graphs/Trencin_Slovakia_temperature.png", :title => 'Temperature', :width => 400, :height => 100, :color => ["FONT#000000", "BACK#FFFFFF"] do
area 'data/rrd/Trencin_Slovakia.rrd', :temp_c => :average, :color => "#FF0000", :label => 'Temperature'
end
#
RRD.graph "public/graphs/Trencin_Slovakia_pressure.png", :title => 'Pressure', :width => 400, :height => 100, :color => ["FONT#000000", "BACK#FFFFFF"] do
area 'data/rrd/Trencin_Slovakia.rrd', :pressure => :average, :color => "#00FF00", :label => 'Pressure'
# line 'data/rrd/Trencin_Slovakia.rrd', :pressure => :average, :color => "#0000FF", :label => "Pressure"
end
RRD.graph "public/graphs/Trencin_Slovakia_humidity.png", :title => 'Humidity', :width => 400, :height => 100, :color => ["FONT#000000", "BACK#FFFFFF"] do
area 'data/rrd/Trencin_Slovakia.rrd', :humidity => :average, :color => "#0000FF", :label => 'Humidity'
# line 'data/rrd/Trencin_Slovakia.rrd', :humidity => :average, :color => "#007f3f", :label => 'Humidity'
end
erb :graph
end
end