@@ -7,7 +7,7 @@ import { IpcOutgoingEvent, IpcService } from "./ipcService";
7
7
import { ExternalPortService } from "./externalPortService" ;
8
8
import { HomebridgeAPI , PluginType } from "./api" ;
9
9
import { HomebridgeOptions } from "./server" ;
10
- import { Logger } from "./logger" ;
10
+ import { Logger , Logging } from "./logger" ;
11
11
import { Plugin } from "./plugin" ;
12
12
import { User } from "./user" ;
13
13
import {
@@ -81,7 +81,7 @@ export interface ChildProcessLoadEventData {
81
81
type : PluginType ;
82
82
identifier : string ;
83
83
pluginPath : string ;
84
- pluginConfig : PlatformConfig | AccessoryConfig ;
84
+ pluginConfig : Array < PlatformConfig | AccessoryConfig > ;
85
85
bridgeConfig : BridgeConfiguration ;
86
86
homebridgeConfig : HomebridgeConfig ;
87
87
bridgeOptions : BridgeOptions ;
@@ -115,26 +115,24 @@ export interface ChildMetadata {
115
115
*/
116
116
export class ChildBridgeService {
117
117
private child ?: child_process . ChildProcess ;
118
- private log = Logger . withPrefix ( this . pluginConfig ?. name || this . plugin . getPluginIdentifier ( ) ) ;
119
118
private args : string [ ] = [ ] ;
120
119
private shuttingDown = false ;
121
120
private lastBridgeStatus : ChildBridgeStatus = ChildBridgeStatus . PENDING ;
121
+ private pluginConfig : Array < PlatformConfig | AccessoryConfig > = [ ] ;
122
+ private log : Logging = Logger . withPrefix ( this . plugin . getPluginIdentifier ( ) ) ;
123
+ private displayName ?: string ;
122
124
123
125
constructor (
124
- private type : PluginType ,
125
- private identifier : string ,
126
+ public type : PluginType ,
127
+ public identifier : string ,
126
128
private plugin : Plugin ,
127
- private pluginConfig : PlatformConfig | AccessoryConfig ,
128
129
private bridgeConfig : BridgeConfiguration ,
129
130
private homebridgeConfig : HomebridgeConfig ,
130
131
private homebridgeOptions : HomebridgeOptions ,
131
132
private api : HomebridgeAPI ,
132
133
private ipcService : IpcService ,
133
134
private externalPortService : ExternalPortService ,
134
135
) {
135
- this . setProcessFlags ( ) ;
136
- this . startChildProcess ( ) ;
137
-
138
136
this . api . on ( "shutdown" , ( ) => {
139
137
this . shuttingDown = true ;
140
138
this . teardown ( ) ;
@@ -144,6 +142,33 @@ export class ChildBridgeService {
144
142
this . api . setMaxListeners ( this . api . getMaxListeners ( ) + 1 ) ;
145
143
}
146
144
145
+ /**
146
+ * Start the child bridge service
147
+ */
148
+ public start ( ) : void {
149
+ this . setProcessFlags ( ) ;
150
+ this . startChildProcess ( ) ;
151
+
152
+ // set display name
153
+ if ( this . pluginConfig . length > 1 || this . pluginConfig . length === 0 ) {
154
+ this . displayName = this . plugin . getPluginIdentifier ( ) ;
155
+ } else {
156
+ this . displayName = this . pluginConfig [ 0 ] ?. name || this . plugin . getPluginIdentifier ( ) ;
157
+ }
158
+
159
+ // re-configured log with display name
160
+ this . log = Logger . withPrefix ( this . displayName ) ;
161
+ }
162
+
163
+ /**
164
+ * Add a config block to a child bridge.
165
+ * Platform child bridges can only contain one config block.
166
+ * @param config
167
+ */
168
+ public addConfig ( config : PlatformConfig | AccessoryConfig ) : void {
169
+ this . pluginConfig . push ( config ) ;
170
+ }
171
+
147
172
private get bridgeStatus ( ) : ChildBridgeStatus {
148
173
return this . lastBridgeStatus ;
149
174
}
@@ -193,13 +218,17 @@ export class ChildBridgeService {
193
218
194
219
switch ( message . id ) {
195
220
case ChildProcessMessageEventType . READY : {
196
- this . log ( `Launched external bridge with PID ${ this . child ?. pid } ` ) ;
221
+ this . log ( `Launched child bridge with PID ${ this . child ?. pid } ` ) ;
197
222
this . loadPlugin ( ) ;
198
223
break ;
199
224
}
200
225
case ChildProcessMessageEventType . LOADED : {
201
226
const version = ( message . data as ChildProcessPluginLoadedEventData ) . version ;
202
- this . log ( `Loaded ${ this . plugin . getPluginIdentifier ( ) } v${ version } successfully` ) ;
227
+ if ( this . pluginConfig . length > 1 ) {
228
+ this . log ( `Loaded ${ this . plugin . getPluginIdentifier ( ) } v${ version } child bridge successfully with ${ this . pluginConfig . length } accessories` ) ;
229
+ } else {
230
+ this . log ( `Loaded ${ this . plugin . getPluginIdentifier ( ) } v${ version } child bridge successfully` ) ;
231
+ }
203
232
this . startBridge ( ) ;
204
233
break ;
205
234
}
@@ -284,7 +313,7 @@ export class ChildBridgeService {
284
313
*/
285
314
private loadPlugin ( ) : void {
286
315
const bridgeConfig : BridgeConfiguration = {
287
- name : this . bridgeConfig . name || this . pluginConfig . name || this . plugin . getPluginIdentifier ( ) ,
316
+ name : this . bridgeConfig . name || this . displayName || this . plugin . getPluginIdentifier ( ) ,
288
317
port : this . bridgeConfig . port ,
289
318
username : this . bridgeConfig . username ,
290
319
advertiser : this . homebridgeConfig . bridge . advertiser ,
@@ -366,18 +395,18 @@ export class ChildBridgeService {
366
395
const homebridgeConfig : HomebridgeConfig = await fs . readJson ( User . configPath ( ) ) ;
367
396
368
397
if ( this . type === PluginType . PLATFORM ) {
369
- const config = homebridgeConfig . platforms ?. find ( x => x . platform === this . identifier && x . _bridge ?. username === this . bridgeConfig . username ) ;
370
- if ( config ) {
398
+ const config = homebridgeConfig . platforms ?. filter ( x => x . platform === this . identifier && x . _bridge ?. username === this . bridgeConfig . username ) ;
399
+ if ( config . length ) {
371
400
this . pluginConfig = config ;
372
- this . bridgeConfig = this . pluginConfig . _bridge || this . bridgeConfig ;
401
+ this . bridgeConfig = this . pluginConfig [ 0 ] . _bridge || this . bridgeConfig ;
373
402
} else {
374
403
this . log . warn ( "Platform config could not be found, using existing config." ) ;
375
404
}
376
405
} else if ( this . type === PluginType . ACCESSORY ) {
377
- const config = homebridgeConfig . accessories ?. find ( x => x . accessory === this . identifier && x . _bridge ?. username === this . bridgeConfig . username ) ;
378
- if ( config ) {
406
+ const config = homebridgeConfig . accessories ?. filter ( x => x . accessory === this . identifier && x . _bridge ?. username === this . bridgeConfig . username ) ;
407
+ if ( config . length ) {
379
408
this . pluginConfig = config ;
380
- this . bridgeConfig = this . pluginConfig . _bridge || this . bridgeConfig ;
409
+ this . bridgeConfig = this . pluginConfig [ 0 ] . _bridge || this . bridgeConfig ;
381
410
} else {
382
411
this . log . warn ( "Accessory config could not be found, using existing config." ) ;
383
412
}
@@ -395,7 +424,7 @@ export class ChildBridgeService {
395
424
return {
396
425
status : this . bridgeStatus ,
397
426
username : this . bridgeConfig . username ,
398
- name : this . bridgeConfig . name || this . pluginConfig . name || this . plugin . getPluginIdentifier ( ) ,
427
+ name : this . bridgeConfig . name || this . displayName || this . plugin . getPluginIdentifier ( ) ,
399
428
plugin : this . plugin . getPluginIdentifier ( ) ,
400
429
identifier : this . identifier ,
401
430
pid : this . child ?. pid ,
0 commit comments