This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathOcfClient.js
52 lines (40 loc) · 1.73 KB
/
OcfClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2016-2017, Intel Corporation.
// Sample client that works with the linux server found in iotivity-constrained
// This sample will find the resource, then periodically retrieve the resource
// To run it on the Arduino 101, you'll need to connect via BLE with your
// host machine (e.g. Linux), then add a new route for the bt0 interface:
// ip -6 route add 2001:db8::/64 dev bt0
var ocf = require('ocf');
var client = ocf.client;
console.log("Started OCF client");
client.on('error', function(error) {
if (error.deviceId)
console.log("Error for device: " + error.deviceId);
});
function onupdate(resource) {
console.log("Resource updated:");
console.log(" deviceId: " + resource.deviceId);
console.log(" resourcePath: " + resource.resourcePath);
if (resource.properties != undefined) {
console.log("Resource property 'state' is " + resource.properties.state);
} else {
console.log("resource.properties not found");
}
}
client.on('update', onupdate);
var lightOn = true;
// TODO: Must save away the timer handle or else GC will destroy it after a few iterations
var t1 = null;
ocf.start();
client.findResources({ resourceType:"core.light" }).then(function(resource) {
console.log("findResources() was successful, deviceId=" + resource.deviceId);
t1 = setInterval(function(resource) {
client.retrieve(resource.deviceId, { observable: false }).then(function(res) {
console.log("retrieve() was successful, deviceId=" + res.deviceId);
}, function(error) {
console.log("retrieve() returned an error: " + error.name);
});
}, 1000, resource);
}, function(error) {
console.log("findResources() returned an error: " + error.name);
});