Skip to content

Commit

Permalink
feat(esm): move backend code to ECMA script modules
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerni10 authored and Nicolas Burger committed Dec 14, 2022
1 parent 166fdb5 commit a192b48
Show file tree
Hide file tree
Showing 73 changed files with 423 additions and 506 deletions.
14 changes: 3 additions & 11 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
"jsx": true,
"js": true
}
},
"plugins": ["react-hooks"],
Expand All @@ -29,12 +30,6 @@
"argsIgnorePattern": "^_"
}
],
"new-cap": [
"error",
{
"capIsNewExceptions": ["OneOf"]
}
],
"no-console": [
"warn",
{
Expand All @@ -49,10 +44,7 @@
"jsx-a11y/anchor-is-valid": 1,
"import/extensions": [
"error",
"ignorePackages",
{
"js": "never"
}
"ignorePackages"
],
"import/no-extraneous-dependencies": ["error", {"devDependencies": ["src/frontend/**/*.jsx", "src/south/**/*.jsx", "src/north/**/*.jsx"]}],
"arrow-parens": ["error", "always"],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "index.js",
"author": "Optimistik SAS",
"license": "EUPL",
"type": "module",
"engines": {
"node": ">=v16.17.0",
"npm": ">=8.15.0"
Expand Down
92 changes: 59 additions & 33 deletions src/engine/base-engine.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
const path = require('node:path')

const VERSION = require('../../package.json').version

const northList = {}
northList.OIAnalytics = require('../north/north-oianalytics/north-oianalytics')
northList.OIConnect = require('../north/north-oiconnect/north-oiconnect')
northList.FileWriter = require('../north/north-file-writer/north-file-writer')
northList.AmazonS3 = require('../north/north-amazon-s3/north-amazon-s3')
northList.InfluxDB = require('../north/north-influx-db/north-influx-db')
northList.TimescaleDB = require('../north/north-timescale-db/north-timescale-db')
northList.MongoDB = require('../north/north-mongo-db/north-mongo-db')
northList.MQTT = require('../north/north-mqtt/north-mqtt')
northList.Console = require('../north/north-console/north-console')
northList.WATSYConnect = require('../north/north-watsy/north-watsy')
northList.CsvToHttp = require('../north/north-csv-to-http/north-csv-to-http')

const southList = {}
southList.SQL = require('../south/south-sql/south-sql')
southList.FolderScanner = require('../south/south-folder-scanner/south-folder-scanner')
southList.OPCUA_HA = require('../south/south-opcua-ha/south-opcua-ha')
southList.OPCUA_DA = require('../south/south-opcua-da/south-opcua-da')
southList.MQTT = require('../south/south-mqtt/south-mqtt')
southList.ADS = require('../south/south-ads/south-ads')
southList.Modbus = require('../south/south-modbus/south-modbus')
southList.OPCHDA = require('../south/south-opchda/south-opchda')
southList.RestApi = require('../south/south-rest/south-rest')

const StatusService = require('../service/status.service')
import path from 'node:path'

// North imports
import OIAnalytics from '../north/north-oianalytics/north-oianalytics.js'
import OIConnect from '../north/north-oiconnect/north-oiconnect.js'
import FileWriter from '../north/north-file-writer/north-file-writer.js'
import AmazonS3 from '../north/north-amazon-s3/north-amazon-s3.js'
import InfluxDB from '../north/north-influx-db/north-influx-db.js'
import TimescaleDB from '../north/north-timescale-db/north-timescale-db.js'
import MongoDB from '../north/north-mongo-db/north-mongo-db.js'
import MQTTNorth from '../north/north-mqtt/north-mqtt.js'
import Console from '../north/north-console/north-console.js'
import WATSYConnect from '../north/north-watsy/north-watsy.js'
import CsvToHttp from '../north/north-csv-to-http/north-csv-to-http.js'

// South imports
import SQL from '../south/south-sql/south-sql.js'
import FolderScanner from '../south/south-folder-scanner/south-folder-scanner.js'
import OPCUA_HA from '../south/south-opcua-ha/south-opcua-ha.js'
import OPCUA_DA from '../south/south-opcua-da/south-opcua-da.js'
import MQTTSouth from '../south/south-mqtt/south-mqtt.js'
import ADS from '../south/south-ads/south-ads.js'
import Modbus from '../south/south-modbus/south-modbus.js'
import OPCHDA from '../south/south-opchda/south-opchda.js'
import RestApi from '../south/south-rest/south-rest.js'

import StatusService from '../service/status.service.js'

const northList = {
OIAnalytics,
OIConnect,
FileWriter,
AmazonS3,
InfluxDB,
TimescaleDB,
MongoDB,
MQTT: MQTTNorth,
Console,
WATSYConnect,
CsvToHttp,
}

const southList = {
SQL,
FolderScanner,
OPCUA_HA,
OPCUA_DA,
MQTT: MQTTSouth,
ADS,
Modbus,
OPCHDA,
RestApi,
}

/**
* Abstract class used to manage North and South connectors
* @class BaseEngine
*/
class BaseEngine {
export default class BaseEngine {
/**
* Constructor for BaseEngine
* @constructor
Expand All @@ -48,7 +72,7 @@ class BaseEngine {
loggerService,
cacheFolder,
) {
this.version = VERSION
this.version = null
this.cacheFolder = path.resolve(cacheFolder)

this.installedNorthConnectors = northList
Expand All @@ -69,6 +93,10 @@ class BaseEngine {
* @returns {Promise<void>} - The result promise
*/
async initEngineServices(engineConfig) {
// const packageJson = JSON.parse(await fs.readFile('package.json'))
// this.version = packageJson.version
// TODO
this.version = '2.4.0'
this.oibusName = engineConfig.name
this.defaultLogParameters = engineConfig.logParameters
this.proxies = engineConfig.proxies
Expand Down Expand Up @@ -178,5 +206,3 @@ class BaseEngine {
}))
}
}

module.exports = BaseEngine
6 changes: 2 additions & 4 deletions src/engine/health-signal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { httpSend, createProxyAgent, addAuthenticationToHeaders } = require('../service/http-request-static-functions')
import { httpSend, createProxyAgent, addAuthenticationToHeaders } from '../service/http-request-static-functions.js'

/**
* Class HealthSignal - sends health signal to a remote host or into the logs
*/
class HealthSignal {
export default class HealthSignal {
/**
* Constructor for HealthSignal
* @constructor
Expand Down Expand Up @@ -162,5 +162,3 @@ class HealthSignal {
}
}
}

module.exports = HealthSignal
20 changes: 9 additions & 11 deletions src/engine/history-query-engine.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const path = require('node:path')
const fs = require('node:fs/promises')
import path from 'node:path'
import fs from 'node:fs/promises'

const humanizeDuration = require('humanize-duration')
import humanizeDuration from 'humanize-duration'

const HistoryQuery = require('./history-query/history-query')
const BaseEngine = require('./base-engine')
const HistoryQueryRepository = require('./history-query/history-query-repository')
const databaseService = require('../service/database.service')
import HistoryQuery from './history-query/history-query.js'
import BaseEngine from './base-engine.js'
import HistoryQueryRepository from './history-query/history-query-repository.js'
import { getHistoryQuerySouthData } from '../service/database.service.js'

const CACHE_FOLDER = './cache/history-query'
const HISTORY_QUERIES_DB = './history-query.db'
Expand All @@ -16,7 +16,7 @@ const HISTORY_TIMER_INTERVAL = 10000
* Manage history queries by running {@link HistoryQuery} one after another
* @class HistoryQueryEngine
*/
class HistoryQueryEngine extends BaseEngine {
export default class HistoryQueryEngine extends BaseEngine {
/**
* Constructor for Engine
* Reads the config file and create the corresponding Object.
Expand Down Expand Up @@ -229,7 +229,7 @@ class HistoryQueryEngine extends BaseEngine {
const databasePath = `${folder}/${historyQueryConfig.southId}.db`
try {
await fs.stat(databasePath)
const entries = databaseService.getHistoryQuerySouthData(databasePath)
const entries = getHistoryQuerySouthData(databasePath)
data.south = entries.map((entry) => ({
scanMode: entry.name.replace('lastCompletedAt-', ''),
lastCompletedDate: entry.value,
Expand All @@ -242,5 +242,3 @@ class HistoryQueryEngine extends BaseEngine {
return data
}
}

module.exports = HistoryQueryEngine
9 changes: 4 additions & 5 deletions src/engine/history-query/history-query-repository.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const db = require('better-sqlite3')
const HistoryQuery = require('./history-query')
import db from 'better-sqlite3'

class HistoryQueryRepository {
import HistoryQuery from './history-query.js'

export default class HistoryQueryRepository {
static TABLE = 'history_queries'

constructor(databasePath) {
Expand Down Expand Up @@ -202,5 +203,3 @@ class HistoryQueryRepository {
return null
}
}

module.exports = HistoryQueryRepository
10 changes: 4 additions & 6 deletions src/engine/history-query/history-query.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require('node:path')
import path from 'node:path'

const StatusService = require('../../service/status.service')
const { createFolder } = require('../../service/utils')
import StatusService from '../../service/status.service.js'
import { createFolder } from '../../service/utils.js'

const FINISH_INTERVAL = 5000

class HistoryQuery {
export default class HistoryQuery {
// Waiting to be started
static STATUS_PENDING = 'pending'

Expand Down Expand Up @@ -172,5 +172,3 @@ class HistoryQuery {
this.engine.historyQueryRepository.update(this.historyConfiguration)
}
}

module.exports = HistoryQuery
16 changes: 7 additions & 9 deletions src/engine/oibus-engine.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const os = require('node:os')
import fs from 'node:fs/promises'
import os from 'node:os'

const timexe = require('timexe')
const humanizeDuration = require('humanize-duration')
import timexe from 'timexe'
import humanizeDuration from 'humanize-duration'

const fs = require('node:fs/promises')
const BaseEngine = require('./base-engine')
const HealthSignal = require('./health-signal')
import BaseEngine from './base-engine.js'
import HealthSignal from './health-signal.js'

const CACHE_FOLDER = './cache/data-stream'

/**
* At startup, handles initialization of configuration, North and South connectors.
* @class OIBusEngine
*/
class OIBusEngine extends BaseEngine {
export default class OIBusEngine extends BaseEngine {
/**
* Constructor for OIBusEngine
* Reads the config file and create the corresponding Object.
Expand Down Expand Up @@ -392,5 +392,3 @@ class OIBusEngine extends BaseEngine {
})
}
}

module.exports = OIBusEngine
29 changes: 15 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
const cluster = require('node:cluster')
const path = require('node:path')
import cluster from 'node:cluster'
import path from 'node:path'

const VERSION = require('../package.json').version
import migrationService from './migration/migration.service.js'
import ConfigurationService from './service/configuration.service.js'
import Server from './web-server/web-server.js'
import OIBusEngine from './engine/oibus-engine.js'
import HistoryQueryEngine from './engine/history-query-engine.js'
import LoggerService from './service/logger/logger.service.js'
import EncryptionService from './service/encryption.service.js'

const migrationService = require('./migration/migration.service')
const ConfigurationService = require('./service/configuration.service')
const Server = require('./web-server/web-server')
const OIBusEngine = require('./engine/oibus-engine')
const HistoryQueryEngine = require('./engine/history-query-engine')
const LoggerService = require('./service/logger/logger.service')
const EncryptionService = require('./service/encryption.service')

const { getCommandLineArguments, createFolder } = require('./service/utils')
import { getCommandLineArguments, createFolder } from './service/utils.js'

// In case there is an error the worker process will exit.
// If this happens MAX_RESTART_COUNT times in less than MAX_INTERVAL_MILLISECOND interval
Expand Down Expand Up @@ -44,14 +42,17 @@ if (cluster.isMaster) {
const baseDir = path.resolve(path.extname(configFile) ? path.parse(configFile).dir : configFile)
process.chdir(baseDir)
createFolder(baseDir).then(async () => {
// TODO
const packageJson = '2.4.0' // JSON.parse(await fs.readFile('package.json'))

// Create the base cache folder
await createFolder(CACHE_FOLDER)

await loggerService.start('OIBus-main', logParameters)
const mainLogger = loggerService.createChildLogger('main-thread')
// Master role is nothing except launching a worker and relaunching another
// one if exit is detected (typically to load a new configuration)
mainLogger.info(`Starting OIBus version ${VERSION}.`)
mainLogger.info(`Starting OIBus version ${packageJson}.`)

let restartCount = 0
let startTime = (new Date()).getTime()
Expand Down Expand Up @@ -115,7 +116,7 @@ if (cluster.isMaster) {
const forkLogger = loggerService.createChildLogger('forked-thread')

// Migrate config file, if needed
await migrationService.migrate(configFilePath, loggerService.createChildLogger('migration'))
await migrationService(configFilePath, loggerService.createChildLogger('migration'))

const configService = new ConfigurationService(configFilePath, CACHE_FOLDER)
const encryptionService = EncryptionService.getInstance()
Expand Down
4 changes: 2 additions & 2 deletions src/migration/database-migration.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const db = require('better-sqlite3')
import db from 'better-sqlite3'

const CACHE_TABLE_NAME = 'cache'

Expand Down Expand Up @@ -31,7 +31,7 @@ const changeColumnValue = (databasePath, columnName, oldValue, newValue) => {
return true
}

module.exports = {
export {
changeColumnName,
addColumn,
removeColumn,
Expand Down
Loading

0 comments on commit a192b48

Please sign in to comment.