-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
91 lines (83 loc) · 3.02 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
var NodeHelper = require('node_helper')
var fetch = require('node-fetch')
var {
DateTime
} = require('luxon')
module.exports = NodeHelper.create({
requiresVersion: '2.23.0',
start: function () {
console.log('Starting node_helper for module: ' + this.name)
this.espUrl = "https://developer.sepush.co.za/business/2.0/area?id="
},
deconstructData: function (data, code) {
var payload = data;
const espEvents = [];
if (payload.events.length >= 1) {
let eventsData = []
payload.events.forEach(event => {
let start1 = DateTime.fromISO(event.start);
let end1 = DateTime.fromISO(event.end);
const diff = end1.diff(start1, ["years", "months", "days", "hours"])
var timeDiff = diff.toObject()
eventsData.push({
"stage": event.note,
"relDate": DateTime.fromISO(event.start).toRelativeCalendar(),
"start": event.start,
"end": event.end,
"startTime": DateTime.fromISO(event.start).hour + ":" + DateTime.fromISO(event.start).minute,
"endTime": DateTime.fromISO(event.end).hour + ":" + DateTime.fromISO(event.end).minute,
"duration": timeDiff.hours
})
});
espEvents.push({
"code": code,
"areaInfo": payload.info.name,
"region": payload.info.region,
"events": eventsData
})
return espEvents;
} else {
espEvents.push({
"code": code,
"areaInfo": payload.areaInfo,
"region": payload.region,
"events": "No upcoming loadshedding"
})
return espEvents;
}
},
async getEspData(payload) {
var tkn = payload.token
var endPoint = this.espUrl + payload.area
const response = await fetch(endPoint, {
method: 'get',
headers: {
'token': tkn
}
});
if (response.status !== 200) {
const errorText = await response.text();
const espEvents = [];
const errorObject = JSON.parse(errorText);
const errorMessage = errorObject.error;
espEvents.push({
"code": response.status,
"areaInfo": payload.area,
"region": "",
"events": errorMessage
})
this.sendSocketNotification("ESP_DATA", espEvents)
return;
} else {
const data = await response.json();
const code = response.status;
var results = this.deconstructData(data, code);
this.sendSocketNotification("ESP_DATA", results)
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_ESP_DATA") {
this.getEspData(payload)
}
}
})