Sha256: aed462c7f0922f8b3814c0caefa3f1122e366b1245da75bbf99bfa36d5c35844

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require 'net/http'
require 'json'
require 'senec/value'
require 'senec/constants'

module Senec
  class Request
    def initialize(host:)
      @host = host
    end

    def house_power
      value = response.dig('ENERGY', 'GUI_HOUSE_POW')
      Senec::Value.new(value).to_i
    end

    def inverter_power
      value = response.dig('ENERGY', 'GUI_INVERTER_POWER')
      Senec::Value.new(value).to_i
    end

    def bat_power
      value = response.dig('ENERGY', 'GUI_BAT_DATA_POWER')
      Senec::Value.new(value).to_i
    end

    def bat_fuel_charge
      value = response.dig('ENERGY', 'GUI_BAT_DATA_FUEL_CHARGE')
      Senec::Value.new(value).to_f
    end

    def bat_charge_current
      value = response.dig('ENERGY', 'GUI_BAT_DATA_CURRENT')
      Senec::Value.new(value).to_f
    end

    def bat_voltage
      value = response.dig('ENERGY', 'GUI_BAT_DATA_VOLTAGE')
      Senec::Value.new(value).to_f
    end

    def grid_power
      value = response.dig('ENERGY', 'GUI_GRID_POW')
      Senec::Value.new(value).to_i
    end

    def wallbox_charge_power
      response.dig('WALLBOX', 'APPARENT_CHARGING_POWER').map do |value|
        Senec::Value.new(value).to_i
      end
    end

    def case_temp
      value = response.dig('TEMPMEASURE', 'CASE_TEMP')
      Senec::Value.new(value).to_f
    end

    def current_state
      value = response.dig('STATISTIC', 'CURRENT_STATE')
      state = Senec::Value.new(value).to_i

      STATE_NAMES[state]
    end

    def measure_time
      value = response.dig('STATISTIC', 'MEASURE_TIME')
      Senec::Value.new(value).to_i
    end

    private

    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

    def uri
      URI.parse("http://#{@host}/lala.cgi")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
senec-0.5.0 lib/senec/request.rb