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

Append new log entries to a file #44

Open
wants to merge 2 commits into
base: main
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@mapbox/geo-viewport": "^0.5.0",
"circular-buffer": "^1.0.3",
"jsonschema": "^1.4.1",
"mustache": "^4.2.0",
"ordinal": "^1.0.3",
"timezones-list": "^3.0.2",
"where": "^0.4.1",
Expand Down
5 changes: 4 additions & 1 deletion plugin/Log.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { EventEmitter } = require('events');
const {
stat,
readdir,
Expand All @@ -9,8 +10,9 @@ const { parse, stringify } = require('yaml');
const { Validator } = require('jsonschema');
const openAPI = require('../schema/openapi.json');

class Log {
class Log extends EventEmitter {
constructor(dir) {
super();
this.dir = dir;
this.validator = null;
}
Expand Down Expand Up @@ -130,6 +132,7 @@ class Log {
datetime: new Date(data.datetime),
};
d.push(normalized);
this.emit('entry', normalized);
return this.writeDate(date, d);
});
}
Expand Down
70 changes: 69 additions & 1 deletion plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const CircularBuffer = require('circular-buffer');
const timezones = require('timezones-list');
const { appendFile } = require('fs/promises');
const mustache = require('mustache');
const { Point } = require('where');
const Log = require('./Log');
const stateToEntry = require('./format');
const { processTriggers, processHourly } = require('./triggers');
Expand Down Expand Up @@ -47,6 +50,31 @@ function sendCrewNames(app, plugin) {
sendDelta(app, plugin, new Date(), 'communication.crewNames', configuration.crewNames || []);
}

function printEntries(log, config, app) {
log.on('entry', (entry) => {
const point = new Point(entry.position.latitude, entry.position.longitude);
const printFriendly = {
...entry,
date: entry.datetime.toISOString().substr(0, 10),
time: entry.datetime.toISOString().substr(11, 5),
latitude: point.toString().split(' ')[0],
longitude: point.toString().split(' ')[1],
};
const output = mustache.render(config.template_entry, printFriendly);
appendFile(config.device, `${output}\n`)
.then(() => {
if (entry.end) {
const endOutput = mustache.render(config.template_end, printFriendly);
return appendFile(config.device, `${endOutput}\n`);
}
return Promise.resolve();
})
.catch((e) => {
app.error(`Error:${e.message}`);
});
});
}

module.exports = (app) => {
const plugin = {};
let unsubscribes = [];
Expand Down Expand Up @@ -88,8 +116,12 @@ module.exports = (app) => {
let log;
let state = {};

plugin.start = () => {
plugin.start = (options = {}) => {
log = new Log(app.getDataDirPath());
if (options.printOutput && options.printOutput.enable && options.printOutput.device) {
printEntries(log, options.printOutput, app);
}

const subscription = {
context: 'vessels.self',
subscribe: paths.map((p) => ({
Expand Down Expand Up @@ -352,6 +384,42 @@ module.exports = (app) => {
title: tz.label,
})),
},
printOutput: {
type: 'object',
title: 'Output new log entries to a printer',
properties: {
enable: {
type: 'boolean',
title: 'Enable print output',
default: false,
},
device: {
type: 'string',
title: 'Printer device path to write to',
default: '/dev/usb/lp0',
},
template_entry: {
type: 'string',
title: 'Mustache template for individual entries',
default: '{{date}}|{{time}}|{{latitude}}|{{longitude}}|{{speed.sog}}|{{course}}|{{wind.speed}}|{{wind.direction}}|{{barometer}}|{{text}}',
},
template_end: {
type: 'string',
title: 'Mustache template for trip end',
default: '*************************************************************',
},
},
},
},
};
plugin.uiSchema = {
printOutput: {
template_entry: {
'ui:widget': 'textarea',
},
template_end: {
'ui:widget': 'textarea',
},
},
};

Expand Down