UNPKG

onem2m_client

Version:

Synctechno's Library for communicating with Mobius on KETI through oneM2M platform

182 lines (149 loc) 4.28 kB
var Http, Uuid, url; Http = require('request-promise'); Uuid = require('uuid'); url = require('url'); const NOTI_EVENT_TYPES = { "CREATED" : 3, "DELETED" : 4 } const CONST_RESOURCE_TYPES = { "acp" : "1", "ae": "2", "cnt": "3", "cin": "4", "cb": "5", "grp": "9", "lcp": "10", // "mgo": "13", // "nod": "14, "csr": "16", // "req": "17, "sub": "23", "sd": "24", "ts": "25", "tsi": "26", "mms": "27", "flx" :"28", "rsp": "99" }; const CONTENT_MODEL = { "1": ["23"], // acp - sub "2": ["23", "3", "9", "1", "24", "25"], // ae - sub, container, group, acp, pollingChannel, schedule, semanticDesc, timeSeries "3": ["4", "23", "3", "24"], // cnt - contentInstance, sub, contain, latest, oldest, semanticDesc, "4": ["24"], // cin - semanticDesc "5": ["2", "3", "9", "1", "23", "26"], // cb - ae, container, group, acp, sub, timeseries "9": ["23", "24"], // grp 0 sub, semanticDesc "10": [], // lcp "16": ["3", "9", "1", "23", "25"], // csr -- container, group, acp, subscription, timeseries "23": [], // sub "24": ["23"], // sd - sub "25": ["26", "23", "24"], // ts - timeSeriesInstance, sub, semanticDesc "26": [], // tsi "27": [], // mms, "28": ["24", "23", "3", "28", "26", "51"], // flex container - semanticDesc, sub, container, flex container, timeseries, transaction "99": [] // rsp }; /** * * @param {string} typeName * @returns {number} */ function getTypeCode(typeName) { // TODO thyun convert various type of resource type name, eg. long, short, qualified var shortName = typeName; var qualifierIndex = typeName.indexOf( ':' ); if( qualifierIndex != -1 ) shortName = typeName.substring(qualifierIndex+1); return CONST_RESOURCE_TYPES[shortName]; } /** * * @param {string} typeName * @returns {Array<number>} */ function getContentModel(typeName) { var typeCode = getTypeCode(typeName); var conentModel = CONTENT_MODEL[typeCode]; return conentModel; } function randomString(len) { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < len; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } /** * * parse resource URL to get access point information * assume that value of passed url is http://domain.name.of.mobius.server:port/mobius-yt/justin/container2?a=va&b=vb&c=vc * result could consisted of * baseUrl : http://domain.name.of.mobius.server:port/ * cseName : mobus-yt * resourcePath : /justin/container2 * query : a=va&b=vb&c=vc * * [accessPoint description] * * @param {string} apUrl [description] * @return {any} [description] */ function getAccessPointInfo(apUrl) { var uri = url.parse(apUrl); var protocol = uri.protocol; var hostname = uri.hostname; var port = uri.port; var pathname = uri.pathname; var query = uri.query; var baseUrl = protocol + '//'; baseUrl += hostname; if( port ) baseUrl += ':' + port; var aryPath = []; if(pathname) { if(pathname.endsWith('/')) pathname = pathname.slice(0, -1); aryPath = pathname.split('/'); aryPath.splice(0,1); } var cseName = ''; if(aryPath.length > 0) cseName = aryPath[0]; var aryQuery = []; if( query ) { var aryQueryItems = query.split( '&' ); for(var i=0; aryQueryItems && i < aryQueryItems.length; i++) { var keyValue = aryQueryItems[i].split( '=' ); var key = null, value = null; if( keyValue.length > 0 ) key = keyValue[0].trim(); if( key.length == 0 ) continue; if( keyValue.length > 1 && keyValue[1].length > 0 ) value = keyValue[1]; if( key ) { var obj = new Object(); obj[key] = value; aryQuery.push( obj ); } } } return { hostname : hostname, baseUrl : baseUrl, path : pathname, aryPaths : aryPath, cseName : aryPath[0], queries: query, aryQueries : aryQuery } } module.exports = { Resource: { getTypeCode: getTypeCode, getContentModel: getContentModel }, notiEventTypes : NOTI_EVENT_TYPES, randomString: randomString, getAccessPointInfo : getAccessPointInfo };