Skip to content

Commit

Permalink
Fixing issue with main switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-winn committed Jun 25, 2023
1 parent a005a24 commit 648a69e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 29 deletions.
26 changes: 13 additions & 13 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

/**
Expand All @@ -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'
}

/**
Expand Down
19 changes: 10 additions & 9 deletions src/services/policies/mowerIsEnabledPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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',
Expand Down
12 changes: 6 additions & 6 deletions test/services/policies/mowerIsEnabledPolicy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand Down

0 comments on commit 648a69e

Please sign in to comment.