Class: CoRE::CoAP::Client

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

Overview

CoAP client library

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client

Parameters:

  • options (defaults to: {})

    Valid options are (all optional): max_payload (maximum payload size, default 256), max_retransmit (maximum retransmission count, default 4), recv_timeout (timeout for ACK responses, default: 2), host (destination host), post (destination port, default 5683).


15
16
17
18
19
20
21
22
23
24
# File 'lib/core/coap/client.rb', line 15

def initialize(options = {})
  @max_payload = options[:max_payload] || 256

  @host = options[:host]
  @port = options[:port] || CoAP::PORT

  @options = options

  @logger = CoAP.logger
end

Instance Attribute Details

#hostObject

Returns the value of attribute host


7
8
9
# File 'lib/core/coap/client.rb', line 7

def host
  @host
end

#max_payloadObject

Returns the value of attribute max_payload


7
8
9
# File 'lib/core/coap/client.rb', line 7

def max_payload
  @max_payload
end

#portObject

Returns the value of attribute port


7
8
9
# File 'lib/core/coap/client.rb', line 7

def port
  @port
end

Instance Method Details

#delete(*args) ⇒ Object

DELETE

Parameters:

  • host

    Destination host

  • port

    Destination port

  • path

    Path

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


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

def delete(*args)
  client(:delete, *args)
end

#delete_by_uri(uri, *args) ⇒ Object

DELETE by URI

Parameters:

  • uri

    URI

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


125
126
127
# File 'lib/core/coap/client.rb', line 125

def delete_by_uri(uri, *args)
  delete(*decode_uri(uri), *args)
end

#get(*args) ⇒ Object

GET

Parameters:

  • path

    Path

  • host

    Destination host

  • port

    Destination port

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


42
43
44
# File 'lib/core/coap/client.rb', line 42

def get(*args)
  client(:get, *args)
end

#get_by_uri(uri, *args) ⇒ Object

GET by URI

Parameters:

  • uri

    URI

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


53
54
55
# File 'lib/core/coap/client.rb', line 53

def get_by_uri(uri, *args)
  get(*decode_uri(uri), *args)
end

#observe(path, host, port, callback, payload = nil, options = {}) ⇒ Object

OBSERVE

Parameters:

  • host

    Destination host

  • port

    Destination port

  • path

    Path

  • callback

    Method to call with the observe data. Must provide arguments payload and socket.

  • payload (defaults to: nil)

    Payload

  • options (defaults to: {})

    Options

Returns:

  • CoAP::Message


140
141
142
143
# File 'lib/core/coap/client.rb', line 140

def observe(path, host, port, callback, payload = nil, options = {})
  options[:observe] = 0
  client(:get, path, host, port, payload, options, callback)
end

#observe_by_uri(uri, *args) ⇒ Object

OBSERVE by URI

Parameters:

  • uri

    URI

  • callback

    Method to call with the observe data. Must provide arguments payload and socket.

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


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

def observe_by_uri(uri, *args)
  observe(*decode_uri(uri), *args)
end

#post(*args) ⇒ Object

POST

Parameters:

  • host

    Destination host

  • port

    Destination port

  • path

    Path

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


66
67
68
# File 'lib/core/coap/client.rb', line 66

def post(*args)
  client(:post, *args)
end

#post_by_uri(uri, *args) ⇒ Object

POST by URI

Parameters:

  • uri

    URI

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


77
78
79
# File 'lib/core/coap/client.rb', line 77

def post_by_uri(uri, *args)
  post(*decode_uri(uri), *args)
end

#put(*args) ⇒ Object

PUT

Parameters:

  • host

    Destination host

  • port

    Destination port

  • path

    Path

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


90
91
92
# File 'lib/core/coap/client.rb', line 90

def put(*args)
  client(:put, *args)
end

#put_by_uri(uri, *args) ⇒ Object

PUT by URI

Parameters:

  • uri

    URI

  • payload

    Payload

  • options

    Options

Returns:

  • CoAP::Message


101
102
103
# File 'lib/core/coap/client.rb', line 101

def put_by_uri(uri, *args)
  put(*decode_uri(uri), *args)
end

#use_dtlsObject

Enable DTLS socket.


27
28
29
30
31
# File 'lib/core/coap/client.rb', line 27

def use_dtls
  require 'CoDTLS'
  @options[:socket] = CoDTLS::SecureSocket
  self
end