Skip to content

Commit

Permalink
Modifications for rendering signalk-logbook files
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Oct 18, 2023
1 parent 470de71 commit 6dae36b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 66 deletions.
10 changes: 8 additions & 2 deletions logbook/log.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ table {
margin-bottom: 2em;
}
thead th,
thead td {
thead td,
tfoot th,
tfoot td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
thead td {
thead td,
tfoot td {
min-width: 42vw;
max-width: 42vw;
}
tfoot {
font-size: smaller;
}
td, th {
padding: 2px;
border: 0;
Expand Down
137 changes: 81 additions & 56 deletions logbook/render.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,92 @@
const { render } = require('mustache');
const { Point } = require('where');
const data = require('./2022-logs.json');
const { load } = require('js-yaml');
const { readFile, writeFile } = require('node:fs/promises');

let previousState = null;
function isEnd(entry, nextEntry) {
if (entry.text.indexOf('Anchored') !== -1) {
return true;
}
if (!entry.end) {
return false;
}
if (!nextEntry) {
return true;
}
const current = new Date(entry.datetime);
const next = new Date(nextEntry.datetime);
const hours = (next - current) / 3600000;
if (hours < 3) {
return false;
}
return true;
}

readFile('template.html', 'utf-8')
.then(template => {
return render(template, {
trips: data,
formatDate: () => (tmpl, render) => {
const d = new Date(render(tmpl));
return `${d.getUTCDate()}.${d.getMonth()+1}. ${String(d.getUTCHours()).padStart(2, '0')}:${String(d.getUTCMinutes()).padStart(2, '0')}Z`;
},
formatTime: () => (tmpl, render) => {
const d = new Date(render(tmpl));
return `${String(d.getUTCHours()).padStart(2, '0')}:${String(d.getUTCMinutes()).padStart(2, '0')}`;
},
formatWind: () => (tmpl, render) => {
const wind = render(tmpl);
if (wind === '0.0kt 000&deg;') {
return 'n/a';
}
return wind;
},
formatBarometer: () => (tmpl, render) => {
const baro = render(tmpl);
return baro.split('.')[0];
},
formatCoordinates: () => (tmpl, render) => {
const [ lat, lon ] = render(tmpl).split(' ');
return new Point(parseFloat(lat), parseFloat(lon)).toString();
},
formatState: () => (tmpl, render) => {
const prev = previousState;
const state = render(tmpl);
previousState = state;
switch (state) {
case 'sailing':
if (prev === 'motoring') {
return 'Motor stopped, sails up';
}
return 'Sails up';
case 'motoring':
if (prev === 'sailing') {
return 'Motor started, sails down';
}
if (prev === 'anchored') {
return 'Motor started, anchor up';
}
return 'Motor started';
case 'moored':
return 'Vessel stopped';
case 'anchored':
return 'Anchored';
default:
return state;
}
},
readFile('2023-cruise.yml', 'utf-8')
.then((yml) => load(yml))
.then((data) => {
const trips = [];
let currentTrip = {
events: [],
sailing: 0,
};
let sailing = false;
data.forEach((entry, idx) => {
if (currentTrip.events.length === 0) {
currentTrip.start = entry.datetime;
}
if (entry.text.indexOf('sailing') !== -1) {
sailing = true;
}
if (entry.text.indexOf('Started main engine') !== -1) {
sailing = false;
}
currentTrip.events.push(entry);
if (isEnd(entry, data[idx + 1])) {
currentTrip.end = entry.datetime;
currentTrip.engineHours = (entry.engine.hours - currentTrip.events[0].engine.hours)
.toFixed(1);
currentTrip.miles = (entry.log - currentTrip.events[0].log).toFixed(1);
trips.push(currentTrip);
currentTrip = {
events: [],
sailing: 0,
};
}
});
return readFile('template.html', 'utf-8')
.then(template => {
return render(template, {
trips,
formatDate: () => (tmpl, rdr) => {
const d = new Date(rdr(tmpl));
return `${d.getUTCDate()}.${d.getMonth() + 1}. ${String(d.getUTCHours()).padStart(2, '0')}:${String(d.getUTCMinutes()).padStart(2, '0')}Z`;
},
formatTime: () => (tmpl, render) => {
const d = new Date(render(tmpl));
return `${String(d.getUTCHours()).padStart(2, '0')}:${String(d.getUTCMinutes()).padStart(2, '0')}`;
},
formatWind: () => (tmpl, render) => {
const wind = render(tmpl);
if (wind === 'kt &deg;') {
return 'n/a';
}
return wind;
},
formatBarometer: () => (tmpl, render) => {
const baro = render(tmpl);
return baro.split('.')[0];
},
formatCoordinates: () => (tmpl, render) => {
const [ lat, lon ] = render(tmpl).split(' ');
return new Point(parseFloat(lat), parseFloat(lon)).toString();
},
});
});
})
.then(output => {
return writeFile('log.html', output, 'utf-8');
writeFile('log.html', output, 'utf-8');
console.log('Done');

Check warning on line 89 in logbook/render.js

View workflow job for this annotation

GitHub Actions / Run test suite (12.x)

Unexpected console statement
})
.catch(e => {
console.error(e.message);

Check warning on line 92 in logbook/render.js

View workflow job for this annotation

GitHub Actions / Run test suite (12.x)

Unexpected console statement
Expand Down
32 changes: 24 additions & 8 deletions logbook/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<th>Course</th>
<th>Speed</th>
<th>Log</th>
<th>Wind</th>
<th>Weather</th>
<th>Baro</th>
<th>Coordinates</th>
<th>Fix</th>
Expand All @@ -29,19 +29,35 @@
</thead>
<tbody>
{{#events}}
<tr data-time="{{ time }}">
<td>{{#formatTime}}{{ time }}{{/formatTime}}</td>
<tr data-time="{{ datetime }}">
<td>{{#formatTime}}{{ datetime }}{{/formatTime}}</td>
<td>{{ heading }}&deg;</td>
<td>{{ speed }}kt</td>
<td>{{ speed.sog }}kt</td>
<td>{{ log }}NM</td>
<td>{{#formatWind}}{{ windSpeed }}kt {{ windDirection }}&deg;{{/formatWind}}</td>
<td>
Wind {{#formatWind}}{{ wind.speed }}kt {{ wind.direction }}&deg;{{/formatWind}}
{{#observations.seaState}}<br>Sea state {{observations.seaState}}{{/observations.seaState}}
{{#observations.cloudCoverage}}<br>Clouds {{observations.cloudCoverage}}/8{{/observations.cloudCoverage}}
{{#observations.visibility}}<br>Visibility {{observations.visibility}}{{/observations.visibility}}
</td>
<td>{{#formatBarometer}}{{ barometer }}{{/formatBarometer}} hPa</td>
<td>{{#formatCoordinates}}{{ position.lat }} {{ position.lon }}{{/formatCoordinates}}</td>
<td>{{ fixType }}</td>
<td contenteditable>{{^hourly}}{{#formatState}}{{ state }}{{/formatState}}{{/hourly}}</td>
<td>{{#formatCoordinates}}{{ position.latitude }} {{ position.longitude }}{{/formatCoordinates}}</td>
<td>{{ position.source }}</td>
<td contenteditable>
{{#text}}{{ text }} {{#author}}<br>-{{author}}{{/author}}{{/text}}
{{^text}}Hourly entry{{/text}}
</td>
</tr>
{{/events}}
</tbody>
<tfoot>
<tr>
<th>Distance</th>
<td colspan="5">{{ miles }}NM</td>
<th>Engine hours</th>
<td colspan="2">{{ engineHours }}h</td>
</tr>
</tfoot>
</table>
{{/trips}}
</body>
Expand Down

0 comments on commit 6dae36b

Please sign in to comment.