/
netatmo
executable file
·211 lines (174 loc) · 4.71 KB
/
netatmo
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'erb'
require 'yaml'
require 'optparse'
module Netatmo
class Token
@@url = 'https://api.netatmo.com/oauth2/token'
def initialize(client_id, client_secret, refresh_token, scope='read_station', grant_type='refresh_token')
@client_id = client_id
@client_secret = client_secret
@refresh_token = refresh_token
@grant_type = grant_type
@scope = scope
auth_refresh_token
end
def auth_refresh_token
res = Net::HTTP.post_form(
URI(@@url), {
'grant_type' => @grant_type,
'client_id' => @client_id,
'client_secret' => @client_secret,
'refresh_token' => @refresh_token,
'scope' => @scope
}
)
raise "login failed: #{res.body}" unless res.is_a?(Net::HTTPSuccess)
data = JSON.parse(res.body)
raise 'API did not return access_token' unless data.key?('access_token')
@token = data['access_token']
end
def to_s
@token
end
end
class Station
@@url = 'https://api.netatmo.com/api/getstationsdata'
attr_accessor :data, :token, :id, :filter
def include_station_name?
!!@include_station_name
end
def initialize(token, id, include_station_name = true)
@token = token
@id = id
@include_station_name = include_station_name
@device_filter = %w[
wifi_status
rf_status
battery_percent
]
@dashboard_filter = %w[
Humidity
Noise
CO2
Pressure
AbsolutePressure
Temperature
Rain
WindStrength
WindAngle
GustStrength
GustAngle
]
update_data
end
def update_data
uri = URI(@@url)
params = { access_token: @token, device_id: @id }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
raise "error updating station data: #{res.body}" unless res.is_a?(Net::HTTPSuccess)
@data = JSON.parse(res.body)
end
def to_s
@data.to_json
end
def devices
@data.to_h['body'].to_h['devices']
end
def process_device_data(data)
output = {}
output.merge!(
data.select { |key, value| @device_filter.include?(key) }
)
if data.key? 'dashboard_data'
dashboard_data = data['dashboard_data'].select { |key, value| @dashboard_filter.include?(key) }
output.merge!(calc_extra_values(dashboard_data))
end
output
end
def calc_extra_values(data)
if data.key?('Temperature') && data.key?('Humidity')
data['AbsoluteHumidity'] = calc_absolute_humidity(
data['Temperature'], data['Humidity']
)
end
data
end
def calc_absolute_humidity(t, r)
a = 13.2471 * (2.718**(17.67 * t / (t + 243.5))) * r / (273.15 + t)
(a * 100).round / 100.0
end
def measurements
device = devices.to_a[0]
raise 'no data for device' if device.nil?
data = {}
data[device['module_name']] = process_device_data(device)
device['modules'].to_a.each do |mod|
data[mod['module_name']] = process_device_data(mod)
end
if include_station_name?
{ device['station_name'] => data }
else
data
end
end
end
class App
attr_reader :config_file, :config
def parse_options
OptionParser.new do |opts|
opts.banner = 'Usage: netatmo [options]'
opts.on('-cPATH', '--config=PATH', 'Path to configuration file') do |c|
@config_file = c
end
end.parse!
end
def initialize
@config_file = '/etc/netatmo.yml'
parse_options
@config = Config.load_file(config_file)
end
def run
puts station.measurements.to_json
end
def token
Netatmo::Token.new(
config.client_id,
config.client_secret,
config.refresh_token
)
end
def station
include_station_name = true
include_station_name = config.include_station_name \
if config.key? 'include_station_name'
Netatmo::Station.new(token, config.device_id, include_station_name)
end
end
class Config < Hash
class MissingSetting < StandardError; end
attr_accessor :file
def self.load_file(file)
raw = File.read(file)
content = ERB.new(raw).result
hash = YAML.safe_load(content, aliases: true).to_hash
config = new
config.merge!(hash)
config.file = file
config
end
def method_missing(name, *_args, &_block)
unless key? name.to_s
raise(
MissingSetting,
"No setting for #{name} in #{file}"
)
end
fetch(name.to_s)
end
end
end
Netatmo::App.new.run