-
Notifications
You must be signed in to change notification settings - Fork 0
/
appconfig.js
68 lines (61 loc) · 1.55 KB
/
appconfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const axios = require('axios');
let selfuri=''
let token=''
function init(uri, user, pwd, log) {
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;
})
.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 (log) {
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 });
})
.catch( (error) => {
log(error);
});
}
else
log('Barograph configuration error: invalid or empty token!')
}
module.exports = {
init,
addSubscription,
addInflux,
setAppUserData
}