UNPKG

signalk-barograph

Version:

SignalK plugin to influx environment data & Barograph to visualize atmospheric pressure

75 lines (68 loc) 1.73 kB
const axios = require('axios'); const log = require('debug')('signalk-barograph:app') let selfuri='' let token='' function init(uri, user, pwd) { axios({ method: 'post', url: uri+'/signalk/v1/auth/login', headers: { 'Content-Type': 'application/json' }, data : {"username":user,"password":pwd} }).then( (response) => { selfuri = uri token = response.data.token; setAppUserData(); }) .catch( (error) => { token = '' if (error.response && error.response.status===401) log("Unauthorized - review connection string configuration!") else log(error.response ? error.response : error); }); } let subscriptions = {}; function addSubscription (type, path) { subscriptions[type] = path; } let influx = {}; function addInflux (key, value) { influx[key] = value; } function setAppUserData () { if (token!=='') { let data = { subscriptions, influx }; let config = { method: 'post', url: selfuri+'/signalk/v1/applicationData/user/signalk_barograph/0.2.0', headers: { 'Content-Type': 'application/json', 'Cookie': 'JAUTHENTICATION='+token }, data : data }; axios(config) .then( (response) => { log('Barograph Config') log({ path: subscriptions.pressure, influx: influx.url, config: response.status }); return true; }) .catch( (error) => { log(error); return false; }); } else { log('Barograph configuration error: invalid or empty token!') return false; } } module.exports = { init, addSubscription, addInflux, setAppUserData }