diff --git a/main.js b/main.js index 84f5db2..338e623 100644 --- a/main.js +++ b/main.js @@ -42,88 +42,88 @@ 'use strict'; const QUERIES = { - 'info': { + info: { query: '/api/v1/info', - parser: parseInfo + parser: parseInfo, }, - 'check_cpu': { + check_cpu: { query: '/api/v1/queries/check_cpu/commands/execute', - parser: parsePerf + parser: parsePerf, }, - 'check_drivesize': { + check_drivesize: { query: '/api/v1/queries/check_drivesize/commands/execute', - parser: parsePerf + parser: parsePerf, }, - 'check_memory': { + check_memory: { query: '/api/v1/queries/check_memory/commands/execute', - parser: parsePerf + parser: parsePerf, }, }; const HTTP_CODES = { - '100': 'Continue', - '101': 'Switching Protocols', - '102': 'Processing', - '103': 'Early Hints', - '200': 'OK', - '201': 'Created', - '202': 'Accepted', - '203': 'Non-Authoritative Information', - '204': 'No Content', - '205': 'Reset Content', - '206': 'Partial Content', - '207': 'Multi-Status', - '208': 'Already Reported', - '226': 'IM Used', - '300': 'Multiple Choices', - '301': 'Moved Permanently', - '302': 'Found', - '303': 'See Other', - '304': 'Not Modified', - '305': 'Use Proxy', - '307': 'Temporary Redirect', - '308': 'Permanent Redirect', - '400': 'Bad Request', - '401': 'Unauthorized', - '402': 'Payment Required', - '403': 'Forbidden', - '404': 'Not Found', - '405': 'Method Not Allowed', - '406': 'Not Acceptable', - '407': 'Proxy Authentication Required', - '408': 'Request Timeout', - '409': 'Conflict', - '410': 'Gone', - '411': 'Length Required', - '412': 'Precondition Failed', - '413': 'Payload Too Large', - '414': 'URI Too Long', - '415': 'Unsupported Media Type', - '416': 'Range Not Satisfiable', - '417': 'Expectation Failed', - '418': "I'm a Teapot", - '421': 'Misdirected Request', - '422': 'Unprocessable Entity', - '423': 'Locked', - '424': 'Failed Dependency', - '425': 'Too Early', - '426': 'Upgrade Required', - '428': 'Precondition Required', - '429': 'Too Many Requests', - '431': 'Request Header Fields Too Large', - '451': 'Unavailable For Legal Reasons', - '500': 'Internal Server Error', - '501': 'Not Implemented', - '502': 'Bad Gateway', - '503': 'Service Unavailable', - '504': 'Gateway Timeout', - '505': 'HTTP Version Not Supported', - '506': 'Variant Also Negotiates', - '507': 'Insufficient Storage', - '508': 'Loop Detected', - '509': 'Bandwidth Limit Exceeded', - '510': 'Not Extended', - '511': 'Network Authentication Required' + 100: 'Continue', + 101: 'Switching Protocols', + 102: 'Processing', + 103: 'Early Hints', + 200: 'OK', + 201: 'Created', + 202: 'Accepted', + 203: 'Non-Authoritative Information', + 204: 'No Content', + 205: 'Reset Content', + 206: 'Partial Content', + 207: 'Multi-Status', + 208: 'Already Reported', + 226: 'IM Used', + 300: 'Multiple Choices', + 301: 'Moved Permanently', + 302: 'Found', + 303: 'See Other', + 304: 'Not Modified', + 305: 'Use Proxy', + 307: 'Temporary Redirect', + 308: 'Permanent Redirect', + 400: 'Bad Request', + 401: 'Unauthorized', + 402: 'Payment Required', + 403: 'Forbidden', + 404: 'Not Found', + 405: 'Method Not Allowed', + 406: 'Not Acceptable', + 407: 'Proxy Authentication Required', + 408: 'Request Timeout', + 409: 'Conflict', + 410: 'Gone', + 411: 'Length Required', + 412: 'Precondition Failed', + 413: 'Payload Too Large', + 414: 'URI Too Long', + 415: 'Unsupported Media Type', + 416: 'Range Not Satisfiable', + 417: 'Expectation Failed', + 418: "I'm a Teapot", + 421: 'Misdirected Request', + 422: 'Unprocessable Entity', + 423: 'Locked', + 424: 'Failed Dependency', + 425: 'Too Early', + 426: 'Upgrade Required', + 428: 'Precondition Required', + 429: 'Too Many Requests', + 431: 'Request Header Fields Too Large', + 451: 'Unavailable For Legal Reasons', + 500: 'Internal Server Error', + 501: 'Not Implemented', + 502: 'Bad Gateway', + 503: 'Service Unavailable', + 504: 'Gateway Timeout', + 505: 'HTTP Version Not Supported', + 506: 'Variant Also Negotiates', + 507: 'Insufficient Storage', + 508: 'Loop Detected', + 509: 'Bandwidth Limit Exceeded', + 510: 'Not Extended', + 511: 'Network Authentication Required', }; // The adapter-core module gives you access to the core ioBroker functions @@ -135,10 +135,9 @@ const utils = require('@iobroker/adapter-core'); const https = require('https'); // #################### global variables #################### -let adapter; // adapter instance - @type {ioBroker.Adapter} -const CTXs = []; // see description at header of file -const STATEs = []; // cache of already created caches - +let adapter; // adapter instance - @type {ioBroker.Adapter} +const CTXs = []; // see description at header of file +const STATEs = []; // cache of already created caches // #################### general utility functions #################### @@ -148,9 +147,8 @@ const STATEs = []; // cache of already created caches * This utility routine replaces all forbidden chars and the characters '-' and any whitespace * with an underscore ('_'). * - * @param {string} pName name of an object - * @return {string} name of the object with all forbidden chars replaced - * + * @param pName name of an object + * @returns name of the object with all forbidden chars replaced */ function name2id(pName) { return (pName || '').replace(adapter.FORBIDDEN_CHARS, '_').replace(/[%]/g, 'pct').replace(/[-\s]/g, '_'); @@ -159,17 +157,18 @@ function name2id(pName) { /** * handleOffline - process online status of device switches to offline * - * @param {object} pCTX device context - * @param {string} pMsg error message to log - * @return nothing - * + * @param pCTX device context + * @param pMsg error message to log + * @returns nothing */ -function handleOffline (pCTX, pMsg) { - adapter.log.debug('handleOffline - ' + pCTX.name ); +function handleOffline(pCTX, pMsg) { + adapter.log.debug(`handleOffline - ${pCTX.name}`); - if ( ! pCTX.offline ){ - if (pMsg && pMsg !== '') { adapter.log.warn('[' + pCTX.name + '] ' + pMsg ); } - adapter.log.info('[' + pCTX.name + '] offline'); + if (!pCTX.offline) { + if (pMsg && pMsg !== '') { + adapter.log.warn(`[${pCTX.name}] ${pMsg}`); + } + adapter.log.info(`[${pCTX.name}] offline`); } pCTX.offline = true; } @@ -177,17 +176,18 @@ function handleOffline (pCTX, pMsg) { /** * handleOnline - process online status of device switches to online * - * @param {object} pCTX device context - * @param {string} pMsg info message to log - * @return nothing - * + * @param pCTX device context + * @param pMsg info message to log + * @returns nothing */ -function handleOnline (pCTX, pMsg) { - adapter.log.debug('handleOnline - ' + pCTX.name ); +function handleOnline(pCTX, pMsg) { + adapter.log.debug(`handleOnline - ${pCTX.name}`); - if ( pCTX.offline ){ - if (pMsg && pMsg !== '') { adapter.log.info('[' + pCTX.name + '] ' + pMsg ); } - adapter.log.info('[' + pCTX.name + '] online'); + if (pCTX.offline) { + if (pMsg && pMsg !== '') { + adapter.log.info(`[${pCTX.name}] ${pMsg}`); + } + adapter.log.info(`[${pCTX.name}] online`); } pCTX.offline = false; } @@ -199,16 +199,15 @@ function handleOnline (pCTX, pMsg) { * creates object if it does not exist * waits for action to complete using await * - * @param {object} pObj object structure - * @return nothing - * + * @param pObj object structure + * @returns nothing */ async function initObject(pObj) { - adapter.log.debug('initobject - ' + pObj._id); + adapter.log.debug(`initobject - ${pObj._id}`); try { await adapter.extendObjectAsync(pObj._id, pObj); } catch (e) { - adapter.log.error('error initializing obj "' + pObj._id + '" ' + e.message); + adapter.log.error(`error initializing obj "${pObj._id}" ${e.message}`); } } @@ -219,29 +218,30 @@ async function initObject(pObj) { * set the state value * waits for action to complete using await * - * @param {string} pId state id - * @param {any} pValue new value the of the state - * @param {number} pQual quality code of the state - * @param {object} pCommon object containing common part of state object - * @return nothing - * + * @param pId state id + * @param pValue new value the of the state + * @param pQual quality code of the state + * @param pCommon object containing common part of state object + * @returns nothing */ -async function updateState( pId, pValue, pQual, pCommon ) { - adapter.log.debug('updateState - ' + pId); +async function updateState(pId, pValue, pQual, pCommon) { + adapter.log.debug(`updateState - ${pId}`); - if ( ! STATEs[pId] || STATEs[pId].type !== pCommon.type) { + if (!STATEs[pId] || STATEs[pId].type !== pCommon.type) { await initObject({ _id: pId, type: 'state', common: pCommon, - native: { } + native: {}, }); - if ( ! STATEs[pId] ) { STATEs[pId] = {}; } + if (!STATEs[pId]) { + STATEs[pId] = {}; + } STATEs[pId].type = pCommon.type; } - await adapter.setStateAsync(pId, {val: pValue, ack: true, q:pQual} ); - adapter.log.debug('state ' + pId + ' updated (value ' + pValue +')'); + await adapter.setStateAsync(pId, { val: pValue, ack: true, q: pQual }); + adapter.log.debug(`state ${pId} updated (value ${pValue})`); } /** @@ -250,23 +250,21 @@ async function updateState( pId, pValue, pQual, pCommon ) { * creates device object if it does not exist * waits for action to complete using await * - * @param {string} pId object id - * @param {string} pName object name - * @return nothing - * + * @param pId object id + * @param pName object name + * @returns nothing */ async function initDeviceObject(pId, pName) { - adapter.log.debug('initFolderObject - ' + pId); + adapter.log.debug(`initFolderObject - ${pId}`); const obj = { _id: pId, type: 'device', common: { - name: pName + name: pName, }, - native: { - } + native: {}, }; - await initObject( obj ); + await initObject(obj); } /** @@ -275,59 +273,55 @@ async function initDeviceObject(pId, pName) { * creates folder object if it does not exist * waits for action to complete using await * - * @param {string} pId object id - * @param {string} pName object name - * @return nothing - * + * @param pId object id + * @param pName object name + * @returns nothing */ async function initFolderObject(pId, pName) { - adapter.log.debug('initFolderObject - ' + pId); + adapter.log.debug(`initFolderObject - ${pId}`); const obj = { _id: pId, type: 'folder', common: { - name: pName + name: pName, }, - native: { - } + native: {}, }; - await initObject( obj ); + await initObject(obj); } /** * initBaseObjects - create base state objects for a single device * - * @param {object} pCTX device context - * @return nothing - * + * @param pCTX device context + * @returns nothing */ -async function initBaseObjects ( pCTX ) { - adapter.log.debug('initBaseObjects - ' + pCTX.name ); +async function initBaseObjects(pCTX) { + adapter.log.debug(`initBaseObjects - ${pCTX.name}`); // await initDeviceObject(pCTX.id, pCTX.name); // .* folder - await initFolderObject(pCTX.id+'.info', ''); + await initFolderObject(`${pCTX.id}.info`, ''); // if ( pCTX.chkCpu ) { await initFolderObject(pCTX.id+'.cpu', ''); } // if ( pCTX.chkDrives ) { await initFolderObject(pCTX.id+'.drives', ''); } // if ( pCTX.chkMem ) { await initFolderObject(pCTX.id+'.mem', ''); } // .online state - await updateState(pCTX.id + '.online', false, 0, { - name: pCTX.name + ' online', + await updateState(`${pCTX.id}.online`, false, 0, { + name: `${pCTX.name} online`, type: 'boolean', role: 'indicator.reachable', read: true, - write: false + write: false, }); } /** * initAllBaseObjects - create base state objects for all devices * - * @return nothing - * + * @returns nothing */ async function initAllBaseObjects() { adapter.log.debug('initAllBaseObjects - init all base objects for all device'); @@ -343,32 +337,29 @@ async function initAllBaseObjects() { * * NOTE: all parseXxxx fucntions must use identical parameters * - * @param {object} pCTX device context - * @param {object} pJdata json object as returnd by client - * @return nothing - * + * @param pCTX device context + * @param pJdata json object as returnd by client + * @returns nothing */ -async function parseInfo(pCTX, pJdata) -{ - adapter.log.debug('parseInfo - ' + pCTX.id ); +async function parseInfo(pCTX, pJdata) { + adapter.log.debug(`parseInfo - ${pCTX.id}`); // note info folder should already exist - await updateState(pCTX.id+'.info.name', pJdata.name, 0, { + await updateState(`${pCTX.id}.info.name`, pJdata.name, 0, { name: 'client name', type: 'string', role: 'info.name', read: true, - write: false + write: false, }); - await updateState(pCTX.id+'.info.version', pJdata.version, 0, { + await updateState(`${pCTX.id}.info.version`, pJdata.version, 0, { name: 'client version', type: 'string', role: 'info.version', read: true, - write: false + write: false, }); - } /** @@ -376,51 +367,49 @@ async function parseInfo(pCTX, pJdata) * * NOTE: all parseXxxx fucntions must use identical parameters * - * @param {object} pCTX device context - * @param {object} pJdata json object as returnd by client - * @return nothing - * + * @param pCTX device context + * @param pJdata json object as returnd by client + * @returns nothing */ -async function parsePerf( pCTX, pJdata) -{ - adapter.log.debug('parsePerf - ' + pCTX.id ); +async function parsePerf(pCTX, pJdata) { + adapter.log.debug(`parsePerf - ${pCTX.id}`); const command = pJdata['command']; - const baseId = name2id( pCTX.id + '.' + command ); - await initFolderObject (baseId, ''); - await updateState(baseId + '.result', pJdata.result, 0, { + const baseId = name2id(`${pCTX.id}.${command}`); + await initFolderObject(baseId, ''); + await updateState(`${baseId}.result`, pJdata.result, 0, { name: 'result', type: 'number', role: 'value.severity', read: true, write: false, - states: {0: 'ok', 1: 'warning', 2: 'error', 3: 'delayed'} + states: { 0: 'ok', 1: 'warning', 2: 'error', 3: 'delayed' }, }); // TODO: check if more than one line could be returned - await updateState(baseId + '.message', pJdata.lines[0].message, 0, { + await updateState(`${baseId}.message`, pJdata.lines[0].message, 0, { name: 'message', type: 'string', role: 'text', read: true, - write: false + write: false, }); - await initFolderObject (baseId + '.perf', ''); - for(const perfKey in pJdata.lines[0].perf){ - const perfId = name2id (baseId + '.perf' + '.' + perfKey); - await initFolderObject (perfId, ''); - for(const state in pJdata.lines[0].perf[perfKey]){ - const stateId = name2id (baseId + '.perf' + '.' + perfKey + '.' + state); + await initFolderObject(`${baseId}.perf`, ''); + for (const perfKey in pJdata.lines[0].perf) { + const perfId = name2id(`${baseId}.perf` + `.${perfKey}`); + await initFolderObject(perfId, ''); + for (const state in pJdata.lines[0].perf[perfKey]) { + const stateId = name2id(`${baseId}.perf` + `.${perfKey}.${state}`); const stateVal = pJdata.lines[0].perf[perfKey][state]; const isNum = /^\d+(\.\d+)?$/.test(stateVal); await updateState(stateId, stateVal, 0, { name: state, - type: isNum?'number':'string', + type: isNum ? 'number' : 'string', role: 'value', read: true, - write: false + write: false, }); } } @@ -430,45 +419,43 @@ async function parsePerf( pCTX, pJdata) /** * httpsGetAsync - async call to https * - * @param {string} pUrl url to query - * @param {number} pTmo timeout (ms) - * @return {Promise} return info structure - * + * @param pUrl url to query + * @param pTmo timeout (ms) + * @returns return info structure */ -async function httpsGetAsync( pUrl, pTmo ){ - adapter.log.debug('httpsGetAsync - ' + pUrl ); - const ret= {}; - ret.httpCode=0; - ret.errCode=0; - ret.errText=''; - ret.data=''; - - return new Promise((resolve,_reject)=>{ - const options= { +async function httpsGetAsync(pUrl, pTmo) { + adapter.log.debug(`httpsGetAsync - ${pUrl}`); + const ret = {}; + ret.httpCode = 0; + ret.errCode = 0; + ret.errText = ''; + ret.data = ''; + + return new Promise((resolve, _reject) => { + const options = { rejectUnauthorized: false, - timeout: pTmo + timeout: pTmo, }; - const req = https.get( pUrl, options, (res) => { + const req = https.get(pUrl, options, res => { console.log('statusCode:', res.statusCode); - ret.httpCode = res.statusCode||0; + ret.httpCode = res.statusCode || 0; // console.log('headers:', res.headers); - res.on('data', (d) => { + res.on('data', d => { console.log('at on'); console.log(d.toString()); - ret.data=d; + ret.data = d; resolve(ret); }); }); req.on('timeout', () => { - ret.httpCode=408; // + ret.httpCode = 408; // resolve(ret); }); - req.on('error', (e) => { - // @ts-ignore - if ( e && e.code !== 'HPE_INVALID_CONSTANT' ) - { - // @ts-ignore + req.on('error', e => { + // @ts-expect-error code is not a property of e + if (e && e.code !== 'HPE_INVALID_CONSTANT') { + // @ts-expect-error code is not a property of e ret.errCode = e.code; ret.errText = e.toString(); resolve(ret); @@ -480,79 +467,74 @@ async function httpsGetAsync( pUrl, pTmo ){ /** * httpsGetAsync - async call to https * - * @param {object} pCTX device context - * @param {string} pQuery name of query - * @return object data query result (or empty string) + * @param pCTX device context + * @param pQuery name of query + * @returns object data query result (or empty string) * jdata parsed query result * errCode any error occured * errText any error text * httpCode any error occured - * */ -async function executeQuery ( pCTX, pQuery) { - adapter.log.debug('executeQuery - ' + pQuery ); - - const url = pCTX.url + QUERIES [ pQuery ].query; - const ret = await httpsGetAsync( url, pCTX.timeout ); - if ( ret.errCode ) { - adapter.log.debug ('[' + pCTX.name + '] ' + ret.errCode + ' - ' + ret.errText ); - handleOffline(pCTX, ret.errCode + ' - ' + ret.errText); - } - else if (ret.httpCode !== 200) { - adapter.log.debug ('[' + pCTX.name + '] HTTP error [' + ret.httpCode + '] ' + HTTP_CODES[ret.httpCode]); - handleOffline(pCTX, 'HTTP error [' + ret.httpCode + '] ' + HTTP_CODES[ret.httpCode]); +async function executeQuery(pCTX, pQuery) { + adapter.log.debug(`executeQuery - ${pQuery}`); + + const url = pCTX.url + QUERIES[pQuery].query; + const ret = await httpsGetAsync(url, pCTX.timeout); + if (ret.errCode) { + adapter.log.debug(`[${pCTX.name}] ${ret.errCode} - ${ret.errText}`); + handleOffline(pCTX, `${ret.errCode} - ${ret.errText}`); + } else if (ret.httpCode !== 200) { + adapter.log.debug(`[${pCTX.name}] HTTP error [${ret.httpCode}] ${HTTP_CODES[ret.httpCode]}`); + handleOffline(pCTX, `HTTP error [${ret.httpCode}] ${HTTP_CODES[ret.httpCode]}`); } else { - adapter.log.debug ('[' + pCTX.name + '] data retrieved "' + ret.data.toString() + '"'); + adapter.log.debug(`[${pCTX.name}] data retrieved "${ret.data.toString()}"`); handleOnline(pCTX, ''); - ret.jdata = JSON.parse( ret.data ); + ret.jdata = JSON.parse(ret.data); } return ret; } - // #################### scanning functions #################### /** * scanDevice - scan a single device * * @param pCTX context of a single device - * @return nothing - * + * @returns nothing */ -async function scanDevice ( pCTX ){ +async function scanDevice(pCTX) { const name = pCTX.name; - adapter.log.debug('[' + name + '] scanDevice starting'); + adapter.log.debug(`[${name}] scanDevice starting`); - if (pCTX.busy){ - adapter.log.warn('[' + name + '] device is still busy - scan interval should be increased'); + if (pCTX.busy) { + adapter.log.warn(`[${name}] device is still busy - scan interval should be increased`); return; } pCTX.busy = true; - if ( ! pCTX.initialized ) - { - const ret = await executeQuery( pCTX, 'info' ); - if ( ret.jdata ) { - QUERIES['info'].parser( pCTX, ret.jdata ); - adapter.log.info('[' + name + '] device connected, client ' + ret.jdata.name + ' / ' + ret.jdata.version); + if (!pCTX.initialized) { + const ret = await executeQuery(pCTX, 'info'); + if (ret.jdata) { + QUERIES['info'].parser(pCTX, ret.jdata); + adapter.log.info(`[${name}] device connected, client ${ret.jdata.name} / ${ret.jdata.version}`); pCTX.initialized = true; } } - for (let ii = 0; pCTX.initialized && (ii < pCTX.queries.length); ii++) { + for (let ii = 0; pCTX.initialized && ii < pCTX.queries.length; ii++) { const query = pCTX.queries[ii]; - adapter.log.debug('[' + name + '] processing query [' + ii + '] ' + query ); - const ret = await executeQuery( pCTX, query ); - if (! ret.jdata) { - adapter.log.debug('[' + name + '] processing query index [' + ii + '] aborted' ); + adapter.log.debug(`[${name}] processing query [${ii}] ${query}`); + const ret = await executeQuery(pCTX, query); + if (!ret.jdata) { + adapter.log.debug(`[${name}] processing query index [${ii}] aborted`); break; } - QUERIES[query].parser( pCTX, ret.jdata ); - adapter.log.debug('[' + name + '] processing query index [' + ii + '] completed' ); + QUERIES[query].parser(pCTX, ret.jdata); + adapter.log.debug(`[${name}] processing query index [${ii}] completed`); } pCTX.busy = false; - adapter.log.debug('[' + name + '] scanDevice completed'); + adapter.log.debug(`[${name}] scanDevice completed`); } // #################### initialization functions #################### @@ -560,8 +542,7 @@ async function scanDevice ( pCTX ){ /** * startReaderThreads - start a reader thread per device * - * @return nothing - * + * @returns nothing */ function startReaderThreads() { adapter.log.debug('startReaderThreads - starting reader threads'); @@ -576,8 +557,7 @@ function startReaderThreads() { /** * setupContices - setup contices for worker threads * - * @return nothing - * + * @returns nothing */ function setupContices() { adapter.log.debug('setupContices - initializing contices'); @@ -589,40 +569,46 @@ function setupContices() { continue; } - adapter.log.debug('adding device "' + dev.devIpAddr + '" (' + dev.devName + ')' ); - adapter.log.debug('timing parameter: timeout ' + dev.devTimeout + 's , polling ' + dev.devPollIntvl + 's'); + adapter.log.debug(`adding device "${dev.devIpAddr}" (${dev.devName})`); + adapter.log.debug(`timing parameter: timeout ${dev.devTimeout}s , polling ${dev.devPollIntvl}s`); // TODO: ipV6 support - const tmp = (dev.devIpAddr||'').trim ().split(':'); + const tmp = (dev.devIpAddr || '').trim().split(':'); const ipAddr = tmp[0]; const ipPort = tmp[1] || 8443; CTXs[jj] = {}; - CTXs[jj].name = (dev.devName||'').trim(); + CTXs[jj].name = (dev.devName || '').trim(); CTXs[jj].id = name2id(CTXs[jj].name); CTXs[jj].ipAddr = ipAddr; CTXs[jj].ipPort = ipPort; - CTXs[jj].user = (dev.devUser||'').trim(); - CTXs[jj].pwd = (dev.devPwd||'').trim(); - CTXs[jj].timeout = (dev.devTimeout||1) * 1000; //s -> ms must be less than 0x7fffffff - CTXs[jj].pollIntvl = (dev.devPollIntvl||1) * 1000; //s -> ms must be less than 0x7fffffff + CTXs[jj].user = (dev.devUser || '').trim(); + CTXs[jj].pwd = (dev.devPwd || '').trim(); + CTXs[jj].timeout = (dev.devTimeout || 1) * 1000; //s -> ms must be less than 0x7fffffff + CTXs[jj].pollIntvl = (dev.devPollIntvl || 1) * 1000; //s -> ms must be less than 0x7fffffff - CTXs[jj].chkCpu = (dev.devChkCpu || false ); - CTXs[jj].chkMem = (dev.devChkMem || false ); - CTXs[jj].chkDrives = (dev.devChkDrives || false ); + CTXs[jj].chkCpu = dev.devChkCpu || false; + CTXs[jj].chkMem = dev.devChkMem || false; + CTXs[jj].chkDrives = dev.devChkDrives || false; - CTXs[jj].url = 'https://' + CTXs[jj].user + ':' + CTXs[jj].pwd + '@'+CTXs[jj].ipAddr + ':' + CTXs[jj].ipPort; + CTXs[jj].url = `https://${CTXs[jj].user}:${CTXs[jj].pwd}@${CTXs[jj].ipAddr}:${CTXs[jj].ipPort}`; CTXs[jj].busy = false; CTXs[jj].initialized = false; CTXs[jj].offline = false; CTXs[jj].queries = []; - if ( CTXs[jj].chkCpu ) { CTXs[jj].queries.push('check_cpu'); } - if ( CTXs[jj].chkDrives ) { CTXs[jj].queries.push('check_drivesize'); } - if ( CTXs[jj].chkMem ) { CTXs[jj].queries.push('check_memory'); } + if (CTXs[jj].chkCpu) { + CTXs[jj].queries.push('check_cpu'); + } + if (CTXs[jj].chkDrives) { + CTXs[jj].queries.push('check_drivesize'); + } + if (CTXs[jj].chkMem) { + CTXs[jj].queries.push('check_memory'); + } - CTXs[jj].pollTimer = null; // poll intervall timer + CTXs[jj].pollTimer = null; // poll intervall timer jj++; } @@ -631,8 +617,7 @@ function setupContices() { /** * validateConfig - scan and validate config data * - * @return - * + * @returns */ function validateConfig() { let ok = true; @@ -651,21 +636,29 @@ function validateConfig() { for (let ii = 0; ii < adapter.config.devs.length; ii++) { const dev = adapter.config.devs[ii]; - if (!dev.devAct) continue; + if (!dev.devAct) { + continue; + } - dev.devName = (dev.devName||'').trim(); - dev.devIpAddr = (dev.devIpAddr||'').trim(); + dev.devName = (dev.devName || '').trim(); + dev.devIpAddr = (dev.devIpAddr || '').trim(); if (dev.devName.endsWith('.')) { - adapter.log.error('devicename "' + dev.devName + '"is invalid. Name must not end with ".". Please correct configuration.'); + adapter.log.error( + `devicename "${dev.devName}"is invalid. Name must not end with ".". Please correct configuration.`, + ); ok = false; } if (dev.devName.includes('..')) { - adapter.log.error('devicename "' + dev.devName + '"is invalid. Name must not include consecutive dots. Please correct configuration.'); + adapter.log.error( + `devicename "${dev.devName}"is invalid. Name must not include consecutive dots. Please correct configuration.`, + ); ok = false; } - if ( chkDevNames[dev.devName] ) { - adapter.log.error('devicenames must be unique, "' + dev.devName + '"use more than once, please correct configuration.'); + if (chkDevNames[dev.devName]) { + adapter.log.error( + `devicenames must be unique, "${dev.devName}"use more than once, please correct configuration.`, + ); ok = false; } chkDevNames[dev.devName] = 'x'; @@ -676,45 +669,61 @@ function validateConfig() { } else if (/^[a-zA-Z0-9.-]+(:\d+)?$/.test(dev.devIpAddr)) { /* domain name */ } else { - adapter.log.error('ip address "' + dev.devIpAddr + '" has invalid format, please correct configuration.'); + adapter.log.error(`ip address "${dev.devIpAddr}" has invalid format, please correct configuration.`); ok = false; } if (!/^\d+$/.test(dev.devTimeout)) { - adapter.log.error('device "' + dev.devName + '" - timeout (' + dev.devTimeout + ') must be numeric, please correct configuration.'); + adapter.log.error( + `device "${dev.devName}" - timeout (${dev.devTimeout}) must be numeric, please correct configuration.`, + ); ok = false; } dev.devTimeout = parseInt(dev.devTimeout, 10) || 5; - if (dev.devTimeout > 600) { // must be less than 0x7fffffff / 1000 - adapter.log.warn('device "' + dev.devName + '" - device timeout (' + dev.devTimeout + ') must be less than 600 seconds, please correct configuration.'); + if (dev.devTimeout > 600) { + // must be less than 0x7fffffff / 1000 + adapter.log.warn( + `device "${dev.devName}" - device timeout (${dev.devTimeout}) must be less than 600 seconds, please correct configuration.`, + ); dev.devTimeout = 600; - adapter.log.warn('device "' + dev.devName + '" - device timeout set to 600 seconds.'); + adapter.log.warn(`device "${dev.devName}" - device timeout set to 600 seconds.`); } if (dev.devTimeout < 1) { - adapter.log.warn('device "' + dev.devName + '" - device timeout (' + dev.devTimeout + ') must be at least 1 second, please correct configuration.'); + adapter.log.warn( + `device "${dev.devName}" - device timeout (${dev.devTimeout}) must be at least 1 second, please correct configuration.`, + ); dev.devTimeout = 1; - adapter.log.warn('device "' + dev.devName + '" - device timeout set to 1 second.'); + adapter.log.warn(`device "${dev.devName}" - device timeout set to 1 second.`); } if (!/^\d+$/.test(dev.devPollIntvl)) { - adapter.log.error('device "' + dev.devName + '" - poll intervall (' + dev.devPollIntvl + ') must be numeric, please correct configuration.'); + adapter.log.error( + `device "${dev.devName}" - poll intervall (${dev.devPollIntvl}) must be numeric, please correct configuration.`, + ); ok = false; } dev.devPollIntvl = parseInt(dev.devPollIntvl, 10) || 30; - if (dev.devPollIntvl > 3600) { // must be less than 0x7fffffff / 1000 - adapter.log.warn('device "' + dev.devName + '" - poll intervall (' + dev.devPollIntvl + ') must be less than 3600 seconds, please correct configuration.'); + if (dev.devPollIntvl > 3600) { + // must be less than 0x7fffffff / 1000 + adapter.log.warn( + `device "${dev.devName}" - poll intervall (${dev.devPollIntvl}) must be less than 3600 seconds, please correct configuration.`, + ); dev.devPollIntvl = 3600; - adapter.log.warn('device "' + dev.devName + '" - poll intervall set to 3600 seconds.'); + adapter.log.warn(`device "${dev.devName}" - poll intervall set to 3600 seconds.`); } if (dev.devPollIntvl < 5) { - adapter.log.warn('device "' + dev.devName + '" - poll intervall (' + dev.devPollIntvl + ') must be at least 5 seconds, please correct configuration.'); + adapter.log.warn( + `device "${dev.devName}" - poll intervall (${dev.devPollIntvl}) must be at least 5 seconds, please correct configuration.`, + ); dev.devPollIntvl = 5; - adapter.log.warn('device "' + dev.devName + '" - poll intervall set to 5 seconds.'); + adapter.log.warn(`device "${dev.devName}" - poll intervall set to 5 seconds.`); } if (dev.devPollIntvl <= dev.devTimeout) { - adapter.log.warn('device "' + dev.devName + '" - poll intervall (' + dev.devPollIntvl + ') must be larger than device timeout (' + dev.devTimeout + '), please correct configuration.'); + adapter.log.warn( + `device "${dev.devName}" - poll intervall (${dev.devPollIntvl}) must be larger than device timeout (${dev.devTimeout}), please correct configuration.`, + ); dev.devPollIntvl = dev.devTimeout + 1; - adapter.log.warn('device "' + dev.devName + '" - poll intervall set to ' + dev.devPollIntvl + ' seconds.'); + adapter.log.warn(`device "${dev.devName}" - poll intervall set to ${dev.devPollIntvl} seconds.`); } } @@ -731,56 +740,56 @@ function validateConfig() { /** * Starts the adapter instance - * @param {Partial} [options] + * + * @param [options] */ function startAdapter(options) { // Create the adapter and define its methods - return adapter = utils.adapter(Object.assign({}, options, { - name: 'nsclient', - - // ready callback is called when databases are connected and adapter received configuration. - ready: onReady, // Main method defined below for readability - - // unload callback is called when adapter shuts down - callback has to be called under any circumstances! - unload: onUnload, - - // If you need to react to object changes, uncomment the following method. - // You also need to subscribe to the objects with `adapter.subscribeObjects`, similar to `adapter.subscribeStates`. - // objectChange: (id, obj) => { - // if (obj) { - // // The object was changed - // adapter.log.info(`object ${id} changed: ${JSON.stringify(obj)}`); - // } else { - // // The object was deleted - // adapter.log.info(`object ${id} deleted`); - // } - // }, - - // is called if a subscribed state changes - //stateChange: (id, state) => { - // if (state) { - // // The state was changed - // adapter.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`); - // } else { - // // The state was deleted - // adapter.log.info(`state ${id} deleted`); - // } - //}, - - // message callback is called whenever some message was sent to this instance over message box. - // message: onMessage, - - })); + return (adapter = utils.adapter( + Object.assign({}, options, { + name: 'nsclient', + + // ready callback is called when databases are connected and adapter received configuration. + ready: onReady, // Main method defined below for readability + + // unload callback is called when adapter shuts down - callback has to be called under any circumstances! + unload: onUnload, + + // If you need to react to object changes, uncomment the following method. + // You also need to subscribe to the objects with `adapter.subscribeObjects`, similar to `adapter.subscribeStates`. + // objectChange: (id, obj) => { + // if (obj) { + // // The object was changed + // adapter.log.info(`object ${id} changed: ${JSON.stringify(obj)}`); + // } else { + // // The object was deleted + // adapter.log.info(`object ${id} deleted`); + // } + // }, + + // is called if a subscribed state changes + //stateChange: (id, state) => { + // if (state) { + // // The state was changed + // adapter.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`); + // } else { + // // The state was deleted + // adapter.log.info(`state ${id} deleted`); + // } + //}, + + // message callback is called whenever some message was sent to this instance over message box. + // message: onMessage, + }), + )); } /** * onReady - will be called as soon as adapter is ready * - * @return - * + * @returns */ async function onReady() { - adapter.log.debug('onReady triggered'); // mark adapter as non active @@ -814,14 +823,13 @@ async function onReady() { await adapter.setStateAsync('info.connection', true, true); adapter.log.debug('startup completed'); - } /** * onMessage - called when adapter receives a message * * @param pObj object message object - * @return + * @returns * *** reserved for future use *** */ //function onMessage (pObj) { @@ -840,8 +848,7 @@ async function onReady() { * onUnload - called when adapter shuts down * * @param callback callback function - * @return - * + * @returns */ function onUnload(callback) { adapter.log.debug('onUnload triggered'); @@ -851,14 +858,18 @@ function onUnload(callback) { // (re)set device online status try { - adapter.setState(CTX.id + '.online', false, true); - } catch (e) { console.log(e); } + adapter.setState(`${CTX.id}.online`, false, true); + } catch (e) { + console.log(e); + } // close session if one exists if (CTX.pollTimer) { try { adapter.clearInterval(CTX.pollTimer); - } catch (e) { console.log(e); } + } catch (e) { + console.log(e); + } CTX.pollTimer = null; } } @@ -880,7 +891,7 @@ function onUnload(callback) { /* ***** here we start ***** */ -console.log('DEBUG : nsclient++ adapter initializing (' + process.argv + ') ...'); //logger not yet initialized +console.log(`DEBUG : nsclient++ adapter initializing (${process.argv}) ...`); //logger not yet initialized // if (process.argv) { // for (let a = 1; a < process.argv.length; a++) { @@ -902,4 +913,4 @@ if (require.main !== module) { } else { // otherwise start the instance directly startAdapter(); -} \ No newline at end of file +}