Skip to content

Latest commit

 

History

History
561 lines (407 loc) · 16.1 KB

API.md

File metadata and controls

561 lines (407 loc) · 16.1 KB

API


.getOid(oid)

Returns an item of the Object identifier.

Arguments

  1. oid (String | Number): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (Object | Undefined): Returns an item of { key: 'sampleId', value: 1234 }, otherwise returns undefined if not found.

Example

const ut = require('lwmqn-util');

ut.getOid('temperature');  // { key: 'temperature', value: 3303 }
ut.getOid(3303);           // { key: 'temperature', value: 3303 }
ut.getOid('3303');         // { key: 'temperature', value: 3303 }

ut.getOid('xxxx');         // undefined
ut.getOid('9999');         // undefined
ut.getOid(9999);           // undefined



.oidKey(oid)

Returns the string of an Object identifier.

Arguments

  1. oid (String | Number): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (String): Returns the id string, otherwise returns the argument 'oid' ifself if not found.

Example

ut.oidKey('temperature');  // 'temperature'
ut.oidKey(3303);           // 'temperature'
ut.oidKey('3303');         // 'temperature'

ut.oidKey('xxxx');         // 'xxxx' (returns the argument itself if id not found)
ut.oidKey('9999');         // '9999'
ut.oidKey(9999);           // 9999



.oidNum(oid)

Returns the number of an Object identifier.

Arguments

  1. oid (String | Number): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (Number): Returns the id number, otherwise returns the argument 'oid' ifself if not found.

Example

ut.oidNum('temperature');  // 3303
ut.oidNum(3303);           // 3303
ut.oidNum('3303');         // 3303

ut.oidKey('xxxx');         // 'xxxx' (returns the argument itself if id not found)
ut.oidKey('9999');         // 9999
ut.oidKey(9999);           // 9999



.getRid([oid,] rid)

Returns an item of the Resource identifier. To query a Resource id specific to an Object, both oid and rid should be given. To query an unique Resource id, only the single argument rid is needed.

Arguments

  1. oid (String | Number, optional): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.
  2. rid (String | Number): rid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (Object | Undefined): Returns an item of { key: 'sampleId', value: 1234 }, otherwise returns undefined if not found.

Example

// get a Resource id specific to an Object
ut.getRid('location', 'lon');             // { key: 'lon', value: 1 }
ut.getRid(6, 1);                          // { key: 'lon', value: 1 }
ut.getRid('temperature', 'sensorValue');  // { key: 'sensorValue', value: 5700 }
ut.getRid(3303, 5700);                    // { key: 'sensorValue', value: 5700 }
ut.getRid('temperature', '5700');         // { key: 'sensorValue', value: 5700 }

// get an unqiue Resource id
ut.getRid('appType');                     // { key: 'appType', value: 5750 }
ut.getRid(5750);                          // { key: 'appType', value: 5700 }
ut.getRid('5750');                        // { key: 'appType', value: 5750 }



.ridKey([oid ,] rid)

Returns the string of an Resource identifier.

Arguments

  1. oid (String | Number, optional): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.
  2. rid (String | Number): rid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (String): Returns the Resource id in string, otherwise returns the argument 'rid' ifself if not found.

Example

// get a Resource id specific to an Object
ut.ridKey('location', 'lon');             // 'lon'
ut.ridKey(6, 1);                          // 'lon'
ut.ridKey('temperature', 'sensorValue');  // 'sensorValue'
ut.ridKey(3303, 5700);                    // 'sensorValue'
ut.ridKey('temperature', '5700');         // 'sensorValue'

// get an unqiue Resource id
ut.ridKey('appType');                     // 'appType'
ut.ridKey(5750);                          // 'appType'
ut.ridKey('5750');                        // 'appType'

// returns rid itself if not found
ut.ridKey('no_such_rid');                  // 'no_such_rid'
ut.ridKey(3303, 'no_such_id');             // 'no_such_id'



.ridNum([oid ,] rid)

Returns the number of an Resource identifier.

Arguments

  1. oid (String | Number, optional): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.
  2. rid (String | Number): rid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (Number): Returns the Resource id in number, otherwise returns the argument 'rid' ifself if not found.

Example

// get a Resource id specific to an Object
ut.ridNum('location', 'lon');             // 1
ut.ridNum(6, 1);                          // 1
ut.ridNum('temperature', 'sensorValue');  // 5700
ut.ridNum(3303, 5700);                    // 5700
ut.ridNum('temperature', '5700');         // 5700

