-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode_helper.js
136 lines (118 loc) · 3.83 KB
/
node_helper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict';
/* MagicMirror²
* Module: MMM-VigilanceMeteoFrance
*
* MagicMirror² By Michael Teeuw https://magicmirror.builders
* MIT Licensed.
*
* Module MMM-VigilanceMeteoFrance By Grena https://github.com/grenagit
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const axios = require('axios');
module.exports = NodeHelper.create({
getData: function() {
var self = this;
// Get Oauth2 token
axios({
url: self.config.oauthEndpoint,
data: {
'grant_type': 'client_credentials'
},
headers: {
'Authorization': 'Basic ' + self.config.appid,
'Accept': 'application/json'
},
method: 'post'
})
.then(function(response) {
if(response.status == 200 && response.data) {
// Get vigilance data
axios({
url: self.config.vigiEndpoint,
headers: {
'Authorization': 'Bearer ' + response.data.access_token,
'Accept': 'application/json'
},
method: 'get'
})
.then(function(response) {
if(response.status == 200 && response.data) {
self.formatData(response.data);
} else {
self.sendSocketNotification("ERROR", 'MeteoFrance Vigilance error: ' + response.statusText);
}
})
.catch(function(error) {
self.sendSocketNotification("ERROR", 'MeteoFrance Vigilance error: ' + error.message);
});
} else {
self.sendSocketNotification("ERROR", 'MeteoFrance Oauth2 error: ' + response.statusText);
}
})
.catch(function(error) {
self.sendSocketNotification("ERROR", 'MeteoFrance Oauth2 error: ' + error.message);
});
},
formatData: function(data) {
var self = this;
let periodsData = data.product.periods;
let departmentDataJ = periodsData[0].timelaps.domain_ids.filter(item => item.domain_id == self.config.department)[0];
let departmentDataJ1 = periodsData[1].timelaps.domain_ids.filter(item => item.domain_id == self.config.department)[0];
let risks = [];
let levels = [];
for(let i = 0; i < 2; i++) {
levels.push({
"id": periodsData[i].echeance,
"level": periodsData[i].timelaps.domain_ids.filter(item => item.domain_id == self.config.department)[0].max_color_id,
"text": periodsData[i].text_items.text.join(' '),
"begin": periodsData[i].begin_validity_time,
"end": periodsData[i].end_validity_time
});
}
risks = self.formatRisks(departmentDataJ.phenomenon_items, periodsData[0]).concat(self.formatRisks(departmentDataJ1.phenomenon_items, periodsData[1]));
risks.sort((a, b) => Number(b.level) - Number(a.level));
self.sendSocketNotification("DATA", JSON.stringify({
"department": self.config.department,
"levels": levels,
"risks": risks
}));
},
formatRisks: function(phenomenonData, periodData) {
var self = this;
let risks = [];
for(let i = 0; i < phenomenonData.length; i++) {
if(!self.config.excludedRisks.includes(parseInt(phenomenonData[i].phenomenon_id)) && phenomenonData[i].phenomenon_max_color_id > 1) {
let timelapsData = phenomenonData[i].timelaps_items;
if(timelapsData.length > 0) {
for(let j = 0; j < timelapsData.length; j++) {
if(timelapsData[j].color_id > 1) {
risks.push({
"id": parseInt(phenomenonData[i].phenomenon_id),
"level": timelapsData[j].color_id,
"begin": timelapsData[j].begin_time,
"end": timelapsData[j].end_time
});
}
}
} else {
risks.push({
"id": parseInt(phenomenonData[i].phenomenon_id),
"level": phenomenonData[i].phenomenon_max_color_id,
"begin": periodData.begin_validity_time,
"end": periodData.end_validity_time
});
}
}
}
return risks;
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if(notification === 'CONFIG') {
self.config = payload;
self.sendSocketNotification("STARTED", true);
self.getData();
}
}
});