From 547b1bccb6d8a304720c765e817377d7fe87a3aa Mon Sep 17 00:00:00 2001 From: pdbjjens Date: Wed, 29 Jan 2025 18:13:06 +0100 Subject: [PATCH] Fix ESLint errors --- main.js | 372 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 217 insertions(+), 155 deletions(-) diff --git a/main.js b/main.js index 6a5e3f2..78d9f5c 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,5 @@ -// @ts-nocheck -/* jshint -W097 */// jshint strict:false +// +/* jshint -W097 */ // jshint strict:false /*jslint node: true */ 'use strict'; @@ -19,14 +19,15 @@ const _ = require('lodash'); const ctx = { headerSet: new vbus.HeaderSet(), hsc: new vbus.HeaderSetConsolidator(), - connection: new vbus.Connection() + connection: new vbus.Connection(), }; - -const adapterName = require('./package.json').name.split('.').pop(); -const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; +//const adapterName = require('./package.json').name.split('.').pop(); +const ipformat = + /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; const fqdnformat = /^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/; -const serialformat = /^(COM|com)[0-9][0-9]?$|^\/dev\/tty.*$|^\/dev\/serial\/by-id\/usb-.*$|^\/dev\/serial\/by-path\/platform-.*$/i; +const serialformat = + /^(COM|com)[0-9][0-9]?$|^\/dev\/tty.*$|^\/dev\/serial\/by-id\/usb-.*$|^\/dev\/serial\/by-path\/platform-.*$/i; const vbusioformat = /vbus.io|vbus.net$/; // Currently unused: const urlformat = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/; @@ -36,7 +37,11 @@ class MyVbus extends utils.Adapter { * @param {Partial} [options={}] */ constructor(options) { - super({...options, name: adapterName}); + // @ts-expect-error won`t work otherwise + super({ + ...options, + name: 'myvbus', + }); this.on('ready', this.onReady.bind(this)); //this.on('objectChange', this.onObjectChange.bind(this)); @@ -46,28 +51,28 @@ class MyVbus extends utils.Adapter { } async main() { - let relayActive = 'Relay X active'; - let language = 'en'; + let language = 'en'; // Get system language and set it for this adapter - await this.getForeignObjectAsync('system.config') - .then(sysConf => { - if (sysConf && (sysConf.common.language === 'de' || sysConf.common.language === 'fr') ) { - // switch language to a language supported by Resol-Lib (de, fr), or default to english - language = sysConf.common.language; - } - // Set translation for relay active state - switch (language) { - case 'de': relayActive = 'Relais X aktiv'; - break; - case 'fr': relayActive = 'Relais X actif'; - break; - } - }); + await this.getForeignObjectAsync('system.config').then(sysConf => { + if (sysConf && (sysConf.common.language === 'de' || sysConf.common.language === 'fr')) { + // switch language to a language supported by Resol-Lib (de, fr), or default to english + language = sysConf.common.language; + } + // Set translation for relay active state + switch (language) { + case 'de': + relayActive = 'Relais X aktiv'; + break; + case 'fr': + relayActive = 'Relais X actif'; + break; + } + }); const spec = new vbus.Specification({ - language: language + language: language, }); // Set up connection depending on connection device @@ -76,46 +81,62 @@ class MyVbus extends utils.Adapter { ctx.connection = new vbus.TcpConnection({ host: this.config.connectionIdentifier, port: this.config.connectionPort, - password: this.config.vbusPassword + password: this.config.vbusPassword, }); - this.log.info('TCP connection via LAN to [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected'); + this.log.info( + `TCP connection via LAN to [${this.config.connectionIdentifier}:${ + this.config.connectionPort + }] selected`, + ); break; - + case 'serial': ctx.connection = new vbus.SerialConnection({ - path: this.config.connectionIdentifier + path: this.config.connectionIdentifier, }); - this.log.info('Serial connection at [' + this.config.connectionIdentifier + '] selected'); + this.log.info(`Serial connection at [${this.config.connectionIdentifier}] selected`); break; - + case 'langw': ctx.connection = new vbus.TcpConnection({ host: this.config.connectionIdentifier, port: this.config.connectionPort, - rawVBusDataOnly: true + rawVBusDataOnly: true, }); - this.log.info('TCP connection via LAN-gw to [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected'); + this.log.info( + `TCP connection via LAN-gw to [${this.config.connectionIdentifier}:${ + this.config.connectionPort + }] selected`, + ); break; - + case 'dl2': if (this.config.connectionIdentifier.match(vbusioformat)) { ctx.connection = new vbus.TcpConnection({ host: this.config.connectionIdentifier, port: this.config.connectionPort, password: this.config.vbusPassword, - viaTag: this.config.vbusViaTag + viaTag: this.config.vbusViaTag, }); - this.log.info('VBus.net connection to KM2/DL2 via [' + this.config.connectionIdentifier + ' via ' + this.config.vbusViaTag + '] selected'); + this.log.info( + `VBus.net connection to KM2/DL2 via [${this.config.connectionIdentifier} via ${ + this.config.vbusViaTag + }] selected`, + ); } else { ctx.connection = new vbus.TcpConnection({ host: this.config.connectionIdentifier, port: this.config.connectionPort, - password: this.config.vbusPassword + password: this.config.vbusPassword, }); - this.log.info('TCP connection to KM2/DL2 on [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected'); + this.log.info( + `TCP connection to KM2/DL2 on [${this.config.connectionIdentifier}:${ + this.config.connectionPort + }] selected`, + ); } break; - + case 'dl3': if (this.config.connectionIdentifier.match(vbusioformat)) { ctx.connection = new vbus.TcpConnection({ @@ -123,30 +144,37 @@ class MyVbus extends utils.Adapter { port: this.config.connectionPort, password: this.config.vbusPassword, channel: this.config.vbusChannel, - viaTag: this.config.vbusViaTag + viaTag: this.config.vbusViaTag, }); - this.log.info('VBus.net connection to DL3 channel ' + this.config.vbusChannel + ' via [' + this.config.connectionIdentifier + ' via ' + this.config.vbusViaTag + '] selected'); + this.log.info( + `VBus.net connection to DL3 channel ${this.config.vbusChannel} via [${ + this.config.connectionIdentifier + } via ${this.config.vbusViaTag}] selected`, + ); } else { ctx.connection = new vbus.TcpConnection({ host: this.config.connectionIdentifier, port: this.config.connectionPort, password: this.config.vbusPassword, - channel: this.config.vbusChannel + channel: this.config.vbusChannel, }); - this.log.info('TCP connection to DL3 channel ' + this.config.vbusChannel + ' on [' + this.config.connectionIdentifier + ':' + this.config.connectionPort + '] selected'); + this.log.info( + `TCP connection to DL3 channel ${this.config.vbusChannel} on [${ + this.config.connectionIdentifier + }:${this.config.connectionPort}] selected`, + ); } } - + // Connection state handler - ctx.connection.on('connectionState', (connectionState) => { - + ctx.connection.on('connectionState', connectionState => { switch (connectionState) { case 'CONNECTED': this.log.info('Connection established'); this.setStateAsync('info.connection', true, true); break; - case 'INTERRUPTED': - this.log.debug('Connection interrupted'); + case 'INTERRUPTED': + this.log.debug('Connection interrupted'); this.setStateAsync('info.connection', false, true); break; case 'RECONNECTING': @@ -154,16 +182,16 @@ class MyVbus extends utils.Adapter { this.setStateAsync('info.connection', false, true); break; default: - this.log.debug('Connection state changed to ' + connectionState); + this.log.debug(`Connection state changed to ${connectionState}`); } }); - + ctx.headerSet = new vbus.HeaderSet(); let hasSettled = false; let settledCountdown = 0; // Packet handler - ctx.connection.on('packet', (packet) => { + ctx.connection.on('packet', packet => { if (!hasSettled) { const headerCountBefore = ctx.headerSet.getHeaderCount(); ctx.headerSet.addHeader(packet); @@ -186,7 +214,7 @@ class MyVbus extends utils.Adapter { ctx.hsc = new vbus.HeaderSetConsolidator({ interval: this.config.vbusInterval * 1000, - timeToLive: (this.config.vbusInterval * 1000) + 1000 + timeToLive: this.config.vbusInterval * 1000 + 1000, }); // HeaderSetConsolidator handler - creates object tree and updates values in preset interval @@ -210,54 +238,72 @@ class MyVbus extends utils.Adapter { }); //this.log.debug('received data: ' + JSON.stringify(data)); - if (data[1]){ + if (data[1]) { // create device - this.createOrExtendObject(data[1].deviceId, { - type: 'device', - common: { - name: data[1].deviceName, - type: 'string' + this.createOrExtendObject( + data[1].deviceId, + { + type: 'device', + common: { + name: data[1].deviceName, + type: 'string', + }, + native: {}, }, - native: {} - }, ''); + '', + ); // create channel - this.createOrExtendObject(data[1].deviceId + '.' + data[1].addressId, { - type: 'channel', - common: { - name: data[1].deviceId + '.' + data[1].addressId, - type: 'string' + this.createOrExtendObject( + `${data[1].deviceId}.${data[1].addressId}`, + { + type: 'channel', + common: { + name: `${data[1].deviceId}.${data[1].addressId}`, + type: 'string', + }, + native: {}, }, - native: {} - }, ''); + '', + ); } // iterate over all data to create datapoints - _.forEach(data, (item) => { + _.forEach(data, item => { //this.log.debug('received item-data: ' + JSON.stringify(item)); const deviceId = item.deviceId.replace(/_/g, ''); - const channelId = deviceId + '.' + item.addressId; - const objectId = channelId + '.' + item.id.replace(/_/g, ''); - const isBitField = ((item.parts.length === 1) && (item.parts[0].mask !== 0xFF)); - const isTimeField = ((item.rootTypeId === 'Time') || (item.rootTypeId === 'Weektime') || (item.rootTypeId === 'DateTime')); + const channelId = `${deviceId}.${item.addressId}`; + const objectId = `${channelId}.${item.id.replace(/_/g, '')}`; + const isBitField = item.parts.length === 1 && item.parts[0].mask !== 0xff; + const isTimeField = + item.rootTypeId === 'Time' || item.rootTypeId === 'Weektime' || item.rootTypeId === 'DateTime'; const common = { name: item.name, type: 'number', unit: item.unitText, read: true, - write: false + write: false, }; let value; - if ((item.rawValue === undefined) || (item.rawValue === null)) { + if (item.rawValue === undefined || item.rawValue === null) { value = 0; } else if (item.rootTypeId === 'Number') { value = +item.rawValue.toFixed(item.precision); } else if (item.rootTypeId === 'Time') { - value = spec.i18n.moment(item.rawValue * 60000).utc().format('HH:mm'); + value = spec.i18n + .moment(item.rawValue * 60000) + .utc() + .format('HH:mm'); } else if (item.rootTypeId === 'Weektime') { - value = spec.i18n.moment((item.rawValue + 5760) * 60000).utc().format('dd,HH:mm'); + value = spec.i18n + .moment((item.rawValue + 5760) * 60000) + .utc() + .format('dd,HH:mm'); } else if (item.rootTypeId === 'DateTime') { - value = spec.i18n.moment((item.rawValue + 978307200) * 1000).utc().format('L HH:mm:ss'); + value = spec.i18n + .moment((item.rawValue + 978307200) * 1000) + .utc() + .format('L HH:mm:ss'); } switch (item.unitId) { @@ -271,17 +317,24 @@ class MyVbus extends utils.Adapter { common.max = 100; common.role = 'level.volume'; // create Relay X active state (as far as we know these are the only percent-unit states ) - this.createOrExtendObject(objectId + '_1', { - type: 'state', - common: { - name: relayActive.replace('X', item.name.substr(item.name.length-2).replace(' ', '')), - type: 'boolean', - role: 'indicator.activity', - unit: '', - read: true, - write: false - } - }, (value > 0)); + this.createOrExtendObject( + `${objectId}_1`, + { + type: 'state', + common: { + name: relayActive.replace( + 'X', + item.name.substr(item.name.length - 2).replace(' ', ''), + ), + type: 'boolean', + role: 'indicator.activity', + unit: '', + read: true, + write: false, + }, + }, + value > 0, + ); break; case 'Hours': common.role = 'value'; @@ -300,29 +353,28 @@ class MyVbus extends utils.Adapter { } else { common.role = 'indicator.maintenance.alarm'; common.type = 'boolean'; - value = (value === 1); + value = value === 1; } break; default: common.role = 'value'; break; } - this.createOrExtendObject(objectId, {type: 'state', common}, value); + this.createOrExtendObject(objectId, { type: 'state', common }, value); }); }); - // Establish connection + // Establish connection this.log.info('Wait for Connection...'); await ctx.connection.connect(); ctx.hsc.startTimer(); } - + /* - * @param {object} config Current active config for adapter - * @returns {string} Result of the check - * */ + * @param {object} config Current active config for adapter + * @returns {string} Result of the check + * */ async configIsValid(config) { - // Log the current config this.log.debug(`Connection Type: ${this.config.connectionDevice}`); this.log.debug(`Connection Identifier: ${this.config.connectionIdentifier}`); @@ -331,71 +383,75 @@ class MyVbus extends utils.Adapter { this.log.debug(`VBus Channel: ${this.config.vbusChannel}`); this.log.debug(`VBus Via Tag: ${this.config.vbusViaTag}`); this.log.debug(`VBus Interval: ${this.config.vbusInterval}`); - - return new Promise( - function (resolve, reject) { - // some helper functions - function testSerialformat(config) { - if (!config.connectionIdentifier.match(serialformat)) { - reject('Serialformat is invalid! Please fix.'); - } - } - - function testIP_and_FQDN_Format(config) { - if (!config.connectionIdentifier.match(ipformat) && !config.connectionIdentifier.match(fqdnformat)) { - reject('Connection ID [' + config.connectionIdentifier + '] is neither a valid IP-Format nor a fully qualified domain name (FQDN)!'); - } + + return new Promise(function (resolve, reject) { + // some helper functions + function testSerialformat(config) { + if (!config.connectionIdentifier.match(serialformat)) { + reject('Serialformat is invalid! Please fix.'); } + } - function testPassword(config) { - if (!config.vbusPassword || '' === config.vbusPassword) { - reject('Password is missing!'); - } + function testIP_and_FQDN_Format(config) { + if (!config.connectionIdentifier.match(ipformat) && !config.connectionIdentifier.match(fqdnformat)) { + reject( + `Connection ID [${ + config.connectionIdentifier + }] is neither a valid IP-Format nor a fully qualified domain name (FQDN)!`, + ); } + } - function testPort(config) { - if ('' === config.connectionPort || 0 === config.connectionPort) { - reject('Invalid connection port! Should be > 0.'); - } + function testPassword(config) { + if (!config.vbusPassword || '' === config.vbusPassword) { + reject('Password is missing!'); } + } - function testChannel(config) { - if (config.vbusChannel < 1 || config.vbusChannel >= 7) { - reject('Invalid DL3 channel! Should be between 1 and 6.'); - } + function testPort(config) { + if ('' === config.connectionPort || 0 === config.connectionPort) { + reject('Invalid connection port! Should be > 0.'); } + } - // switch connectionDevice seleted by User - if (config.connectionDevice === 'serial') { - testSerialformat(config); - resolve('Config valid for USB/Serial.'); - } else if (config.connectionDevice === 'lan') { - testIP_and_FQDN_Format(config); - testPassword(config); - testPort(config); - resolve('Config valid for LAN.'); - } else if (config.connectionDevice === 'langw') { - testIP_and_FQDN_Format(config); - testPort(config); - resolve('Config valid for Serial-to-LAN-Gateway.'); - } else if (config.connectionDevice === 'dl2') { - testIP_and_FQDN_Format(config); - testPort(config); - testPassword(config); - resolve('Config valid for KM2/DL2.'); - } else if (config.connectionDevice === 'dl3') { - testIP_and_FQDN_Format(config); - testPort(config); - testPassword(config); - testChannel(config); - resolve('Config valid for DL3/DL2Plus.'); - } else { - reject('Configuration is invalid! Please select a connection device and complete or fix the configuration.'); + function testChannel(config) { + if (config.vbusChannel < 1 || config.vbusChannel >= 7) { + reject('Invalid DL3 channel! Should be between 1 and 6.'); } } - ); + + // switch connectionDevice seleted by User + if (config.connectionDevice === 'serial') { + testSerialformat(config); + resolve('Config valid for USB/Serial.'); + } else if (config.connectionDevice === 'lan') { + testIP_and_FQDN_Format(config); + testPassword(config); + testPort(config); + resolve('Config valid for LAN.'); + } else if (config.connectionDevice === 'langw') { + testIP_and_FQDN_Format(config); + testPort(config); + resolve('Config valid for Serial-to-LAN-Gateway.'); + } else if (config.connectionDevice === 'dl2') { + testIP_and_FQDN_Format(config); + testPort(config); + testPassword(config); + resolve('Config valid for KM2/DL2.'); + } else if (config.connectionDevice === 'dl3') { + testIP_and_FQDN_Format(config); + testPort(config); + testPassword(config); + testChannel(config); + resolve('Config valid for DL3/DL2Plus.'); + } else { + reject( + 'Configuration is invalid! Please select a connection device and complete or fix the configuration.', + ); + } + }); } - + // Is called when databases are connected and adapter received configuration. async onReady() { this.configIsValid(this.config) @@ -416,35 +472,41 @@ class MyVbus extends utils.Adapter { // Create or extend object createOrExtendObject(id, objData, value) { + // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; this.getObject(id, function (err, oldObj) { if (!err && oldObj) { - self.extendObject(id, objData, () => {self.setState(id, value, true);}); + self.extendObject(id, objData, () => { + self.setState(id, value, true); + }); } else { - self.setObjectNotExists(id, objData, () => {self.setState(id, value, true);}); + self.setObjectNotExists(id, objData, () => { + self.setState(id, value, true); + }); } }); } - // Exit adapter + // Exit adapter onUnload(callback) { try { ctx.connection.disconnect(); this.log.info('Cleaned up everything...'); callback(); + // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (e) { callback(); } } } -// @ts-ignore parent is a valid property on module +// @ts-expect-error parent is a valid property on module if (module.parent) { // Export the constructor in compact mode /** * @param {Partial} [options={}] */ - module.exports = (options) => new MyVbus(options); + module.exports = options => new MyVbus(options); } else { // otherwise start the instance directly new MyVbus();