forked from jclarke0000/MMM-MyScoreboard
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnode_helper.js
More file actions
130 lines (116 loc) · 5.02 KB
/
Copy pathnode_helper.js
File metadata and controls
130 lines (116 loc) · 5.02 KB
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
const Log = require('logger')
const NodeHelper = require('node_helper')
const moment = require('moment-timezone')
const fs = require('node:fs')
const path = require('node:path')
module.exports = NodeHelper.create({
providers: {},
start: function () {
Log.log('Starting node_helper for: ' + this.name)
this.providers.SNET = require('./providers/SNET.js')
this.providers.SNET_YD = require('./providers/SNET_YD.js')
this.providers.ESPN = require('./providers/ESPN.js')
this.providers.Scorepanel = require('./providers/ESPN_Scorepanel.js')
this.providers.CPL = require('./providers/CPL.js')
this.providers.PWHL = require('./providers/PWHL.js')
this.localLogos = {}
var fsTree = this.getDirectoryTree('./modules/MMM-MyScoreboard/logos')
fsTree.forEach((league) => {
if (league.children) {
var logoFiles = []
league.children.forEach((file) => {
logoFiles.push(file.name)
})
this.localLogos[league.name] = logoFiles
}
})
this.localLogosCustom = {}
fsTree = this.getDirectoryTree('./modules/MMM-MyScoreboard/logos_custom')
fsTree.forEach((league) => {
if (league.children) {
var logoFiles = []
league.children.forEach((file) => {
logoFiles.push(file.name)
})
this.localLogosCustom[league.name] = logoFiles
}
})
},
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
},
getDirectoryTree(dirPath) {
const result = []
const files = fs.readdirSync(dirPath, { withFileTypes: true })
files.forEach((file) => {
const filePath = path.join(dirPath, file.name)
if (file.name.endsWith('.svg') || file.name.endsWith('.png')) {
result.push({ name: file.name })
}
else if (file.isDirectory()) {
const children = this.getDirectoryTree(filePath)
if (children.length > 0) {
result.push({ name: file.name, children })
}
}
})
return result
},
async socketNotificationReceived(notification, payload) {
if (notification == 'MMM-MYSCOREBOARD-GET-SCORES') {
/*
payload contains:
provider to get data from
game date for which to retrive scores,
league
teams
module instance identifier
sport's index from the config's order
*/
var self = this
var provider = this.providers[payload.provider]
var provider2 = this.providers[payload.provider]
if (payload.provider == 'SNET') {
provider2 = this.providers['SNET_YD']
}
if (payload.whichDay.today) {
provider.getScores(payload, moment(payload.gameDate), function (scores, sortIdx, noGamesToday) {
self.sendSocketNotification('MMM-MYSCOREBOARD-SCORE-UPDATE', { instanceId: payload.instanceId, index: payload.league, scores: scores, label: payload.label, sortIdx: sortIdx, provider: payload.provider, noGamesToday: noGamesToday })
})
}
else {
self.sendSocketNotification('MMM-MYSCOREBOARD-SCORE-UPDATE', { instanceId: payload.instanceId, index: payload.league, scores: [], notRun: true, label: payload.label, sortIdx: 999, provider: payload.provider, noGamesToday: false })
}
if (payload.whichDay.yesterday === 'yes') {
await this.sleep(4000)
provider2.getScores(payload, moment(payload.gameDate).subtract(1, 'day'), function (scores, sortIdx, noGamesToday) {
self.sendSocketNotification('MMM-MYSCOREBOARD-SCORE-UPDATE-YD', { instanceId: payload.instanceId, index: payload.league, scores: scores, label: payload.label, sortIdx: sortIdx, provider: payload.provider, noGamesToday: noGamesToday })
})
}
/* else if (payload.whichDay.yesterday === 'erase') {
Log.debug('it\'s suppsoed to erase')
self.sendSocketNotification('MMM-MYSCOREBOARD-SCORE-UPDATE-YD', { instanceId: payload.instanceId, index: payload.league, scores: [], label: payload.label, sortIdx: 999 })
} */
}
else if (notification == 'MMM-MYSCOREBOARD-GET-UPCOMING') {
const helper = this
const upcomingProvider = this.providers[payload.provider]
if (!upcomingProvider || typeof upcomingProvider.getTeamSchedule !== 'function') {
helper.sendSocketNotification('MMM-MYSCOREBOARD-UPCOMING-UPDATE', {
instanceId: payload.instanceId, label: payload.label, league: payload.league,
sortIdx: payload.sortIdx, games: {}, unsupported: true,
})
return
}
upcomingProvider.getTeamSchedule(payload, function (gamesByTeam) {
helper.sendSocketNotification('MMM-MYSCOREBOARD-UPCOMING-UPDATE', {
instanceId: payload.instanceId, label: payload.label, league: payload.league,
sortIdx: payload.sortIdx, games: gamesByTeam,
})
})
}
else if (notification == 'MMM-MYSCOREBOARD-GET-LOCAL-LOGOS') {
this.sendSocketNotification('MMM-MYSCOREBOARD-LOCAL-LOGO-LIST', { instanceId: payload.instanceId, index: payload.league, logos: this.localLogos, logosCustom: this.localLogosCustom })
}
},
})