Class: LEDENET::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/ledenet/api.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    reuse_connection: false,
    max_retries: 3,
    port: 5577
}

Instance Method Summary collapse

Constructor Details

#initialize(device_address, options = {}) ⇒ Api

Returns a new instance of Api.



14
15
16
17
# File 'lib/ledenet/api.rb', line 14

def initialize(device_address, options = {})
  @device_address = device_address
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#current_color_data(response = request_status) ⇒ Object



63
64
65
# File 'lib/ledenet/api.rb', line 63

def current_color_data(response = request_status)
  select_status_keys(response, *%w{red green blue warm_white})
end

#current_function_data(response = request_status) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/ledenet/api.rb', line 95

def current_function_data(response = request_status)
  raw_function_data = select_status_keys(response, *%w{mode speed})
  function_data = {
    running_function?: raw_function_data[:mode] != LEDENET::Functions::NO_FUNCTION,
    speed: FunctionSpeed.from_packet_value(raw_function_data[:speed]).value,
    speed_packet_value: raw_function_data[:speed],
    function_name: LEDENET::Functions.value_of(raw_function_data[:mode]),
    function_id: raw_function_data[:mode]
  }
end

#current_rgbObject



55
56
57
# File 'lib/ledenet/api.rb', line 55

def current_rgb
  current_color_data.values_at(:red, :green, :blue)
end

#current_warm_whiteObject



59
60
61
# File 'lib/ledenet/api.rb', line 59

def current_warm_white
  current_color_data[:warm_white]
end

#offObject



30
31
32
# File 'lib/ledenet/api.rb', line 30

def off
  send_packet(LEDENET::Packets::SetPowerRequest.off_request)
end

#onObject



26
27
28
# File 'lib/ledenet/api.rb', line 26

def on
  send_packet(LEDENET::Packets::SetPowerRequest.on_request)
end

#on?(response = request_status) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ledenet/api.rb', line 38

def on?(response = request_status)
  response.on?
end

#reconnect!Object



106
107
108
109
# File 'lib/ledenet/api.rb', line 106

def reconnect!
  create_socket
  true
end

#send_packet(packet) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/ledenet/api.rb', line 111

def send_packet(packet)
  socket_action do
    @socket.write(packet.to_binary_s)

    if packet.response_reader != LEDENET::Packets::EmptyResponse
      packet.response_reader.read(@socket)
    end
  end
end

#set_power(v) ⇒ Object



34
35
36
# File 'lib/ledenet/api.rb', line 34

def set_power(v)
  v ? on : off
end

#statusObject



19
20
21
22
23
24
# File 'lib/ledenet/api.rb', line 19

def status
  response = request_status
  status = { is_on: on?(response) }
  status.merge!(current_color_data(response))
  status.merge!(current_function_data(response))
end

#update_color_data(o) ⇒ Object



50
51
52
53
# File 'lib/ledenet/api.rb', line 50

def update_color_data(o)
  updated_data = current_color_data.merge(o)
  send_packet(LEDENET::Packets::UpdateColorRequest.new(updated_data))
end

#update_function(fn) ⇒ Object



67
68
69
70
71
72
# File 'lib/ledenet/api.rb', line 67

def update_function(fn)
  if fn.is_a?(String) or fn.is_a?(Symbol)
    fn = LEDENET::Functions.const_get(fn.upcase)
  end
  update_function_data(function_id: fn)
end

#update_function_data(o) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ledenet/api.rb', line 78

def update_function_data(o)
  o = {}.merge(o)
  current_data = current_function_data
  updated_data = {
    function_id: current_data[:function_id],
    speed: current_data[:speed_packet_value]
  }

  if o[:speed]
    speed = LEDENET::FunctionSpeed.from_value(o.delete(:speed))
    updated_data[:speed] = speed.packet_value
  end
  updated_data.merge!(o)

  send_packet(LEDENET::Packets::SetFunctionRequest.new(updated_data))
end

#update_function_speed(s) ⇒ Object



74
75
76
# File 'lib/ledenet/api.rb', line 74

def update_function_speed(s)
  update_function_data(speed: s)
end

#update_rgb(r, g, b) ⇒ Object



42
43
44
# File 'lib/ledenet/api.rb', line 42

def update_rgb(r, g, b)
  update_color_data(red: r, green: g, blue: b)
end

#update_warm_white(warm_white) ⇒ Object



46
47
48
# File 'lib/ledenet/api.rb', line 46

def update_warm_white(warm_white)
  update_color_data(warm_white: warm_white)
end