class Senec::Request

Public Class Methods

new(host:) click to toggle source
# File lib/senec/request.rb, line 8
def initialize(host:)
  @host = host
end

Public Instance Methods

bat_charge_current() click to toggle source
# File lib/senec/request.rb, line 32
def bat_charge_current
  value = response.dig('ENERGY', 'GUI_BAT_DATA_CURRENT')
  Senec::Value.new(value).to_f
end
bat_fuel_charge() click to toggle source
# File lib/senec/request.rb, line 27
def bat_fuel_charge
  value = response.dig('ENERGY', 'GUI_BAT_DATA_FUEL_CHARGE')
  Senec::Value.new(value).to_f
end
bat_power() click to toggle source
# File lib/senec/request.rb, line 22
def bat_power
  value = response.dig('ENERGY', 'GUI_BAT_DATA_POWER')
  Senec::Value.new(value).to_i
end
bat_voltage() click to toggle source
# File lib/senec/request.rb, line 37
def bat_voltage
  value = response.dig('ENERGY', 'GUI_BAT_DATA_VOLTAGE')
  Senec::Value.new(value).to_f
end
current_state() click to toggle source
# File lib/senec/request.rb, line 53
def current_state
  value = response.dig('STATISTIC', 'CURRENT_STATE')
  state = Senec::Value.new(value).to_i

  STATE_NAMES[state]
end
grid_power() click to toggle source
# File lib/senec/request.rb, line 42
def grid_power
  value = response.dig('ENERGY', 'GUI_GRID_POW')
  Senec::Value.new(value).to_i
end
house_power() click to toggle source
# File lib/senec/request.rb, line 12
def house_power
  value = response.dig('ENERGY', 'GUI_HOUSE_POW')
  Senec::Value.new(value).to_i
end
inverter_power() click to toggle source
# File lib/senec/request.rb, line 17
def inverter_power
  value = response.dig('ENERGY', 'GUI_INVERTER_POWER')
  Senec::Value.new(value).to_i
end
wallbox_charge_power() click to toggle source
# File lib/senec/request.rb, line 47
def wallbox_charge_power
  response.dig('WALLBOX', 'APPARENT_CHARGING_POWER').map do |value|
    Senec::Value.new(value).to_i
  end
end

Private Instance Methods

response() click to toggle source
# File lib/senec/request.rb, line 62
def response
  @response ||= begin
    res = Net::HTTP.post uri, Senec::BASIC_REQUEST.to_json

    case res
    when Net::HTTPOK
      JSON.parse(res.body)
    else
      throw "Failure: #{res.value}"
    end
  end
end
uri() click to toggle source
# File lib/senec/request.rb, line 75
def uri
  URI.parse("http://#{@host}/lala.cgi")
end