From 753cd6b3fb9867a77e095ea772df6962526aac56 Mon Sep 17 00:00:00 2001 From: burgerni10 Date: Wed, 2 Nov 2022 11:47:08 +0100 Subject: [PATCH] fix(south/north): rename init into start and create stop method --- src/engine/health-signal.spec.js | 1 + src/engine/history-query/history-query.js | 10 ++-- src/engine/oibus-engine.js | 11 ++-- src/north/north-amazon-s3/north-amazon-s3.js | 6 +-- .../north-amazon-s3/north-amazon-s3.spec.js | 6 +-- src/north/north-connector.js | 51 +++++++++++-------- src/north/north-connector.spec.js | 22 +++++--- src/north/north-console/north-console.spec.js | 4 +- .../north-csv-to-http.spec.js | 10 ++-- .../north-file-writer.spec.js | 2 +- .../north-influx-db/north-influx-db.spec.js | 10 ++-- .../north-mongo-db/north-mongo-db.spec.js | 14 ++--- src/north/north-mqtt/north-mqtt.js | 6 +-- src/north/north-mqtt/north-mqtt.spec.js | 4 +- .../north-oianalytics.spec.js | 2 +- src/north/north-oiconnect/north-oiconnect.js | 6 +-- .../north-oiconnect/north-oiconnect.spec.js | 2 +- .../north-timescale-db.spec.js | 16 +++--- src/north/north-watsy/north-watsy.js | 6 +-- src/north/north-watsy/north-watsy.spec.js | 2 +- .../deferred-promise.js | 0 .../deferred-promise.spec.js | 0 src/south/south-ads/south-ads.spec.js | 2 +- src/south/south-connector.js | 13 ++++- src/south/south-connector.spec.js | 27 ++++++---- .../south-folder-scanner.spec.js | 2 +- src/south/south-modbus/south-modbus.spec.js | 2 +- src/south/south-mqtt/south-mqtt.js | 6 +-- src/south/south-mqtt/south-mqtt.spec.js | 6 +-- src/south/south-opchda/south-opchda.js | 2 +- src/south/south-opchda/south-opchda.spec.js | 22 ++++---- src/south/south-opcua-da/south-opcua-da.js | 6 +-- .../south-opcua-da/south-opcua-da.spec.js | 6 +-- src/south/south-opcua-ha/south-opcua-ha.js | 6 +-- .../south-opcua-ha/south-opcua-ha.spec.js | 32 ++++++------ src/south/south-rest/south-rest.js | 6 +-- src/south/south-rest/south-rest.spec.js | 24 ++++----- src/south/south-sql/south-sql.js | 6 +-- src/south/south-sql/south-sql.spec.js | 40 +++++++-------- src/south/south-sql/south-sql.test.js | 6 +-- 40 files changed, 218 insertions(+), 187 deletions(-) rename src/{south/south-opchda => service}/deferred-promise.js (100%) rename src/{south/south-opchda => service}/deferred-promise.spec.js (100%) diff --git a/src/engine/health-signal.spec.js b/src/engine/health-signal.spec.js index 2828f86198..83eed4a87f 100644 --- a/src/engine/health-signal.spec.js +++ b/src/engine/health-signal.spec.js @@ -192,6 +192,7 @@ describe('HealthSignal', () => { }) it('should forward health signal', async () => { + utils.createProxyAgent.mockImplementation(() => null) const data = { status: 'status' } healthSignal.http.enabled = true await healthSignal.forwardRequest(data) diff --git a/src/engine/history-query/history-query.js b/src/engine/history-query/history-query.js index 97f59c7bb9..3b0806f2ab 100644 --- a/src/engine/history-query/history-query.js +++ b/src/engine/history-query/history-query.js @@ -64,14 +64,14 @@ class HistoryQuery { this.overwriteConnectorsSettings() - await this.north.init( + await this.north.start( path.resolve(this.cacheFolder, `north-${this.north.id}`), this.engine.oibusName, this.engine.defaultLogParameters, ) await this.north.connect() - await this.south.init( + await this.south.start( path.resolve(this.cacheFolder, `south-${this.south.id}`), this.engine.oibusName, this.engine.defaultLogParameters, @@ -86,7 +86,7 @@ class HistoryQuery { this.setStatus(HistoryQuery.STATUS_RUNNING) this.statusService.updateStatusDataStream({ status: HistoryQuery.STATUS_RUNNING }) - // In the south.init method, queryParts is set for each scanMode to 0 + // In the south.start method, queryParts is set for each scanMode to 0 // Because of scan groups, associated to aggregates, each scan mode must be queried independently // Map each scanMode to a history query and run each query sequentially await Object.keys(this.south.queryParts).reduce((promise, scanMode) => promise.then( @@ -104,11 +104,11 @@ class HistoryQuery { } if (this.south) { this.logger.info(`Stopping South connector "${this.southConfiguration.name}".`) - await this.south.disconnect() + await this.south.stop() } if (this.north) { this.logger.info(`Stopping North connector "${this.northConfiguration.name}".`) - await this.north.disconnect() + await this.north.stop() } this.statusService.stop() } diff --git a/src/engine/oibus-engine.js b/src/engine/oibus-engine.js index 10de2e5b80..d602fd9c8c 100644 --- a/src/engine/oibus-engine.js +++ b/src/engine/oibus-engine.js @@ -185,7 +185,7 @@ class OIBusEngine extends BaseEngine { await Promise.allSettled(this.activeSouths.map((south) => { const initAndConnect = async () => { try { - await south.init(this.cacheFolder, this.oibusName, this.defaultLogParameters) + await south.start(this.cacheFolder, this.oibusName, this.defaultLogParameters) await south.connect() } catch (error) { this.logger.error(error) @@ -201,7 +201,7 @@ class OIBusEngine extends BaseEngine { await Promise.allSettled(this.activeNorths.map((north) => { const initAndConnect = async () => { try { - await north.init(this.cacheFolder, this.oibusName, this.defaultLogParameters) + await north.start(this.cacheFolder, this.oibusName, this.defaultLogParameters) await north.connect() } catch (error) { this.logger.error(error) @@ -269,16 +269,13 @@ class OIBusEngine extends BaseEngine { this.scanLists = {} // Stop South connectors - await Promise.allSettled(this.activeSouths.map((south) => { - this.logger.info(`Stopping South "${south.name}" (${south.id}).`) - return south.disconnect() - })) + await Promise.allSettled(this.activeSouths.map((south) => south.stop())) this.activeSouths = [] // Stop North connectors await Promise.allSettled(this.activeNorths.map((north) => { this.logger.info(`Stopping North "${north.name}" (${north.id}).`) - return north.disconnect() + return north.stop() })) this.activeNorths = [] diff --git a/src/north/north-amazon-s3/north-amazon-s3.js b/src/north/north-amazon-s3/north-amazon-s3.js index d74ff84f83..3cd38a622c 100644 --- a/src/north/north-amazon-s3/north-amazon-s3.js +++ b/src/north/north-amazon-s3/north-amazon-s3.js @@ -50,14 +50,14 @@ class NorthAmazonS3 extends NorthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.s3 = new S3Client({ region: this.region, diff --git a/src/north/north-amazon-s3/north-amazon-s3.spec.js b/src/north/north-amazon-s3/north-amazon-s3.spec.js index d1274bd6a0..46aabb9ff0 100644 --- a/src/north/north-amazon-s3/north-amazon-s3.spec.js +++ b/src/north/north-amazon-s3/north-amazon-s3.spec.js @@ -82,7 +82,7 @@ describe('NorthAmazonS3', () => { subscribedTo: [], } north = new AmazonS3(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { @@ -122,7 +122,7 @@ describe('NorthAmazonS3', () => { NodeHttpHandler.mockReturnValueOnce(expectedAgent) const amazonS3WithProxy = new AmazonS3(amazonS3WithProxyConfig, proxies) - await amazonS3WithProxy.init('baseFolder', 'oibusName', {}) + await amazonS3WithProxy.start('baseFolder', 'oibusName', {}) expect(amazonS3WithProxy.bucket).toEqual(amazonS3WithProxyConfig.settings.bucket) expect(amazonS3WithProxy.folder).toEqual(amazonS3WithProxyConfig.settings.folder) @@ -159,7 +159,7 @@ describe('NorthAmazonS3', () => { NodeHttpHandler.mockReturnValueOnce(expectedAgent) const amazonS3WithProxy = new AmazonS3(amazonS3WithProxyConfig, proxies) - await amazonS3WithProxy.init('baseFolder', 'oibusName', {}) + await amazonS3WithProxy.start('baseFolder', 'oibusName', {}) expect(amazonS3WithProxy.bucket).toEqual(amazonS3WithProxyConfig.settings.bucket) expect(amazonS3WithProxy.folder).toEqual(amazonS3WithProxyConfig.settings.folder) diff --git a/src/north/north-connector.js b/src/north/north-connector.js index 3e7f67f0d5..5572d8a99c 100644 --- a/src/north/north-connector.js +++ b/src/north/north-connector.js @@ -79,13 +79,13 @@ class NorthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { + async start(baseFolder, oibusName, defaultLogParameters) { this.baseFolder = path.resolve(baseFolder, `north-${this.id}`) this.statusService = new StatusService() @@ -129,6 +129,34 @@ class NorthConnector { this.proxyAgent = await this.getProxy(this.proxySettings) } + /** + * Stop services and timer + * @returns {Promise} - The result promise + */ + async stop() { + this.logger.info(`Stopping North "${this.name}" (${this.id}).`) + await this.disconnect() + + this.numberOfSentValues = 0 + this.valuesRetryCount = 0 + this.sendingValuesInProgress = false + this.resendValuesImmediately = false + + this.numberOfSentFiles = 0 + this.filesRetryCount = 0 + this.sendingFilesInProgress = false + this.resendFilesImmediately = false + + if (this.valuesTimeout) { + clearTimeout(this.valuesTimeout) + } + if (this.filesTimeout) { + clearTimeout(this.filesTimeout) + } + this.fileCache.stop() + this.valueCache.stop() + } + /** * Method called by Engine to initialize a North connector. This method can be surcharged in the * North connector implementation to allow connection to a third party application for example. @@ -154,25 +182,6 @@ class NorthConnector { this.connected = false this.statusService.updateStatusDataStream({ 'Connected at': 'Not connected' }) this.logger.info(`North connector "${this.name}" (${this.id}) disconnected.`) - - this.numberOfSentValues = 0 - this.valuesRetryCount = 0 - this.sendingValuesInProgress = false - this.resendValuesImmediately = false - - this.numberOfSentFiles = 0 - this.filesRetryCount = 0 - this.sendingFilesInProgress = false - this.resendFilesImmediately = false - - if (this.valuesTimeout) { - clearTimeout(this.valuesTimeout) - } - if (this.filesTimeout) { - clearTimeout(this.filesTimeout) - } - this.fileCache.stop() - this.valueCache.stop() } /** diff --git a/src/north/north-connector.spec.js b/src/north/north-connector.spec.js index a2f832149f..7b2eca91d7 100644 --- a/src/north/north-connector.spec.js +++ b/src/north/north-connector.spec.js @@ -43,7 +43,7 @@ describe('NorthConnector', () => { }, } north = new NorthConnector(configuration, [{ name: 'proxyTest' }]) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) afterEach(() => { @@ -58,7 +58,7 @@ describe('NorthConnector', () => { north.canHandleFiles = true north.canHandleValues = true - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) expect(north.statusService.updateStatusDataStream).toHaveBeenCalledWith({ 'Number of values sent since OIBus has started': 0, 'Number of files sent since OIBus has started': 0, @@ -78,27 +78,35 @@ describe('NorthConnector', () => { north.resetFilesTimeout = jest.fn() north.caching.sendInterval = 0 - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) expect(north.resetValuesTimeout).not.toHaveBeenCalled() expect(north.resetFilesTimeout).not.toHaveBeenCalled() expect(north.logger.warn).toHaveBeenCalledWith('No send interval. No values or files will be sent.') }) it('should properly disconnect', async () => { - const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout') await north.connect() expect(north.connected).toBeTruthy() await north.disconnect() expect(north.connected).toBeFalsy() expect(north.logger.info).toHaveBeenCalledWith('North connector "north" (id) disconnected.') + }) + + it('should properly stop', async () => { + const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout') + north.disconnect = jest.fn() + + await north.stop() + + expect(north.logger.info).toHaveBeenCalledWith('Stopping North "north" (id).') + expect(north.disconnect).toHaveBeenCalledTimes(1) expect(clearTimeoutSpy).toHaveBeenCalledTimes(2) clearTimeoutSpy.mockClear() - north.valuesTimeout = null north.filesTimeout = null - await north.disconnect() + await north.stop() expect(clearTimeoutSpy).toHaveBeenCalledTimes(0) }) @@ -128,8 +136,8 @@ describe('NorthConnector', () => { it('should get proxy', async () => { utils.createProxyAgent.mockImplementation(() => ({ proxyAgent: 'a field' })) expect(await north.getProxy()).toBeNull() - expect(await north.getProxy('proxyTest')).toEqual({ proxyAgent: 'a field' }) + expect(await north.getProxy('anotherProxy')).toBeNull() }) it('should not retrieve values if already sending it', async () => { diff --git a/src/north/north-console/north-console.spec.js b/src/north/north-console/north-console.spec.js index 96e25874a5..66d5cdd38c 100644 --- a/src/north/north-console/north-console.spec.js +++ b/src/north/north-console/north-console.spec.js @@ -40,7 +40,7 @@ beforeEach(async () => { }, } north = new Console(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) describe('North Console', () => { @@ -51,7 +51,7 @@ describe('North Console', () => { }) it('should properly handle values in non verbose mode', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const values = [ { pointId: 'pointId', diff --git a/src/north/north-csv-to-http/north-csv-to-http.spec.js b/src/north/north-csv-to-http/north-csv-to-http.spec.js index bd430022ed..869bcf1e0d 100644 --- a/src/north/north-csv-to-http/north-csv-to-http.spec.js +++ b/src/north/north-csv-to-http/north-csv-to-http.spec.js @@ -79,14 +79,14 @@ describe('NorthCsvToHttp', () => { }) it('should be properly initialized', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) expect(north.canHandleValues).toBeFalsy() expect(north.canHandleFiles).toBeTruthy() }) it('should properly reject file if type is other than csv', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) await expect(north.handleFile('filePath')).rejects .toThrowError('Invalid file format: .csv file expected. File "filePath" skipped.') @@ -107,7 +107,7 @@ describe('NorthCsvToHttp', () => { ['5', '2020-12-17 05:00'], ]) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) await north.handleFile('csvToHttpTest.csv') @@ -115,7 +115,7 @@ describe('NorthCsvToHttp', () => { }) it('should properly test validity of header', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const jsonObject = {} @@ -140,7 +140,7 @@ describe('NorthCsvToHttp', () => { }) it('should properly send data (body.length <= bodyMaxLength)', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const httpBody = [] for (let i = 0; i < north.bodyMaxLength - 1; i += 1) { diff --git a/src/north/north-file-writer/north-file-writer.spec.js b/src/north/north-file-writer/north-file-writer.spec.js index 40d0271db2..899bed19e3 100644 --- a/src/north/north-file-writer/north-file-writer.spec.js +++ b/src/north/north-file-writer/north-file-writer.spec.js @@ -47,7 +47,7 @@ describe('NorthFileWriter', () => { subscribedTo: [], } north = new FileWriter(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { diff --git a/src/north/north-influx-db/north-influx-db.spec.js b/src/north/north-influx-db/north-influx-db.spec.js index bb4619d68f..761b4bfa48 100644 --- a/src/north/north-influx-db/north-influx-db.spec.js +++ b/src/north/north-influx-db/north-influx-db.spec.js @@ -65,7 +65,7 @@ describe('North InfluxDB', () => { }) it('should call makeRequest and manage error', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) serviceUtils.httpSend.mockImplementation(() => { throw new Error('http error') @@ -81,7 +81,7 @@ describe('North InfluxDB', () => { }) it('should log error when there are not enough groups for placeholders in measurement', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) north.measurement = '%5$s' @@ -91,7 +91,7 @@ describe('North InfluxDB', () => { }) it('should log error when there are not enough groups for placeholders in tags', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) north.tags = 'site=%2$s,unit=%3$s,sensor=%5$s' await north.handleValues(values) @@ -100,7 +100,7 @@ describe('North InfluxDB', () => { }) it('should properly handle values with useDataKeyValue', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const valueWithDataLevel = [{ pointId: 'ANA/BL1RCP05', @@ -183,7 +183,7 @@ describe('North InfluxDB', () => { }) it('should properly retrieve timestamp with timestampPathInDataValue', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) north.timestampPathInDataValue = 'associatedTimestamp.timestamp' north.useDataKeyValue = true diff --git a/src/north/north-mongo-db/north-mongo-db.spec.js b/src/north/north-mongo-db/north-mongo-db.spec.js index 272a533a2b..ab36a53618 100644 --- a/src/north/north-mongo-db/north-mongo-db.spec.js +++ b/src/north/north-mongo-db/north-mongo-db.spec.js @@ -67,7 +67,7 @@ describe('NorthMongoDB', () => { }) it('should properly connect and disconnect', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) expect(north.canHandleValues).toBeTruthy() expect(north.canHandleFiles).toBeFalsy() @@ -100,7 +100,7 @@ describe('NorthMongoDB', () => { }) it('should fail to connect', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(() => { @@ -119,7 +119,7 @@ describe('NorthMongoDB', () => { }) it('should fail to list collections', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), @@ -138,7 +138,7 @@ describe('NorthMongoDB', () => { }) it('should properly handle values', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) north.createCollection = true @@ -188,7 +188,7 @@ describe('NorthMongoDB', () => { }) it('should not create collection if it already exists', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const mongoDatabase = { listCollections: jest.fn(() => ({ toArray: jest.fn(() => ([{ name: 'collection1' }, { name: 'collection2' }])) })), @@ -211,7 +211,7 @@ describe('NorthMongoDB', () => { }) it('should properly manage collection creation errors', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) north.mongoDatabase = { collection: jest.fn(() => ({ @@ -248,7 +248,7 @@ describe('NorthMongoDB', () => { }) it('should properly handle values with index fields and collection values errors', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const insertManyFunction = jest.fn() const mongoDatabase = { diff --git a/src/north/north-mqtt/north-mqtt.js b/src/north/north-mqtt/north-mqtt.js index 96de3b121c..63c9fa5a62 100644 --- a/src/north/north-mqtt/north-mqtt.js +++ b/src/north/north-mqtt/north-mqtt.js @@ -60,14 +60,14 @@ class NorthMQTT extends NorthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.clientId = `${oibusName}-${this.id}` } diff --git a/src/north/north-mqtt/north-mqtt.spec.js b/src/north/north-mqtt/north-mqtt.spec.js index 7cb03e2591..b124e96c2c 100644 --- a/src/north/north-mqtt/north-mqtt.spec.js +++ b/src/north/north-mqtt/north-mqtt.spec.js @@ -59,7 +59,7 @@ describe('NorthMQTT', () => { subscribedTo: [], } north = new MQTT(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) it('should properly connect', async () => { @@ -109,7 +109,7 @@ describe('NorthMQTT', () => { } const mqttNorthCert = new MQTT(testMqttConfigWithFiles, []) - await mqttNorthCert.init('baseFolder', 'oibusName', {}) + await mqttNorthCert.start('baseFolder', 'oibusName', {}) mqttNorthCert.certificate = CertificateService await mqttNorthCert.connect() diff --git a/src/north/north-oianalytics/north-oianalytics.spec.js b/src/north/north-oianalytics/north-oianalytics.spec.js index ee95bc2310..c7c2e2ccbc 100644 --- a/src/north/north-oianalytics/north-oianalytics.spec.js +++ b/src/north/north-oianalytics/north-oianalytics.spec.js @@ -55,7 +55,7 @@ describe('NorthOIAnalytics', () => { subscribedTo: [], } north = new OIAnalytics(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { diff --git a/src/north/north-oiconnect/north-oiconnect.js b/src/north/north-oiconnect/north-oiconnect.js index cd76acfdb9..ee059befa4 100644 --- a/src/north/north-oiconnect/north-oiconnect.js +++ b/src/north/north-oiconnect/north-oiconnect.js @@ -48,14 +48,14 @@ class NorthOIConnect extends NorthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) const name = `${oibusName}:${this.name}` this.valuesUrl = `${this.host}${this.valuesEndpoint}?name=${name}` this.fileUrl = `${this.host}${this.fileEndpoint}?name=${name}` diff --git a/src/north/north-oiconnect/north-oiconnect.spec.js b/src/north/north-oiconnect/north-oiconnect.spec.js index 0448427e13..cd103b2fc5 100644 --- a/src/north/north-oiconnect/north-oiconnect.spec.js +++ b/src/north/north-oiconnect/north-oiconnect.spec.js @@ -53,7 +53,7 @@ describe('NorthOIConnect', () => { subscribedTo: [], } north = new OIConnect(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { diff --git a/src/north/north-timescale-db/north-timescale-db.spec.js b/src/north/north-timescale-db/north-timescale-db.spec.js index 5c15be4f8a..e49d94c295 100644 --- a/src/north/north-timescale-db/north-timescale-db.spec.js +++ b/src/north/north-timescale-db/north-timescale-db.spec.js @@ -71,7 +71,7 @@ describe('North TimescaleDB', () => { end: jest.fn(), } pg.Client.mockReturnValue(client) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) await north.connect() expect(north.canHandleValues).toBeTruthy() @@ -97,7 +97,7 @@ describe('North TimescaleDB', () => { }) it('should properly handle connection errors', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(() => { throw new Error('test') @@ -113,7 +113,7 @@ describe('North TimescaleDB', () => { }) it('should properly handle values with publish error', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), @@ -134,7 +134,7 @@ describe('North TimescaleDB', () => { }) it('should properly handle values with optional fields and table errors', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), query: jest.fn() } pg.Client.mockReturnValue(client) @@ -155,7 +155,7 @@ describe('North TimescaleDB', () => { }) it('should properly handle values with optional fields', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), query: jest.fn() } pg.Client.mockReturnValue(client) @@ -181,7 +181,7 @@ describe('North TimescaleDB', () => { }) it('should properly handle values with only optional fields and timestamp', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), query: jest.fn() } pg.Client.mockReturnValue(client) @@ -207,7 +207,7 @@ describe('North TimescaleDB', () => { }) it('should properly handle values with useDataKeyValue', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), @@ -244,7 +244,7 @@ describe('North TimescaleDB', () => { }) it('should properly retrieve timestamp with timestampPathInDataValue', async () => { - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) const client = { connect: jest.fn(), diff --git a/src/north/north-watsy/north-watsy.js b/src/north/north-watsy/north-watsy.js index fb722e2575..a4c1944267 100644 --- a/src/north/north-watsy/north-watsy.js +++ b/src/north/north-watsy/north-watsy.js @@ -67,14 +67,14 @@ class NorthWATSY extends NorthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.mqttTopic = initMQTTTopic(oibusName, this.host) } diff --git a/src/north/north-watsy/north-watsy.spec.js b/src/north/north-watsy/north-watsy.spec.js index dc0e38d825..cbb9ec7847 100644 --- a/src/north/north-watsy/north-watsy.spec.js +++ b/src/north/north-watsy/north-watsy.spec.js @@ -61,7 +61,7 @@ describe('NorthWATSY', () => { subscribedTo: [], } north = new WATSYConnect(configuration, []) - await north.init('baseFolder', 'oibusName', {}) + await north.start('baseFolder', 'oibusName', {}) }) it('should properly connect', async () => { diff --git a/src/south/south-opchda/deferred-promise.js b/src/service/deferred-promise.js similarity index 100% rename from src/south/south-opchda/deferred-promise.js rename to src/service/deferred-promise.js diff --git a/src/south/south-opchda/deferred-promise.spec.js b/src/service/deferred-promise.spec.js similarity index 100% rename from src/south/south-opchda/deferred-promise.spec.js rename to src/service/deferred-promise.spec.js diff --git a/src/south/south-ads/south-ads.spec.js b/src/south/south-ads/south-ads.spec.js index 8c22ba2f56..ae362dd08b 100644 --- a/src/south/south-ads/south-ads.spec.js +++ b/src/south/south-ads/south-ads.spec.js @@ -776,7 +776,7 @@ describe('South ADS', () => { ], } south = new ADS(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { diff --git a/src/south/south-connector.js b/src/south/south-connector.js index 978f33fbb5..d68bd95c69 100644 --- a/src/south/south-connector.js +++ b/src/south/south-connector.js @@ -87,13 +87,13 @@ class SouthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { + async start(baseFolder, oibusName, defaultLogParameters) { this.baseFolder = path.resolve(baseFolder, `south-${this.id}`) this.statusService = new StatusService() @@ -203,6 +203,15 @@ class SouthConnector { this.logger.info(`South connector "${this.name}" (${this.id}) of type ${this.type} started.`) } + /** + * Stop services and timer + * @returns {Promise} - The result promise + */ + async stop() { + this.logger.info(`Stopping South "${this.name}" (${this.id}).`) + await this.disconnect() + } + /** * Method called by Engine to initialize a South connector. This method can be surcharged in the * South connector implementation to allow connection to a third party application for example. diff --git a/src/south/south-connector.spec.js b/src/south/south-connector.spec.js index 736076f851..85dc6f7f6d 100644 --- a/src/south/south-connector.spec.js +++ b/src/south/south-connector.spec.js @@ -34,7 +34,7 @@ describe('SouthConnector', () => { configuration = { id: 'id', name: 'south', type: 'test', settings: {} } south = new SouthConnector(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized without support, without scan mode', async () => { @@ -61,7 +61,7 @@ describe('SouthConnector', () => { } south.scanMode = 'scanModeTest' - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.lastCompletedAt[south.scanMode]).toEqual(new Date()) expect(south.queryParts[south.scanMode]).toEqual(0) @@ -84,7 +84,7 @@ describe('SouthConnector', () => { ] south.scanGroups = [{ scanMode: 'scanGroupTest' }] - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.lastCompletedAt.scanGroupTest).toEqual(new Date()) expect(south.queryParts.scanGroupTest).toEqual(0) expect(south.ignoredReadsCounters.scanGroupTest).toEqual(0) @@ -102,7 +102,7 @@ describe('SouthConnector', () => { { pointId: 'myPointId3', scanMode: 'listen' }, ] - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.lastCompletedAt.scanGroupTest).toEqual(new Date()) expect(south.queryParts.scanGroupTest).toEqual(0) expect(south.ignoredReadsCounters.scanGroupTest).toEqual(0) @@ -121,7 +121,7 @@ describe('SouthConnector', () => { { pointId: 'myPointId3', scanMode: 'listen' }, ] - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.lastCompletedAt.scanGroupTest).toEqual(new Date(south.startTime)) expect(south.queryParts.scanGroupTest).toEqual(0) expect(south.ignoredReadsCounters.scanGroupTest).toEqual(0) @@ -149,11 +149,18 @@ describe('SouthConnector', () => { expect(south.statusService.updateStatusDataStream).toHaveBeenCalledWith({ 'Connected at': 'Not connected' }) }) + it('should properly stop', async () => { + south.disconnect = jest.fn() + await south.stop() + expect(south.disconnect).toHaveBeenCalledTimes(1) + expect(south.logger.info).toHaveBeenCalledWith('Stopping South "south" (id).') + }) + it('should query a specific interval', async () => { south.scanMode = 'scanModeTest' south.historyQuery = jest.fn() south.southDatabase = {} - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const startTime = new Date('2021-01-01T00:00:00.000Z') const endTime = new Date('2022-01-01T00:00:00.000Z') @@ -189,7 +196,7 @@ describe('SouthConnector', () => { endTime: new Date(), }] utils.generateIntervals.mockReturnValue(mockedIntervals) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.historyQueryHandler('scanModeTest', new Date('2019-01-01T00:00:00.000Z'), new Date()) expect(south.logger.trace).toHaveBeenCalledWith('Take back to interval number 0: \r\n' @@ -234,7 +241,7 @@ describe('SouthConnector', () => { endTime: new Date('2021-01-01T00:00:00.000Z'), }] utils.generateIntervals.mockReturnValue(mockedIntervals) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.historyQueryHandler('scanModeTest', new Date('2019-01-01T00:00:00.000Z'), new Date()) expect(south.querySpecificInterval).toHaveBeenCalledTimes(2) @@ -267,7 +274,7 @@ describe('SouthConnector', () => { endTime: new Date('2020-01-01T00:00:00.000Z'), }] utils.generateIntervals.mockReturnValue(mockedIntervals) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.historyQueryHandler('scanModeTest', new Date('2019-01-01T00:00:00.000Z'), new Date()) expect(south.querySpecificInterval).toHaveBeenCalledTimes(1) @@ -433,7 +440,7 @@ describe('SouthConnector', () => { }) it('should properly add file', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const filePath = 'path' const preserveFiles = true diff --git a/src/south/south-folder-scanner/south-folder-scanner.spec.js b/src/south/south-folder-scanner/south-folder-scanner.spec.js index 5277e9122d..5a66c360cf 100644 --- a/src/south/south-folder-scanner/south-folder-scanner.spec.js +++ b/src/south/south-folder-scanner/south-folder-scanner.spec.js @@ -49,7 +49,7 @@ describe('SouthFolderScanner', () => { scanMode: 'every10Second', } south = new FolderScanner(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) databaseService.getConfig.mockClear() }) diff --git a/src/south/south-modbus/south-modbus.spec.js b/src/south/south-modbus/south-modbus.spec.js index 482470f941..a8783c4ad5 100644 --- a/src/south/south-modbus/south-modbus.spec.js +++ b/src/south/south-modbus/south-modbus.spec.js @@ -109,7 +109,7 @@ describe('SouthModbus', () => { ], } south = new Modbus(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { diff --git a/src/south/south-mqtt/south-mqtt.js b/src/south/south-mqtt/south-mqtt.js index 8dcd33e51a..973b051466 100644 --- a/src/south/south-mqtt/south-mqtt.js +++ b/src/south/south-mqtt/south-mqtt.js @@ -84,14 +84,14 @@ class SouthMQTT extends SouthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.clientId = `${oibusName}-${this.id}` if (!this.timezone || !DateTime.local().setZone(this.timezone).isValid) { diff --git a/src/south/south-mqtt/south-mqtt.spec.js b/src/south/south-mqtt/south-mqtt.spec.js index 315b8bfa76..09a643c7b6 100644 --- a/src/south/south-mqtt/south-mqtt.spec.js +++ b/src/south/south-mqtt/south-mqtt.spec.js @@ -81,7 +81,7 @@ describe('SouthMQTT', () => { ], } south = new MQTT(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized with correct timezone', () => { @@ -107,7 +107,7 @@ describe('SouthMQTT', () => { }, } const mqttInvalidSouth = new MQTT(testMqttConfig, addValues, addFiles) - await mqttInvalidSouth.init('baseFolder', 'oibusName', {}) + await mqttInvalidSouth.start('baseFolder', 'oibusName', {}) expect(mqttInvalidSouth.url).toEqual(testMqttConfig.settings.url) expect(mqttInvalidSouth.qos).toEqual(testMqttConfig.settings.qos) @@ -190,7 +190,7 @@ describe('SouthMQTT', () => { } const mqttSouthWithFiles = new MQTT(testMqttConfigWithFiles, addValues, addFiles) - await mqttSouthWithFiles.init('baseFolder', 'oibusName', {}) + await mqttSouthWithFiles.start('baseFolder', 'oibusName', {}) mqttSouthWithFiles.certificate = CertificateService await mqttSouthWithFiles.connect() diff --git a/src/south/south-opchda/south-opchda.js b/src/south/south-opchda/south-opchda.js index d1435d6b29..53e8630896 100644 --- a/src/south/south-opchda/south-opchda.js +++ b/src/south/south-opchda/south-opchda.js @@ -2,7 +2,7 @@ const { spawn } = require('node:child_process') const SouthConnector = require('../south-connector') const TcpServer = require('./tcp-server') -const DeferredPromise = require('./deferred-promise') +const DeferredPromise = require('../../service/deferred-promise') // Time to wait before closing the connection by timeout and killing the HDA Agent process const DISCONNECTION_TIMEOUT = 10000 diff --git a/src/south/south-opchda/south-opchda.spec.js b/src/south/south-opchda/south-opchda.spec.js index cdea456c39..562f1e5d6d 100644 --- a/src/south/south-opchda/south-opchda.spec.js +++ b/src/south/south-opchda/south-opchda.spec.js @@ -2,7 +2,7 @@ const child = require('node:child_process') const Stream = require('node:stream') const OPCHDA = require('./south-opchda') const tcpServer = require('./tcp-server') -const deferredPromise = require('./deferred-promise') +const deferredPromise = require('../../service/deferred-promise') const mockSpawnChild = new Stream() mockSpawnChild.stdout = new Stream() @@ -11,7 +11,6 @@ mockSpawnChild.kill = jest.fn() jest.mock('node:child_process') jest.mock('./tcp-server') -jest.mock('./deferred-promise') // Mock fs jest.mock('node:fs/promises') @@ -23,6 +22,7 @@ const addFiles = jest.fn() jest.mock('../../service/database.service') jest.mock('../../service/logger/logger.service') jest.mock('../../service/status.service') +jest.mock('../../service/deferred-promise') jest.mock('../../service/encryption.service', () => ({ getInstance: () => ({ decryptText: (password) => password }) })) let configuration = null @@ -84,7 +84,7 @@ describe('South OPCHDA', () => { }], } south = new OPCHDA(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) }) afterAll(() => { @@ -246,7 +246,7 @@ describe('South OPCHDA', () => { }) it('should properly disconnect when connected', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.runTcpServer() south.agentConnected = true @@ -264,7 +264,7 @@ describe('South OPCHDA', () => { }) it('should send init message', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) south.sendTCPMessageToHdaAgent = jest.fn() south.generateTransactionId = jest.fn(() => 1234) @@ -289,7 +289,7 @@ describe('South OPCHDA', () => { }) it('should send read message', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) south.sendTCPMessageToHdaAgent = jest.fn() south.generateTransactionId = jest.fn(() => 1234) @@ -310,7 +310,7 @@ describe('South OPCHDA', () => { }) it('should send stop message', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) south.sendTCPMessageToHdaAgent = jest.fn() south.generateTransactionId = jest.fn(() => 1234) @@ -329,7 +329,7 @@ describe('South OPCHDA', () => { }) it('should send TCP message', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) south.agentConnected = true south.tcpServer = { sendMessage: jest.fn() } @@ -341,7 +341,7 @@ describe('South OPCHDA', () => { it('should not send TCP message and try to reconnect', async () => { const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) south.disconnect = jest.fn() south.connect = jest.fn() @@ -453,7 +453,7 @@ describe('South OPCHDA', () => { }) it('should handle Read message when the scan group is not found', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const receivedMessage = { Reply: 'Read', Content: { Group: 'myScanMode', Points: [{}] } } south.historyRead$ = { reject: jest.fn() } @@ -462,7 +462,7 @@ describe('South OPCHDA', () => { }) it('should handle Read message and cache values', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const receivedMessage = { Reply: 'Read', Content: { diff --git a/src/south/south-opcua-da/south-opcua-da.js b/src/south/south-opcua-da/south-opcua-da.js index f5d1982693..c32e9fdb49 100644 --- a/src/south/south-opcua-da/south-opcua-da.js +++ b/src/south/south-opcua-da/south-opcua-da.js @@ -80,14 +80,14 @@ class SouthOPCUADA extends SouthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.clientName = this.id await initOpcuaCertificateFolders(this.encryptionService.certsFolder) diff --git a/src/south/south-opcua-da/south-opcua-da.spec.js b/src/south/south-opcua-da/south-opcua-da.spec.js index 1192ff89e1..08869c9549 100644 --- a/src/south/south-opcua-da/south-opcua-da.spec.js +++ b/src/south/south-opcua-da/south-opcua-da.spec.js @@ -62,7 +62,7 @@ describe('SouthOPCUADA', () => { }], } south = new OPCUA_DA(configuration, addValues, addFiles) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) }) it('should be properly initialized', () => { @@ -130,7 +130,7 @@ describe('SouthOPCUADA', () => { it('should properly connect to OPCUA server with certificate', async () => { const setTimeoutSpy = jest.spyOn(global, 'setTimeout') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const expectedOptions = { applicationName: 'OIBus', clientName: 'southId', @@ -187,7 +187,7 @@ describe('SouthOPCUADA', () => { } const opcuaSouthTest = new OPCUA_DA(testOpcuaConfig, addValues, addFiles) - await opcuaSouthTest.init('baseFolder', 'oibusName', {}) + await opcuaSouthTest.start('baseFolder', 'oibusName', {}) await opcuaSouthTest.connect() opcuaSouthTest.connected = true opcuaSouthTest.session = { readVariableValue: jest.fn(), close: jest.fn() } diff --git a/src/south/south-opcua-ha/south-opcua-ha.js b/src/south/south-opcua-ha/south-opcua-ha.js index 0b74181d4a..dc2e120939 100644 --- a/src/south/south-opcua-ha/south-opcua-ha.js +++ b/src/south/south-opcua-ha/south-opcua-ha.js @@ -96,14 +96,14 @@ class SouthOPCUAHA extends SouthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.clientName = this.id await initOpcuaCertificateFolders(this.encryptionService.certsFolder) if (!this.clientCertificateManager) { diff --git a/src/south/south-opcua-ha/south-opcua-ha.spec.js b/src/south/south-opcua-ha/south-opcua-ha.spec.js index 23bfcef214..68be5dcdd9 100644 --- a/src/south/south-opcua-ha/south-opcua-ha.spec.js +++ b/src/south/south-opcua-ha/south-opcua-ha.spec.js @@ -82,7 +82,7 @@ describe('SouthOPCUAHA', () => { }) it('should be properly initialized', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.url).toEqual(configuration.settings.url) expect(south.retryInterval).toEqual(configuration.settings.retryInterval) expect(south.maxReadInterval).toEqual(configuration.settings.maxReadInterval) @@ -91,7 +91,7 @@ describe('SouthOPCUAHA', () => { // Test no new certificate manager creation south.clientCertificateManager.state = -1 - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.clientCertificateManager.state).toEqual(-1) }) @@ -129,7 +129,7 @@ describe('SouthOPCUAHA', () => { } const expectedUserIdentity = { type: 0 } - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connectToOpcuaServer() expect(south.connected).toBeTruthy() @@ -156,7 +156,7 @@ describe('SouthOPCUAHA', () => { south.username = 'username' south.password = 'password' - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() delete configuration.settings.username @@ -173,7 +173,7 @@ describe('SouthOPCUAHA', () => { it('should properly connect to OPCUA server with certificate', async () => { const setTimeoutSpy = jest.spyOn(global, 'setTimeout') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const expectedOptions = { applicationName: 'OIBus', clientName: 'southId', @@ -213,7 +213,7 @@ describe('SouthOPCUAHA', () => { throw new Error('connection error') }) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connectToOpcuaServer() expect(south.connected).toBeFalsy() @@ -222,7 +222,7 @@ describe('SouthOPCUAHA', () => { }) it('should properly format and sent values', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) south.addValues = jest.fn() const startTime = new Date('2020-01-01T00:00:00.000Z') @@ -328,7 +328,7 @@ describe('SouthOPCUAHA', () => { }) it('should log a message when zero value is received', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const nodesToRead = [ { pointId: 'point1' }, @@ -341,7 +341,7 @@ describe('SouthOPCUAHA', () => { }) it('should properly read history value with response error', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const isNot = jest.fn(() => true) south.session = { performMessageTransaction: jest.fn(() => ({ @@ -366,7 +366,7 @@ describe('SouthOPCUAHA', () => { }) it('should properly read history value with aggregateFn option', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const isNot = jest.fn(() => true) south.session = { performMessageTransaction: jest.fn(() => ({ @@ -427,7 +427,7 @@ describe('SouthOPCUAHA', () => { }) it('should properly read history value with response success', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const isNot = jest.fn(() => false) south.session = { performMessageTransaction: jest.fn(() => ({ @@ -466,7 +466,7 @@ describe('SouthOPCUAHA', () => { }) it('should properly read history value of many points with response success', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) const isNot = jest.fn(() => true) const badHistoryResult = { historyData: { dataValues: [] }, @@ -523,7 +523,7 @@ describe('SouthOPCUAHA', () => { it('should returns the requested number of node IDs after historyQuery', async () => { databaseService.getConfig.mockReturnValue('2020-02-02T02:02:02.222Z') databaseService.createConfigDatabase.mockReturnValue('configDatabase') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.maxReadInterval = 24 * 60 * 60 * 1000 @@ -600,7 +600,7 @@ describe('SouthOPCUAHA', () => { }) it('should catch historyQuery errors', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.connected = true south.currentlyOnScan[configuration.settings.scanGroups[0].scanMode] = 0 @@ -640,7 +640,7 @@ describe('SouthOPCUAHA', () => { it('should properly disconnect when trying to connect', async () => { const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.reconnectTimeout = true south.connected = false @@ -656,7 +656,7 @@ describe('SouthOPCUAHA', () => { it('should properly disconnect when connected', async () => { const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.reconnectTimeout = false south.connected = true diff --git a/src/south/south-rest/south-rest.js b/src/south/south-rest/south-rest.js index 1eb2e84a1a..c15f0f547f 100644 --- a/src/south/south-rest/south-rest.js +++ b/src/south/south-rest/south-rest.js @@ -88,14 +88,14 @@ class SouthRest extends SouthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) this.tmpFolder = path.resolve(this.baseFolder, 'tmp') // Create tmp folder to write files locally before sending them to the cache diff --git a/src/south/south-rest/south-rest.spec.js b/src/south/south-rest/south-rest.spec.js index 4f1f068eb1..3d62cd2b48 100644 --- a/src/south/south-rest/south-rest.spec.js +++ b/src/south/south-rest/south-rest.spec.js @@ -108,7 +108,7 @@ describe('SouthRest', () => { it('should create RestApi connector and connect', async () => { databaseService.getConfig.mockReturnValue(null) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.requestMethod).toEqual(configuration.settings.requestMethod) expect(south.host).toEqual(configuration.settings.host) @@ -138,14 +138,14 @@ describe('SouthRest', () => { throw new Error('mkdir error test') }) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.logger.error).toHaveBeenCalledWith(new Error('mkdir error test')) }) it('should fail to scan', async () => { fetch.mockReturnValue(Promise.resolve({ ok: false, status: 400, statusText: 'statusText' })) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() await expect(south.historyQuery( @@ -242,7 +242,7 @@ describe('SouthRest', () => { resolve(endpointResult) }), })) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() await south.historyQuery(configuration.scanMode, new Date('2020-01-01T00:00:00.000Z'), new Date('2021-01-01T00:00:00.000Z')) @@ -276,7 +276,7 @@ describe('SouthRest', () => { resolve(endpointResult) }), })) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() await south.historyQuery(configuration.scanMode, new Date('2019-10-03T13:36:38.590Z'), new Date('2019-10-03T15:36:38.590Z')) @@ -285,7 +285,7 @@ describe('SouthRest', () => { }) it('should use http get with body function with self signed certificates accepted and without authentication', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.body = '{ startTime: @StartTime, endTime: @EndTime }' @@ -314,7 +314,7 @@ describe('SouthRest', () => { }) it('should use http get with body function with ISO dates', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.body = '{ startTime: @StartTime, endTime: @EndTime }' @@ -342,7 +342,7 @@ describe('SouthRest', () => { }) it('should use http get with body function with numerical dates', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.body = '{ startTime: @StartTime, endTime: @EndTime }' @@ -382,7 +382,7 @@ describe('SouthRest', () => { }), })) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.requestMethod = 'PUT' @@ -420,7 +420,7 @@ describe('SouthRest', () => { }), })) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.variableDateFormat = 'number' @@ -459,7 +459,7 @@ describe('SouthRest', () => { resolve([]) }), })) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() await expect(south.historyQuery( @@ -500,7 +500,7 @@ describe('SouthRest', () => { const expectedCompressedPath = path.join(tmpFolder, 'myFile.gz') const startTime = new Date('2019-10-03T13:36:38.590Z') const endTime = new Date('2019-10-03T15:36:38.590Z') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() await south.historyQuery(configuration.scanMode, startTime, endTime) diff --git a/src/south/south-sql/south-sql.js b/src/south/south-sql/south-sql.js index 07092e0c89..2da3eed836 100644 --- a/src/south/south-sql/south-sql.js +++ b/src/south/south-sql/south-sql.js @@ -98,14 +98,14 @@ class SouthSQL extends SouthConnector { } /** - * Initialize services (logger, certificate, status data) + * Initialize services (logger, certificate, status data) at startup * @param {String} baseFolder - The base cache folder * @param {String} oibusName - The OIBus name * @param {Object} defaultLogParameters - The default logs parameters * @returns {Promise} - The result promise */ - async init(baseFolder, oibusName, defaultLogParameters) { - await super.init(baseFolder, oibusName, defaultLogParameters) + async start(baseFolder, oibusName, defaultLogParameters) { + await super.start(baseFolder, oibusName, defaultLogParameters) try { // eslint-disable-next-line global-require,import/no-unresolved,import/no-extraneous-dependencies oracledb = require('oracledb') diff --git a/src/south/south-sql/south-sql.spec.js b/src/south/south-sql/south-sql.spec.js index 1fe64c327b..0199c46656 100644 --- a/src/south/south-sql/south-sql.spec.js +++ b/src/south/south-sql/south-sql.spec.js @@ -98,7 +98,7 @@ describe('SouthSQL', () => { it('should properly connect and set lastCompletedAt from database', async () => { databaseService.getConfig.mockReturnValue('2020-04-23T11:09:01.001Z') - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() expect(databaseService.createConfigDatabase).toBeCalledWith(path.resolve(`baseFolder/south-${south.id}/cache.db`)) @@ -111,7 +111,7 @@ describe('SouthSQL', () => { throw new Error('mkdir error test') }) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) expect(south.logger.error).toHaveBeenCalledWith(new Error('mkdir error test')) }) @@ -122,7 +122,7 @@ describe('SouthSQL', () => { const tempConfig = { ...settings } tempConfig.startTime = '2020-02-02 02:02:02' const tempSqlSouth = new SQL(tempConfig, addValues, addFiles) - await tempSqlSouth.init('baseFolder', 'oibusName', {}) + await tempSqlSouth.start('baseFolder', 'oibusName', {}) await tempSqlSouth.connect() expect(tempSqlSouth.lastCompletedAt[settings.scanMode]).toEqual(new Date('2020-02-02 02:02:02')) @@ -138,7 +138,7 @@ describe('SouthSQL', () => { }, } const badSqlSouth = new SQL(badConfig, addValues, addFiles) - await badSqlSouth.init('baseFolder', 'oibusName', {}) + await badSqlSouth.start('baseFolder', 'oibusName', {}) expect(badSqlSouth.logger.error).toHaveBeenCalledWith('Invalid timezone supplied: "undefined".') @@ -147,7 +147,7 @@ describe('SouthSQL', () => { it('should properly connect and set lastCompletedAt to now', async () => { databaseService.getConfig.mockReturnValue(null) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() expect(databaseService.createConfigDatabase).toBeCalledWith(path.resolve(`baseFolder/south-${south.id}/cache.db`)) @@ -156,7 +156,7 @@ describe('SouthSQL', () => { }) it('should quit historyQuery if timezone is invalid', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.timezone = undefined @@ -168,7 +168,7 @@ describe('SouthSQL', () => { }) it('should interact with MSSQL server if driver is mssql', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mssql' @@ -201,7 +201,7 @@ describe('SouthSQL', () => { }) it('should interact with MSSQL server and catch request error', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mssql' @@ -228,7 +228,7 @@ describe('SouthSQL', () => { const endTime = new Date('2019-10-03T13:40:40.400Z') utils.generateReplacementParameters.mockReturnValue([startTime, endTime]) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mysql' @@ -284,7 +284,7 @@ describe('SouthSQL', () => { }) it('should interact with MySQL server and catch request error', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mysql' @@ -316,7 +316,7 @@ describe('SouthSQL', () => { const endTime = new Date('2019-10-03T13:40:40.400Z') utils.generateReplacementParameters.mockReturnValue([startTime, endTime]) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'postgresql' @@ -359,7 +359,7 @@ describe('SouthSQL', () => { }) it('should interact with PostgreSQL server and catch request error', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'postgresql' @@ -382,7 +382,7 @@ describe('SouthSQL', () => { }) it('should interact with Oracle server if driver is oracle', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'oracle' @@ -425,7 +425,7 @@ describe('SouthSQL', () => { }) it('should interact with Oracle server and catch request error', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'oracle' @@ -449,7 +449,7 @@ describe('SouthSQL', () => { it('should interact with SQLite database server if driver is sqlite', async () => { const all = jest.fn(() => ([])) mockDatabase.prepare.mockReturnValue({ all }) - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'sqlite' @@ -466,7 +466,7 @@ describe('SouthSQL', () => { mockDatabase.prepare = () => { throw new Error('test') } - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'sqlite' @@ -479,7 +479,7 @@ describe('SouthSQL', () => { }) it('should log an error if an invalid driver is specified', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'invalid' @@ -492,7 +492,7 @@ describe('SouthSQL', () => { }) it('should not send file on emtpy result', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mysql' @@ -505,7 +505,7 @@ describe('SouthSQL', () => { }) it('should send an uncompressed file when the result is not empty and compression is false', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mysql' @@ -539,7 +539,7 @@ describe('SouthSQL', () => { }) it('should send a compressed file when the result is not empty and compression is true', async () => { - await south.init('baseFolder', 'oibusName', {}) + await south.start('baseFolder', 'oibusName', {}) await south.connect() south.driver = 'mysql' diff --git a/src/south/south-sql/south-sql.test.js b/src/south/south-sql/south-sql.test.js index 223417c1e3..a857d08271 100644 --- a/src/south/south-sql/south-sql.test.js +++ b/src/south/south-sql/south-sql.test.js @@ -54,7 +54,7 @@ describe('MySQL Integration test', () => { it('should retrieve some values in the MySQL database', async () => { const south = new SQL(configuration, engine) - await south.init() + await south.start('baseFolder', 'oibusName', {}) await south.connect() expect(south.connected).toEqual(true) @@ -104,7 +104,7 @@ describe('PostgreSQL Integration test', () => { it('should retrieve some values in the PostgreSQL database', async () => { const south = new SQL(configuration, engine) - await south.init() + await south.start('baseFolder', 'oibusName', {}) await south.connect() expect(south.connected).toEqual(true) @@ -160,7 +160,7 @@ describe('MSSQL Integration test', () => { it('should retrieve some values in the MSSQL database', async () => { const south = new SQL(configuration, engine) - await south.init() + await south.start('baseFolder', 'oibusName', {}) await south.connect() expect(south.connected).toEqual(true)