class SmaApi::Client

Ruby client for communicating with SMA Inverter web interface

Public Class Methods

new(host:, password:, sid: nil) click to toggle source
# File lib/sma_api/client.rb, line 6
def initialize(host:, password:, sid: nil)
  @host = host
  @password = password
  @client = Http.new(host: host, password: password, sid: sid)
end

Public Instance Methods

destroy_session() click to toggle source

Logout and clear the session. This is the same as using Logout from the web interface

@return [String] An empty session id

# File lib/sma_api/client.rb, line 22
def destroy_session
  @client.destroy_session
end
download(path, target) click to toggle source

Download the file specified by url and store it in a file on the local file system.

@param url [String] URL without /fs prefix. It should start with a '/' @param path [String] Path of the local file

# File lib/sma_api/client.rb, line 60
def download(path, target)
  @client.download(path, target)
end
get_fs(path) click to toggle source

Retrieve list of files and directories for the path.

@return [Array] List of directories and files

# File lib/sma_api/client.rb, line 42
def get_fs(path)
  result = @client.post('/dyn/getFS.json', { destDev: [], path: path })

  result['result'].first[1][path].map do |f|
    type = f.key?('f') ? 'f' : 'd'
    {
      name: f['d'] || f['f'],
      type: type,
      last_modified: Time.at(f['tm']),
      size: f['s']
    }
  end
end
get_values(keys) click to toggle source

Retrieve values specified by the keys using getValues.json endpoint.

@param keys [Array<String>] List of keys @return [Hash] Key-value pairs

# File lib/sma_api/client.rb, line 30
def get_values(keys)
  result = @client.post('/dyn/getValues.json', { destDev: [], keys: keys })
  return nil unless result['result']

  keys.each_with_object({}) do |k, h|
    h[k] = scalar_value(result['result'].first[1][k])
  end
end
object_metadata() click to toggle source

ObjectMetadata_Istl endpoint

@return [Array] List of available object metadata

# File lib/sma_api/client.rb, line 67
def object_metadata
  @client.post('/data/ObjectMetadata_Istl.json')
end
sid() click to toggle source

The current session id. If empty, it will create a new session

@return [String] the session id that will be used in subsequent requests.

# File lib/sma_api/client.rb, line 15
def sid
  @client.sid
end

Private Instance Methods

scalar_value(value) click to toggle source
# File lib/sma_api/client.rb, line 73
def scalar_value(value)
  value['1'].first['val']
end