-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
118 lines (106 loc) · 4.23 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
var moment = require('moment')
const NodeHelper = require('node_helper')
var google = require('@google/maps')
var fs = require('fs')
var util = require("./backendUtil")
var googleClient = undefined
module.exports = NodeHelper.create({
start: function () {
var self = this
self.log('Starting helper: ', this.name)
this.started = false
},
scheduleUpdate: function () {
var self = this
this.updatetimer = setInterval(function () {
self.getOpeningHours()
}, this.config.scheduleTime) // 1min
},
mockGooglePlacesApi(self, index) {
self.log('Using mock data.')
return new Promise(function (resolve, reject) {
const files = fs.readdirSync('./modules/MMM-OpeningHours/mock_data')
const file = files[index];
const data = fs.readFileSync('./modules/MMM-OpeningHours/mock_data/' + file, 'utf8');
resolve({
json: JSON.parse(data)
}).catch((error) => {
reject(error);
})
})
},
getOpeningHours: function () {
var self = this
self.log('Fetching opening hours')
let places = this.config.places;
if (this.config.mockData) {
const files = fs.readdirSync('./modules/MMM-OpeningHours/mock_data')
places = Array.from('x'.repeat(files.length));
}
let opening_hours_promises = places.map((place, index) => {
if (Array.isArray(place)) {
place = place[0] // place=[placeid, placename]
}
let googlePromise
if (this.config.mockData) {
googlePromise = self.mockGooglePlacesApi(self, index)
} else {
self.log('Using Google Places API.')
googlePromise = googleClient.place({
placeid: place === null ? '' : place,
fields: ['name', 'opening_hours', 'place_id'],
}).asPromise()
}
self.debugLog('googlePromise - ', googlePromise)
return googlePromise.then(function (response) {
self.debugLog('Response - ', JSON.stringify(response))
// self.debugLog('Response.json.result - ', response.json.result)
return response.json.result
}).catch(function (error) {
self.log(': Error: ', error)
// self.sendSocketNotification('SERVICE_FAILURE', error)
})
})
Promise.all(opening_hours_promises).then(function (result) {
this.places = util.replacePlaceNamesWithUsersOwnAlias(result, places);
self.debugLog('Sending to frontend - ', JSON.stringify(this.places))
self.sendSocketNotification('PLACES_UPDATE', result)
})
},
// --------------------------------------- Handle notifications
socketNotificationReceived: function (notification, payload) {
var self = this
this.config = payload
self.debugLog('Notification - ' + notification)
self.debugLog('Started - ' + this.started)
self.debugLog('Config - ', this.config)
if (notification === 'SETUP') {
this.config = payload
if (!this.config.mockData) {
if (this.started === false) {
moment.locale(this.config.language)
googleClient = google.createClient({
key: this.config.googleApiKey,
Promise: Promise,
language: this.config.language
})
self.scheduleUpdate()
}
}
this.started = true
self.getOpeningHours()
} else {
self.sendSocketNotification('PLACES_UPDATE', this.places)
}
}
,
log: function (msg, object) {
console.log('[' + (new Date(Date.now())).toLocaleTimeString() + '] - ' + this.name + ' : ' + msg, object === undefined ? '' : object)
}
,
debugLog: function (msg, object) {
if (this.config.debug) {
console.log('[' + (new Date(Date.now())).toLocaleTimeString() + '] - DEBUG - ' + this.name + ' : ' + msg, object === undefined ? '' : object)
}
}
})