Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit d0574cf

Browse files
committed
OCF fix #21, #26, #36: add start(), stop(), configure(); support device and platform init
Signed-off-by: Zoltan Kis <[email protected]>
1 parent 1c392b2 commit d0574cf

File tree

5 files changed

+270
-212
lines changed

5 files changed

+270
-212
lines changed

api/ocf/README.md

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
OCF Web API
22
===========
33

4-
* [OCF API object](#api-entry-point)
5-
- [OCF Client API](./client.md)
6-
- [OCF Server API](./server.md)
4+
* [OCF API object](#api-object)
5+
- [OCF Client API](./client.md) to access remote resources
6+
- [OCF Server API](./server.md) to implement local resources
77
* The [OcfDevice](#ocfdevice) dictionary
88
* The [OcfPlatform](#ocfplatform) dictionary
99
* The [OcfError](#ocferror) interface
@@ -32,31 +32,59 @@ This version of the API supports OCF Resource, Device, and Platform related func
3232

3333
Since implementations may run on constrained hardware, examples use [ECMAScript 5.1](http://www.ecma-international.org/ecma-262/5.1/).
3434

35+
<a name="api-object"></a>
3536
The API object
3637
--------------
37-
The API object is exposed in a platform-specific manner. As an example, on Node.js it can be obtained by requiring the package that implements this API. On other platforms, it can be exposed as a property of a global object.
38+
The API object represents an OCF stack (device) and is exposed in a platform-specific manner. As an example, on Node.js it can be obtained by requiring the package that implements this API. On other platforms, it can be exposed as a property of a global object.
3839

3940
```javascript
4041
let module = 'ocf'; // use your own implementation's name
4142
var ocf = require(module);
4243
```
4344
If the functionality is not supported by the platform, `require` should throw `NotSupportedError`. If there is no permission for using the functionality, `require` should throw `SecurityError`.
4445

45-
When `require` is successful, it MUST return an object with the following read-only properties:
46-
- `client` is an object that implements the [OCF Client API](./client.md), i.e. CRUDN (Create, Retrieve, Update, Delete, Notify) functionality:
47-
* remote access to resources on the network,
48-
* listening to presence notifications in the OCF network, and
49-
* discovery for platforms, devices and resources on the OCF network.
50-
- `server` is an object that implements the [OCF Server API](./server.md), i.e. the functionality to serve CRUDN requests on a device, to register and unregister resources, to notify of resource changes, and to enable and disable presence functionality on the device.
51-
- `device` is an [`OcfDevice`](#ocfdevice) object that represents properties of the current device.
52-
- `platform` is an [`OcfPlatform`](#ocfplatform) object that represents properties of the platform that hosts the current device.
53-
54-
|Property |Type |Optional |Default value |
55-
| --- | --- | --- | --- |
56-
| `client` | [OcfClient](./client.md#ocfclient) object | no | platform provided |
57-
| `server` | [OcfServer](./server.md#ocfserver) object | no | platform provided |
58-
| `device` | [OcfDevice](#ocfdevice) object | no | platform provided |
59-
| `platform` | [OcfPlatform](#ocfplatform) object | no | platform provided |
46+
When `require` is successful, it MUST return an object with the following methods.
47+
48+
| Method signature | Description |
49+
| --- | --- |
50+
| [`start(mode, options)`](#ocfstart)| start the OCF stack |
51+
| [`stop()`](#ocfstop) | stop the OCF stack |
52+
53+
<a name="ocfstart"></a>
54+
The `start(options)` method initializes the underlying platform and resolves with an API object providing OCF client, or server, or both client-server functionality.
55+
56+
The `mode` optional parameter is a string that can be `"client-server"` (by default), or `"client"`, or `"server"`.
57+
58+
The `options` optional parameter is an object that may contain the following properties:
59+
60+
|Property |Type |Optional |Default value |
61+
| --- | --- | --- | --- |
62+
| `device` | [OcfDevice](#ocfdevice) object | yes | `undefined` |
63+
| `platform` | [OcfPlatform](#ocfplatform) object | yes | `undefined` |
64+
65+
The method runs the following steps:
66+
- Return a [`Promise`](../README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
67+
- Let `ocf` denote the API object (equal to the `this` property of `start()`).
68+
- If the `mode` argument is `undefined`, let `mode` be `"client-server"`.
69+
- Otherwise if the `mode` argument is not `"client"`, or `"server"`, or `"client-server"`, reject `promise` with a `TypeError` and abort these steps.
70+
- If the `mode` parameter is `"client"`, then run the following sub-steps:
71+
* If `options` or `options.device` or `options.platform` are not `undefined`, reject `promise` with `NotSupportedError` and abort these steps. (Client-only device has no `platform` property, and its `device` property cannot be configured).
72+
* Initialize the underlying OCF stack in client mode.
73+
* If there is an error, reject `promise` with `OcfDeviceError` and abort these steps.
74+
* Let `result` be an object that implements [`OcfClient`](./client.md#ocfclient). Let `result.device` be an [`OcfDevice`](#ocfdevice) object initialized from the underlying platform.
75+
- Otherwise if the `mode` parameter is `"server"`, then run the following sub-steps.
76+
* Initialize the underlying OCF stack in server mode. If `options.device` is defined, use the defined properties for initializing the OCF stack. If `options.platform` is defined, use the defined properties for initializing the OCF stack.
77+
* If there is an error, reject `promise` with `OcfDeviceError` and abort these steps.
78+
* Let `result` be a new object that implements [`OcfServer`](./server.md#ocfserver). Initialize the `result.device` and `result.platform` properties from the underlying OCF stack.
79+
- Otherwise if the `mode` parameter is `client-server`, then run the following sub-steps.
80+
* Initialize the underlying OCF stack in client-server mode. If `options.device` is defined, use the defined properties for initializing the OCF stack. If `options.platform` is defined, use the defined properties for initializing the OCF stack.
81+
* If there is an error, reject `promise` with `OcfDeviceError` and abort these steps.
82+
* Let `result` be a new object that implements both [`OcfServer`](./server.md#ocfserver) and [`OcfClient`](./client.md#ocfclient).
83+
* Initialize the `result.device` and `result.platform` properties from the underlying OCF stack.
84+
- Resolve `promise` with `result`.
85+
86+
<a name="ocfstop"></a>
87+
The `stop()` method frees resources in the underlying platform, so that a next invocation to `start()` is able to start the device from a clean state. After the `stop()` method returns, all properties of `ocf` should be `undefined`.
6088

6189
<a name="ocfdevice"></a>
6290
### The `OcfDevice` object
@@ -71,7 +99,7 @@ Exposes information about the OCF device that runs the current OCF stack instanc
7199
| `dataModels` | array of strings | no | `[]` | List of supported OCF data models |
72100
| `coreSpecVersion` | string | no | `undefined` | OCF Core Specification version |
73101

74-
The `types` property is a list of the OCF Device types that are supported. It comforms to the same syntax constraints as [resource](./client.md/#resource) types. OCF mandates that every device supports at least the properties defined in the `"oic.wk.d"` resource type, that represents a resource for a "basic device". Other specifications, such as the OCF Smart Home Device Specification can define more device types, for instance `"oic.d.fan"`, `"oic.d.thermostat"`, `"oic.d.light"`, `"oic.d.airconditioner"`, etc. The properties exposed by these device types are defined in [oneiota.org](http://wwww.oneiota.org). [Device discovery](./client.md/#finddevices) may use filters based upon device types.
102+
The `types` property is a list of the OCF Device types that are supported. It comforms to the same syntax constraints as [resource](./client.md/#resource) types. OCF mandates that every device supports at least the properties defined in the `"oic.wk.d"` resource type, that represents a resource for a "basic device". Other specifications, such as the OCF Smart Home Device Specification can define more device types, for instance `"oic.d.fan"`, `"oic.d.thermostat"`, `"oic.d.light"`, `"oic.d.airconditioner"`, etc. The properties exposed by these device types are defined in [oneiota.org](http://www.oneiota.org). The values in `types` may be used in [device discovery](./client.md/#finddevices) filters. For a client-only device `types` is an empty array.
75103

76104
The `dataModels` property is in the following format: `vertical.major.minor` where `major` and `minor` are numbers and `vertical` is a string such as `"Smart Home"`.
77105

@@ -96,7 +124,7 @@ Exposes information about the OCF platform that hosts the current device.
96124

97125
Errors during OCF network operations are exposed via `onerror` events and `Promise` rejections.
98126

99-
OCF errors (see also [these notes](../README.md#errors)) are represented as augmented instances of [`Error`](https://nodejs.org/api/errors.html#errors_class_error) objects.
127+
[OCF errors](../README.md#error) are represented as augmented instances of [`Error`](https://nodejs.org/api/errors.html#errors_class_error) objects.
100128

101129
The following [`Error` names](https://nodejs.org/api/errors.html) are used for signaling OCF issues:
102130
- `OcfResourceNotFound` used if the resource cannot be located in the OCF network, or when an observed resource is deleted from the network.

api/ocf/client.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Introduction
1010
------------
1111
<a name="ocfclient"></a>
1212
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.
13-
The Client API object does not expose its own properties, only events and methods.
13+
14+
|Property |Type |Optional |Default value |
15+
| --- | --- | --- | --- |
16+
| `device` | [`OcfDevice`](./README.md/#ocfdevice) object | no | implementation provided |
1417

1518
| Event name | Event callback argument |
1619
| --- | --- |
@@ -34,7 +37,7 @@ The Client API object does not expose its own properties, only events and method
3437
| [`retrieve(resourceId, options, listener)`](#retrieve) | retrieve/observe a resource |
3538
| [`update(resource)`](#update) | update a resource |
3639
| [`delete(resourceId)`](#delete) | delete a resource |
37-
40+
| [`configure(deviceId, options)`](#configure) | configure a remote device |
3841

3942
## Structures
4043
<a name="resourceid"></a>
@@ -102,7 +105,7 @@ Note that applications should not create `ClientResource` objects, as they are c
102105
<a name="onresourceupdate"></a>
103106
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.
104107

105-
The recommended way to observe and unobserve resources from applications is by using the [`retrieve()`](#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)](#retrieve). When the last listener is removed, the implementations SHOULD call [`retrieve(resource)`](#retrieve), i.e. make an OCF retrieve request with the observe flag off.
108+
The recommended way to observe and unobserve resources from applications is by using the [`retrieve()`](#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)`](#retrieve). When the last listener is removed, the implementations SHOULD call [`retrieve(resource)`](#retrieve), i.e. make an OCF retrieve request with the observe flag off.
106109

107110
<a name="onresourcelost"></a>
108111
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.
@@ -313,3 +316,21 @@ The method runs the following steps:
313316
- Return a [`Promise`](./README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
314317
- Send a request to delete the resource specified by `resourceId`, and wait for the answer.
315318
- If there is an error during the request, reject `promise` with that error, otherwise resolve `promise`.
319+
320+
<a name="configure"></a>
321+
##### 4.4. The `configure(deviceId, options)` method
322+
Configures a remote device using its `/oic/con` resource with the properties defined by the `options` argument. The following properties are supported:
323+
324+
|Property |Type |Optional |Default value |Represents |
325+
| --- | --- | --- | --- | --- |
326+
| `deviceName` | string | no | `undefined` | User provided device name |
327+
| `location` | a [`Coordinates`](https://www.w3.org/TR/geolocation-API/#coordinates_interface) object | yes | `undefined` | Device coordinates |
328+
| `locationName` | string | yes | `undefined` | User provided location name |
329+
| `region` | string | yes | `undefined` | User provided region name |
330+
| `currency` | string | yes | `undefined` | User provided currency name |
331+
332+
The method runs the following steps:
333+
- Return a [`Promise`](./README.md/#promise) object `promise` and continue [in parallel](https://html.spec.whatwg.org/#in-parallel).
334+
- If `options` is not a dictionary, or `options.deviceName` is not a string, reject `promise` with `TypeError` and abort these steps.
335+
- Send a request to update the `/oic/con` resource on the device specified by the `deviceId` argument with the dictionary specified by `options` argument, and wait for the answer.
336+
- If there is an error during the request, reject `promise` with that error, otherwise resolve `promise`.

0 commit comments

Comments
 (0)