-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsunpower.rb
executable file
·62 lines (51 loc) · 2.09 KB
/
sunpower.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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
Bundler.require(:default)
class Sunpower < RecorderBotBase
no_commands do
def authorize
sunpower_credentials = load_credentials
response = RestClient.post 'https://edp-api.edp.sunpower.com/v1/auth/okta/signin',
sunpower_credentials.to_json,
{
Content_Type: 'application/json; charset=utf-8'
}
authorization = JSON.parse response
@logger.debug authorization
authorization
end
def get_current_power(authorization)
sunpower_credentials = load_credentials
change_query = [{
operationName: 'FetchCurrentPower',
variables: { siteKey: sunpower_credentials['siteKey'] },
query: 'query FetchCurrentPower($siteKey: String!) {currentPower(siteKey: $siteKey) { production, timestamp }}'
}].to_json
response = RestClient.post 'https://edp-api-graphql.edp.sunpower.com/graphql',
change_query,
{
Authorization: "Bearer #{authorization['access_token']}",
Content_Type: 'application/json; charset=utf-8'
}
@logger.debug response.headers
@logger.info response
parsed_response = JSON.parse(response.body)
parsed_response[0]['data']['currentPower']
end
end
no_commands do
def main
power = with_rescue([Errno::ECONNRESET, RestClient::BadGateway, RestClient::Exceptions::OpenTimeout], @logger, retries: 6) do |_try|
authorization = authorize
get_current_power authorization
end
influxdb = InfluxDB::Client.new 'sunpower' unless options[:dry_run]
data = [{ series: 'production',
values: { value: power['production'].to_f },
timestamp: power['timestamp'] / 1000 }] # convert ms to sec
influxdb.write_points(data) unless options[:dry_run]
end
end
end
Sunpower.start