generated from K-FOSS/TS-Core-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Much more typing and docs, and examples
- Loading branch information
Showing
25 changed files
with
907 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// src/Lab/Devices/Jumper/index.ts | ||
import { BaseDevice } from '../../../Modules/Device/BaseDevice'; | ||
import { DeviceType } from '../../../Modules/Device/Device'; | ||
import { DeviceInfo } from '../../../Modules/Device/DeviceInfo'; | ||
import { TimerTrait } from '@k-foss/ts-gactions/Modules/Trait/Traits/TimerTrait'; | ||
import { CommandType } from '@k-foss/ts-gactions/Modules/Command/BaseCommand'; | ||
import { ModesTrait } from '@k-foss/ts-gactions/Modules/Trait/Traits/ModeTrait'; | ||
import { OnOffTrait } from '@k-foss/ts-gactions/Modules/Trait'; | ||
|
||
interface CustomData { | ||
timer?: NodeJS.Timeout; | ||
timerPaused: boolean; | ||
|
||
timerRemainingSec: number; | ||
|
||
modeSetting: ObjectGet<Record<string, unknown>>; | ||
|
||
on: boolean; | ||
} | ||
|
||
export class Jumper extends BaseDevice<Jumper['traits']> { | ||
public id = '123'; | ||
|
||
public deviceInfo: DeviceInfo = { | ||
hwVersion: '0.0.1', | ||
manufacturer: 'KJDev', | ||
model: '0.0.1', | ||
swVersion: '0.0.1', | ||
}; | ||
|
||
public name: BaseDevice<Jumper['traits']>['name'] = { | ||
name: 'Jumper', | ||
defaultNames: ['Jumper'], | ||
nicknames: ['Jumper'], | ||
}; | ||
|
||
public type = DeviceType.Switch; | ||
|
||
public attributes: BaseDevice<Jumper['traits']>['attributes'] = { | ||
availableModes: [ | ||
{ | ||
name: 'range', | ||
name_values: [ | ||
{ | ||
name_synonym: ['Distance'], | ||
lang: 'en', | ||
}, | ||
], | ||
settings: [ | ||
{ | ||
setting_name: '1h', | ||
setting_values: [ | ||
{ | ||
setting_synonym: ['1 Hour', '1', '1h'], | ||
lang: 'en', | ||
}, | ||
], | ||
}, | ||
{ | ||
setting_name: '2h', | ||
setting_values: [ | ||
{ | ||
setting_synonym: ['2 Hours', '2 hour', '2', '2h'], | ||
lang: 'en', | ||
}, | ||
], | ||
}, | ||
], | ||
ordered: true, | ||
}, | ||
], | ||
commandOnlyTimer: false, | ||
maxTimerLimitSec: 60000, | ||
commandOnlyModes: false, | ||
commandOnlyOnOff: false, | ||
queryOnlyOnOff: false, | ||
}; | ||
|
||
public traits = [ | ||
new TimerTrait(), | ||
new ModesTrait(), | ||
new OnOffTrait(), | ||
] as const; | ||
|
||
public customData: CustomData = { | ||
timerPaused: false, | ||
timer: undefined, | ||
timerRemainingSec: -1, | ||
modeSetting: { | ||
distance: '1h', | ||
}, | ||
on: false, | ||
}; | ||
|
||
public startInterval(): void { | ||
this.customData.timer = setInterval(() => { | ||
this.customData.timerRemainingSec -= 1; | ||
|
||
if (this.customData.timerRemainingSec <= -1) { | ||
if (!this.customData.timer) { | ||
return; | ||
} | ||
|
||
clearInterval(this.customData.timer); | ||
} | ||
}, 1000); | ||
} | ||
|
||
public startTimer(timerTimeSec: number): void { | ||
this.customData.timerRemainingSec = timerTimeSec; | ||
|
||
this.startInterval(); | ||
} | ||
|
||
public stopTimer(): void { | ||
if (!this.customData.timer) { | ||
return; | ||
} | ||
|
||
clearInterval(this.customData.timer); | ||
this.customData.timerPaused = false; | ||
} | ||
|
||
public pauseTimer(): void { | ||
if (!this.customData.timer) { | ||
return; | ||
} | ||
|
||
clearInterval(this.customData.timer); | ||
this.customData.timerPaused = true; | ||
} | ||
|
||
public getStatus: BaseDevice<Jumper['traits']>['getStatus'] = async () => { | ||
return { | ||
timerPaused: this.customData.timerPaused, | ||
timerRemainingSec: this.customData.timerRemainingSec, | ||
currentModeSettings: this.customData.modeSetting, | ||
on: this.customData.on, | ||
}; | ||
}; | ||
|
||
public executeCommand: BaseDevice< | ||
Jumper['traits'] | ||
>['executeCommand'] = async (command) => { | ||
switch (command.type) { | ||
case CommandType.TimerStart: | ||
break; | ||
case CommandType.TimerCancel: | ||
this.stopTimer(); | ||
break; | ||
case CommandType.TimerPause: | ||
this.pauseTimer(); | ||
break; | ||
case CommandType.TimerResume: | ||
this.startInterval(); | ||
this.customData.timerPaused = false; | ||
break; | ||
case CommandType.TimerAdjust: | ||
break; | ||
case CommandType.SetMode: | ||
this.customData.modeSetting = command.updateModeSettings; | ||
break; | ||
case CommandType.OnOff: | ||
this.customData.on = command.on; | ||
break; | ||
} | ||
}; | ||
} |
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,16 @@ | ||
// src/Modules/Command/Commands/SetModeCommand.ts | ||
import { BaseComamnd, CommandType } from '../BaseCommand'; | ||
import { ObjectGet } from '@k-foss/ts-gactions/Utils/types'; | ||
|
||
/** | ||
* | ||
* https://developers.google.com/assistant/smarthome/traits/modes#device-commands | ||
*/ | ||
export class SetModesCommand extends BaseComamnd { | ||
public type = CommandType.SetMode as const; | ||
|
||
/** | ||
* Object containing the new string value (setting_name) for each mode that's being set. | ||
*/ | ||
public updateModeSettings: ObjectGet<Record<string, unknown>>; | ||
} |
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,18 @@ | ||
// src/Modules/Command/Commands/TimerAdjustCommand.ts | ||
import { BaseComamnd, CommandType } from '../BaseCommand'; | ||
|
||
/** | ||
* Adjust a running timer. | ||
* https://developers.google.com/assistant/smarthome/traits/timer#device-commands | ||
*/ | ||
export class TimerAdjustCommand extends BaseComamnd { | ||
public type = CommandType.TimerAdjust as const; | ||
|
||
/** | ||
* Positive or negative integer in seconds [-maxTimerLimitSec, maxTimerLimitSec]. | ||
* Agent may return valueOutOfRange error if remaining seconds is smaller than the | ||
* absolute value of the specified timerTimeSec. For instance, agent may return a | ||
* valueOutOfRange error if remaining seconds is 20 but timerTimeSec is -30. | ||
*/ | ||
public timerTimeSec: number; | ||
} |
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,10 @@ | ||
// src/Modules/Command/Commands/TimerCancelCommand.ts | ||
import { BaseComamnd, CommandType } from '../BaseCommand'; | ||
|
||
/** | ||
* Stop and delete the currently running or paused timer. | ||
* https://developers.google.com/assistant/smarthome/traits/timer#device-commands | ||
*/ | ||
export class TimerCancelCommand extends BaseComamnd { | ||
public type = CommandType.TimerCancel as const; | ||
} |
Oops, something went wrong.