Skip to content

Commit

Permalink
Logging enhanced
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrofta committed Feb 1, 2021
1 parent 18f0e61 commit 625a2ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function (app) {
plugin.start = function (options, restartPlugin) {

app.debug('Plugin started');
ow.init(log);

let localSubscription = {
context: '*',
Expand Down Expand Up @@ -77,5 +78,7 @@ module.exports = function (app) {
ow.onDeltasPushed();
}

function log(msg) { app.debug(msg); }

return plugin;
};
50 changes: 41 additions & 9 deletions openweather.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var owm = require ('openweather-apis')
let log = null

const navigationPosition = 'navigation.position';
const outsideBattery = 'environment.outside.battery';
const navigationElevation = 'navigation.gnss.antennaAltitude';
const environmentRPi = 'environment.cpu.temperature';
const oneMinute = 60*1000;
const oneHour = 60*60*1000;

const subscriptions = [
{ path: navigationPosition, period: oneHour, policy: "instant", minPeriod: oneHour },
{ path: outsideBattery, period: oneHour, policy: "instant", minPeriod: oneMinute },
{ path: navigationPosition, period: oneHour, policy: "instant", minPeriod: oneMinute },
{ path: navigationElevation, period: oneHour, policy: "instant", minPeriod: oneMinute },
// workaround required as policy "ideal" not available for navigationPosition
{ path: environmentRPi, period: oneHour, policy: "instant", minPeriod: oneMinute },
];

// SmartJSON
Expand Down Expand Up @@ -44,14 +48,18 @@ const latest = {
},
full: {
time: null,
},
altitude: {
elevation: 0,
}
}

let deltaMessages = [];

const subscriptionHandler = [
{ path: navigationPosition, handle: (value) => onUpdate(value, addMessages) },
{ path: outsideBattery, handle: (value) => onUpdate({ "latitude":latest.forecast.lat, "longitude":latest.forecast.lon }, addMessages) },
{ path: navigationPosition, handle: (value) => onPositionUpdate(value, addMessages) },
{ path: navigationElevation, handle: (value) => onElevationUpdate(value) },
{ path: environmentRPi, handle: (value) => onPositionUpdate({ "latitude":latest.forecast.lat, "longitude":latest.forecast.lon }, addMessages) },
]

function onDeltasUpdate(deltas) {
Expand Down Expand Up @@ -82,21 +90,23 @@ function addMessages (updates) {
}
}

function onUpdate(value, callback) {
if (value == null) throw new Error("Cannot add null value");
function onPositionUpdate(value, callback) {
if (value == null) log("PositionUpdate: Cannot add null value");

latest.forecast.lat = value.latitude;
latest.forecast.lon = value.longitude;

if (!lastUpdateWithin(oneHour))
if (!lastUpdateWithin(oneHour) && isValidPosition(latest.forecast.lat, latest.forecast.lon))
{
latest.forecast.time = Date.now();

owm.setCoordinate(value.latitude, value.longitude);
log("OWM Coordinates "+value.latitude+","+value.longitude);
// get a simple JSON Object with temperature, humidity, pressure and description
owm.getSmartJSON(function(err, smart){
if (!err)
{
log(smart);
latest.simple.temp = smart.temp
latest.simple.humidity = smart.humidity,
latest.simple.pressure = smart.pressure * 100, // getting hPa instead of Pa
Expand All @@ -106,6 +116,7 @@ function onUpdate(value, callback) {
}
else
{
log(err);
latest.simple.temp = null
latest.simple.humidity = null,
latest.simple.pressure = null,
Expand All @@ -118,6 +129,19 @@ function onUpdate(value, callback) {
}
}

function onElevationUpdate(value) {
if (value == null)
{
log("ElevationUpdate: Cannot add null value - using 0 instead");
latest.altitude.elevation = 0
}
else
{
latest.altitude.elevation = value
log("ElevationUpdate: Elevation set to "+value+"m above sea level");
}
}

function prepareUpdate(forecast, weather) {
const noData = "waiting ..."
return [
Expand Down Expand Up @@ -164,9 +188,17 @@ function lastUpdateWithin(interval) {
return latest.forecast.time !== null ? (Date.now() - latest.forecast.time) <= interval : false;
}

function isValidPosition(lat, lon) {
return (lat!==null&&lon!==null && lat!==undefined&&lon!==undefined);
}

module.exports = {
subscriptions,
preLoad,
onDeltasUpdate,
onDeltasPushed
onDeltasPushed,

init: function(loghandler) {
log = loghandler;
}
}

0 comments on commit 625a2ce

Please sign in to comment.