Class: NL::KndClient::SimpleKndClient
- Inherits:
-
Object
- Object
- NL::KndClient::SimpleKndClient
- Defined in:
- lib/nl/knd_client/simple_knd_client.rb
Overview
A simple KND client that uses a background thread for I/O and works without EventMachine. This client does not (yet) support all of KND’s features.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection to KND and stop the background thread.
-
#get_depth ⇒ Object
Waits for and then returns depth data.
-
#initialize(host: 'localhost', port: 14308) ⇒ SimpleKndClient
constructor
A new instance of SimpleKndClient.
-
#on_zone(name, &block) ⇒ Object
Calls the given block with the current full state of a zone, the type of command received for a zone, and the updates for the zone.
-
#open ⇒ Object
Connect to KND.
-
#remove_callback(name, &block) ⇒ Object
Removes the given callback from the given zone name.
-
#request_depth ⇒ Object
Sends the getdepth command to KND to request async delivery of a single depth frame.
-
#subscribe_depth(count = nil) ⇒ Object
Sends the subdepth command to KND to request continuous async delivery of depth frames.
Constructor Details
#initialize(host: 'localhost', port: 14308) ⇒ SimpleKndClient
Returns a new instance of SimpleKndClient.
9 10 11 12 13 14 15 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 9 def initialize(host: 'localhost', port: 14308) @host = host @port = 14308 @callbacks = {} @zones = {} end |
Instance Method Details
#close ⇒ Object
Close the connection to KND and stop the background thread.
40 41 42 43 44 45 46 47 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 40 def close @run = false @t&.wakeup @t&.kill @socket&.close @t = nil @socket = nil end |
#get_depth ⇒ Object
Waits for and then returns depth data.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 63 def get_depth data = nil t = Thread.current cb = ->(d) { data = d; t.wakeup } on_zone('! DEPTH', &cb) request_depth sleep(1) raise "Data wasn't set within 1 second" if data.nil? data ensure remove_callback('! DEPTH', &cb) end |
#on_zone(name, &block) ⇒ Object
Calls the given block with the current full state of a zone, the type of command received for a zone, and the updates for the zone. ‘! DEPTH’ is a special zone name for depth images [HACK].
20 21 22 23 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 20 def on_zone(name, &block) @callbacks[name] ||= [] @callbacks[name] << block end |
#open ⇒ Object
Connect to KND.
32 33 34 35 36 37 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 32 def open @socket = TCPSocket.new(@host, @port) @run = true @t = Thread.new do read_loop end @socket.puts('sub') end |
#remove_callback(name, &block) ⇒ Object
Removes the given callback from the given zone name. ‘! DEPTH’ is a special zone name for depth images [HACK].
27 28 29 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 27 def remove_callback(name, &block) @callbacks[name]&.delete(block) end |
#request_depth ⇒ Object
Sends the getdepth command to KND to request async delivery of a single depth frame.
51 52 53 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 51 def request_depth @socket.puts('getdepth') end |
#subscribe_depth(count = nil) ⇒ Object
Sends the subdepth command to KND to request continuous async delivery of depth frames. The optional count
will cause only that number of frames to be delivered.
58 59 60 |
# File 'lib/nl/knd_client/simple_knd_client.rb', line 58 def subscribe_depth(count = nil) @socket.puts("subdepth#{count && " #{count}"}") end |