// get an unqiue Resource id
ut.ridNum('appType');                     // 5750
ut.ridNum(5750);                          // 5750
ut.ridNum('5750');                        // 5750

// returns rid itself if not found
ut.ridNum('no_such_rid');                  // 'no_such_rid'
ut.ridNum('99999');                        // 99999 (no such rid)
ut.ridNum(99999);                          // 99999 (no such rid)
ut.ridNum(3303, 7654);                     // 7654  (no such rid)



.getRspCode(code)

Returns an item of the Response code.

Arguments

  1. code (String | Number): code can be given with a string or a number.

Returns:

  • (Object | Undefined): Returns an item in the form of { key: 'Created', value: 201 }, otherwise returns undefined if not found.

Example

ut.getRspCode('BadRequest');  // { key: 'BadRequest', value: 400 }
ut.getRspCode(400);           // { key: 'BadRequest', value: 400 }
ut.getRspCode(302);           // undefined



.rspCodeKey(code)

Returns the string of an Response code.

Arguments

  1. code (String | Number): code can be given with a string or a number.

Returns:

  • (String | Undefined): Returns the string of an Response code, otherwise returns undefined if not found.

Example

ut.rspCodeKey('BadRequest');  // 'BadRequest'
ut.rspCodeKey(400);           // 'BadRequest'
ut.rspCodeKey(302);           // undefined



.rspCodeNum(code)

Returns the number of an Response code.

Arguments

  1. code (String | Number): code can be given with a string or a number.

Returns:

  • (Number | Undefined): Returns the number of an Response code, otherwise returns undefined if not found.

Example

ut.rspCodeKey('BadRequest');  // 400
ut.rspCodeKey(400);           // 400
ut.rspCodeKey(302);           // undefined



.getCmd(cmd)

Returns an item of the command idetifier.

Arguments

  1. code (String | Number): cmd can be given with a string or a number.

Returns:

  • (Object | Undefined): Returns an item in the form of { key: 'discover', value: 2 }, otherwise returns undefined if not found.

Example

ut.getCmd('read');          // { key: 'read', value: 0 }
ut.getCmd('write');         // { key: 'write', value: 1 }
ut.getCmd('discover');      // { key: 'discover', value: 2 }
ut.getCmd('writeAttrs');    // { key: 'writeAttrs', value: 3 }
ut.getCmd('execute');       // { key: 'execute', value: 4 }
ut.getCmd('observe');       // { key: 'observe', value: 5 }
ut.getCmd('notify');        // { key: 'execute', value: 6 }

ut.getCmd(0);   // { key: 'read', value: 0 }
ut.getCmd(1);   // { key: 'write', value: 1 }
ut.getCmd(2);   // { key: 'discover', value: 2 }
ut.getCmd(3);   // { key: 'writeAttrs', value: 3 }
ut.getCmd(4);   // { key: 'execute', value: 4 }
ut.getCmd(5);   // { key: 'observe', value: 5 }
ut.getCmd(6);   // { key: 'execute', value: 6 }



.cmdKey(cmd)

Returns the string of an command identifier.

Arguments

  1. code (String | Number): cmd can be given with a string or a number.

Returns:

  • (String | Undefined): Returns the string of an command id, otherwise returns undefined if unrecognized.

Example

ut.cmdKey('read');        // 'read'
ut.cmdKey(4);             // 'execute'
ut.cmdKey('no_such_cmd'); // undefined
ut.cmdKey(22);            // undefined



.cmdNum(cmd)

Returns the number of an command identifier.

Arguments

  1. code (String | Number): cmd can be given with a string or a number.

Returns:

  • (Number | Undefined): Returns the number of an command id, otherwise returns undefined if unrecognized.

Example

ut.cmdKey('read');        // 0
ut.cmdKey(4);             // 4
ut.cmdKey('no_such_cmd'); // undefined
ut.cmdKey(22);            // undefined



.createPath(cnnt, ...)

Returns a path string in conjunction of the given connector.

Arguments

  1. cnt (String|Number): The connector, can be '.' or '/'
  2. ... (String, variadic): Words to be connected

Returns:

  • (String): Returns the combined result.

Example

ut.createPath('.', 'dev', 0, 'sensor');          // 'dev.0.sensor'
ut.createPath('/', 'dev', 0, 'sensor', 'value'); // 'dev/0/sensor/value'



