Sha256: 62c426f784980a823fa35d9744d3b1774ccaeb0e2a500fb3efefd52d21b7d70c
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
# frozen_string_literal: true require "rmodbus" require_relative "huawei_solar/array_ext" require "yaml" class HuaweiSolar def initialize(host, port=502, **opts) @client = ModBus::TCPClient.connect(host, port, opts) @client.debug = !ENV["DEBUG"].nil? @slave = @client.with_slave(1) @registers = YAML.load_file(File.expand_path("../data/registers.yml", __dir__)) end def read(name, **options) name.is_a?(Array) ? read_multiple(name, **options) : read_single(name, **options) end def read_all(**options) read_multiple(@registers.keys, **options) end def close @client.close end private def read_single(name, **options) # rubocop:disable Metrics/AbcSize register = register_by_name(name) type = find_type(register["type"]) gain = register["gain"] unit = register["unit"] value = type.call( @slave.read_holding_registers(register["address"], register["quantity"]) ) value = value.is_a?(Array) && value.size == 1 ? value.first : value value = gain ? value / (gain * 1.0) : value (options[:units] || options[:unit]) && !unit.nil? ? "#{value} #{unit}" : value end def read_multiple(names, **options) if options[:keys] names.to_h { |name| [name, read_single(name, **options)] } else names.map { |name| read_single(name, **options) } end end def register_by_name(name) @registers[name.to_s] end def find_type(type) Object.const_get("HuaweiSolar::#{type.capitalize}") end end require_relative "huawei_solar/string" require_relative "huawei_solar/i16" require_relative "huawei_solar/i32" require_relative "huawei_solar/u16" require_relative "huawei_solar/u32" require_relative "huawei_solar/version"
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
huawei_solar-0.0.6 | lib/huawei_solar.rb |
huawei_solar-0.0.5 | lib/huawei_solar.rb |