Skip to content

Commit

Permalink
Added diveId as an argument to planner
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Dec 24, 2023
1 parent eb3a760 commit d82bfdc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions projects/planner/src/app/shared/planner.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('PlannerService', () => {
const profile = CalculatedProfile.fromErrors(depthsService.segments, events);
const profileDto = DtoSerialization.fromProfile(profile);
return {
diveId: 1,
profile: profileDto,
events: []
};
Expand Down
42 changes: 29 additions & 13 deletions projects/planner/src/app/shared/planner.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -18,14 +19,15 @@ 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';


@Injectable()
export class PlannerService extends Streamed {
// TODO move calculating flags to dive
private calculating = false;
private calculatingDiveInfo = false;
private calculatingProfile = false;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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()),
Expand All @@ -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();
Expand Down Expand Up @@ -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];
}
}
5 changes: 5 additions & 0 deletions projects/planner/src/app/shared/serialization.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface ITankBound {
}

export interface ProfileRequestDto {
diveId: number;
tanks: TankDto[];
plan: SegmentDto[];
options: OptionsDto;
Expand All @@ -63,6 +64,7 @@ export interface EventOptionsDto {
}

export interface DiveInfoResultDto {
diveId: number;
noDeco: number;
otu: number;
cns: number;
Expand All @@ -76,6 +78,7 @@ export interface DensityDto {
}

export interface ProfileResultDto {
diveId: number;
profile: CalculatedProfileDto;
events: EventDto[];
}
Expand Down Expand Up @@ -103,6 +106,7 @@ export interface LoadedTissueDto {
}

export interface ConsumptionRequestDto {
diveId: number;
plan: SegmentDto[];
profile: SegmentDto[];
options: OptionsDto;
Expand All @@ -118,6 +122,7 @@ export interface ConsumedDto {
}

export interface ConsumptionResultDto {
diveId: number;
maxTime: number;
timeToSurface: number;
tanks: ConsumedDto[];
Expand Down
7 changes: 5 additions & 2 deletions projects/planner/src/app/workers/planning.tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class PlanningTasks {
const eventsDto = DtoSerialization.fromEvents(events.items);

return {
diveId: task.diveId,
profile: profileDto,
events: eventsDto
};
Expand All @@ -47,6 +48,7 @@ export class PlanningTasks {
const density = new DensityAtDepth(depthConverter).forProfile(originalProfile);

return {
diveId: task.diveId,
noDeco: noDecoLimit,
otu: otu,
cns: cns,
Expand Down Expand Up @@ -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),
};
}
Expand Down

0 comments on commit d82bfdc

Please sign in to comment.