Package Exports
- smartobject
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (smartobject) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
smartobject
Table of Contents
1. Overview
smartobject is an utility to help you with creating IPSO Smart Objects in your applications.
[Note]: The italic words, such Object, Object Id, Object Instance, and Object Instance Id, are used to distinguish the IPSO Objects from the JavaScript objects.
2. Installation
$ npm install smartobject --save
3. Usage
var SmartObject = require('smartobject');
// step 1: New a SmartObject instance
var so = new SmartObject();
// step 2: Create an IPSO Object 'temperature' on it.
// This 'temperature' Object can have many IPSO Object Instances in it, it's like a namespace.
so.create('temperature');
// step 3: Add IPSO Resources to IPSO Object Instance 0 and 1 in the 'temperature' Object.
so.addResource('temperature', 0, { sensorValue: 31 });
so.addResource('temperature', 0, { units : 'Celsius' });
so.addResource('temperature', 1, {
sensorValue: {
read: function (cb) {
adc1.read(function (err, val) {
cb(err, val);
});
}
}
});
// ...
// step 4: dump data of this Smart Object somewhere in your code
so.dump(function (err, data) {
if (!err)
console.log(data);
// {
// temperature: {
// '0': {
// sensorValue: 31,
// units : 'Celsius'
// },
// '1': {
// sensorValue: 24.6,
// }
// }
// }
});
4. Resources Planning
[TBD]
5. APIs
SmartObject Class
Exposed by require('smartobject')
.
new SmartObject()
Create a new instance of SmartObject class. This document will use so to indicate this kind of instance. A so can hold many IPSO Objects in it.
Arguments:
- none
Returns:
- (Object): so.
Examples:
var SmartObject = require('smartobject');
var so = new SmartObject();
create(oid)
Create an IPSO Object in so. An IPSO Object Id is like a namespace to manage the same kind of IPSO Object Instances. For example, our so has a 'temperature' namespace(IPSO Object Id), and there are 6 temperature sensors(IPSO Object Instances) within this namespace.
The IPSO Object will be an empty object at first created, you have to use addResource()
to put something into it.
Arguments:
oid
(String | Number): IPSO Object Id you'd like to create with.
Returns:
- (String): Returns the IPSO Object Id if succeeds, otherwise returns
null
to indicate an invalidoid
was given.
Examples:
so.create('temperature'); // 'temperature'
so.create(3303); // 'temperature'
so.create('foo'); // null
so.create(9453); // null
addResource(oid[, iid], resrc)
Add a single piece of IPSO Resource to an Object Instance in so. If iid
is not explicitly specified, the so will create a new Object Instance as well as assign an unused iid to it.
Arguments:
oid
(String | Number): IPSO Object Id.iid
(String | Number): Object Instance Id to specify which Instance owns the Resources. It's common to use a number asiid
, but using a string is also accepted. The so will assign an unused iid to the created Object Instance ifiid
is not given.resrc
(Object): An object with a rid-value pair to describe the Resource. Resource value can be a primitive, an data object, or an object with specific methods, i.e. read(), write(), exec().
Returns:
- (Object): An object with identifiers to tell how to point to this Resource.
Examples:
// oid = 'humidity', iid = 0
so.addResource('humidity', 0, { sensorValue: 33 }); // { oid: 'humidity', iid: '0', rid: 'sensorValue' }
so.addResource(3304, 1, { 5700: 56 }); // { oid: 'humidity', iid: '1', rid: 'sensorValue' }
so.addResource('3304', '2', { '5700': 87 }); // { oid: 'humidity', iid: '2', rid: 'sensorValue' }
- Resource value is read from particular operations:
so.addResource('dIn', 0, {
dInState: {
read: function (cb) {
// you should call cb(err, value) and pass the read value through its second argument
// when read operation accomplishes.
var val = gpio.read('gpio0');
cb(null, val);
}
// if write method is not given, this Resource will be considered as unwritable
}
});
- Resource value should be written through particular operations:
so.addResource('dOut', 0, {
dOutState: {
// if read method is not given, this Resource will be considered as unreadable
write: function (val, cb) {
gpio.write('gpio0', val);
cb(null, val);
}
}
});
- Resource is an executable procedure that can be called remotely:
so.addResource('led', 0, {
blink: {
exec: function (t, cb) {
blinkLed('led0', t); // blink led0 for t times
cb(null, t); // you can send anything back to the requester
// through the second argument
}
}
});
dump(callback)
Dump data of this so. The dumped data will pass to the second argument of the callback
if succeeds.
Arguments:
callback
(Function):function (err, data) { }
.
Returns:
- (none)
Examples:
so.dump(function (err, data) {
console.log(data);
});
// {
// temperature: {
// '0': {
// sensorValue: 31,
// units: 'C'
// },
// '1': {
// sensorValue: 87.8,
// units: 'F'
// }
// },
// humidity: {
// '0': {
// sensorValue: 21,
// units: 'percent'
// }
// }
// }
has(oid[, iid[, rid]])
To see if the target exists.
Arguments:
oid
(String | Number): Object Id of the target.iid
(String | Number): Object Instance Id of the target.rid
(String | Number): Resource Id of the target.
Returns:
- (Boolean): Returns
true
if target exists, otherwisefalse
.
Examples:
so.has('humidity'); // true
so.has('foo', 0); // false
so.has('temperature', 0, 'sensorValue'); // true