1
- import electron , { Tray , Menu , BrowserWindow , Notification , ipcMain , session } from 'electron' ;
1
+ import {
2
+ Tray , Menu , BrowserWindow , Notification , ipcMain , session , nativeImage , systemPreferences , app as electron_app ,
3
+ } from 'electron' ;
2
4
import path from 'path' ;
3
5
import fs from 'fs' ;
4
6
import url from 'url' ;
@@ -16,9 +18,21 @@ log.info('Starting');
16
18
const persist_path = path . resolve ( __dirname , '..' , '..' , 'data' ) ;
17
19
const server_url = 'http://127.0.0.1:8082' ;
18
20
19
- export class App {
21
+ class App {
22
+ static tray : Tray ;
23
+ static menu : Menu ;
24
+ static storage : typeof persist ;
25
+ static preferences_window ?: BrowserWindow ;
26
+
27
+ readonly client : Client ;
28
+ private _url : string ;
29
+ window ?: BrowserWindow ;
30
+
31
+ ready = false ;
32
+
20
33
constructor ( ) {
21
34
this . client = new Client ( server_url , WebSocket ) ;
35
+ this . client . loadAccessories ( this ) ;
22
36
23
37
this . url = server_url ;
24
38
@@ -39,6 +53,7 @@ export class App {
39
53
}
40
54
set url ( base_url ) {
41
55
this . _url = base_url ;
56
+ // @ts -ignore
42
57
if ( this . window ) this . window . send ( 'url' , base_url ) ;
43
58
44
59
const parsed = url . parse ( base_url ) ;
@@ -50,17 +65,20 @@ export class App {
50
65
return this . client . url ;
51
66
}
52
67
set websocket_url ( url ) {
68
+ // @ts -ignore
53
69
this . client . url = url ;
54
70
if ( this . client . connected ) this . client . disconnect ( ) . then ( ( ) => this . client . tryConnect ( ) ) ;
55
71
// if (this.window) this.window.send('reset-has-connected');
56
72
}
57
73
58
74
async connected ( connection ) {
59
- log . info ( 'Connected to %s' , client . url , client ) ;
75
+ log . info ( 'Connected to %s' , this . client . url , this . client ) ;
60
76
77
+ // @ts -ignore
61
78
if ( this . window ) this . window . send ( 'up' ) ;
62
79
63
80
connection . on ( 'received-broadcast' , data => {
81
+ // @ts -ignore
64
82
if ( this . window ) this . window . send ( 'b' , data ) ;
65
83
} ) ;
66
84
@@ -70,7 +88,7 @@ export class App {
70
88
this . constructor . menu . items [ 2 ] . enabled = true ;
71
89
72
90
this . constructor . tray . setContextMenu ( this . constructor . menu ) ;
73
- if ( process . platform === 'darwin' ) electron . app . dock . setMenu ( this . constructor . menu ) ;
91
+ if ( process . platform === 'darwin' ) electron_app . dock . setMenu ( this . constructor . menu ) ;
74
92
75
93
const token = await this . constructor . storage . getItem ( 'Token' ) ;
76
94
if ( this . ready && token ) {
@@ -83,9 +101,11 @@ export class App {
83
101
}
84
102
85
103
disconnected ( event ) {
86
- log . warn ( 'Disconnected from %s' , client . url , event ) ;
104
+ log . warn ( 'Disconnected from %s' , this . client . url , event ) ;
87
105
106
+ // @ts -ignore
88
107
if ( this . window ) this . window . send ( 'down' ) ;
108
+ // @ts -ignore
89
109
if ( this . constructor . preferences_window ) this . constructor . preferences_window . send ( 'authenticated-user' , null ) ;
90
110
91
111
if ( event !== 1005 ) this . client . tryConnect ( ) ;
@@ -94,7 +114,7 @@ export class App {
94
114
this . constructor . menu . items [ 2 ] . enabled = false ;
95
115
96
116
this . constructor . tray . setContextMenu ( this . constructor . menu ) ;
97
- if ( process . platform === 'darwin' ) electron . app . dock . setMenu ( this . constructor . menu ) ;
117
+ if ( process . platform === 'darwin' ) electron_app . dock . setMenu ( this . constructor . menu ) ;
98
118
}
99
119
100
120
handleUpdateCharateristic ( accessory_uuid , service_uuid , characteristic_uuid , details ) {
@@ -118,6 +138,7 @@ export class App {
118
138
title : this . name ,
119
139
subtitle : 'Characteristic updated' ,
120
140
body : service . name + ' ' + (
141
+ // @ts -ignore
121
142
characteristic . type === characteristic . constructor . On ? characteristic . value ? 'on' : 'off' :
122
143
'' ) ,
123
144
} ) ;
@@ -139,9 +160,11 @@ export class App {
139
160
Object . defineProperty ( authenticated_user , 'asset_token' , { value : response . asset_token } ) ;
140
161
Object . assign ( authenticated_user , response . data ) ;
141
162
163
+ // @ts -ignore
142
164
this . client . connection . authenticated_user = authenticated_user ;
143
165
144
166
if ( this . constructor . preferences_window ) {
167
+ // @ts -ignore
145
168
this . constructor . preferences_window . send ( 'authenticated-user' , {
146
169
id : authenticated_user . id ,
147
170
token : authenticated_user . token ,
@@ -151,7 +174,7 @@ export class App {
151
174
}
152
175
153
176
this . client . connection . getHomeSettings ( ) . then ( d => this . client . home_settings = d ) ;
154
- this . client . refreshAccessories ( ) ;
177
+ this . client . refreshLoaded ( ) ;
155
178
156
179
return authenticated_user ;
157
180
}
@@ -166,12 +189,12 @@ export class App {
166
189
} else if ( data . type === 'get-accessories' ) {
167
190
return event . sender . send ( 'r' , {
168
191
messageid,
169
- response : data . id . map ( uuid => this . client . accessories [ uuid ] . _details ) ,
192
+ response : data . id . map ( uuid => this . client . accessories [ uuid ] . details ) ,
170
193
} ) ;
171
194
} else if ( data . type === 'get-accessories-data' ) {
172
195
return event . sender . send ( 'r' , {
173
196
messageid,
174
- response : data . id . map ( uuid => this . client . accessories [ uuid ] . _data ) ,
197
+ response : data . id . map ( uuid => this . client . accessories [ uuid ] . data ) ,
175
198
} ) ;
176
199
} else if ( data . type === 'get-home-settings' ) {
177
200
return event . sender . send ( 'r' , {
@@ -191,9 +214,11 @@ export class App {
191
214
Object . assign ( authenticated_user , response . data ) ;
192
215
193
216
log . info ( 'AuthenticatedUser' , authenticated_user ) ;
217
+ // @ts -ignore
194
218
this . client . connection . authenticated_user = authenticated_user ;
195
219
196
220
if ( this . constructor . preferences_window ) {
221
+ // @ts -ignore
197
222
this . constructor . preferences_window . send ( 'authenticated-user' , {
198
223
id : authenticated_user . id ,
199
224
token : authenticated_user . token ,
@@ -205,12 +230,12 @@ export class App {
205
230
await this . constructor . storage . setItem ( 'Token' , authenticated_user . token ) ;
206
231
207
232
this . client . connection . getHomeSettings ( ) . then ( d => this . client . home_settings = d ) ;
208
- this . client . refreshAccessories ( ) ;
233
+ this . client . refreshLoaded ( ) ;
209
234
} else if ( data . type === 'set-accessories-data' ) {
210
- for ( const [ uuid , data ] of data . id_data ) {
235
+ for ( const [ uuid , accessory_data ] of data . id_data ) {
211
236
this . client . handleBroadcastMessage ( {
212
237
type : 'update-accessory-data' ,
213
- uuid, data,
238
+ uuid, data : accessory_data ,
214
239
} ) ;
215
240
}
216
241
} else if ( data . type === 'set-home-settings' ) {
@@ -252,7 +277,9 @@ export class App {
252
277
this . window . setMenuBarVisibility ( false ) ;
253
278
this . window . setAutoHideMenuBar ( true ) ;
254
279
280
+ // @ts -ignore
255
281
this . window . base_url = this . url ;
282
+ // @ts -ignore
256
283
this . window . connected = this . client . connected ;
257
284
258
285
window_state . manage ( this . window ) ;
@@ -262,7 +289,7 @@ export class App {
262
289
this . window . once ( 'ready-to-show' , ( ) => {
263
290
this . window . show ( ) ;
264
291
265
- if ( process . platform === 'darwin' ) electron . app . dock . show ( ) ;
292
+ if ( process . platform === 'darwin' ) electron_app . dock . show ( ) ;
266
293
} ) ;
267
294
268
295
// Emitted when the window is closed
@@ -273,16 +300,16 @@ export class App {
273
300
this . window = null ;
274
301
window_state . unmanage ( ) ;
275
302
276
- if ( process . platform === 'darwin' && ! BrowserWindow . getAllWindows ( ) . length ) electron . app . dock . hide ( ) ;
303
+ if ( process . platform === 'darwin' && ! BrowserWindow . getAllWindows ( ) . length ) electron_app . dock . hide ( ) ;
277
304
} ) ;
278
305
}
279
306
280
- static async ready ( launch_info ) {
307
+ static async ready ( launch_info ? ) {
281
308
log . info ( 'Launch info' , launch_info ) ;
282
309
283
310
require ( './menu' ) ;
284
311
285
- const icon = electron . nativeImage
312
+ const icon = nativeImage
286
313
. createFromPath ( path . resolve ( __dirname , '..' , '..' , 'assets' , 'home-icon.png' ) )
287
314
. resize ( { height : 22 } ) ;
288
315
icon . setTemplateImage ( true ) ;
@@ -298,7 +325,7 @@ export class App {
298
325
] ) ;
299
326
300
327
this . tray . setContextMenu ( this . menu ) ;
301
- if ( process . platform === 'darwin' ) electron . app . dock . setMenu ( this . menu ) ;
328
+ if ( process . platform === 'darwin' ) electron_app . dock . setMenu ( this . menu ) ;
302
329
303
330
session . defaultSession . webRequest . onBeforeSendHeaders ( this . onBeforeSendHeaders . bind ( this ) ) ;
304
331
@@ -327,6 +354,7 @@ export class App {
327
354
app . ready = true ;
328
355
329
356
// If we're not authenticated open the main window so the user can authenticate
357
+ // @ts -ignore
330
358
if ( ! app . client . connection . authenticated_user ) app . showWindow ( ) ;
331
359
}
332
360
@@ -363,7 +391,7 @@ export class App {
363
391
this . preferences_window . once ( 'ready-to-show' , ( ) => {
364
392
this . preferences_window . show ( ) ;
365
393
366
- if ( process . platform === 'darwin' ) electron . app . dock . show ( ) ;
394
+ if ( process . platform === 'darwin' ) electron_app . dock . show ( ) ;
367
395
} ) ;
368
396
369
397
// Emitted when the window is closed
@@ -374,14 +402,18 @@ export class App {
374
402
this . preferences_window = null ;
375
403
window_state . unmanage ( ) ;
376
404
377
- if ( process . platform === 'darwin' && ! BrowserWindow . getAllWindows ( ) . length ) electron . app . dock . hide ( ) ;
405
+ if ( process . platform === 'darwin' && ! BrowserWindow . getAllWindows ( ) . length ) electron_app . dock . hide ( ) ;
378
406
} ) ;
379
407
}
380
408
381
409
static onBeforeSendHeaders ( details , callback ) {
382
- if ( ! app . client . connection || ! app . client . connection . authenticated_user ) return callback ( { requestHeaders : details . requestHeaders } ) ;
410
+ // @ts -ignore
411
+ if ( ! app . client . connection || ! app . client . connection . authenticated_user ) {
412
+ return callback ( { requestHeaders : details . requestHeaders } ) ;
413
+ }
383
414
384
415
const requestHeaders = Object . assign ( { } , details . requestHeaders , {
416
+ // @ts -ignore
385
417
'Cookie' : 'asset_token=' + app . client . connection . authenticated_user . asset_token ,
386
418
} ) ;
387
419
@@ -393,10 +425,15 @@ export class App {
393
425
}
394
426
395
427
static handleGetAuthenticatedUser ( event ) {
428
+ // @ts -ignore
396
429
event . sender . send ( 'authenticated-user' , app . client . connection && app . client . connection . authenticated_user ? {
430
+ // @ts -ignore
397
431
id : app . client . connection . authenticated_user . id ,
432
+ // @ts -ignore
398
433
token : app . client . connection . authenticated_user . token ,
434
+ // @ts -ignore
399
435
asset_token : app . client . connection . authenticated_user . asset_token ,
436
+ // @ts -ignore
400
437
data : app . client . connection . authenticated_user ,
401
438
} : null ) ;
402
439
}
@@ -410,7 +447,13 @@ export class App {
410
447
}
411
448
}
412
449
413
- electron . app . setAboutPanelOptions ( {
450
+ interface App {
451
+ constructor : typeof App ;
452
+ }
453
+
454
+ export { App } ;
455
+
456
+ electron_app . setAboutPanelOptions ( {
414
457
applicationName : 'Home' ,
415
458
applicationVersion : require ( '../../package' ) . version ,
416
459
credits : 'https://gitlab.fancy.org.uk/hap-server/electron-app' ,
@@ -420,10 +463,17 @@ electron.app.setAboutPanelOptions({
420
463
421
464
export const app = new App ( ) ;
422
465
423
- electron . app . whenReady ( ) . then ( ( ) => App . ready ( ) ) ;
424
- electron . app . on ( 'activate' , app . showWindow . bind ( app ) ) ;
466
+ electron_app . whenReady ( ) . then ( ( ) => App . ready ( ) ) ;
467
+ electron_app . on ( 'activate' , app . showWindow . bind ( app ) ) ;
468
+
469
+ electron_app . on ( 'browser-window-created' , ( event , window ) => {
470
+ // @ts -ignore
471
+ window . base_url = app . url ;
472
+ // @ts -ignore
473
+ window . connected = app . client . connected ;
474
+ } ) ;
425
475
426
- electron . app . on ( 'window-all-closed' , ( ) => {
476
+ electron_app . on ( 'window-all-closed' , ( ) => {
427
477
// Don't quit as we want to stay connected to the server to show notifications
428
478
} ) ;
429
479
@@ -432,26 +482,29 @@ ipcMain.on('get-authenticated-user', App.handleGetAuthenticatedUser.bind(App));
432
482
ipcMain . on ( 'set-preferences' , App . handleSetPreferences . bind ( App ) ) ;
433
483
434
484
if ( process . platform === 'darwin' ) {
435
- electron . app . dock . hide ( ) ;
485
+ electron_app . dock . hide ( ) ;
436
486
437
- electron . systemPreferences . subscribeNotification ( 'AppleInterfaceThemeChangedNotification' , ( ) => {
438
- electron . systemPreferences . setAppLevelAppearance ( electron . systemPreferences . isDarkMode ( ) ? 'dark' : 'light' ) ;
487
+ systemPreferences . subscribeNotification ( 'AppleInterfaceThemeChangedNotification' , ( ) => {
488
+ systemPreferences . setAppLevelAppearance ( systemPreferences . isDarkMode ( ) ? 'dark' : 'light' ) ;
439
489
} ) ;
440
- electron . systemPreferences . setAppLevelAppearance ( electron . systemPreferences . isDarkMode ( ) ? 'dark' : 'light' ) ;
490
+ systemPreferences . setAppLevelAppearance ( systemPreferences . isDarkMode ( ) ? 'dark' : 'light' ) ;
441
491
}
442
492
443
- const interactive = ! electron . app . isPackaged && process . argv . includes ( '--interactive' ) ;
493
+ const interactive = ! electron_app . isPackaged && process . argv . includes ( '--interactive' ) ;
444
494
445
495
if ( interactive ) {
446
- electron . app . on ( 'quit' , ( ) => {
496
+ electron_app . on ( 'quit' , ( ) => {
447
497
require ( 'repl' ) . repl . close ( ) ;
448
498
} ) ;
449
499
450
500
log . warn ( 'Disabling logging for REPL' ) ;
451
501
console . log = console . error = ( ) => { } ;
452
502
503
+ // @ts -ignore
453
504
global . App = App ;
505
+ // @ts -ignore
454
506
global . app = app ;
455
507
508
+ // @ts -ignore
456
509
global . accessories = app . client . accessories ;
457
510
}
0 commit comments