Skip to content

Commit

Permalink
refactor: decouple binding types & task types
Browse files Browse the repository at this point in the history
- servo binding task handled as "set angle" task
- removed stored task hashing in favor of on-demand hashing
  • Loading branch information
nvsukhanov committed Apr 4, 2024
1 parent cf926c3 commit b1c5ae2
Show file tree
Hide file tree
Showing 89 changed files with 627 additions and 428 deletions.
11 changes: 2 additions & 9 deletions modules/bindings/src/lib/binding-l10n.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { IBindingControllerInputNameResolver, IBindingTypeToL10nKeyMapper } from '@app/shared-control-schemes';
import { IBindingInputNameResolver, IPortCommandTaskSummaryBuilder } from '@app/control-scheme-view';
import { ControlSchemeBinding, ControlSchemeBindingInputs, ControlSchemeInputConfig, PortCommandTask } from '@app/store';
import { IBindingInputNameResolver } from '@app/control-scheme-view';
import { ControlSchemeBinding, ControlSchemeBindingInputs, ControlSchemeInputConfig } from '@app/store';
import { ControlSchemeBindingType } from '@app/shared-misc';

import { IBindingL10n } from './i-binding-l10n';
Expand All @@ -17,7 +17,6 @@ import { TrainBindingL10nService } from './train';
export class BindingL10nService implements
IBindingControllerInputNameResolver,
IBindingInputNameResolver,
IPortCommandTaskSummaryBuilder,
IBindingTypeToL10nKeyMapper {
private readonly bindingL10nServices: { [k in ControlSchemeBindingType]: IBindingL10n<k> } = {
[ControlSchemeBindingType.Gearbox]: this.gearboxBindingL10nService,
Expand All @@ -38,12 +37,6 @@ export class BindingL10nService implements
) {
}

public buildTaskSummary<T extends ControlSchemeBindingType>(
portCommandTask: PortCommandTask<T>
): Observable<string> {
return this.getL10nService(portCommandTask.payload.bindingType).buildTaskSummary(portCommandTask);
}

public getBindingActionName<T extends ControlSchemeBinding>(
binding: T,
action: keyof T['inputs']
Expand Down
17 changes: 2 additions & 15 deletions modules/bindings/src/lib/binding-task-factory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import { SpeedBindingTaskPayloadBuilderService } from './speed';
import { TrainBindingTaskPayloadBuilderService } from './train';
import { StepperBindingTaskPayloadBuilderService } from './stepper';
import { GearboxBindingTaskPayloadBuilderService } from './gearbox';
import { BindingTaskPayloadHashBuilderService } from './binding-task-payload-hash-builder.service';
import { ITaskPayloadBuilder } from './i-task-payload-factory';
import { BindingInputExtractionResult } from './i-binding-task-input-extractor';
import { calculateTaskHash } from './common';

@Injectable()
export class BindingTaskFactoryService implements ITaskFactory {
Expand All @@ -31,7 +29,6 @@ export class BindingTaskFactoryService implements ITaskFactory {
private readonly trainTaskPayloadBuilder: TrainBindingTaskPayloadBuilderService,
private readonly stepperTaskPayloadBuilder: StepperBindingTaskPayloadBuilderService,
private readonly gearboxTaskPayloadBuilder: GearboxBindingTaskPayloadBuilderService,
private readonly hashBuilder: BindingTaskPayloadHashBuilderService
) {
}

Expand All @@ -56,8 +53,7 @@ export class BindingTaskFactoryService implements ITaskFactory {
if (payload) {
return {
...previousTask,
payload,
hash: this.calculateHash(previousTask.hubId, previousTask.portId, payload)
payload
};
}
return null;
Expand All @@ -83,15 +79,7 @@ export class BindingTaskFactoryService implements ITaskFactory {
private buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
return this.taskPayloadBuilders[previousTask.payload.bindingType].buildCleanupPayload(previousTask);
}

private calculateHash(
hubId: string,
portId: number,
payload: PortCommandTaskPayload
): string {
return calculateTaskHash(hubId, portId, this.hashBuilder.buildHash(payload));
return this.taskPayloadBuilders[previousTask.payload.type].buildCleanupPayload(previousTask);
}

private composeTask(
Expand All @@ -103,7 +91,6 @@ export class BindingTaskFactoryService implements ITaskFactory {
hubId: binding.hubId,
portId: binding.portId,
payload,
hash: this.calculateHash(binding.hubId, binding.portId, payload),
inputTimestamp
};
}
Expand Down

This file was deleted.

43 changes: 0 additions & 43 deletions modules/bindings/src/lib/binding-task-runner.service.ts

This file was deleted.

3 changes: 0 additions & 3 deletions modules/bindings/src/lib/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ export * from './controls';
export * from './calc-input-gain';
export * from './calculate-next-looping-index';
export * from './snap-speed';
export * from './map-use-profile';
export * from './hash-compare-filter.service';
export * from './most-recent-task-filter.service';
export * from './common-bindings-form-controls-builder.service';
export * from './common-form-mapper.service';
export * from './binding-edit-common.selectors';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Provider } from '@angular/core';

import { MostRecentTaskFilterService } from './most-recent-task-filter.service';
import { HashCompareFilterService } from './hash-compare-filter.service';
import { BindingTaskPayloadHashBuilderService } from '../binding-task-payload-hash-builder.service';
import { ControllerInputNameService } from './controller-input-name.service';
import { InputExtractorService } from './input-extractor.service';

export function provideBindingCommonServices(): Provider[] {
return [
MostRecentTaskFilterService,
HashCompareFilterService,
BindingTaskPayloadHashBuilderService,
ControllerInputNameService,
InputExtractorService
];
Expand Down
11 changes: 1 addition & 10 deletions modules/bindings/src/lib/gearbox/gearbox-binding-l10n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Observable } from 'rxjs';
import { TranslocoService } from '@ngneat/transloco';
import { Injectable } from '@angular/core';
import { ControlSchemeBindingType } from '@app/shared-misc';
import { ControlSchemeInputConfig, GearboxBindingInputAction, PortCommandTask } from '@app/store';
import { ControlSchemeInputConfig, GearboxBindingInputAction } from '@app/store';

import { IBindingL10n } from '../i-binding-l10n';
import { ControllerInputNameService } from '../common';
Expand All @@ -17,15 +17,6 @@ export class GearboxBindingL10nService implements IBindingL10n<ControlSchemeBind
) {
}

public buildTaskSummary(
task: PortCommandTask<ControlSchemeBindingType.Gearbox>
): Observable<string> {
const level = task.payload.initialLevelIndex - task.payload.angleIndex;
const angle = task.payload.angle;
const isLooping = task.payload.isLooping;
return this.translocoService.selectTranslate('controlScheme.gearboxBinding.taskSummary', { level, angle, isLooping });
}

public getBindingInputName(
actionType: GearboxBindingInputAction
): Observable<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
GearboxBindingInputAction,
GearboxTaskPayload,
PortCommandTask,
PortCommandTaskPayload
PortCommandTaskPayload,
TaskType
} from '@app/store';

import { calculateNextLoopingIndex, isTriggeredInputActivated } from '../common';
Expand All @@ -22,8 +23,8 @@ export class GearboxBindingTaskPayloadBuilderService implements ITaskPayloadBuil
ioProps: Omit<AttachedIoPropsModel, 'hubId' | 'portId'> | null,
previousTask: PortCommandTask | null
): { payload: GearboxTaskPayload; inputTimestamp: number } | null {
const gearboxPrevTask = previousTask && previousTask.payload.bindingType === ControlSchemeBindingType.Gearbox
? previousTask as PortCommandTask<ControlSchemeBindingType.Gearbox>
const gearboxPrevTask = previousTask && previousTask.payload.type === TaskType.Gearbox
? previousTask as PortCommandTask<TaskType.Gearbox>
: null;
return this.buildPayloadUsingPreviousTask(
binding,
Expand All @@ -37,11 +38,11 @@ export class GearboxBindingTaskPayloadBuilderService implements ITaskPayloadBuil
public buildCleanupPayload(
previousTask: PortCommandTask
): PortCommandTaskPayload | null {
if (previousTask.payload.bindingType !== ControlSchemeBindingType.Gearbox) {
if (previousTask.payload.type !== TaskType.Gearbox) {
return null;
}
return {
bindingType: ControlSchemeBindingType.Speed,
type: TaskType.Speed,
speed: 0,
power: 0,
brakeFactor: 0,
Expand All @@ -55,7 +56,7 @@ export class GearboxBindingTaskPayloadBuilderService implements ITaskPayloadBuil
currentInput: BindingInputExtractionResult<ControlSchemeBindingType.Gearbox>,
previousInput: BindingInputExtractionResult<ControlSchemeBindingType.Gearbox>,
motorEncoderOffset: number,
previousTask: PortCommandTask<ControlSchemeBindingType.Gearbox> | null
previousTask: PortCommandTask<TaskType.Gearbox> | null
): { payload: GearboxTaskPayload; inputTimestamp: number } | null {
const nextLevelInput = this.getActiveInput(binding, currentInput, previousInput, GearboxBindingInputAction.NextGear);
const prevLevelInput = this.getActiveInput(binding, currentInput, previousInput, GearboxBindingInputAction.PrevGear);
Expand Down Expand Up @@ -88,7 +89,7 @@ export class GearboxBindingTaskPayloadBuilderService implements ITaskPayloadBuil

return {
payload: {
bindingType: ControlSchemeBindingType.Gearbox,
type: TaskType.Gearbox,
initialLevelIndex: binding.initialLevelIndex,
angleIndex: nextIndex,
offset: motorEncoderOffset,
Expand All @@ -109,7 +110,7 @@ export class GearboxBindingTaskPayloadBuilderService implements ITaskPayloadBuil
motorEncoderOffset: number
): GearboxTaskPayload {
return {
bindingType: ControlSchemeBindingType.Gearbox,
type: TaskType.Gearbox,
initialLevelIndex: binding.initialLevelIndex,
angleIndex: binding.initialLevelIndex,
offset: motorEncoderOffset,
Expand Down
2 changes: 0 additions & 2 deletions modules/bindings/src/lib/gearbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ export * from './gearbox-binding-edit.component';
export * from './gearbox-binding-form-builder.service';
export * from './gearbox-binding-form-mapper.service';
export * from './gearbox-binding-task-payload-builder.service';
export * from './gearbox-binding-task-runner.service';
export * from './gearbox-binding-payload-hash';
export * from './provide-gearbox-binding';
export * from './gearbox-binding-input-extractor.service';
export * from './gearbox-binding-l10n.service';
2 changes: 0 additions & 2 deletions modules/bindings/src/lib/gearbox/provide-gearbox-binding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Provider } from '@angular/core';

import { GearboxBindingTaskPayloadBuilderService } from './gearbox-binding-task-payload-builder.service';
import { GearboxBindingTaskRunnerService } from './gearbox-binding-task-runner.service';
import { GearboxBindingFormBuilderService } from './gearbox-binding-form-builder.service';
import { GearboxBindingFormMapperService } from './gearbox-binding-form-mapper.service';
import { GearboxBindingInputExtractorService } from './gearbox-binding-input-extractor.service';
Expand All @@ -10,7 +9,6 @@ import { GearboxBindingL10nService } from './gearbox-binding-l10n.service';
export function provideGearboxBinding(): Provider[] {
return [
GearboxBindingTaskPayloadBuilderService,
GearboxBindingTaskRunnerService,
GearboxBindingFormBuilderService,
GearboxBindingFormMapperService,
GearboxBindingInputExtractorService,
Expand Down
6 changes: 1 addition & 5 deletions modules/bindings/src/lib/i-binding-l10n.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Observable } from 'rxjs';
import { ControlSchemeBindingType } from '@app/shared-misc';
import { ControlSchemeBinding, ControlSchemeBindingInputs, ControlSchemeInputConfig, PortCommandTask } from '@app/store';
import { ControlSchemeBinding, ControlSchemeBindingInputs, ControlSchemeInputConfig } from '@app/store';

export interface IBindingL10n<T extends ControlSchemeBindingType> {
readonly bindingTypeL10nKey: string;

buildTaskSummary(
task: PortCommandTask<T>
): Observable<string>;

getBindingInputName(
actionType: keyof ControlSchemeBindingInputs<T>,
binding: ControlSchemeBinding & { bindingType: T },
Expand Down
10 changes: 0 additions & 10 deletions modules/bindings/src/lib/i-binding-task-filter.ts

This file was deleted.

11 changes: 0 additions & 11 deletions modules/bindings/src/lib/i-binding-task-runner.ts

This file was deleted.

2 changes: 1 addition & 1 deletion modules/bindings/src/lib/i-task-payload-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ITaskPayloadBuilder<TBindingType extends ControlSchemeBindingTy
previousInput: BindingInputExtractionResult<TBindingType>,
ioProps: Omit<AttachedIoPropsModel, 'hubId' | 'portId'> | null,
previousTaskPayload: PortCommandTask | null
): { payload: PortCommandTaskPayload & { bindingType: TBindingType }; inputTimestamp: number } | null;
): { payload: PortCommandTaskPayload; inputTimestamp: number } | null;

buildCleanupPayload(
previousTask: PortCommandTask
Expand Down
9 changes: 2 additions & 7 deletions modules/bindings/src/lib/provide-bindings.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Provider } from '@angular/core';
import { TASKS_INPUT_EXTRACTOR, TASK_FACTORY, TASK_FILTER, TASK_RUNNER } from '@app/store';
import { TASKS_INPUT_EXTRACTOR, TASK_FACTORY } from '@app/store';
import {
BINDING_CONTROLLER_INPUT_NAME_RESOLVER,
BINDING_DETAILS_EDIT_FORM_RENDERER_FACTORY,
BINDING_TYPE_TO_L10N_KEY_MAPPER,
BINDING_VALIDATOR
} from '@app/shared-control-schemes';
import { BINDING_INPUT_NAME_RESOLVER, PORT_COMMAND_TASK_SUMMARY_BUILDER } from '@app/control-scheme-view';
import { BINDING_INPUT_NAME_RESOLVER } from '@app/control-scheme-view';

import { provideGearboxBinding } from './gearbox';
import { provideSpeedBinding } from './speed';
import { provideSetAngleBinding } from './set-angle';
import { provideStepperBinding } from './stepper';
import { BindingTaskFactoryService } from './binding-task-factory.service';
import { provideServoBinding } from './servo';
import { BindingTaskRunnerService } from './binding-task-runner.service';
import { provideBindingCommonServices } from './common';
import { BindingTaskFilterService } from './binding-task-filter.service';
import { BindingDetailsEditFormRendererFactoryService } from './binding-details-edit-form-renderer-factory.service';
import { BindingValidatorService } from './binding-validator.service';
import { provideTrainBinding } from './train';
Expand All @@ -33,11 +31,8 @@ export function provideBindings(): Provider[] {
...provideTrainBinding(),
...provideBindingCommonServices(),
{ provide: TASK_FACTORY, useClass: BindingTaskFactoryService },
{ provide: TASK_RUNNER, useClass: BindingTaskRunnerService },
{ provide: TASK_FILTER, useClass: BindingTaskFilterService },
{ provide: BINDING_DETAILS_EDIT_FORM_RENDERER_FACTORY, useClass: BindingDetailsEditFormRendererFactoryService },
{ provide: BINDING_VALIDATOR, useClass: BindingValidatorService },
{ provide: PORT_COMMAND_TASK_SUMMARY_BUILDER, useClass: BindingL10nService },
{ provide: TASKS_INPUT_EXTRACTOR, useClass: BindingInputExtractorService },
{ provide: BINDING_CONTROLLER_INPUT_NAME_RESOLVER, useClass: BindingL10nService },
{ provide: BINDING_INPUT_NAME_RESOLVER, useClass: BindingL10nService },
Expand Down
2 changes: 0 additions & 2 deletions modules/bindings/src/lib/servo/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export * from './servo-binding-edit.component';
export * from './servo-binding-payload-hash';
export * from './servo-binding-task-payload-builder.service';
export * from './servo-binding-task-runner.service';
export * from './servo-binding-form-builder.service';
export * from './servo-binding-form-mapper.service';
export * from './provide-servo-binding';
Expand Down
Loading

0 comments on commit b1c5ae2

Please sign in to comment.