This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
forked from andreknieriem/photobooth
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathremotebuzzer_client.js
378 lines (315 loc) · 12.3 KB
/
remotebuzzer_client.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* exported rotaryController initRemoteBuzzerFromDOM remoteBuzzerClient */
/* global photoBooth photoboothTools globalGalleryHandle io */
let remoteBuzzerClient;
let rotaryController;
let buttonController;
// eslint-disable-next-line no-unused-vars
function initRemoteBuzzerFromDOM() {
photoboothTools.console.logDev(
'Remote Buzzer client:',
config.remotebuzzer.usebuttons || config.remotebuzzer.userotary || config.remotebuzzer.usenogpio
? 'enabled'
: 'disabled'
);
/*
** Communication with Remote Buzzer Server
*/
remoteBuzzerClient = (function () {
let ioClient;
const api = {};
api.enabled = function () {
return config.remotebuzzer.usebuttons || config.remotebuzzer.userotary || config.remotebuzzer.usenogpio;
};
api.init = function () {
if (!this.enabled()) {
return;
}
if (config.webserver.ip) {
ioClient = io('http://' + config.webserver.ip + ':' + config.remotebuzzer.port);
photoboothTools.console.logDev(
'Remote buzzer connecting to http://' + config.webserver.ip + ':' + config.remotebuzzer.port
);
ioClient.on('photobooth-socket', function (data) {
switch (data) {
case 'start-picture':
buttonController.takePicture();
break;
case 'start-collage':
buttonController.takeCollage();
break;
case 'collage-next':
// Need to handle collage process in button handler
if (buttonController.waitingToProcessCollage) {
buttonController.processCollage();
} else {
buttonController.takeCollageNext();
}
break;
case 'print':
buttonController.print();
break;
case 'rotary-cw':
rotaryController.focusNext();
break;
case 'rotary-ccw':
rotaryController.focusPrev();
break;
case 'rotary-btn-press':
rotaryController.click();
break;
default:
break;
}
});
ioClient.on('connect_error', function () {
photoboothTools.console.log(
'ERROR: remotebuzzer_client unable to connect to webserver ip - please ensure remotebuzzer_server is running on Photobooth server. Use Photobooth dev mode to create log file for debugging'
);
});
ioClient.on('connect', function () {
photoboothTools.console.logDev(
'remotebuzzer_client successfully connected to Photobooth webserver ip'
);
});
buttonController.init();
rotaryController.init();
rotaryController.focusSet('#start');
} else {
photoboothTools.console.log(
'ERROR: remotebuzzer_client unable to connect - webserver.ip not defined in photobooth config'
);
}
};
api.inProgress = function (flag) {
if (this.enabled()) {
if (flag) {
this.emitToServer('in-progress');
} else {
this.emitToServer('completed');
}
}
};
api.collageWaitForNext = function () {
if (this.enabled()) {
this.emitToServer('collage-wait-for-next');
}
};
api.collageWaitForProcessing = function () {
buttonController.waitingToProcessCollage = true;
if (this.enabled()) {
this.emitToServer('collage-wait-for-next');
}
};
api.startPicture = function () {
if (this.enabled()) {
this.emitToServer('start-picture');
}
};
api.startCollage = function () {
if (this.enabled()) {
this.emitToServer('start-collage');
}
};
api.emitToServer = function (cmd) {
switch (cmd) {
case 'start-picture':
ioClient.emit('photobooth-socket', 'start-picture');
break;
case 'start-collage':
ioClient.emit('photobooth-socket', 'start-collage');
break;
case 'in-progress':
ioClient.emit('photobooth-socket', 'in-progress');
break;
case 'completed':
ioClient.emit('photobooth-socket', 'completed');
break;
case 'collage-wait-for-next':
ioClient.emit('photobooth-socket', 'collage-wait-for-next');
break;
default:
break;
}
};
return api;
})();
/*
** Controls PB with hardware BUTTONS
*/
buttonController = (function () {
// vars
const api = {};
api.waitingToProcessCollage = false;
api.init = function () {
// nothing to init
};
api.enabled = function () {
return (
(config.remotebuzzer.usebuttons || config.remotebuzzer.usenogpio) &&
typeof onStandaloneGalleryView === 'undefined' &&
typeof onLiveChromaKeyingView === 'undefined'
);
};
api.takePicture = function () {
if (this.enabled()) {
$('.resultInner').removeClass('show');
photoBooth.thrill('photo');
}
};
api.takeCollage = function () {
if (this.enabled() && config.collage.enabled) {
$('.resultInner').removeClass('show');
this.waitingToProcessCollage = false;
photoBooth.thrill('collage');
}
};
api.takeCollageNext = function () {
$('#btnCollageNext').trigger('click');
};
api.processCollage = function () {
this.waitingToProcessCollage = false;
$('#btnCollageProcess').trigger('click');
};
api.print = function () {
if ($('#result').is(':visible')) {
$('.printbtn').trigger('click');
$('.printbtn').blur();
} else if ($('.pswp__button--print').is(':visible')) {
$('.pswp__button--print').trigger('click');
} else {
remoteBuzzerClient.emitToServer('completed');
}
};
return api;
})();
/*
** Controls PB with ROTARY encoder
*/
rotaryController = (function () {
// vars
const api = {};
// API functions
api.enabled = function () {
return (
config.remotebuzzer.userotary &&
!config.remotebuzzer.usenogpio &&
(typeof onStandaloneGalleryView === 'undefined' ? true : config.remotebuzzer.enable_standalonegallery)
);
};
api.init = function () {
if (config.dev.enabled && typeof onStandaloneGalleryView !== 'undefined') {
photoboothTools.console.log(
'Rotary Controller is ',
config.remotebuzzer.enable_standalonegallery ? 'enabled' : 'disabled',
' for standalone gallery view'
);
}
};
api.focusSet = function (id) {
if (this.enabled()) {
this.focusRemove();
$(id).find('.rotaryfocus').first().addClass('focused');
}
};
api.focusRemove = function () {
$('.focused').removeClass('focused');
};
api.focusNext = function () {
if (this.rotationInactive() || !this.enabled()) {
return;
}
if ($('.pswp.pswp--open').is(':visible')) {
// photoswipe navigation
const buttonList = $('.pswp.pswp--open').find('.rotaryfocus:visible');
let focusIndex = buttonList.index($('.focused'));
if (buttonList.eq(focusIndex + 1).exists()) {
focusIndex += 1;
} else if (buttonList.eq(0).exists()) {
focusIndex = 0;
}
globalGalleryHandle.ui.setIdle(false);
$('.focused')
.removeClass('focused pswp-rotary-focus')
.parents('.pswp.pswp--open')
.find('.rotaryfocus:visible')
.eq(focusIndex)
.addClass('focused pswp-rotary-focus')
.find('i.fa')
.css('z-index', '1');
} else {
const buttonList = $('.focused').parents('.rotarygroup').find('.rotaryfocus:visible');
let focusIndex = buttonList.index($('.focused'));
if (buttonList.eq(focusIndex + 1).exists()) {
focusIndex += 1;
} else if (buttonList.eq(0).exists()) {
focusIndex = 0;
}
$('.focused')
.removeClass('focused')
.parents('.rotarygroup')
.find('.rotaryfocus:visible')
.eq(focusIndex)
.addClass('focused')
.focus();
}
};
api.focusPrev = function () {
if (this.rotationInactive() || !this.enabled()) {
return;
}
if ($('.pswp.pswp--open').is(':visible')) {
// photoswipe navigation
const buttonList = $('.pswp.pswp--open').find('.rotaryfocus:visible');
const focusIndex = buttonList.index($('.focused'));
if (buttonList.eq(focusIndex - 1).exists()) {
globalGalleryHandle.ui.setIdle(false);
$('.focused')
.removeClass('focused pswp-rotary-focus')
.parents('.pswp.pswp--open')
.find('.rotaryfocus:visible')
.eq(focusIndex - 1)
.addClass('focused pswp-rotary-focus')
.find('i.fa')
.css('z-index', '1');
}
} else {
const buttonList = $('.focused').parents('.rotarygroup').find('.rotaryfocus:visible');
const focusIndex = buttonList.index($('.focused'));
if (buttonList.eq(focusIndex - 1).exists()) {
$('.focused')
.removeClass('focused')
.parents('.rotarygroup')
.find('.rotaryfocus:visible')
.eq(focusIndex - 1)
.addClass('focused')
.focus();
}
}
};
api.rotationInactive = function () {
if ($('.modal.modal--show').exists()) {
return true;
}
return false;
};
api.click = function () {
if (this.enabled()) {
// click modal if open
if ($('#qrCode.modal.modal--show').exists()) {
$('#qrCode').click();
} else {
$('.focused').blur().trigger('click');
if ($('.pswp.pswp--open').is(':visible')) {
globalGalleryHandle.ui.setIdle(true);
}
}
}
};
// private helper functions
$.fn.exists = function () {
return this.length !== 0;
};
return api;
})();
remoteBuzzerClient.init();
}