Class: SmartEnergyGroup::Client
- Inherits:
-
Object
- Object
- SmartEnergyGroup::Client
- Includes:
- HTTParty
- Defined in:
- lib/smart_energy_group/client.rb
Constant Summary collapse
- DATA_TYPES =
{ :power => 'p', :energy => 'e', :amps => 'a', :channel => 'c', :temperature => 'temperature', :voltage => 'v', :state => 'state', :seconds => 'seconds', :heartbeat => 'heartbeat', :pulse => 'pulse', }.freeze
Instance Method Summary collapse
- #generate_data(options) ⇒ Object
-
#initialize(site_token) ⇒ Client
constructor
debug_output $stdout.
- #send_data(node_name, options) ⇒ Object
Constructor Details
#initialize(site_token) ⇒ Client
debug_output $stdout
10 11 12 |
# File 'lib/smart_energy_group/client.rb', line 10 def initialize(site_token) @site_token = site_token end |
Instance Method Details
#generate_data(options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/smart_energy_group/client.rb', line 39 def generate_data() data = [] DATA_TYPES.keys.each do |type| case [type] when Array data += [type].map.with_index(1) { |value, i| "(#{DATA_TYPES[type]}_#{i} #{value})" } when Hash data += [type].map { |i, value| "(#{DATA_TYPES[type]}_#{i} #{value})" } when NilClass next else raise('Unsupported object type') end end data.join('') end |
#send_data(node_name, options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/smart_energy_group/client.rb', line 27 def send_data(node_name, ) time = [:when] || Time.now time = time.utc.strftime('%FT%T') data = generate_data() 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 |