Class: Sonamp::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sonamp/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device = nil, logger: nil) ⇒ Client

Returns a new instance of Client.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sonamp/client.rb', line 44

def initialize(device = nil, logger: nil)
  @logger = logger

  if device.nil?
    device = Sonamp.detect_device(logger: logger)
    if device
      logger&.info("Using #{device} as TTY device")
    end
  end

  unless device
    raise ArgumentError, "No device specified and device could not be detected automatically"
  end

  @device = device
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



61
62
63
# File 'lib/sonamp/client.rb', line 61

def device
  @device
end

#loggerObject

Returns the value of attribute logger.



62
63
64
# File 'lib/sonamp/client.rb', line 62

def logger
  @logger
end

Instance Method Details

#get_auto_trigger_input(zone = nil) ⇒ Object



146
147
148
# File 'lib/sonamp/client.rb', line 146

def get_auto_trigger_input(zone = nil)
  get_zone_state('ATI', zone)
end

#get_bbe(zone = nil) ⇒ Object



134
135
136
# File 'lib/sonamp/client.rb', line 134

def get_bbe(zone = nil)
  get_zone_state('BP', zone)
end

#get_bbe_high_boost(zone = nil) ⇒ Object



138
139
140
# File 'lib/sonamp/client.rb', line 138

def get_bbe_high_boost(zone = nil)
  get_zone_state('BH', zone)
end

#get_bbe_low_boost(zone = nil) ⇒ Object



142
143
144
# File 'lib/sonamp/client.rb', line 142

def get_bbe_low_boost(zone = nil)
  get_zone_state('BL', zone)
end

#get_channel_front_panel_level(channel = nil) ⇒ Object



126
127
128
# File 'lib/sonamp/client.rb', line 126

def get_channel_front_panel_level(channel = nil)
  get_channel_value('TVL', channel)
end

#get_channel_mute(channel = nil) ⇒ Object



122
123
124
# File 'lib/sonamp/client.rb', line 122

def get_channel_mute(channel = nil)
  get_channel_state('MC', channel)
end

#get_channel_volume(channel = nil) ⇒ Object



98
99
100
# File 'lib/sonamp/client.rb', line 98

def get_channel_volume(channel = nil)
  get_channel_value('VC', channel)
end

#get_firmware_versionObject



154
155
156
# File 'lib/sonamp/client.rb', line 154

def get_firmware_version
  global_query('VER')
end

#get_temperatureObject



158
159
160
# File 'lib/sonamp/client.rb', line 158

def get_temperature
  Integer(global_query('TP'))
end

#get_voltage_trigger_input(zone = nil) ⇒ Object



150
151
152
# File 'lib/sonamp/client.rb', line 150

def get_voltage_trigger_input(zone = nil)
  get_zone_state('VTI', zone)
end

#get_zone_fault(zone = nil) ⇒ Object



130
131
132
# File 'lib/sonamp/client.rb', line 130

def get_zone_fault(zone = nil)
  get_zone_state('FP', zone)
end

#get_zone_mute(zone = nil) ⇒ Object



118
119
120
# File 'lib/sonamp/client.rb', line 118

def get_zone_mute(zone = nil)
  get_zone_state('M', zone)
end

#get_zone_power(zone = nil) ⇒ Object



64
65
66
# File 'lib/sonamp/client.rb', line 64

def get_zone_power(zone = nil)
  get_zone_state('P', zone)
end

#get_zone_volume(zone = nil) ⇒ Object



83
84
85
# File 'lib/sonamp/client.rb', line 83

def get_zone_volume(zone = nil)
  get_zone_value('V', zone)
end

#power_offObject



77
78
79
80
81
# File 'lib/sonamp/client.rb', line 77

def power_off
  1.upto(4).each do |zone|
    set_zone_power(zone, false)
  end
end

#set_channel_mute(channel, state) ⇒ Object



114
115
116
# File 'lib/sonamp/client.rb', line 114

def set_channel_mute(channel, state)
  set_channel_value('MC', channel, state ? 1 : 0)
end

#set_channel_volume(channel, volume) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sonamp/client.rb', line 102

def set_channel_volume(channel, volume)
  if channel < 1 || channel > 8
    raise ArgumentError, "Channel must be between 1 and 4: #{channel}"
  end
  if volume < 0 || volume > 100
    raise ArgumentError, "Volume must be between 0 and 100: #{volume}"
  end
  cmd = ":VC#{channel}#{volume}"
  expected = cmd[1...cmd.length]
  dispatch_assert(cmd, expected)
end

#set_zone_mute(zone, state) ⇒ Object



94
95
96
# File 'lib/sonamp/client.rb', line 94

def set_zone_mute(zone, state)
  set_zone_value('M', zone, state ? 1 : 0)
end

#set_zone_power(zone, state) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/sonamp/client.rb', line 68

def set_zone_power(zone, state)
  if zone < 1 || zone > 4
    raise ArgumentError, "Zone must be between 1 and 4: #{zone}"
  end
  cmd = ":P#{zone}#{state ? 1 : 0}"
  expected = cmd[1...cmd.length]
  dispatch_assert(cmd, expected)
end

#set_zone_volume(zone, volume) ⇒ Object



87
88
89
90
91
92
# File 'lib/sonamp/client.rb', line 87

def set_zone_volume(zone, volume)
  if volume < 0 || volume > 100
    raise ArgumentError, "Volume must be between 0 and 100: #{volume}"
  end
  set_zone_value('V', zone, volume)
end

#statusObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sonamp/client.rb', line 162

def status
  # Reusing the opened device file makes :VTIG? fail even with a delay
  # in front.
  #open_device do
    {
      firmware_version: get_firmware_version,
      temperature: get_temperature,
      zone_power: get_zone_power,
      zone_fault: get_zone_fault,
      zone_volume: get_zone_volume,
      channel_volume: get_channel_volume,
      zone_mute: get_zone_mute,
      channel_mute: get_channel_mute,
      bbe: get_bbe,
      bbe_high_boost: get_bbe_high_boost,
      bbe_low_boost: get_bbe_low_boost,
      auto_trigger_input: get_auto_trigger_input,
      voltage_trigger_input: get_voltage_trigger_input,
      channel_front_panel_level: get_channel_front_panel_level,
    }
  #end
end