JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q12467F
  • License MIT

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

NPM

Build Status npm npm

Table of Contents

  1. Overview
  2. Installation
  3. Usage
  4. Resources Planning
  5. APIs

1. Overview

2. Installation

$ npm install smartobject --save

3. Usage

var Smartobject = require('smartobject');

var smartObj = new Smartobject();

smartObj.create('temperature');
smartObj.addResource('temperature', 0, { sensorValue: '31' });
smartObj.addResource('temperature', 0, { units : 'Celsius' });

4. Resources Planning

5. APIs


Smartobject Class

Exposed by require('smartobject').

new Smartobject()

Create a new instance of Smartobject class.

Arguments:

  1. none

Returns:

  • (Object): Smartobject instance.

Examples:

var Smartobject = require('smartobject');

var smartObj = new Smartobject();

create(oid)

Create an Object on smartObj.

Arguments:

  1. oid (String | Number): Id of the Object that you want to create.

Returns:

  • (String): IPSO Smart Object Id.

Examples:

smartObj.create('temperature');     // 'temperature'
smartObj.create(3303);              // 'temperature'
smartObj.create('foo');             // null
smartObj.create(9453);              // null

addResource(oid[, iid], rsc)

Add the Resources on smartObj.

Arguments:

  1. oid (String | Number): Id of the Object that owns the Resources.
  2. iid (String | Number): Id of the Object Instance that owns the Resources. It's common to use a number as iid, but using a string is also accepted. If without iid, Smart Object will be given an unoccupied iid.
  3. rsc (Object): An object with rid-value pairs 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): A Smart Object instance.

Examples:

// oid = 'humidity', iid = 0
smartObj.addResource('humidity', 0, { sensorValue: 33 });   // { oid: 'humidity', iid: 0, rid: 'sensorValue' }
smartObj.addResource(3304, 1, { 5700: 56 });                // { oid: 'humidity', iid: 1, rid: 'sensorValue' }
smartObj.addResource('3304', '2', { '5700': 87 });          // { oid: 'humidity', iid: 2, rid: 'sensorValue' }
  • Resource value is read from particular operations:
smartObj.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:
smartObj.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:
smartObj.addResource('led', 0, {
    blink: {
        exec: function (t, cb) {
            blinkLed('led0', t);    // bink led0 for t times
            cb(null, t);            // 
        }
    }
});

dump(callback)

Dump record of the Smart Object.

Arguments:

  1. callback (Function): function (err, result) { }.

Returns:

  • (none)

Examples:

smartObj.dump(function (err, result) {
    console.log(result);
});    

// {
//  temperature: {
//      '0': {
//          sensorValue: 31,
//          units: 'Celsius'
//      },
//      '1': {
//          sensorValue: 87.8,
//          units: 'Fahrenheit'
//      }
//  },
//  humidity: {
//      '0': {
//          sensorValue: 21,
//          units: 'percent'
//      }
//  }
// }

has(oid[, iid[, rid]])

To see if the target exist.

Arguments:

  1. oid (String | Number): The Object Id of the target.
  2. iid (String | Number): The Object Instance Id of the target.
  3. rid (String | Number): The Resource Id of the target.

Returns:

  • (Boolean): It will be true if target is exist, else false.

Examples:

smartObj.has('humidity');                       // true
smartObj.has('foo', 0);                         // false
smartObj.has('temperature', 0, 'sensorValue');  // true