Class: OpenWeatherMap::VirtualController

Inherits:
ActiveSensor::Controller show all
Defined in:
lib/open_weather_map/virtual_controller.rb

Instance Method Summary collapse

Methods inherited from ActiveSensor::Controller

has_parameter, inherited, parameters, parameters=

Instance Method Details

#retrieve(parameters, _options = {}) ⇒ Object


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
# File 'lib/open_weather_map/virtual_controller.rb', line 7

def retrieve(parameters, _options = {})
  latitude = parameters[:latitude].to_f
  longitude = parameters[:longitude].to_f
  http = Net::HTTP.new('api.openweathermap.org')
  http.open_timeout = 3
  http.read_timeout = 3
  path = '/data/2.5/weather?units=metric'
  path += "&lat=#{latitude}&lon=#{longitude}" if latitude && longitude
  api_key = parameters[:api_key]
  path += "&APPID=#{api_key}" if api_key.present?

  response = http.get(path)
  unless response.code == 200
    return { status: :error, message: response.message }
  end

  json = JSON.parse(response.body).deep_symbolize_keys

  unless json[:cod] == 200
    return { status: :error, message: json[:message] }
  end

  values = {}
  values[:temperature] = json[:main][:temp].to_f.in_celsius
  values[:atmospheric_pressure] = json[:main][:pressure].to_f.in_hectopascal
  values[:relative_humidity] = json[:main][:humidity].to_f.in_percent
  values[:wind_speed] = json[:wind][:speed].to_f.in_meter_per_second
  values[:wind_direction] = json[:wind][:deg].to_f.in_degree
  average_rain = Maybe(json[:rain])[:"3h"].or_else(0).to_f / 3.0
  values[:rainfall] = average_rain.in_millimeter_per_hour

  report = {
    nature: :meteorological_analysis,
    sampled_at: Time.at(json[:dt]),
    sampling_temporal_mode: 'instant',
    geolocation: Charta.new_point(latitude, longitude),
    values: values,
    status: :ok
  }

  report
end