Class: Oschii::HttpDevice

Inherits:
Device
  • Object
show all
Defined in:
lib/oschii/http_device.rb

Instance Attribute Summary collapse

Attributes inherited from Device

#log_lines

Instance Method Summary collapse

Methods inherited from Device

#clear!, #config, #config=, #config_description, #config_name, #config_valid?, #cycle_time_micros, #description, #device_details, #failsafe_mode?, #files_used, #files_used_percent, #mem_used, #mem_used_percent, #monitor, #name, #name=, #poke!, #settings, #status, #total_files, #total_mem, #update!, #uptime, #version

Methods included from Oschii::Helpers::Prompt

#prompt

Methods included from Logging

#logger, #tail

Methods included from ConfigFile

#save_config, #upload_config

Constructor Details

#initialize(ip) ⇒ HttpDevice

Returns a new instance of HttpDevice.



3
4
5
6
7
# File 'lib/oschii/http_device.rb', line 3

def initialize(ip)
  @ip_address = ip
  @osc_clients = {}
  super()
end

Instance Attribute Details

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



9
10
11
# File 'lib/oschii/http_device.rb', line 9

def ip_address
  @ip_address
end

#log_socketObject (readonly)

Returns the value of attribute log_socket.



9
10
11
# File 'lib/oschii/http_device.rb', line 9

def log_socket
  @log_socket
end

#osc_clientsObject (readonly)

Returns the value of attribute osc_clients.



9
10
11
# File 'lib/oschii/http_device.rb', line 9

def osc_clients
  @osc_clients
end

Instance Method Details

#adminObject



146
147
148
# File 'lib/oschii/http_device.rb', line 146

def admin
  `open http://#{ip}/admin`
end

#clear_logObject



142
143
144
# File 'lib/oschii/http_device.rb', line 142

def clear_log
  RestClient.delete("http://#{ip}/log")&.body
end

#fire(address, *values) ⇒ Object

Network Commands



103
104
105
# File 'lib/oschii/http_device.rb', line 103

def fire(address, *values)
  send_osc address, *values
end

#get(path, value = 1) ⇒ Object



116
117
118
# File 'lib/oschii/http_device.rb', line 116

def get(path, value = 1)
  send_http path, value, method: :get
end

#inspectObject



154
155
156
# File 'lib/oschii/http_device.rb', line 154

def inspect
  "<#{self.class.name}[#{name}] #{ip_address} (v#{version})>"
end

#ipObject



22
23
24
# File 'lib/oschii/http_device.rb', line 22

def ip
  ip_address
end

#logObject



138
139
140
# File 'lib/oschii/http_device.rb', line 138

def log
  RestClient.get("http://#{ip}/log")&.body
end

#osc_client(port) ⇒ Object



112
113
114
# File 'lib/oschii/http_device.rb', line 112

def osc_client(port)
  osc_clients[port] ||= OSC::Client.new(ip, port)
end

#pokeObject



26
27
28
29
30
31
32
33
34
# File 'lib/oschii/http_device.rb', line 26

def poke
  device_details
  begin
    start_web_socket
  rescue
    # ignored
  end
  true
end

#post(path, value = 1) ⇒ Object



120
121
122
# File 'lib/oschii/http_device.rb', line 120

def post(path, value = 1)
  send_http path, value, method: :post
end

#raw_configObject



50
51
52
# File 'lib/oschii/http_device.rb', line 50

def raw_config
  RestClient.get("http://#{ip}/config.json")&.body || ''
end

#raw_config=(json) ⇒ Object

Updaters



56
57
58
59
60
61
62
63
# File 'lib/oschii/http_device.rb', line 56

def raw_config=(json)
  RestClient.post(
    "http://#{ip}/config",
    json,
    content_type: 'application/json'
  )
  # refresh
end

#raw_device_detailsObject

Raw getters



38
39
40
# File 'lib/oschii/http_device.rb', line 38

def raw_device_details
  RestClient.get("http://#{ip}/device")&.body || ''
end

#raw_settingsObject



46
47
48
# File 'lib/oschii/http_device.rb', line 46

def raw_settings
  RestClient.get("http://#{ip}/settings.json")&.body || ''
end

#raw_statusObject



42
43
44
# File 'lib/oschii/http_device.rb', line 42

def raw_status
  RestClient.get("http://#{ip}/status")&.body || ''
end

#refreshObject

Connection



17
18
19
20
# File 'lib/oschii/http_device.rb', line 17

def refresh
  stop_web_socket
  super
end

#restartObject



11
12
13
# File 'lib/oschii/http_device.rb', line 11

def restart
  RestClient.post("http://#{ip}/restart", {})&.body
end

#send_http(path, value = 1, method: :post) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/oschii/http_device.rb', line 124

def send_http(path, value = 1, method: :post)
  path = "/#{path}" unless %w(/ :).include?(path[0])
  method = method.to_s.downcase.to_sym
  url = "http://#{ip}#{path}"
  if method == :get
    args = [url]
  else
    args = [url, value.to_s]
  end
  puts RestClient.send(method, *args)
rescue RestClient::BadRequest => e
  puts "Oschii didn't like that: #{e.http_body}"
end

#send_osc(address, *values, port: 3333) ⇒ Object



107
108
109
110
# File 'lib/oschii/http_device.rb', line 107

def send_osc(address, *values, port: 3333)
  address = "/#{address}" unless address[0] == '/'
  osc_client(port).send(OSC::Message.new(address, *values))
end

#settings=(new_settings) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/oschii/http_device.rb', line 65

def settings=(new_settings)
  RestClient.post(
    "http://#{ip}/settings",
    new_settings.to_json,
    content_type: 'application/json'
  )
  refresh
end

#start_web_socketObject

Logging



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/oschii/http_device.rb', line 76

def start_web_socket
  EM.run {
    @log_socket = Faye::WebSocket::Client.new("ws://#{ip}/logger_ws")

    log_socket.on :open do |event|
      log_socket.send('WebSocket connected')
    end

    log_socket.on :message do |event|
      event.data.split("\n").each do |line|
        log_lines << line
      end
    end

    log_socket.on :close do |event|
      log_socket.send("WebSocket DISCONNECTED: #{event.code} #{event.reason}")
      @log_socket = nil
    end
  }
end

#stop_web_socketObject



97
98
99
# File 'lib/oschii/http_device.rb', line 97

def stop_web_socket
  log_socket&.close
end

#to_sObject



150
151
152
# File 'lib/oschii/http_device.rb', line 150

def to_s
  inspect
end