- .getOid()
- .oidKey()
- .oidNum()
- .getRid()
- .ridKey()
- .ridNum()
- .getRspCode()
- .rspCodeKey()
- .rspCodeNum()
- .getCmd()
- .cmdKey()
- .cmdNum()
- .createPath()
- .slashPath()
- .dotPath()
- .pathItems()
- .buildPathValuePairs()
- .isGoodResponse()
- .getAccessCtrl()
- .jsonify()
Returns an item of the Object identifier.
Arguments
- 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 returnsundefined
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
Returns the string of an Object identifier.
Arguments
- 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
Returns the number of an Object identifier.
Arguments
- 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
Returns an item of the Resource identifier. To query a Resource id specific to an Object, both
oid
andrid
should be given. To query an unique Resource id, only the single argumentrid
is needed.
Arguments
- 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. - 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 returnsundefined
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 }
Returns the string of an Resource identifier.
Arguments
- 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. - 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'
Returns the number of an Resource identifier.
Arguments
- 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. - 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)
Returns an item of the Response code.
Arguments
- 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 returnsundefined
if not found.
Example
ut.getRspCode('BadRequest'); // { key: 'BadRequest', value: 400 }
ut.getRspCode(400); // { key: 'BadRequest', value: 400 }
ut.getRspCode(302); // undefined
Returns the string of an Response code.
Arguments
- 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
Returns the number of an Response code.
Arguments
- 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
Returns an item of the command idetifier.
Arguments
- 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 returnsundefined
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 }
Returns the string of an command identifier.
Arguments
- 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
Returns the number of an command identifier.
Arguments
- 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
Returns a path string in conjunction of the given connector.
Arguments
- cnt (String|Number): The connector, can be
'.'
or'/'
- ... (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'
Returns a path with '/' separator.
Arguments
- 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'
Returns a path with '.' separator.
Arguments
- 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'
Returns an array of words that are separated by
'.'
or'/'
.
Arguments
- 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' ]
Returns an object in path-value pair.
Arguments
- rootPath (String): The path where the
obj
attachs to - 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
// }
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
- 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
Get the access control flag of an Resource. Please refer to lwm2m-id documentation.
Arguments
- 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. - 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 a string into a Javascript object.
Arguments
- 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'