Sha256: 266570f17726d8c0509bb752146744892fcbe0f172d3cc35c5245cc0bf2b53ab

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

require 'httparty'

module SmartEnergyGroup
  class Client
    include HTTParty
    base_uri 'api.smartenergygroups.com'
    format :plain
    # debug_output $stdout

    def initialize(site_token)
      @site_token = site_token
    end

    DATA_TYPES = {
      :power       => 'p',
      :energy      => 'e',
      :amps        => 'a',
      :channel     => 'c',
      :temperature => 'temperature',
      :voltage     => 'v',
      :state       => 'state',
      :seconds     => 'seconds',
      :heartbeat   => 'heartbeat',
      :pulse       => 'pulse',
    }.freeze

    def send_data(node_name, options)
      time = options[:when] || Time.now
      time = time.strftime('%FT%T')

      data = generate_data(options)
      msg = "(site #{@site_token} (node #{node_name} #{time} #{data}))"

      response = self.class.put('/api_sites/stream', :body => msg)

      raise("Bad Post: #{response.body}") unless response.code == 200 && response.body == "(status ok)\n"
    end

    def generate_data(options)
      data = []
      DATA_TYPES.keys.each do |type|
        next unless options[type]
        data += options[type].map.with_index(1) { |value, i| "(#{DATA_TYPES[type]}_#{i} #{value})" }
      end

      data.join('')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_energy_group-0.0.1 lib/smart_energy_group/client.rb