From d82bfdcf62634d2db70908df86cb08e1478e8f0a Mon Sep 17 00:00:00 2001 From: Jiri Pokorny Date: Sun, 24 Dec 2023 23:00:14 +0100 Subject: [PATCH] Added diveId as an argument to planner --- .../src/app/shared/planner.service.spec.ts | 1 + .../planner/src/app/shared/planner.service.ts | 42 +++++++++++++------ .../src/app/shared/serialization.model.ts | 5 +++ .../planner/src/app/workers/planning.tasks.ts | 7 +++- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/projects/planner/src/app/shared/planner.service.spec.ts b/projects/planner/src/app/shared/planner.service.spec.ts index 139f2612..9c53f112 100644 --- a/projects/planner/src/app/shared/planner.service.spec.ts +++ b/projects/planner/src/app/shared/planner.service.spec.ts @@ -191,6 +191,7 @@ describe('PlannerService', () => { const profile = CalculatedProfile.fromErrors(depthsService.segments, events); const profileDto = DtoSerialization.fromProfile(profile); return { + diveId: 1, profile: profileDto, events: [] }; diff --git a/projects/planner/src/app/shared/planner.service.ts b/projects/planner/src/app/shared/planner.service.ts index dd4a49d0..298cba66 100644 --- a/projects/planner/src/app/shared/planner.service.ts +++ b/projects/planner/src/app/shared/planner.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; import { takeUntil } from 'rxjs'; +import _ from 'lodash'; import { DiveResults } from './diveresults'; import { WayPointsService } from './waypoints.service'; @@ -18,7 +19,7 @@ import { Streamed } from './streamed'; import { TanksService } from './tanks.service'; import { OptionsService } from './options.service'; import { DepthsService } from './depths.service'; -import { DiveSchedules } from './dive.schedules'; +import { DiveSchedule, DiveSchedules } from './dive.schedules'; import { UnitConversion } from './UnitConversion'; import { WayPoint } from './models'; import { ReloadDispatcher } from './reloadDispatcher'; @@ -26,6 +27,7 @@ import { ReloadDispatcher } from './reloadDispatcher'; @Injectable() export class PlannerService extends Streamed { + // TODO move calculating flags to dive private calculating = false; private calculatingDiveInfo = false; private calculatingProfile = false; @@ -82,15 +84,15 @@ export class PlannerService extends Streamed { } /** Not called by default, needs to be called manually */ - public calculate(diveIndex: number = 0): void { + public calculate(diveId: number = 1): void { this.startCalculatingState(); setTimeout(() => { this.showStillRunning(); }, 500); - const diveResult = this.diveResult(diveIndex); - const profileRequest = this.createProfileRequest(diveResult.finalTissues); + const diveResult = this.diveResult(diveId); + const profileRequest = this.createProfileRequest(diveResult.finalTissues, diveId); this.profileTask.calculate(profileRequest); } @@ -100,11 +102,11 @@ export class PlannerService extends Streamed { this.calculatingDiveInfo = true; } - private endCalculatingState(): void { + private endCalculatingState(diveId: number): void { this.calculating = false; this.calculatingProfile = false; this.calculatingDiveInfo = false; - const dive = this.dive; + const dive = this.diveResult(diveId); dive.profileCalculated = true; dive.diveInfoCalculated = true; dive.calculated = true; @@ -139,10 +141,11 @@ export class PlannerService extends Streamed { dive.averageDepth = Segments.averageDepth(calculatedProfile.segments); if (dive.endsOnSurface) { - const infoRequest = this.createProfileRequest(calculatedProfile.tissues); + const infoRequest = this.createProfileRequest(calculatedProfile.tissues, result.diveId); this.diveInfoTask.calculate(infoRequest); const consumptionRequest = { + diveId: result.diveId, plan: infoRequest.plan, profile: DtoSerialization.fromSegments(calculatedProfile.segments), options: infoRequest.options, @@ -156,13 +159,14 @@ export class PlannerService extends Streamed { this.dispatcher.sendWayPointsCalculated(); } else { // fires info finished before the profile finished, case of error it doesn't matter - this.profileFailed(); + this.profileFailed(result.diveId); console.table(calculatedProfile.errors); } } - private createProfileRequest(previousDivetissues: LoadedTissue[]): ProfileRequestDto { + private createProfileRequest(previousDivetissues: LoadedTissue[], diveId: number): ProfileRequestDto { return { + diveId: diveId, tanks: DtoSerialization.fromTanks(this.serializableTanks), plan: DtoSerialization.fromSegments(this.depths.segments), options: DtoSerialization.fromOptions(this.optionsService.getOptions()), @@ -180,13 +184,14 @@ export class PlannerService extends Streamed { return this.waypoints.calculateWayPoints(calculatedProfile.segments); } - private profileFailed(): void { + // TODO profileFailed, but for which dive? + private profileFailed(diveId: number = 0): void { const dive = this.dive; dive.calculationFailed = true; dive.events = []; dive.wayPoints = []; dive.ceilings = []; - this.endCalculatingState(); + this.endCalculatingState(diveId); // fire events, because there will be no continuation this.dispatcher.sendWayPointsCalculated(); this.dispatcher.sendInfoCalculated(); @@ -231,7 +236,18 @@ export class PlannerService extends Streamed { }; } - private diveResult(diveIndex: number): DiveResults { - return this.schedules.dives[diveIndex].diveResult; + private diveResult(diveId: number): DiveResults { + return this.diveBy(diveId).diveResult; + } + private diveBy(diveId: number): DiveSchedule { + const found = _(this.schedules.dives) + .filter(d => d.id === diveId) + .first(); + + if(found) { + return found; + } + + return this.schedules.dives[0]; } } diff --git a/projects/planner/src/app/shared/serialization.model.ts b/projects/planner/src/app/shared/serialization.model.ts index 38b6a9cb..55cbdd09 100644 --- a/projects/planner/src/app/shared/serialization.model.ts +++ b/projects/planner/src/app/shared/serialization.model.ts @@ -50,6 +50,7 @@ export interface ITankBound { } export interface ProfileRequestDto { + diveId: number; tanks: TankDto[]; plan: SegmentDto[]; options: OptionsDto; @@ -63,6 +64,7 @@ export interface EventOptionsDto { } export interface DiveInfoResultDto { + diveId: number; noDeco: number; otu: number; cns: number; @@ -76,6 +78,7 @@ export interface DensityDto { } export interface ProfileResultDto { + diveId: number; profile: CalculatedProfileDto; events: EventDto[]; } @@ -103,6 +106,7 @@ export interface LoadedTissueDto { } export interface ConsumptionRequestDto { + diveId: number; plan: SegmentDto[]; profile: SegmentDto[]; options: OptionsDto; @@ -118,6 +122,7 @@ export interface ConsumedDto { } export interface ConsumptionResultDto { + diveId: number; maxTime: number; timeToSurface: number; tanks: ConsumedDto[]; diff --git a/projects/planner/src/app/workers/planning.tasks.ts b/projects/planner/src/app/workers/planning.tasks.ts index f08d4212..659cecaf 100644 --- a/projects/planner/src/app/workers/planning.tasks.ts +++ b/projects/planner/src/app/workers/planning.tasks.ts @@ -27,6 +27,7 @@ export class PlanningTasks { const eventsDto = DtoSerialization.fromEvents(events.items); return { + diveId: task.diveId, profile: profileDto, events: eventsDto }; @@ -47,6 +48,7 @@ export class PlanningTasks { const density = new DensityAtDepth(depthConverter).forProfile(originalProfile); return { + diveId: task.diveId, noDeco: noDecoLimit, otu: otu, cns: cns, @@ -76,8 +78,9 @@ export class PlanningTasks { consumption.consumeFromTanks2(originProfile, emergencyAscent, options, tanks, diver); return { - maxTime, - timeToSurface, + diveId: task.diveId, + maxTime: maxTime, + timeToSurface: timeToSurface, tanks: DtoSerialization.toConsumed(tanks), }; }