Class: Wizrb::Shared::Products::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/wizrb/shared/products/device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip:, port: 38_899, state: Wizrb::Shared::State.new) ⇒ Device

Returns a new instance of Device.



19
20
21
22
23
24
# File 'lib/wizrb/shared/products/device.rb', line 19

def initialize(ip:, port: 38_899, state: Wizrb::Shared::State.new)
  @ip = ip
  @port = port
  @state = state
  connect
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



17
18
19
# File 'lib/wizrb/shared/products/device.rb', line 17

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



17
18
19
# File 'lib/wizrb/shared/products/device.rb', line 17

def port
  @port
end

#stateObject (readonly)

Returns the value of attribute state.



17
18
19
# File 'lib/wizrb/shared/products/device.rb', line 17

def state
  @state
end

Instance Method Details

#dispatch_event(event) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/wizrb/shared/products/device.rb', line 72

def dispatch_event(event)
  unless event.is_a?(Wizrb::Shared::Events::Base)
    raise ArgumentError, "Not an instance of Wizrb::Shared::Events::Base"
  end

  dispatch(event)
end

#dispatch_events(*events) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/wizrb/shared/products/device.rb', line 80

def dispatch_events(*events)
  events.each do |event|
    unless event.is_a?(Wizrb::Shared::Events::Base)
      raise ArgumentError, "Not an instance of Wizrb::Shared::Events::Base"
    end
  end

  params = events.reduce({}) { |h, e| h.merge(e.params) }
  dispatch_event(Wizrb::Shared::Events::Base.new(method: "setState", params: params))
end

#model_configObject



30
31
32
# File 'lib/wizrb/shared/products/device.rb', line 30

def model_config
  @model_config ||= dispatch({method: "getModelConfig", params: {}})&.dig("result")
end

#module_nameObject



38
39
40
# File 'lib/wizrb/shared/products/device.rb', line 38

def module_name
  @module_name ||= system_config&.dig("moduleName")
end

#power_offObject



46
47
48
# File 'lib/wizrb/shared/products/device.rb', line 46

def power_off
  dispatch_event(Wizrb::Shared::Events::PowerEvent.new(false))
end

#power_onObject



42
43
44
# File 'lib/wizrb/shared/products/device.rb', line 42

def power_on
  dispatch_event(Wizrb::Shared::Events::PowerEvent.new(true))
end

#power_switchObject



50
51
52
53
54
55
56
# File 'lib/wizrb/shared/products/device.rb', line 50

def power_switch
  if refresh.state.power
    power_off
  else
    power_on
  end
end

#rebootObject



58
59
60
# File 'lib/wizrb/shared/products/device.rb', line 58

def reboot
  dispatch_event(Wizrb::Shared::Events::RebootEvent.new)
end

#refreshObject



66
67
68
69
70
# File 'lib/wizrb/shared/products/device.rb', line 66

def refresh
  response = dispatch_event(Wizrb::Shared::Events::RefreshEvent.new)
  state.parse!(response)
  self
end

#resetObject



62
63
64
# File 'lib/wizrb/shared/products/device.rb', line 62

def reset
  dispatch_event(Wizrb::Shared::Events::ResetEvent.new)
end

#system_configObject



26
27
28
# File 'lib/wizrb/shared/products/device.rb', line 26

def system_config
  @system_config ||= dispatch({method: "getSystemConfig", params: {}})&.dig("result")
end

#user_configObject



34
35
36
# File 'lib/wizrb/shared/products/device.rb', line 34

def user_config
  @user_config ||= dispatch({method: "getUserConfig", params: {}})&.dig("result")
end