Piping GPS data to OTDT
If there is a GPSD available "near" OTDT, the positioning data often must be used in the Actions; so the GPS data must be piped to OTDT.
Certainly the GPS data could always be used as a Trigger for OTDT. But, if you only want to store the current values to have them available in a Trigger initiated Action, then you simply could do that.
// The following code should be cone via the `_init_` of the `_module_` section
//
// Using the `vars` state to GPS data. Initialize it.
//
vars.gps = {
run: 0,
fixed: 0
};
// Now `spawn` `gpspipe` and attach it to `readline`
//
var p = app.spawn("gpspipe -w");
var r = app.readline(p);
// Some error handline on the pipe
//
p.stderr.on('data', (data) => {
console.error("Error", data.toString());
});
// Getting to know when pipe is closed
//
p.on('close', (code) => {
console.info("Closed", code);
r.close();
vars.gps = {
run: 0,
fixed: 0
};
});
// Getting GPS data
//
r.on('line', async (line) => {
console.log("Data", line);
// Getting GPS data
let data = JSON.parse(line);
// Only consider `TPV` records
if (data.class != 'TPV')
return;
// If GPS data is valid, store it to the `vars` state
if ('lat' in data && data.lat != 0 && 'lon' in data && data.lon != 0) {
vars.gps = data;
vars.gps.run = 1;
vars.gps.fixed = 0;
if ('mode' in data && data.mode > 1)
vars.gps.fixed = 1;
}
});With this code snippet you store incoming GPS data for usage in any Action on any participating OTDT installation.