From 734f449786d493679575ae3285c5d76f130450a5 Mon Sep 17 00:00:00 2001 From: Michael Balzer Date: Sat, 6 Aug 2022 12:01:14 +0200 Subject: [PATCH 1/2] Fix usage example & extend by example command response handler --- README.md | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 570b2e8..1e17efc 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,11 @@ This is a simple OVMS client written for NodeJS. ## Usage ```js +const { OVMSClient } = require('ovms-client'); + // host, port, vehicle id, password -const client = new OVMSClient('tmc.openvehicles.com', 6867, 'DEMO', 'DEMO'); +const client = new OVMSClient('api.openvehicles.com', 6867, 'DEMO', 'DEMO'); +// Note: commands won't work with the DEMO vehicle // connect client.connect(); @@ -24,11 +27,31 @@ client.on('raw', msg => { console.log('raw message', msg); }) -// send command -client.send('stat'); +// command responses: +client.on('commandReceived', response => { + // see https://docs.openvehicles.com/en/latest/protocol_v2/messages.html#command-response-0x63-c + let [ command, result, message ] = response.split(','); + console.log('command', command, 'result', result); + if (message) { + console.log(message.replace(/\r/g, String.fromCharCode(10))); + } +}); + +// wait for connection: +client.on('connected', callback => { + + // Send a ping + // (see https://docs.openvehicles.com/en/latest/protocol_v2/messages.html) + client.sendRaw('MP-0 A'); -// Send raw command -client.sendRaw('MP-0 A'); + // Send a shell command + // (see https://docs.openvehicles.com/en/latest/protocol_v2/commands.html) + client.send('7,stat'); + + // Send a lock command using PIN 1234 + client.send('20,1234'); + +}); ``` ## Contribute From e4d331d28c1b5382a1d9e7893399c4a74555060a Mon Sep 17 00:00:00 2001 From: Michael Balzer Date: Sat, 4 Nov 2023 17:06:02 +0100 Subject: [PATCH 2/2] Add command execution utility --- utils/README.md | 20 ++++++++++++++++++++ utils/cmd.js | 34 ++++++++++++++++++++++++++++++++++ utils/config.json | 6 ++++++ 3 files changed, 60 insertions(+) create mode 100644 utils/README.md create mode 100644 utils/cmd.js create mode 100644 utils/config.json 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" +}