- The OcfClient object
- The ResourceId dictionary
- The Resource dictionary
- The ClientResource interface
The OCF Client API implements CRUDN (Create, Retrieve, Update, Delete, Notify) functionality that enables remote access to resources on the network, as well as OCF discovery.
Property | Type | Optional | Default value |
---|---|---|---|
device |
OcfDevice object |
no | implementation provided |
Event name | Event callback argument |
---|---|
platformfound |
Platform object |
devicefound |
Device object |
devicelost |
Device object |
resourcefound |
ClientResource object |
error |
Error object |
Discovery method signature | Description |
---|---|
getPlatformInfo(deviceId) |
fetch platform information |
getDeviceInfo(deviceId) |
fetch device information |
findPlatforms(listener) |
discover platforms |
findDevices(listener) |
discover devices |
findResources(options, listener) |
discover resources |
Client method signature | Description |
---|---|
create(target, resource) |
create a resource |
retrieve(resourceId, options, listener) |
retrieve/observe a resource |
update(resource) |
update a resource |
delete(resourceId) |
delete a resource |
configure(deviceId, options) |
configure a remote device |
Identifies an OCF resource by the UUID of the device that hosts the resource, and the URI path of the resource that uniquely identifies the resource inside a device.
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
deviceId |
string | no | undefined |
UUID of the device |
resourcePath |
string | no | undefined |
URI path of the resource |
Extends ResourceId
. Represents an OCF resource Link used in Collections.
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
resourceTypes |
array of strings | no | [] |
List of OCF resource types |
interfaces |
array of strings | no | [] |
List of supported interfaces |
discoverable |
boolean | no | true |
Whether the resource is discoverable |
observable |
boolean | no | true |
Whether the resource is observable |
ins |
string | yes | undefined |
Immutable instance identifier of a link |
The discoverable
and observable
properties come from the p
(policy) property of a Web Link.
Extends ResourceId
. Used for creating and registering resources, exposes the properties of an OCF resource. All properties are read-write.
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
resourceTypes |
array of strings | no | [] |
List of OCF resource types |
interfaces |
array of strings | no | ["oic.if.baseline"] |
List of supported interfaces |
mediaTypes |
array of strings | no | [] |
List of supported Internet media types |
discoverable |
boolean | no | true |
Whether the resource is discoverable |
observable |
boolean | no | true |
Whether the resource is observable |
links |
array of ResourceLink | no | undefined |
Collection of links |
secure |
boolean | no | true |
Whether the resource is secure |
slow |
boolean | yes | false |
Whether the resource is constrained |
properties |
object | yes | undefined |
Resource representation properties as described in the data model |
The properties
property is a resource representation object that contains resource-specific properties and values usually described in the RAML data model definition of the resource.
The links
property, when present, means the resource is an OCF Collection resource that contains zero or more RFC5988 Web Links represented by the ResourceLink
dictionary.
Either the properties
or the links
property MUST be defined.
The interfaces
property MUST at least contain "oic.if.baseline"
.
ClientResource
extends Resource
. It has all the properties of Resource
, and in addition it has events.
Note that applications should not create ClientResource
objects, as they are created and tracked by implementations. Applications can create and use ResourceId
and Resource
objects as method arguments, but client-created ClientResource
objects are not tracked by implementations and will not receive events.
ClientResource
objects support the following events:
Event name | Event callback argument |
---|---|
update |
partial Resource dictionary |
delete |
ResourceId dictionary |
The update
event is fired on a ClientResource
object when the implementation receives an OCF resource update notification because the resource representation has changed. The event listener receives a dictionary object that contains the resource properties that have changed. In addition, the resource property values are already updated to the new values when the event is fired.
The recommended way to observe and unobserve resources from applications is by using the retrieve()
method, in order to be able to specify OCF retrieve options. However, for convenience, when the first listener function listener
is added to the update
event of resource
, implementations SHOULD call retrieve(resource, null, listener)
. When the last listener is removed, the implementations SHOULD call retrieve(resource)
, i.e. make an OCF retrieve request with the observe flag off.
The delete
event is fired on a ClientResource
object when the implementation gets notified about the resource being deleted or unregistered from the OCF network. The event listener receives a dictionary object that contains the deviceId
and resourcePath
of the deleted resource.
The Client API supports the following events.
Fired when a platform is discovered. The event callback receives as argument a Platform
object.
client.on('platformfound', function(platform) {
console.log("Platform found with id: " + platform.id);
});
Fired when a device is discovered. The event callback receives as argument a Device
object.
client.on('devicefound', function(device) {
console.log("Device found with id: " + device.uuid);
});
Fired when a device is lost. The event callback receives as argument a Device
object.
client.on('devicelost', function(device) {
console.log("Device disappeared: " + device.uuid);
});
When the first listener is added to the ondevicefound
or the ondevicelost
event, implementations SHOULD enable watching device status, if supported by the underlying platform.
When the last listener is removed from the ondevicefound
and the ondevicelost
event, implementations SHOULD disable watching device status.
Implementation Note
The mechanism used for watching device status depends on the underlying native stack. According to the OCF Core Specification, presence (using the /oic/ad
resource) is not supported any longer, but e.g. iotivity still supports it. So implementations MAY use the presence related mechanisms provided by the native stack, but this will work only with devices that run on the same underlying native stack.
Fired when a resource is discovered. The event callback receives as argument a ClientResource
object.
client.on('resourcefound', function(resource) {
console.log("Resource found with path: " + resource.resourcePath);
});
Fired when there is a protocol error about which the application needs to know. The Event
object contains an error
property whose value is an OcfError
object.
client.on('error', function(error) {
if (error.deviceId)
console.log("Error for device: " + error.deviceId);
});
Fetches a remote platform information object. The deviceId
argument is a string that contains an OCF device UUID. The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Send a direct discovery request
GET /oic/p
with the givendeviceId
(which can be either a device UUID or a device URL, and wait for the answer. - If there is an error during the request, reject
promise
with that error. - When the answer is received, resolve
promise
with aPlatform
object created from the response.
Fetches a remote device information object. The deviceId
argument is a string that contains an OCF device UUID. The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Send a direct discovery request
GET /oic/d
with the givendeviceId
, and wait for the answer. - If there is an error during the request, reject
promise
with that error. - When the answer is received, resolve
promise
with aDevice
object created from the response.
- Initiates a platform discovery network operation.
- Returns a
Promise
object. - The
listener
argument is optional, and is an event listener for theplatformfound
event that receives as argument aPlatform
object.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Send a multicast request for retrieving
/oic/p
and wait for the answer. - If sending the request fails, reject
promise
with"NetworkError"
, otherwise resolvepromise
. - If there is an error during the discovery protocol, fire an
error
event. - If the
listener
argument is specified, add it as a listener to the 'platformfound' event. - When a platform is discovered, fire a
platformfound
event that contains a property namedplatform
, whose value is aPlatform
object.
- Initiates a device discovery network operation.
- Returns a
Promise
object. - The
options
parameter is optional, and its value is an object that contains one or more of the following properties:
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
deviceTypes |
array of string | yes | undefined |
OCF device type |
resourceTypes |
array of string | yes | undefined |
OCF resource type |
- The
listener
argument is optional, and is an event listener for thedevicefound
event that receives as argument aDevice
object.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel.- If
options.deviceTypes
is an array, include every string element in thert
parameter in a new OCF discovery request.
- If
- Send the OCF discovery request and wait for the answer.
- If sending the request fails, reject
promise
with"NetworkError"
, otherwise resolvepromise
. - If there is an error during the discovery protocol, fire an
error
event. - If the
listener
argument is specified, add it as a listener to thedevicefound
event. - When a device is discovered, fire a
devicefound
event that contains a property nameddevice
, whose value is aDevice
object.
- Initiates a resource discovery network operation.
- Returns a
Promise
object. - The
options
parameter is optional, and its value is an object that contains one or more of the following properties:
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
deviceId |
string | yes | undefined |
OCF device UUID |
deviceTypes |
array of string | yes | undefined |
OCF device type |
resourceTypes |
array of string | yes | undefined |
OCF device type |
resourcePath |
string | yes | undefined |
OCF resource path |
timeout |
number | yes | undefined |
Timeout for discovery |
- The
listener
argument is optional, and is an event listener for theresourcefound
event that receives as argument aClientResource
object.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Configure an OCF resource discovery request as follows:
- If
options.deviceId
is specified, make a direct resource discovery request to that device - If
options.deviceTypes
is an array, include every string element in thert
parameter in a new OCF discovery request. - If
options.resourceTypes
is an array, include every string element in thert
parameter in the OCF resource discovery request. - If
options.resourcePath
is specified, filter results locally. - If the
listener
argument is specified, add it as a listener to theresourcefound
event.
- If
- If
timeout
is a positive number, start a timer withtimeout
. When it expires, discard any further discovery responses matching this query. - Execute the following sub-steps:
- Send the OCF discovery request.
- If sending the request fails, reject
promise
with"NetworkError"
, otherwise resolvepromise
. - Wait for discovery responses. If there is an error during the discovery protocol, fire an
error
event. Whenever a resourceresource
is discovered, fire theresourcefound
event withresource
as an argument to the listener function.
- Creates a remote resource on a given device, and optionally specifies a target resource that is supposed to create the new resource. The device's
oncreate
event handler takes care of dispatching the request to the target resource that will handle creating the resource, and responds with the created resource, or with an error. - Returns a
Promise
object which resolves with a Resource object. - The optional
target
argument is a ResourceId object that contains at least a device UUID and a resource path that identifies the target resource responsible for creating the requested resource. - The
resource
argument is a Resource object. It should contain at least the properties that don't have default values in the resource registration steps:resourcePath
andresourceTypes
.
The method sends a request to the device specified in target
and the device's create
event handler takes care of creating the resource and replying with the created resource, or with an error.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - If
target
is not specified, lettarget.deviceId
beresource.deviceId
andtarget.resourcePath
benull
. - If
target.deviceId
isundefined
, rejectpromise
withTypeError
. - Send a request to create the resource described by
resource
to the device specified bytarget.deviceId
and the target resource on the device specified bytarget.resourcePath
. Wait for the answer. - If there is an error during the request, reject
promise
with that error, otherwise resolvepromise
.
- Retrieves a resource based on resource id by sending a request to the device specified in
resourceId.deviceId
. The device'sretrieve
event handler takes care of fetching the resource representation and replying with the created resource, or with an error. - Returns a
Promise
object which resolves with a Resource object. - The
resourceId
argument is a ResourceId object that contains a device UUID and a resource path. Note that anyResource
object can also be provided. - The
options
argument is optional, and it is an object whose properties represent theREST
query parameters passed along with theGET
request as a JSON-serializable dictionary. Implementations SHOULD validate this client input to fit OCF requirements. The semantics of the parameters are application-specific (e.g. requesting a resource representation in metric or imperial units). Similarly, the properties of an OIC resource representation are application-specific and are represented as a JSON-serializable dictionary. - The
listener
argument is optional, and is an event listener for theClientResource
update
event that is added on the returnedClientResource
object. In the OCF retrieve request it is possible to set anobserve
flag if the client wants to observe changes to that resource (and get a retrieve response with a resource representation for each resource change). If a listener is provided, the OCF observe flag is turned on, otherwise it is turned off. If implementations need to make internal retrieve requests, the value of the OCF observe flag SHOULD be preserved unless there have been errors and observing is turned off.
The options
argument usually contains the interface the retrieve method if called on, denoted by the if
property.
For instance,
client.retrieve({ deviceId: "xxx", resourceId: "/light/room/1"}, options: { if: "oic.if.ll"; });
retrieves the collection of lights from room 1. Note that the /light/room/1
resource may contain more than one interface in its interfaces
property, and the options
argument needs to specify which interface of the resource should be used with the retrieve operation. By default, the resource definition defines the default interface for an operation. If not specified, the oic.if.baseline
is used.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Let
observe
benull
. - If the
listener
argument is specified, add it as a listener to theClientResource
update
event, and setobserve
totrue
. - Let
resource
be the resource identified byresourceId
. - Send a request to retrieve the resource specified by
resourceId
with the OCFobserve
flag set to the value ofobserve
, and wait for the answer. - If there is an error during the request, reject
promise
with that error. - If
observe
isfalse
, resolvepromise
withresource
updated from the retrieve response. - Otherwise, if
observe
istrue
, for each OCF retrieve response received for resource representation change whileresource
is being observed, updateresource
with the new values, and fire theClientResource
update
event onresource
, providing the event listener an object that contains the resource properties that have changed, in addition toresourcePath
anddeviceId
. - If there is an OCF protocol error during observation, fire an
error
event with a newOcfObserveError
objecterror
witherror.deviceId
set to the value ofresourceId.deviceId
andresourcePath
set to the value ofresourceId.resourcePath
.
- Updates a resource on the network by sending a request to the device specified by
resource.deviceId
. The device'supdate
event handler takes care of updating the resource and replying with the updated resource, or with an error. The resource identified byresource
is updated. - Returns: a
Promise
object. - The
resource
argument is a Resource object.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Send a request to update the resource specified by
resource
with the properties present inresource
, and wait for the answer. - If there is an error during the request, reject
promise
with that error, otherwise resolvepromise
.
- Deletes a resource from the network by sending a request to the device specified in
resourceId.deviceId
. The device'sdelete
event handler takes care of deleting (unregistering) the resource and reporting success or error. - Returns: a
Promise
object. - The
resourceId
argument is a ResourceId object that contains a device UUID and a resource path that identifies the resource to be deleted.
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - Send a request to delete the resource specified by
resourceId
, and wait for the answer. - If there is an error during the request, reject
promise
with that error, otherwise resolvepromise
.
Configures a remote device using its /oic/con
resource with the properties defined by the options
argument. The following properties are supported:
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
deviceName |
string | no | undefined |
User provided device name |
location |
a Coordinates object |
yes | undefined |
Device coordinates |
locationName |
string | yes | undefined |
User provided location name |
region |
string | yes | undefined |
User provided region name |
currency |
string | yes | undefined |
User provided currency name |
The method runs the following steps:
- Return a
Promise
objectpromise
and continue in parallel. - If
options
is not a dictionary, oroptions.deviceName
is not a string, rejectpromise
withTypeError
and abort these steps. - Send a request to update the
/oic/con
resource on the device specified by thedeviceId
argument with the dictionary specified byoptions
argument, and wait for the answer. - If there is an error during the request, reject
promise
with that error, otherwise resolvepromise
.