-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·227 lines (198 loc) · 6.15 KB
/
Copy pathindex.js
File metadata and controls
executable file
·227 lines (198 loc) · 6.15 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const {app,
BrowserView,
BrowserWindow,
dialog,
globalShortcut,
net} = require('electron');
const electron = require('electron');
const fs = require('fs');
// Globally referencing the main browser window + subwindows
var env, pres_win, tourn_win, twitch_win = null;
var is_streaming = false;
var mode = 0;
var apiKeyFileName = "api_key";
// Twitch API
function checkTwitch(env) {
console.log('Querying Twitch API');
var request = net.request({
method: 'GET',
protocol: 'https:',
hostname: 'api.twitch.tv',
port: 443,
path: '/helix/streams/?user_login=' + env.twitch_user});
request.setHeader('Client-ID', env.twitch_client_id);
request.on('response', (res) => {
console.log('Got status: ' + res.statusCode);
res.setEncoding('utf8');
res.on('error', (err) => {
console.log('Got error: ' + JSON.stringify(err));
console.log('Could not query Twitch API!');
return;
});
res.on('data', (stream) => {
try {
var twitch_json = JSON.parse(stream);
if (twitch_json.data.length > 0) {
console.log(env.twitch_user + ' is live!');
if (!is_streaming) {
is_streaming = true;
switchToTwitch(env);
}
} else {
console.log(env.twitch_user + ' is offline.');
if (is_streaming) {
is_streaming = false;
switchToPres(env);
}
}
} catch (ex) {
console.log('Could not query Twitch API!');
}
});
});
request.end();
}
function refreshPres(type) {
if (!is_streaming) {
switch (type) {
case 0:
pres_win.reload();
break;
case 1:
tourn_win.reload();
break;
default:
console.log('Unknown refresh code!');
break;
}
}
}
function setup() {
console.log('Loading api_key...');
var apiKey = fs.readFileSync(apiKeyFileName, 'utf8');
if (!apiKey) {
dialog.showErrorBox('Key Error', 'Could not load API key!');
app.quit();
}
console.log('Attempting to load configuration from LAN website...');
var request = net.request('https://lan.lsucs.org.uk/api/presentationdata?api_key=' + apiKey);
request.on('response', (res) => {
console.log('Got status: ' + res.statusCode);
res.setEncoding('utf8');
res.on('error', (err) => {
console.log('Got error: ' + JSON.stringify(err));
console.log('Could not query LAN API!');
app.quit();
});
res.on('data', (stream) => {
try {
var lanEnv = JSON.parse(stream);
parseConfig(lanEnv);
} catch (ex) {
console.log('Could not query LAN API: ' + ex);
app.quit();
}
});
});
request.end();
}
function parseConfig(lanEnv) {
console.log('Parsing webconfig...');
try {
// make global environment
env = lanEnv;
// Add screen size to the env
const {width, height} = electron.screen.getPrimaryDisplay().size;
// Add width and height to the environment
env.scrWidth = width; env.scrHeight = height;
// Evoke the kiosk now.
createWindow(env);
} catch (ex) {
console.log('Could not parse webconfig: ' + ex);
app.quit();
}
}
function createWindow(env) {
console.log('Opening kiosk browser window...');
// Create two frameless adjacent browser windows.
pres_win = new BrowserWindow({ x: 0, y: 0, width: env.scrWidth / 2, height: env.scrHeight,
backgroundColor: '#000000', // Give a black background for refresh.
autoHideMenuBar: true,
fullscreen: false,
resizable: false, frame: false
});
tourn_win = new BrowserWindow({ x: env.scrWidth / 2, y: 0, width: env.scrWidth / 2, height: env.scrHeight,
backgroundColor: '#000000',
autoHideMenuBar: true,
resizable: false, frame: false
});
twitch_win = new BrowserWindow({ x: 0, y: 0, width: env.scrWidth, height: env.scrHeight,
backgroundColor: '#000000',
autoHideMenuBar: true,
fullscreen: true, show: false,
resizable: true, frame: false
});
// By default, load up the presentation URL + set up twitch API checking.
switchToPres(env);
setInterval(refreshPres, env.pres_refresh * 60000, 0);
setInterval(refreshPres, env.twitch_check * 60000, 1);
// Register manual view switch shortcut (Ctrl+Alt+S)
globalShortcut.register('CmdOrCtrl+Alt+S', () => {
if (mode == 1) {
switchToPres(env);
} else {
switchToTwitch(env);
}
});
// Register manual refresh shortcut (Ctrl+Alt+R)
globalShortcut.register('CmdOrCtrl+Alt+R', () => {
if (mode == 0) {
refreshPres(0);
refreshPres(1);
}
});
// Add on closed listeners to free window pointer on app exit.
pres_win.on('closed', () => {
globalShortcut.unregisterAll()
pres_win = null;
tourn_win = null;
twitch_win = null;
app.quit();
});
tourn_win.on('closed', () => {
globalShortcut.unregisterAll()
pres_win = null;
tourn_win = null;
twitch_win = null;
app.quit();
});
twitch_win.on('closed', () => {
globalShortcut.unregisterAll()
pres_win = null;
tourn_win = null;
twitch_win = null;
app.quit();
});
}
function switchToTwitch(env) {
console.log('Switching to twitch player...');
mode = 1;
// Only show the twitch window.
tourn_win.hide();
pres_win.hide();
twitch_win.show();
twitch_win.loadURL('https://player.twitch.tv/?channel=' + env.twitchUser + '&autoplay=true');
}
function switchToPres(env) {
console.log('Switching to presentation player...');
mode = 0;
// redisplay both windows.
twitch_win.hide();
pres_win.show();
tourn_win.show();
pres_win.loadURL(env.main_pres_url);
tourn_win.loadURL(env.tourn_pres_url);
}
// Open setup menu when electron has loaded.
app.on('ready', setup);
console.log('Chromble 3.0.0 running');