diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000..4a5ee33 --- /dev/null +++ b/utils/README.md @@ -0,0 +1,20 @@ +# OVMS NodeJS Client Utilities + +## cmd.js + +Single shell command execution utility. + +Enter your server & vehicle login/password in ``config.json``. + +Example usage: + +``` +> node cmd.js "stat" +Not charging +SOC: 76.5% +Ideal range: 185km +Est. range: 149km +ODO: 10877.0km +CAC: 111.7Ah +SOH: 93% +``` diff --git a/utils/cmd.js b/utils/cmd.js new file mode 100644 index 0000000..83bfbc3 --- /dev/null +++ b/utils/cmd.js @@ -0,0 +1,34 @@ +// Init OVMS client library: +const { OVMSClient } = require('ovms-client'); + +// Read client configuration: +const config = require('./config.json'); + +// Process command line arguments: +if (process.argv.length !== 3) { + process.stderr.write('Usage: cmd.js "ovms shell command"\n'); + process.exit(1); +} +const command = process.argv[2]; + +// Connect to OVMS server: +const client = new OVMSClient(config.host, config.port, config.vehicleid, config.password); +client.connect(); + +client.on('commandReceived', response => { + // Process command response: + let [ command, result, message ] = response.split(','); + if (message) { + process.stdout.write(message.replace(/\r/g, String.fromCharCode(10))); + } else { + const resname = [ 'OK', 'FAILED', 'UNSUPPORTED', 'UNIMPLEMENTED' ]; + process.stdout.write(resname[result] + '\n'); + } + // Exit: + process.exit(result); +}); + +client.on('connected', callback => { + // Send command: + client.send('7,' + command); +}); diff --git a/utils/config.json b/utils/config.json new file mode 100644 index 0000000..52239c9 --- /dev/null +++ b/utils/config.json @@ -0,0 +1,6 @@ +{ + "host": "api.openvehicles.com", + "port": 6867, + "vehicleid": "DEMO", + "password": "DEMO" +}