Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage example & extend by example command response handler #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions utils/README.md
Original file line number Diff line number Diff line change
@@ -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%
```
34 changes: 34 additions & 0 deletions utils/cmd.js
Original file line number Diff line number Diff line change
@@ -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);
});
6 changes: 6 additions & 0 deletions utils/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"host": "api.openvehicles.com",
"port": 6867,
"vehicleid": "DEMO",
"password": "DEMO"
}