.slashPath(path)

Returns a path with '/' separator.

Arguments

  1. path (String): The path string

Returns:

  • (String): Returns the result. The first and last character will be ignored if they are '.' or '/'.

Example

ut.slashPath('dev.0.sensor.value');     // 'dev/0/sensor/value'
ut.slashPath('.dev.0.sensor.value');    // 'dev/0/sensor/value'
ut.slashPath('/dev.0.sensor/value/');   // 'dev/0/sensor/value'
ut.slashPath('/dev/0/sensor/value/');   // 'dev/0/sensor/value'



.dotPath(path)

Returns a path with '.' separator.

Arguments

  1. path (String): The path string

Returns:

  • (String): Returns the result. The first and last character will be ignored if they are '.' or '/'.

Example

ut.dotPath('dev.0.sensor.value');     // 'dev.0.sensor.value'
ut.dotPath('.dev.0.sensor.value');    // 'dev.0.sensor.value'
ut.dotPath('/dev.0.sensor/value/');   // 'dev.0.sensor.value'
ut.dotPath('/dev/0/sensor/value/');   // 'dev.0.sensor.value'



.pathItems(path)

Returns an array of words that are separated by '.' or '/'.

Arguments

  1. path (String): The path string

Returns:

  • (Array): Returns the words in an array.

Example

ut.pathItems('dev.0.sensor.value');     // [ 'dev', '0', 'sensor', 'value' ]
ut.pathItems('.dev.0.sensor.value');    // [ 'dev', '0', 'sensor', 'value' ]
ut.pathItems('/dev.0.sensor/value/');   // [ 'dev', '0', 'sensor', 'value' ]
ut.pathItems('/dev/0/sensor/value/');   // [ 'dev', '0', 'sensor', 'value' ]



.buildPathValuePairs(rootPath, obj)

Returns an object in path-value pair.

Arguments

  1. rootPath (String): The path where the obj attachs to
  2. obj (Object): The object to be built from

Returns:

  • (Object): Returns a new object that is orginized in path-vale pair.

Example

const myObj = {
    a: {
        a1: {
            a11: 1,
            a12: 'hi',
        },
        a2: 'foo'
    },
    b: 'hello',
    c: 3
};

const newObj = ut.buildPathValuePairs('/', myObj);
// newObj = {
//     'a.a1.a11': 1,
//     'a.a1.a12': 'hi',
//     'a.a2': 'foo',
//     'b': 'hello',
//     'c': 3
// }

const newObj = ut.buildPathValuePairs('/dev/0', myObj);
// newObj = {
//     'dev.0.a.a1.a11': 1,
//     'dev.0.a.a1.a12': 'hi',
//     'dev.0.a.a2': 'foo',
//     'dev.0.b': 'hello',
//     'dev.0.c': 3
// }



.isGoodResponse(status)

Check if a status code is a good response. The good responses are those with status codes in either one of 200, 201, 202, 204, and 205.

Arguments

  1. status (String | Number): status can be given with a string or a number.

Returns:

  • (Boolean): Returns true if status is a good response, otherwise returns false.

Example

ut.isGoodResponse(200);         // true
ut.isGoodResponse('Deleted');   // true
ut.isGoodResponse('NotFound');  // false
ut.isGoodResponse(409);         // false



.getAccessCtrl()

Get the access control flag of an Resource. Please refer to lwm2m-id documentation.

Arguments

  1. oid (String | Number, optional): oid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.
  2. rid (String | Number): rid can be given with a string or a number. Notice that a numbered string will be recognized as a number, e.g. '128' is equal to 128.

Returns:

  • (String | Null | Undefined): Returns the access control flag which can be 'R' (read-only), 'W' (write-only), 'RW', 'E' (executable), and null (cannot access). Returns undfined if the Resource is not found.

Example

ut.getAccessCtrl('temperature', 'sensorValue'); // 'R'
ut.getAccessCtrl('lightCtrl', 5850);            // 'RW'
ut.getAccessCtrl('xxxx', 1234);                 // undfined



.jsonify(str)

Jsonify a string into a Javascript object.

Arguments

  1. str (String): The string to be jsonified.

Returns:

  • (Object | String): Returns an object if the string can be jsonified, otherwise returns the string itself.

Example

ut.jsonify('{"x": 3, "y": 'hi'}');  // { x: 3, y: 'hi' }
ut.jsonify('hello there');          // 'hello there'