-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
Closes #151 Signed-off-by: Lukas Mertens <[email protected]> commit-id:b922e502
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# EVerest Simulator Data Updater | ||
|
||
## Overview | ||
|
||
This directory contains a Node.js script (`update-simulator-data.js`) designed to update the dummy data directly from a | ||
running EVerest instance. The script interfaces with the EVerest WebSocket API to fetch and refresh module and interface | ||
data and converts the sample-configs in `./sample-configs` in a config list ensuring that the TypeScript files here | ||
reflect the latest system definitions. | ||
|
||
## Files | ||
|
||
- `update-simulator-data.js`: The main script responsible for fetching data from the EVerest WebSocket API and updating | ||
the TypeScript files. | ||
- `sample_module_info.ts`: Updated to contain module information as EverestModuleDefinitionList. | ||
- `sample_interfaces_list.ts`: Updated to contain interface definitions as EverestInterfaceDefinitionList. | ||
- `sample_config_list.ts`: Updated to contain a few sample configs as EverestConfigList. | ||
|
||
## Usage | ||
|
||
Run the script using the following command: | ||
|
||
```bash | ||
node update-simulator-data.js [--url=<custom-ws-url>] | ||
``` | ||
|
||
Options: | ||
`--url=<custom-ws-url>`: Use this option to specify a custom WebSocket URL if your EVerest instance is not running on | ||
the default ws://localhost:8849. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#!/usr/bin/env node | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2020 - 2024 Pionix GmbH and Contributors to EVerest | ||
import WebSocket from 'ws'; | ||
import fs from 'fs'; | ||
import path, {dirname} from 'path'; | ||
import process from 'process'; | ||
import {fileURLToPath} from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
|
||
// Default URL of the EVerest WebSocket instance | ||
const defaultUrl = 'ws://localhost:8849'; | ||
const args = process.argv.slice(2); | ||
const url = args.find(arg => arg.startsWith('--url='))?.split('=')[1] || defaultUrl; | ||
|
||
const help = ` | ||
Usage: node update-simulator-data.js [options] | ||
Options: | ||
--url=<ws-url> Specify the WebSocket URL of the EVerest instance (default: ${defaultUrl}) | ||
--help Show this help message | ||
`; | ||
|
||
if (args.includes('--help')) { | ||
console.log(help); | ||
process.exit(0); | ||
} | ||
|
||
const ws = new WebSocket(url); | ||
|
||
const resolvers = {}; | ||
|
||
// Handler to manage both module and interface requests | ||
ws.on('open', async () => { | ||
console.log('Connected to EVerest WebSocket'); | ||
await Promise.all([ | ||
new Promise((resolve) => { | ||
resolvers[1] = resolve; | ||
sendRequest('get_modules', 1); | ||
}), | ||
new Promise((resolve) => { | ||
resolvers[2] = resolve; | ||
sendRequest('get_interfaces', 2); | ||
}), | ||
new Promise((resolve) => { | ||
compileConfigFiles(); | ||
resolve(); | ||
}) | ||
]) | ||
ws.close(); | ||
console.log('Disconnected from EVerest WebSocket'); | ||
}); | ||
|
||
ws.on('message', function incoming(data) { | ||
const response = JSON.parse(data); | ||
if (response.id === 1 && response.result) { | ||
updateFiles(response.result, 'sample_module_info.ts', 'EverestModuleDefinitionList'); | ||
} else if (response.id === 2 && response.result) { | ||
updateFiles(response.result, 'sample_interfaces_list.ts', 'EverestInterfaceDefinitionList'); | ||
} else { | ||
throw new Error("Invalid response received from EVerest WebSocket"); | ||
} | ||
resolvers[response.id](); | ||
}); | ||
|
||
ws.on('error', function error(e) { | ||
console.error('Failed to connect to EVerest WebSocket:', e.message); | ||
process.exit(1); | ||
}); | ||
|
||
function sendRequest(method, id) { | ||
const request = { | ||
method: method, | ||
params: null, | ||
id: id | ||
}; | ||
ws.send(JSON.stringify(request)); | ||
Check warning on line 78 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
} | ||
|
||
function updateFiles(data, filename, typecast) { | ||
const content = generateContent(data, typecast); | ||
writeToFile(filename, content); | ||
} | ||
|
||
function generateContent(data, typecast) { | ||
let content = licenseHeader(); | ||
content += `import {${typecast}} from "../index";\n\n`; | ||
content += `export default ${JSON.stringify(data, null, 2)} as ${typecast};\n`; | ||
return content; | ||
} | ||
|
||
function licenseHeader() { | ||
return `// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2020 - ${new Date().getFullYear()} Pionix GmbH and Contributors to EVerest\n\n`; | ||
} | ||
|
||
function writeToFile(filename, content) { | ||
const filePath = path.join(__dirname, filename); | ||
Check warning on line 99 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
try { | ||
fs.writeFileSync(filePath, content); | ||
Check warning on line 101 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
console.log(`${filename} updated successfully!`); | ||
} catch (err) { | ||
console.error(`Failed to write data to ${filename}:`, err); | ||
Check notice on line 104 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
} | ||
} | ||
|
||
function compileConfigFiles() { | ||
const configDirPath = path.join(__dirname, 'sample-configs'); | ||
const configFiles = fs.readdirSync(configDirPath); | ||
Check warning on line 110 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
|
||
let configs = {}; | ||
|
||
configFiles.forEach(file => { | ||
if (path.extname(file) === '.json') { | ||
const configName = path.basename(file, '.json'); | ||
const filePath = path.join(configDirPath, file); | ||
Check warning on line 117 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
const fileContent = fs.readFileSync(filePath, 'utf8'); | ||
Check warning on line 118 in src/modules/evbc/simulator-sample-data/update-simulator-data.js
|
||
configs[`${configName}`] = JSON.parse(fileContent); | ||
} else { | ||
console.warn(`Ignoring file ${file} as it is not a JSON file.`); | ||
} | ||
}); | ||
|
||
const content = generateContent(configs, 'EverestConfigList'); | ||
writeToFile('sample_config_list.ts', content); | ||
} |