class Marantz::Client

Public Class Methods

new() click to toggle source
# File lib/marantz/client.rb, line 5
def initialize
  raise UnsupportedModel unless SUPPORTED_MODELS.values.include?(status[:model])
end

Public Instance Methods

mute() click to toggle source
# File lib/marantz/client.rb, line 28
def mute
  toggle_mute(:on)
end
off() click to toggle source
# File lib/marantz/client.rb, line 40
def off
  toggle_power(:off)
end
on() click to toggle source
# File lib/marantz/client.rb, line 36
def on
  toggle_power(:on)
end
source() click to toggle source
# File lib/marantz/client.rb, line 13
def source
  status[:source]
end
source=(name) click to toggle source
# File lib/marantz/client.rb, line 9
def source=(name)
  perform(PATHS[:main_zone], COMMANDS[:source] % (SOURCES[name] or raise UnknownSource))
end
unmute() click to toggle source
# File lib/marantz/client.rb, line 32
def unmute
  toggle_mute(:off)
end
volume() click to toggle source
# File lib/marantz/client.rb, line 24
def volume
  status[:volume]
end
volume=(db) click to toggle source
# File lib/marantz/client.rb, line 17
def volume=(db)
  db = db.to_f
  raise VolumeTooHigh if db > Marantz.config.max_volume
  path = PATHS[:main_zone]
  perform(path, COMMANDS[:volume] % db_to_volume(db))
end

Private Instance Methods

db_to_volume(db) click to toggle source
# File lib/marantz/client.rb, line 54
def db_to_volume(db)
  (VOLUME_THRESHOLD - db.to_f)
end
perform(path, commands) click to toggle source
# File lib/marantz/client.rb, line 76
def perform(path, commands)
  commands = ([commands].flatten << 'aspMainZone_WebUpdateStatus')
  params = {}.tap do |hash|
    commands.each.with_index { |c, i| hash["cmd#{i}"] = c }
  end
  uri = URI('http://' + Marantz.config.host + path)
  result = Net::HTTP.post_form(uri, params)
end
status() click to toggle source
# File lib/marantz/client.rb, line 62
def status
  uri = URI('http://' + Marantz.config.host + PATHS[:status])
  uri.query = URI.encode_www_form({ _: Time.now.to_i * 1_000 })
  response = Net::HTTP.get(uri)
  parser = XML::Parser.string(response, encoding: XML::Encoding::UTF_8)
  doc = parser.parse
  {
    power: doc.find('//Power').first.content,
    source: SOURCES.key(doc.find('//NetFuncSelect').first.content) || :unknown,
    volume: volume_to_db(doc.find('//MasterVolume').first.content),
    model: SUPPORTED_MODELS[doc.find('//ModelId').first.content.to_i]
  }
end
toggle_mute(action) click to toggle source
# File lib/marantz/client.rb, line 46
def toggle_mute(action)
  perform(PATHS[:main_zone], COMMANDS[:mute] % action.downcase)
end
toggle_power(action) click to toggle source
# File lib/marantz/client.rb, line 50
def toggle_power(action)
  perform(PATHS[:main_zone], COMMANDS[:power] % action.upcase)
end
volume_to_db(volume) click to toggle source
# File lib/marantz/client.rb, line 58
def volume_to_db(volume)
  (VOLUME_THRESHOLD + volume.to_f)
end