Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shell): add option to configure switches
- Loading branch information
Showing
7 changed files
with
169 additions
and
5 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
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
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,43 @@ | ||
const mockExec = jest.fn(); | ||
|
||
import { ShellSwitch } from './shell.switch'; | ||
|
||
jest.mock('util', () => ({ | ||
...jest.requireActual('util'), | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
promisify: () => mockExec | ||
})); | ||
|
||
describe('ShellSwitch', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should execute the onCommand when turned on', async () => { | ||
const onCommand = 'echo on'; | ||
const shellSwitch = new ShellSwitch('test', 'Test', onCommand, 'echo off'); | ||
await shellSwitch.turnOn(); | ||
|
||
expect(mockExec).toHaveBeenCalledWith(onCommand); | ||
expect(shellSwitch.state).toBeTruthy(); | ||
}); | ||
|
||
it('should execute the offCommand when turned off', async () => { | ||
const offCommand = 'echo off'; | ||
const shellSwitch = new ShellSwitch('test', 'Test', 'echo on', offCommand); | ||
await shellSwitch.turnOff(); | ||
|
||
expect(mockExec).toHaveBeenCalledWith(offCommand); | ||
expect(shellSwitch.state).toBeFalsy(); | ||
}); | ||
|
||
it('should not switch the state if command execution fails', async () => { | ||
mockExec.mockRejectedValue(new Error('command failed')); | ||
|
||
const shellSwitch = new ShellSwitch('test', 'Test', 'echo on', 'echo off'); | ||
shellSwitch.state = false; | ||
await shellSwitch.turnOn(); | ||
|
||
expect(shellSwitch.state).toBeFalsy(); | ||
}); | ||
}); |
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,43 @@ | ||
import { Switch } from '../../entities/switch'; | ||
import * as util from 'util'; | ||
import { exec } from 'child_process'; | ||
import { Logger } from '@nestjs/common'; | ||
|
||
const execPromise = util.promisify(exec); | ||
|
||
export class ShellSwitch extends Switch { | ||
onCommand: string; | ||
offCommand: string; | ||
|
||
constructor(id: string, name: string, onCommand: string, offCommand: string) { | ||
super(id, name, false); | ||
this.onCommand = onCommand; | ||
this.offCommand = offCommand; | ||
} | ||
|
||
async turnOn(): Promise<void> { | ||
try { | ||
await execPromise(this.onCommand); | ||
super.turnOn(); | ||
} catch (e) { | ||
Logger.error( | ||
`Turning ${this.id} on failed: ${e.message}`, | ||
e.stack, | ||
ShellSwitch.name | ||
); | ||
} | ||
} | ||
|
||
async turnOff(): Promise<void> { | ||
try { | ||
await execPromise(this.offCommand); | ||
super.turnOff(); | ||
} catch (e) { | ||
Logger.error( | ||
`Turning ${this.id} off failed: ${e.message}`, | ||
e.stack, | ||
ShellSwitch.name | ||
); | ||
} | ||
} | ||
} |