Skip to content

Commit

Permalink
Broadlink Rf Toggle Error #157
Browse files Browse the repository at this point in the history
  • Loading branch information
= authored and haimkastner committed Jul 24, 2020
1 parent b396843 commit 90c8a00
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
16 changes: 8 additions & 8 deletions backend/src/modules/broadlink/broadlinkHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void | ErrorResponse> {
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<void | ErrorResponse> {
Expand All @@ -294,37 +294,37 @@ 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<void | ErrorResponse> {
const broadlink = (await this.getBroadlinkInstance(minion)) as BroadlinkAPI;

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<void | ErrorResponse> {
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(
minion: Minion,
statusToRecordFor: MinionStatus,
): Promise<void | ErrorResponse> {
const generatedCode = BroadlinkCodeGeneration.generate('RF433');
await this.commandsCacheManager.setToggleRFCommand(minion, statusToRecordFor, generatedCode);
await this.commandsCacheManager.cacheRFToggleCommand(minion, statusToRecordFor, generatedCode);
}

private async generateRollerRFCommand(
minion: Minion,
statusToRecordFor: MinionStatus,
): Promise<void | ErrorResponse> {
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<void | ErrorResponse> {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/tasmota/tasmotaHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 32 additions & 4 deletions backend/src/utilities/cacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ export class CommandsCacheManager extends CacheManager {
return hexCommandCode;
}

public async getRFRollerCommand(minion: Minion, status: MinionStatus): Promise<string | ErrorResponse> {

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<string | ErrorResponse> {

const minionCache = this.getOrCreateMinionCache(minion);
Expand Down Expand Up @@ -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<void | ErrorResponse> {
public async cacheIRACommand(minion: Minion, statusToRecordFor: MinionStatus, hexIRCommand: string): Promise<void | ErrorResponse> {

const minionCache = this.getOrCreateMinionCache(minion);

Expand Down Expand Up @@ -257,7 +285,7 @@ export class CommandsCacheManager extends CacheManager {
await this.saveCache();
}

public async setRollerRFCommand(minion: Minion, statusToRecordFor: MinionStatus, hexRfCommand: string): Promise<void | ErrorResponse> {
public async cacheRFRollerCommand(minion: Minion, statusToRecordFor: MinionStatus, hexRfCommand: string): Promise<void | ErrorResponse> {

const minionCache = this.getOrCreateMinionCache(minion);

Expand All @@ -281,7 +309,7 @@ export class CommandsCacheManager extends CacheManager {
await this.saveCache();
}

public async setToggleRFCommand(
public async cacheRFToggleCommand(
minion: Minion,
statusToRecordFor: MinionStatus,
hexRfCommand: string
Expand Down

0 comments on commit 90c8a00

Please sign in to comment.