From 648a69e7165b2391d3caf84b567b240024682a8c Mon Sep 17 00:00:00 2001 From: Jeff Winn <6961614+jeff-winn@users.noreply.github.com> Date: Sun, 25 Jun 2023 13:37:02 -0400 Subject: [PATCH] Fixing issue with main switch. --- src/model.ts | 26 +++--- src/services/policies/mowerIsEnabledPolicy.ts | 19 +++-- .../automowerMowerStateConverter.spec.ts | 84 ++++++++++++++++++- .../policies/mowerIsEnabledPolicy.spec.ts | 12 +-- 4 files changed, 112 insertions(+), 29 deletions(-) diff --git a/src/model.ts b/src/model.ts index 3795bdff..64bc2e3f 100644 --- a/src/model.ts +++ b/src/model.ts @@ -29,22 +29,22 @@ export enum Activity { /** * The mower is mowing. */ - MOWING, + MOWING = 'mowing', /** * The mower is parked. */ - PARKED, + PARKED = 'parked', /** * The mower is off. */ - OFF, + OFF = 'off', /** * The mower activity is unknown. */ - UNKNOWN + UNKNOWN = 'unknown' } /** @@ -54,47 +54,47 @@ export enum State { /** * The mower is going home. */ - GOING_HOME, + GOING_HOME = 'going_home', /** * The mower is leaving home. */ - LEAVING_HOME, + LEAVING_HOME = 'leaving_home', /** * The mower is charging in the docking station. */ - CHARGING, + CHARGING = 'charging', /** * The mower is in operation. */ - IN_OPERATION, + IN_OPERATION = 'in_operation', /** * The mower is idle while waiting for an instruction, or until the next scheduled execution. */ - IDLE, + IDLE = 'idle', /** * The mower is paused. */ - PAUSED, + PAUSED = 'paused', /** * The mower has faulted. */ - FAULTED, + FAULTED = 'faulted', /** * The mower has been tampered. */ - TAMPERED, + TAMPERED = 'tampered', /** * The mower state is unknown. */ - UNKNOWN + UNKNOWN = 'unknown' } /** diff --git a/src/services/policies/mowerIsEnabledPolicy.ts b/src/services/policies/mowerIsEnabledPolicy.ts index fbc4ae2c..249b5cda 100644 --- a/src/services/policies/mowerIsEnabledPolicy.ts +++ b/src/services/policies/mowerIsEnabledPolicy.ts @@ -49,6 +49,11 @@ export abstract class AbstractMowerIsEnabledPolicy implements MowerIsEnabledPoli public setMowerState(state: MowerState): void { this.mowerState = state; } + + protected isMowerMowing(mowerState: MowerState): boolean { + return mowerState.activity === Activity.MOWING && (mowerState.state === State.IN_OPERATION || mowerState.state === State.LEAVING_HOME || + mowerState.state === State.GOING_HOME || mowerState.state === State.CHARGING); + } } /** @@ -78,14 +83,11 @@ export class DeterministicMowerIsScheduledPolicy extends AbstractMowerIsEnabledP return false; } - if (this.mowerSchedule.runContinuously) { - // The mower is set to run continuously, which means the switch is now being used to control whether the - // mower is actually mowing the yard rather than whether a schedule is enabled. - return mowerState.activity === Activity.MOWING && (mowerState.state === State.IN_OPERATION || mowerState.state === State.LEAVING_HOME || - mowerState.state === State.GOING_HOME || mowerState.state === State.CHARGING); - } + return this.isMowerScheduledToRunLater(this.mowerSchedule) || this.isMowerMowing(mowerState); + } - return this.mowerSchedule.runOnSchedule && this.mowerSchedule.runInFuture; + protected isMowerScheduledToRunLater(mowerSchedule: MowerSchedule): boolean { + return mowerSchedule.runOnSchedule && mowerSchedule.runInFuture; } } @@ -94,7 +96,6 @@ export class DeterministicMowerIsScheduledPolicy extends AbstractMowerIsEnabledP */ export class DeterministicMowerIsActivePolicy extends AbstractMowerIsEnabledPolicy { protected override checkCore(mowerState: MowerState): boolean { - return mowerState.activity === Activity.MOWING && (mowerState.state === State.IN_OPERATION || mowerState.state === State.LEAVING_HOME || - mowerState.state === State.GOING_HOME || mowerState.state === State.CHARGING); + return this.isMowerMowing(mowerState); } } \ No newline at end of file diff --git a/test/services/husqvarna/automower/converters/automowerMowerStateConverter.spec.ts b/test/services/husqvarna/automower/converters/automowerMowerStateConverter.spec.ts index 9cc786ea..321e930d 100644 --- a/test/services/husqvarna/automower/converters/automowerMowerStateConverter.spec.ts +++ b/test/services/husqvarna/automower/converters/automowerMowerStateConverter.spec.ts @@ -2,7 +2,7 @@ import { It, Mock, Times } from 'moq.ts'; import * as model from '../../../../../src/model'; -import { Activity, HeadlightMode, Mode, Mower, RestrictedReason, State } from '../../../../../src/clients/automower/automowerClient'; +import { Activity, HeadlightMode, Mode, Mower, OverrideAction, RestrictedReason, State } from '../../../../../src/clients/automower/automowerClient'; import { PlatformLogger } from '../../../../../src/diagnostics/platformLogger'; import { AutomowerMowerStateConverterImpl } from '../../../../../src/services/husqvarna/automower/converters/automowerMowerStateConverter'; @@ -16,6 +16,88 @@ describe('AutomowerMowerStateConverterImpl', () => { target = new AutomowerMowerStateConverterImpl(log.object()); }); + it('should return mowing when forced to mow', () => { + const mower: Mower = { + type: 'mower', + id: 'ed6c1900-5a15-4143-8c9b-6fb01ad1a606', + attributes: { + system: { + name: 'Dobby', + model: 'HUSQVARNA AUTOMOWER® 430XH', + serialNumber: 192401442 + }, + battery: { + batteryPercent: 69 + }, + mower: { + mode: Mode.MAIN_AREA, + activity: Activity.MOWING, + state: State.IN_OPERATION, + errorCode: 0, + errorCodeTimestamp: 0 + }, + calendar: { + tasks: [ + { + start: 0, + duration: 1440, + monday: true, + tuesday: true, + wednesday: true, + thursday: true, + friday: true, + saturday: false, + sunday: true + }, + { + start: 750, + duration: 60, + monday: false, + tuesday: false, + wednesday: false, + thursday: false, + friday: false, + saturday: true, + sunday: false + } + ] + }, + planner: { + nextStartTimestamp: 0, + override: { + action: OverrideAction.FORCE_MOW + }, + restrictedReason: RestrictedReason.NOT_APPLICABLE + }, + metadata: { + connected: true, + statusTimestamp: 1687708005776 + }, + positions: [], + settings: { + cuttingHeight: 8, + headlight: { + mode: HeadlightMode.EVENING_AND_NIGHT + } + }, + statistics: { + numberOfChargingCycles: 350, + numberOfCollisions: 11473, + totalChargingTime: 1170000, + totalCuttingTime: 3315600, + totalRunningTime: 3636000, + totalSearchingTime: 320400 + } + } + }; + + const result = target.convertMower(mower); + + expect(result).toBeDefined(); + expect(result.activity).toEqual(model.Activity.MOWING); + expect(result.state).toEqual(model.State.IN_OPERATION); + }); + it('should return parked when charging', () => { const mower: Mower = { id: '12345', diff --git a/test/services/policies/mowerIsEnabledPolicy.spec.ts b/test/services/policies/mowerIsEnabledPolicy.spec.ts index 5b40e56a..a7faddd2 100644 --- a/test/services/policies/mowerIsEnabledPolicy.spec.ts +++ b/test/services/policies/mowerIsEnabledPolicy.spec.ts @@ -139,8 +139,8 @@ describe('DeterministicMowerIsScheduledPolicy', () => { it('should return false when run on schedule and not run in future', () => { target.setMowerState({ - activity: Activity.MOWING, - state: State.IN_OPERATION + activity: Activity.PARKED, + state: State.IDLE }); target.setMowerSchedule({ @@ -154,8 +154,8 @@ describe('DeterministicMowerIsScheduledPolicy', () => { it('should return false when not run on schedule and run in future', () => { target.setMowerState({ - activity: Activity.MOWING, - state: State.IN_OPERATION + activity: Activity.PARKED, + state: State.IDLE }); target.setMowerSchedule({ @@ -169,8 +169,8 @@ describe('DeterministicMowerIsScheduledPolicy', () => { it('should return false when not run on schedule and not run in future', () => { target.setMowerState({ - activity: Activity.MOWING, - state: State.IN_OPERATION + activity: Activity.PARKED, + state: State.IDLE }); target.setMowerSchedule({