diff --git a/backend/src/modules/broadlink/broadlinkHandler.ts b/backend/src/modules/broadlink/broadlinkHandler.ts index 9362aa47..09157928 100644 --- a/backend/src/modules/broadlink/broadlinkHandler.ts +++ b/backend/src/modules/broadlink/broadlinkHandler.ts @@ -271,17 +271,17 @@ export class BroadlinkHandler extends BrandModuleBase { await this.sendBeamCommand(broadlink, hexCommandCode); - await this.commandsCacheManager.setLastStatus(minion, setStatus); + await this.commandsCacheManager.cacheLastStatus(minion, setStatus); } private async setRFRollerStatus(minion: Minion, setStatus: MinionStatus): Promise { const broadlink = (await this.getBroadlinkInstance(minion)) as BroadlinkAPI; - const hexCommandCode = await this.commandsCacheManager.getRFToggleCommand(minion, setStatus) as string; + const hexCommandCode = await this.commandsCacheManager.getRFRollerCommand(minion, setStatus) as string; await this.sendBeamCommand(broadlink, hexCommandCode); - await this.commandsCacheManager.setLastStatus(minion, setStatus); + await this.commandsCacheManager.cacheLastStatus(minion, setStatus); } private async setIrAcStatus(minion: Minion, setStatus: MinionStatus): Promise { @@ -294,7 +294,7 @@ export class BroadlinkHandler extends BrandModuleBase { await Delay(moment.duration(1, 'seconds')); await this.sendBeamCommand(broadlink, hexCommandCode); - await this.commandsCacheManager.setLastStatus(minion, setStatus); + await this.commandsCacheManager.cacheLastStatus(minion, setStatus); } private async recordIRACommands(minion: Minion, statusToRecordFor: MinionStatus): Promise { @@ -302,13 +302,13 @@ export class BroadlinkHandler extends BrandModuleBase { const hexIRCommand = await this.enterBeamLearningMode(broadlink); - await this.commandsCacheManager.setIRACommands(minion, statusToRecordFor, hexIRCommand); + await this.commandsCacheManager.cacheIRACommand(minion, statusToRecordFor, hexIRCommand); } private async recordRollerRFCommand(minion: Minion, statusToRecordFor: MinionStatus): Promise { const broadlink = (await this.getBroadlinkInstance(minion)) as BroadlinkAPI; const hexIRCommand = await this.enterBeamLearningMode(broadlink); - await this.commandsCacheManager.setRollerRFCommand(minion, statusToRecordFor, hexIRCommand); + await this.commandsCacheManager.cacheRFRollerCommand(minion, statusToRecordFor, hexIRCommand); } private async generateToggleRFCommand( @@ -316,7 +316,7 @@ export class BroadlinkHandler extends BrandModuleBase { statusToRecordFor: MinionStatus, ): Promise { const generatedCode = BroadlinkCodeGeneration.generate('RF433'); - await this.commandsCacheManager.setToggleRFCommand(minion, statusToRecordFor, generatedCode); + await this.commandsCacheManager.cacheRFToggleCommand(minion, statusToRecordFor, generatedCode); } private async generateRollerRFCommand( @@ -324,7 +324,7 @@ export class BroadlinkHandler extends BrandModuleBase { statusToRecordFor: MinionStatus, ): Promise { const generatedCode = BroadlinkCodeGeneration.generate('RF433'); - await this.commandsCacheManager.setRollerRFCommand(minion, statusToRecordFor, generatedCode); + await this.commandsCacheManager.cacheRFRollerCommand(minion, statusToRecordFor, generatedCode); } private async recordRFToggleCommands(minion: Minion, statusToRecordFor: MinionStatus): Promise { diff --git a/backend/src/modules/tasmota/tasmotaHandler.ts b/backend/src/modules/tasmota/tasmotaHandler.ts index 0679a6b1..2f43814f 100644 --- a/backend/src/modules/tasmota/tasmotaHandler.ts +++ b/backend/src/modules/tasmota/tasmotaHandler.ts @@ -149,7 +149,7 @@ export class TasmotaHandler extends BrandModuleBase { if (results.IRSend !== 'Done') { throw new Error(`[tasmotaHandler.setAcStatus] Sending IR command failed ${JSON.stringify(results)}`); } - await this.commandsCacheManager.setLastStatus(minion, setStatus); + await this.commandsCacheManager.cacheLastStatus(minion, setStatus); } catch (error) { logger.warn(`Sent Tasmota command ${minion.minionId} fail, ${JSON.stringify(error.message)}`); throw { diff --git a/backend/src/utilities/cacheManager.ts b/backend/src/utilities/cacheManager.ts index 825bebd8..059b16bc 100644 --- a/backend/src/utilities/cacheManager.ts +++ b/backend/src/utilities/cacheManager.ts @@ -185,6 +185,34 @@ export class CommandsCacheManager extends CacheManager { return hexCommandCode; } + public async getRFRollerCommand(minion: Minion, status: MinionStatus): Promise { + + const minionCache = this.getOrCreateMinionCache(minion); + + if (!minionCache.rollerCommands) { + throw { + responseCode: 4503, + message: 'there is no available command. record a roller commands set.', + } as ErrorResponse; + } + + const hexCommandCode = + status.roller.status === 'off' ? + minionCache.rollerCommands.off : + status.roller.direction === 'up' ? + minionCache.rollerCommands.up : + minionCache.rollerCommands.down; + + if (!hexCommandCode) { + throw { + responseCode: 4503, + message: 'there is no available command. record a roller commands set.', + } as ErrorResponse; + } + + return hexCommandCode; + } + public async getIrCommand(minion: Minion, setStatus: MinionStatus): Promise { const minionCache = this.getOrCreateMinionCache(minion); @@ -223,13 +251,13 @@ export class CommandsCacheManager extends CacheManager { return hexCommandCode; } - public async setLastStatus(minion: Minion, setStatus: MinionStatus) { + public async cacheLastStatus(minion: Minion, setStatus: MinionStatus) { const minionCache = this.getOrCreateMinionCache(minion); minionCache.lastStatus = setStatus; await this.saveCache(); } - public async setIRACommands(minion: Minion, statusToRecordFor: MinionStatus, hexIRCommand: string): Promise { + public async cacheIRACommand(minion: Minion, statusToRecordFor: MinionStatus, hexIRCommand: string): Promise { const minionCache = this.getOrCreateMinionCache(minion); @@ -257,7 +285,7 @@ export class CommandsCacheManager extends CacheManager { await this.saveCache(); } - public async setRollerRFCommand(minion: Minion, statusToRecordFor: MinionStatus, hexRfCommand: string): Promise { + public async cacheRFRollerCommand(minion: Minion, statusToRecordFor: MinionStatus, hexRfCommand: string): Promise { const minionCache = this.getOrCreateMinionCache(minion); @@ -281,7 +309,7 @@ export class CommandsCacheManager extends CacheManager { await this.saveCache(); } - public async setToggleRFCommand( + public async cacheRFToggleCommand( minion: Minion, statusToRecordFor: MinionStatus, hexRfCommand: string