Sha256: 51efdea3b10ba152842747f3242a8e67e4119686d6e6666ee1422327d8cd61e4

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

module ParseP1

  module Electricity

    def electra_meter_id
      match_within_one_p1_record('0:96.1.1\S(\d{1}[A-Z]{1}\d{1,96})\S')
    end

    def electricity_tariff_indicator
      result = match_within_one_p1_record('0-0:96.14.0\S(\d{1,9})\S')
      result.to_i if result
    end

    def electricity_actual_threshold
      electricity('0-0:17.0.0')
    end

    def electra_import_low
      electricity(:type => :import, :tariff => :low)
    end

    def electra_import_normal
      electricity(:type => :import, :tariff => :normal)
    end

    def electra_export_low
      electricity(:type => :export, :tariff => :low)
    end

    def electra_export_normal
      electricity(:type => :export, :tariff => :normal)
    end

    def actual_electra
      electricity(:type => :import, :actual => true)
    end

    def electricity(options)
      if options.is_a?(Hash)
        if options[:actual] == true
          get_actual_electricity(options[:type])
        else
          get_electricity("1-0:#{first_electricity_code(options[:type])}.8.#{second_electricity_code(options[:tariff])}")
        end
      else
        get_electricity(options)
      end
    end

    private

    def get_electricity(obis_code)
      data.match(/\/[\W|\w]*#{obis_code}\S(\d{1,9}\.\d{1,3})\S[\W|\w]*!/)
      $1.to_f if $1
    end

    def get_actual_electricity(type)
      power = get_electricity("1-0:#{first_electricity_code(type)}.7.0")
      (power * 1000).to_i if power#Return as watts instead of kW
    end

    def first_electricity_code(code)
      case code
      when :import; '1'
      when :export; '2'
      end
    end

    def second_electricity_code(tariff)
      case tariff
      when :low; 1
      when :normal; 2
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parse_p1-0.0.6 lib/parse_p1/electricity.rb