Skip to content

Commit

Permalink
refactor(task processing): remove port command task type
Browse files Browse the repository at this point in the history
  • Loading branch information
nvsukhanov committed Aug 23, 2023
1 parent 9fc720a commit 1784872
Show file tree
Hide file tree
Showing 29 changed files with 142 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Pipe, PipeTransform } from '@angular/core';
import { Observable, filter, switchMap } from 'rxjs';
import { Store } from '@ngrx/store';
import { ATTACHED_IO_PROPS_SELECTORS, AttachedIoPropsModel, PortCommandTask, PortCommandTaskType } from '@app/store';
import { ATTACHED_IO_PROPS_SELECTORS, AttachedIoPropsModel, PortCommandTask } from '@app/store';
import { ControlSchemeBindingType } from '@app/shared';

import { LinearPortCommandTaskSummaryBuilderService } from './linear-port-command-task-summary-builder.service';
import { ServoPortCommandTaskSummaryBuilderService } from './servo-port-command-task-summary-builder.service';
Expand Down Expand Up @@ -29,28 +30,28 @@ export class PortCommandTaskSummaryPipe implements PipeTransform {
portCommandTask: PortCommandTask
): Observable<string> {
const payload = portCommandTask.payload;
switch (payload.taskType) {
case PortCommandTaskType.SetSpeed:
switch (payload.bindingType) {
case ControlSchemeBindingType.Linear:
return this.linearPortCommandTaskSummaryBuilder.build(payload);
case PortCommandTaskType.SetAngle:
case ControlSchemeBindingType.SetAngle:
return this.store.select(ATTACHED_IO_PROPS_SELECTORS.selectById(portCommandTask)).pipe(
filter((ioProps): ioProps is AttachedIoPropsModel => !!ioProps),
switchMap((ioProps) => this.setAnglePortCommandTaskSummaryBuilder.build(
ioProps,
payload
))
);
case PortCommandTaskType.Servo:
case ControlSchemeBindingType.Servo:
return this.store.select(ATTACHED_IO_PROPS_SELECTORS.selectById(portCommandTask)).pipe(
filter((ioProps): ioProps is AttachedIoPropsModel => !!ioProps),
switchMap((ioProps) => this.servoPortCommandTaskSummaryBuilder.build(
ioProps,
payload
))
);
case PortCommandTaskType.Stepper:
case ControlSchemeBindingType.Stepper:
return this.stepperPortCommandTaskSummaryBuilder.build(payload);
case PortCommandTaskType.SpeedStepper:
case ControlSchemeBindingType.SpeedStepper:
return this.speedStepperPortCommandTaskSummaryBuilder.build(payload);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { concatLatestFrom, createEffect } from '@ngrx/effects';
import { NEVER, Observable, animationFrames, filter, from, map, merge, share, switchMap } from 'rxjs';
import { Action, Store } from '@ngrx/store';
import { inject } from '@angular/core';
import { CONTROLLER_CONNECTION_SELECTORS, CONTROLLER_INPUT_ACTIONS, CONTROLLER_INPUT_SELECTORS, controllerInputIdFn } from '@app/store';
import { APP_CONFIG, ControllerInputType, ControllerType, IAppConfig, WINDOW } from '@app/shared';

import { CONTROLLER_CONNECTION_SELECTORS, CONTROLLER_INPUT_SELECTORS } from '../../../selectors';
import { CONTROLLER_INPUT_ACTIONS } from '../../../actions';
import { controllerInputIdFn } from '../../../reducers';
import { GamepadValueTransformService } from '../../../../controller-profiles';

function readGamepads(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PortCommandTaskPayload, PortCommandTaskType } from '../../../models';
import { ControlSchemeBindingType } from '@app/shared';

import { PortCommandTaskPayload } from '../../../models';
import { servoPayloadHash } from './servo-payload-hash';
import { setAnglePayloadHash } from './set-angle-payload-hash';
import { setSpeedPayloadHash } from './set-speed-payload-hash';
Expand All @@ -8,16 +10,16 @@ import { speedStepperPayloadHash } from './speed-stepper-payload-hash';
export function payloadHash(
payload: PortCommandTaskPayload
): string {
switch (payload.taskType) {
case PortCommandTaskType.Servo:
switch (payload.bindingType) {
case ControlSchemeBindingType.Servo:
return servoPayloadHash(payload);
case PortCommandTaskType.SetAngle:
case ControlSchemeBindingType.SetAngle:
return setAnglePayloadHash(payload);
case PortCommandTaskType.SetSpeed:
case ControlSchemeBindingType.Linear:
return setSpeedPayloadHash(payload);
case PortCommandTaskType.Stepper:
case ControlSchemeBindingType.Stepper:
return stepperPayloadHash(payload);
case PortCommandTaskType.SpeedStepper:
case ControlSchemeBindingType.SpeedStepper:
return speedStepperPayloadHash(payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function servoPayloadHash(
payload: ServoTaskPayload
): string {
return [
payload.taskType,
payload.bindingType,
payload.angle,
payload.speed,
payload.power,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function setSpeedPayloadHash(
payload: SetLinearSpeedTaskPayload
): string {
return [
payload.taskType,
payload.bindingType,
payload.speed,
payload.power,
].join('_');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SpeedStepperTaskPayload } from '@app/store';
import { SpeedStepperTaskPayload } from '../../../models';

export function speedStepperPayloadHash(
payload: SpeedStepperTaskPayload
): string {
return [
payload.taskType,
payload.bindingType,
payload.level,
payload.nextSpeedActiveInput,
payload.prevSpeedActiveInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function stepperPayloadHash(
payload: StepperTaskPayload
): string {
return [
payload.taskType,
payload.bindingType,
payload.degree,
payload.speed,
payload.power,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ControlSchemeBindingType, getTranslationArcs } from '@app/shared';
import { controllerInputIdFn } from '@app/store';

import { BaseTaskBuilder } from './base-task-builder';
import { ControlSchemeBinding, ControllerInputModel, PortCommandTask, PortCommandTaskPayload, PortCommandTaskType, ServoTaskPayload } from '../../../models';
import { ControlSchemeBinding, ControllerInputModel, PortCommandTask, PortCommandTaskPayload, ServoTaskPayload } from '../../../models';
import { calcInputGain } from './calc-input-gain';

export class ServoTaskBuilder extends BaseTaskBuilder {
Expand Down Expand Up @@ -35,7 +35,7 @@ export class ServoTaskBuilder extends BaseTaskBuilder {
const snappedAngle = this.snapAngle(targetAngle, resultingCenter, minAngle, maxAngle);

const payload: ServoTaskPayload = {
taskType: PortCommandTaskType.Servo,
bindingType: ControlSchemeBindingType.Servo,
angle: Math.round(snappedAngle),
speed: Math.round(binding.speed),
power: binding.power,
Expand All @@ -49,11 +49,11 @@ export class ServoTaskBuilder extends BaseTaskBuilder {
protected buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
if (previousTask.payload.taskType !== PortCommandTaskType.Servo) {
if (previousTask.payload.bindingType !== ControlSchemeBindingType.Servo) {
return null;
}
return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: 0,
power: 0,
activeInput: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Dictionary } from '@ngrx/entity';
import { ControlSchemeBindingType } from '@app/shared';
import { controllerInputIdFn } from '@app/store';

import { controllerInputIdFn } from '../../../reducers';
import { BaseTaskBuilder } from './base-task-builder';
import { ControlSchemeBinding, ControllerInputModel, PortCommandTask, PortCommandTaskPayload, PortCommandTaskType, SetAngleTaskPayload } from '../../../models';
import { ControlSchemeBinding, ControllerInputModel, PortCommandTask, PortCommandTaskPayload, SetAngleTaskPayload } from '../../../models';

export class SetAngleTaskBuilder extends BaseTaskBuilder {
private readonly inputValueThreshold = 0.5;
Expand All @@ -23,7 +23,7 @@ export class SetAngleTaskBuilder extends BaseTaskBuilder {
return null;
}
const payload: SetAngleTaskPayload = {
taskType: PortCommandTaskType.SetAngle,
bindingType: ControlSchemeBindingType.SetAngle,
angle: binding.angle,
speed: binding.speed,
power: binding.power,
Expand All @@ -38,11 +38,11 @@ export class SetAngleTaskBuilder extends BaseTaskBuilder {
protected buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
if (previousTask.payload.taskType !== PortCommandTaskType.SetAngle) {
if (previousTask.payload.bindingType !== ControlSchemeBindingType.SetAngle) {
return null;
}
return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: 0,
power: 0,
activeInput: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
ControllerInputModel,
PortCommandTask,
PortCommandTaskPayload,
PortCommandTaskType,
SetLinearSpeedTaskPayload
} from '../../../models';
import { calcInputGain } from './calc-input-gain';
Expand Down Expand Up @@ -58,7 +57,7 @@ export class SetSpeedTaskBuilder extends BaseTaskBuilder {
);

const payload: SetLinearSpeedTaskPayload = {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: targetSpeed,
power: this.calculatePower(targetSpeed, brakeInputValue, binding.power),
activeInput: accelerateInputValue !== 0,
Expand All @@ -72,11 +71,11 @@ export class SetSpeedTaskBuilder extends BaseTaskBuilder {
protected buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
if (previousTask.payload.taskType !== PortCommandTaskType.SetSpeed) {
if (previousTask.payload.bindingType !== ControlSchemeBindingType.Linear) {
return null;
}
return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: 0,
power: 0,
activeInput: false,
Expand Down Expand Up @@ -107,7 +106,7 @@ export class SetSpeedTaskBuilder extends BaseTaskBuilder {
binding.inputs.accelerate.gain
);
return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed,
power: this.calculatePower(speed, assumedBrakeInput, binding.power),
activeInput: true,
Expand All @@ -117,7 +116,7 @@ export class SetSpeedTaskBuilder extends BaseTaskBuilder {
}

return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: 0,
power: this.calculatePower(0, assumedBrakeInput, binding.power),
activeInput: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ControllerInputModel,
PortCommandTask,
PortCommandTaskPayload,
PortCommandTaskType,
SpeedStepperTaskPayload,
controllerInputIdFn
} from '@app/store';
Expand All @@ -30,11 +29,11 @@ export class SpeedStepperTaskBuilder extends BaseTaskBuilder {
const isPrevSpeedInputActive = !!binding.inputs.prevSpeed
&& (inputsState[controllerInputIdFn(binding.inputs.prevSpeed)]?.value ?? 0) > this.inputThreshold;
const isStopInputActive = !!binding.inputs.stop && (inputsState[controllerInputIdFn(binding.inputs.stop)]?.value ?? 0) > this.inputThreshold;

if (isStopInputActive) {
return {
payload: {
taskType: PortCommandTaskType.SpeedStepper,
bindingType: ControlSchemeBindingType.SpeedStepper,
nextSpeedActiveInput: isNextSpeedInputActive,
prevSpeedActiveInput: isPrevSpeedInputActive,
speed: 0,
Expand Down Expand Up @@ -64,7 +63,7 @@ export class SpeedStepperTaskBuilder extends BaseTaskBuilder {
}

const payload: SpeedStepperTaskPayload = {
taskType: PortCommandTaskType.SpeedStepper,
bindingType: ControlSchemeBindingType.SpeedStepper,
level: nextLevel,
speed: binding.steps[nextLevel],
power: binding.power,
Expand All @@ -79,11 +78,11 @@ export class SpeedStepperTaskBuilder extends BaseTaskBuilder {
protected buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
if (previousTask.payload.taskType !== PortCommandTaskType.SetAngle) {
if (previousTask.payload.bindingType !== ControlSchemeBindingType.SetAngle) {
return null;
}
return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: 0,
power: 0,
activeInput: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Dictionary } from '@ngrx/entity';
import { ControlSchemeBindingType } from '@app/shared';
import { controllerInputIdFn } from '@app/store';

import { ControlSchemeBinding, ControllerInputModel, PortCommandTask, PortCommandTaskPayload, StepperTaskPayload } from '../../../models';
import { controllerInputIdFn } from '../../../reducers';
import { BaseTaskBuilder } from './base-task-builder';
import { ControlSchemeBinding, ControllerInputModel, PortCommandTask, PortCommandTaskPayload, PortCommandTaskType, StepperTaskPayload } from '../../../models';

export class StepperTaskBuilder extends BaseTaskBuilder {
protected buildPayload(
Expand All @@ -22,7 +22,7 @@ export class StepperTaskBuilder extends BaseTaskBuilder {
}

const payload: StepperTaskPayload = {
taskType: PortCommandTaskType.Stepper,
bindingType: ControlSchemeBindingType.Stepper,
degree: binding.degree,
speed: binding.speed,
power: binding.power,
Expand All @@ -37,11 +37,11 @@ export class StepperTaskBuilder extends BaseTaskBuilder {
protected buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
if (previousTask.payload.taskType !== PortCommandTaskType.Stepper) {
if (previousTask.payload.bindingType !== ControlSchemeBindingType.Stepper) {
return null;
}
return {
taskType: PortCommandTaskType.SetSpeed,
bindingType: ControlSchemeBindingType.Linear,
speed: 0,
power: 0,
activeInput: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { PortCommandTask, PortCommandTaskType } from '../../../models';
import { ControlSchemeBindingType } from '@app/shared';

import { PortCommandTask } from '../../../models';

const SERVO_DEGREES_THRESHOLD = 10;

export function servoTaskFilter(
task: PortCommandTask<PortCommandTaskType.Servo>,
task: PortCommandTask<ControlSchemeBindingType.Servo>,
lastExecutedTask: PortCommandTask | null
): boolean {
if (!lastExecutedTask || lastExecutedTask.payload.taskType !== PortCommandTaskType.Servo) {
if (!lastExecutedTask || lastExecutedTask.payload.bindingType !== ControlSchemeBindingType.Servo) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { PortCommandTask, PortCommandTaskType } from '../../../models';
import { ControlSchemeBindingType } from '@app/shared';

import { PortCommandTask } from '../../../models';

export function setAngleTaskFilter(
task: PortCommandTask<PortCommandTaskType.SetAngle>,
task: PortCommandTask<ControlSchemeBindingType.SetAngle>,
lastExecutedTask: PortCommandTask | null
): boolean {
if (!lastExecutedTask || lastExecutedTask.payload.taskType !== PortCommandTaskType.SetAngle) {
if (!lastExecutedTask || lastExecutedTask.payload.bindingType !== ControlSchemeBindingType.SetAngle) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { PortCommandTask, PortCommandTaskType } from '../../../models';
import { ControlSchemeBindingType } from '@app/shared';

import { PortCommandTask } from '../../../models';

export function setSpeedTaskFilter(
task: PortCommandTask<PortCommandTaskType.SetSpeed>,
task: PortCommandTask<ControlSchemeBindingType.Linear>,
lastExecutedTask: PortCommandTask | null
): boolean {
if (!lastExecutedTask || lastExecutedTask.payload.taskType !== PortCommandTaskType.SetSpeed) {
if (!lastExecutedTask || lastExecutedTask.payload.bindingType !== ControlSchemeBindingType.Linear) {
return task.payload.activeInput;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { PortCommandTask, PortCommandTaskType } from '@app/store';
import { ControlSchemeBindingType } from '@app/shared';

import { PortCommandTask } from '../../../models';

export function speedStepperTaskFilter(
task: PortCommandTask<PortCommandTaskType.SpeedStepper>,
task: PortCommandTask<ControlSchemeBindingType.SpeedStepper>,
lastExecutedTask: PortCommandTask | null
): boolean {
if (!lastExecutedTask || lastExecutedTask.payload.taskType !== PortCommandTaskType.SpeedStepper) {
if (!lastExecutedTask || lastExecutedTask.payload.bindingType !== ControlSchemeBindingType.SpeedStepper) {
return true;
}

Expand Down
Loading

0 comments on commit 1784872

Please sign in to comment.