-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Includes lots of refactoring No more need of SHELLY_GEN
- Loading branch information
Showing
25 changed files
with
829 additions
and
584 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'solectrus_record' | ||
require 'forwardable' | ||
require 'faraday' | ||
require 'faraday-request-timer' | ||
|
||
class ShellyCloudAdapter | ||
extend Forwardable | ||
def_delegators :config, :logger | ||
|
||
def initialize(config:) | ||
@config = config | ||
|
||
logger.info "Pulling from Shelly (Cloud) for device #{config.shelly_device_id} every #{config.shelly_interval} seconds" | ||
end | ||
|
||
attr_reader :config | ||
|
||
def connection | ||
@connection ||= Faraday.new(url: config.shelly_cloud_server) do |f| | ||
f.adapter Faraday.default_adapter | ||
f.request :timer | ||
end | ||
end | ||
|
||
def solectrus_record(id = 1) | ||
# Reset cache | ||
@data = nil | ||
@raw_response = nil | ||
|
||
parser = ShellyResponseParser.new(raw_response.body) | ||
record = parser.solectrus_record(id:, response_duration:) | ||
logger.info success_message(record) | ||
record | ||
rescue StandardError => e | ||
logger.error failure_message(e) | ||
nil | ||
end | ||
|
||
private | ||
|
||
def raw_response | ||
@raw_response ||= begin | ||
response = connection.get('/device/status') do |req| | ||
req.params['id'] = config.shelly_device_id | ||
req.params['auth_key'] = config.shelly_auth_key | ||
end | ||
|
||
raise StandardError, response.status unless response.success? | ||
|
||
response | ||
end | ||
end | ||
|
||
def success_message(record) | ||
"\nGot record ##{record.id} at " \ | ||
"#{Time.at(record.time).localtime} " \ | ||
"within #{record.response_duration} ms, " \ | ||
"Power #{record.power.round(1)} W, " \ | ||
"Temperature #{record.temp} °C" | ||
end | ||
|
||
def failure_message(error) | ||
"Error getting data from Shelly at #{config.shelly_host}: #{error}" | ||
end | ||
|
||
def response_duration | ||
(raw_response.env[:duration] * 1000).round | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.