-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use common ipc service for hb-service homebridge control
- Loading branch information
Showing
18 changed files
with
267 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "homebridge-config-ui-x", | ||
"displayName": "Homebridge UI", | ||
"version": "4.37.1-test.3", | ||
"version": "4.37.1-test.5", | ||
"description": "A web based management, configuration and control platform for Homebridge", | ||
"license": "MIT", | ||
"author": "oznu <[email protected]>", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LoggerModule } from '../logger/logger.module'; | ||
import { HomebridgeIpcService } from './homebridge-ipc.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
LoggerModule, | ||
], | ||
providers: [ | ||
HomebridgeIpcService | ||
], | ||
exports: [ | ||
HomebridgeIpcService | ||
], | ||
}) | ||
export class HomebridgeIpcModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { Injectable, ServiceUnavailableException } from '@nestjs/common'; | ||
import { ChildProcess } from 'child_process'; | ||
import { EventEmitter } from 'events'; | ||
import { Logger } from '../logger/logger.service'; | ||
|
||
@Injectable() | ||
export class HomebridgeIpcService extends EventEmitter { | ||
private homebridge: ChildProcess; | ||
|
||
private permittedEvents = [ | ||
'childBridgeMetadataResponse', | ||
'childBridgeStatusUpdate', | ||
]; | ||
|
||
constructor( | ||
private logger: Logger | ||
) { | ||
super(); | ||
} | ||
|
||
/** | ||
* Set the current homebridge process. | ||
* This method is called from hb-service. | ||
*/ | ||
public setHomebridgeProcess(process: ChildProcess) { | ||
this.homebridge = process; | ||
|
||
this.homebridge.on('message', (message: { id: string; data: unknown }) => { | ||
if (typeof message !== 'object' || !message.id) { | ||
return; | ||
} | ||
if (this.permittedEvents.includes(message.id)) { | ||
this.emit(message.id, message.data); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Send a message to the homebridge child process | ||
*/ | ||
private sendMessage(type: string, data: unknown) { | ||
if (this.homebridge && this.homebridge.connected) { | ||
this.homebridge.send({ id: type, data: data }); | ||
} else { | ||
throw new ServiceUnavailableException('The Homebridge Service Is Unavailable'); | ||
} | ||
} | ||
|
||
/** | ||
* Send a data request to homebridge and wait for the reply | ||
*/ | ||
private async requestResponse(requestEvent: string, responseEvent: string) { | ||
return new Promise((resolve, reject) => { | ||
const actionTimeout = setTimeout(() => { | ||
this.removeListener(responseEvent, listener); | ||
reject('The Homebridge service did not respond'); | ||
}, 3000); | ||
|
||
const listener = (data) => { | ||
clearTimeout(actionTimeout); | ||
resolve(data); | ||
}; | ||
|
||
this.once(responseEvent, listener); | ||
this.homebridge.send({ id: requestEvent }); | ||
}); | ||
} | ||
|
||
/** | ||
* Restarts the main bridge process and any child bridges | ||
*/ | ||
public restartHomebridge(): void { | ||
if (this.homebridge) { | ||
this.logger.log('Sending SIGTERM to Homebridge'); | ||
|
||
// send SIGTERM command | ||
this.homebridge.kill('SIGTERM'); | ||
|
||
// prepare a timeout to send SIGKILL after 7 seconds if not shutdown before then | ||
const shutdownTimeout = setTimeout(() => { | ||
try { | ||
this.logger.warn('Sending SIGKILL to Homebridge'); | ||
this.homebridge.kill('SIGKILL'); | ||
} catch (e) { } | ||
}, 7000); | ||
|
||
// if homebridge ends before the timeout, clear the timeout | ||
this.homebridge.once('close', () => { | ||
clearTimeout(shutdownTimeout); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Restarts and resolves once homebridge is stopped. | ||
*/ | ||
public async restartAndWaitForClose(): Promise<boolean> { | ||
if (!this.homebridge || !this.homebridge.connected) { | ||
return true; | ||
} else { | ||
return new Promise((resolve) => { | ||
this.homebridge.once('close', () => { | ||
resolve(true); | ||
}); | ||
this.restartHomebridge(); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Send a SIGKILL to the homebridge process | ||
*/ | ||
public async killHomebridge() { | ||
if (this.homebridge) { | ||
this.logger.log('Sending SIGKILL to Homebridge'); | ||
this.homebridge.kill('SIGKILL'); | ||
} | ||
} | ||
|
||
/** | ||
* Restart a Homebridge child bridge | ||
*/ | ||
public async restartChildBridge(username: string) { | ||
await this.sendMessage('restartChildBridge', username); | ||
} | ||
|
||
/** | ||
* Request a list of child bridges from the Homebridge process | ||
*/ | ||
public async getChildBridgeMetadata() { | ||
return await this.requestResponse('childBridgeMetadataRequest', 'childBridgeMetadataResponse'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.