-
Notifications
You must be signed in to change notification settings - Fork 2
/
SequenceLogic.ts
454 lines (381 loc) · 16 KB
/
SequenceLogic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import { CameraDeviceSettings, BackofficeStatus, SequenceStatus, Sequence, SequenceStep, SequenceStepStatus, SequenceStepParameters} from '@bo/BackOfficeStatus';
import { hasKey } from './shared/Obj';
export type SequenceWithStatus = {
step: SequenceStep;
status: SequenceStepStatus;
};
export type SequenceSize = {
totalCount: number;
totalTime: number;
}
export type Progress = {
/** 0 based */
imagePosition: number;
timeSpent: number;
} & SequenceSize;
export class SequenceLogic {
private readonly sequence: Sequence;
private readonly uuid:()=>string;
constructor(sequence: Sequence, uuid:()=>string) {
this.sequence = sequence;
this.uuid = uuid;
}
statusDone(current: SequenceWithStatus):boolean {
if ((current.status.finishedLoopCount || 0) >= Math.max(current.step.repeat || 0, 1)) {
return true;
}
return false;
}
// Assume a foreach is ongoing. null if nothing left to iterate
getNextForeach(current: SequenceWithStatus) : string | null {
for(let i = 0; i < current.step.foreach!.list.length; ++i) {
const foreachId = current.step.foreach!.list[i];
if (!Object.prototype.hasOwnProperty.call(current.status.finishedForeach, foreachId)) {
return foreachId;
}
}
return null;
}
// Get the finished foreach count, ignored removed foreach
getEffectiveFinishedForeachCount(current: SequenceWithStatus): number {
if (!current.step.foreach) {
return 0;
}
if (!current.status.finishedForeach) {
return 0;
}
let ret = 0;
for(const foreachId of current.step.foreach.list) {
if (Object.prototype.hasOwnProperty.call(current.status.finishedForeach, foreachId)) {
ret++;
}
}
return ret;
}
finish(current: SequenceWithStatus){
// We can't decide here if foreach is the last...
if (current.status.currentForeach !== null) {
if (!current.status.finishedForeach) {
current.status.finishedForeach = {};
}
current.status.finishedForeach[current.status.currentForeach] = true;
const newForeach = this.getNextForeach(current);
if (newForeach !== null) {
current.status.currentForeach = newForeach;
current.status.execUuid = this.uuid();
return;
}
}
current.status.finishedLoopCount = (current.status.finishedLoopCount || 0) + 1;
// Set those here to allow proper display of paused sequence.
current.status.finishedForeach = (current.step.foreach ? {} : null);
current.status.currentForeach = (current.step.foreach ? this.getNextForeach(current) : null);
current.status.execUuid = this.uuid();
}
getExecution(stepUuid: string, parentExec?: SequenceStepStatus): SequenceStepStatus|null {
if (Object.prototype.hasOwnProperty.call(this.sequence.stepStatus, stepUuid)) {
const currentStatus = this.sequence.stepStatus[stepUuid];
if (parentExec === undefined
|| currentStatus.parentExecUuid === parentExec.execUuid)
{
return currentStatus;
}
}
return null;
}
getOrCreateExecution(stepUuid: string, parentExec?: SequenceStepStatus): SequenceStepStatus {
const status = this.getExecution(stepUuid, parentExec);
if (status !== null) {
return status;
}
const sequence = this.sequence;
const newStatus:SequenceStepStatus = {
execUuid: this.uuid(),
parentExecUuid: parentExec ? parentExec.execUuid : null,
finishedLoopCount: 0,
currentForeach: null,
finishedForeach: null,
};
sequence.stepStatus[stepUuid] = newStatus;
// Read back
return sequence.stepStatus[stepUuid];
}
calcExposure(step: SequenceStep[]): number {
let exp: number = 0;
for(const s of step) {
if (s.exposure !== undefined) {
exp = s.exposure;
}
}
return exp;
}
totalCount(steps:SequenceStep[]):SequenceSize {
const step = steps[steps.length - 1]
const mult = Math.max(step.repeat || 1, 1);
const size: SequenceSize = {
totalCount: 0,
totalTime: 0,
};
if (step.childs && step.childs.list.length) {
for(const child of step.childs.list) {
const childSize = this.totalCount(steps.concat([step.childs.byuuid[child]]));
size.totalCount += childSize.totalCount;
size.totalTime += childSize.totalTime;
}
} else {
size.totalCount= 1;
size.totalTime= this.calcExposure(steps);
}
size.totalCount *= mult;
size.totalTime *= mult;
return size;
}
getProgress(stepStack: Array<SequenceWithStatus>, start?: number):Progress {
if (start === undefined) {
start = 0;
}
const v = stepStack[start];
const loopCount = Math.max(v.step.repeat || 0, 1);
const doneCount = v.status.finishedLoopCount;
const foreachCount = Math.max(v.step.foreach?.list.length || 0, 1);
const doneForeachCount = this.getEffectiveFinishedForeachCount(v);
if (start === stepStack.length -1) {
const expValue = this.calcExposure(stepStack.map(e=>e.step));
return {
totalCount: foreachCount * loopCount,
imagePosition: doneCount * foreachCount + doneForeachCount,
totalTime: foreachCount * loopCount * expValue,
timeSpent: (doneCount * foreachCount + doneForeachCount) * expValue,
};
}
let ret: Progress = {
imagePosition: 0,
totalCount: 0,
timeSpent: 0,
totalTime: 0,
}
// We have childs. Account in position until active child is viewed
let activeChildFound : boolean = v.status.activeChild === undefined;
for(const childUuid of v.step.childs!.list) {
if (childUuid === v.status.activeChild) {
const v = this.getProgress(stepStack, start + 1);
ret.imagePosition += v.imagePosition;
ret.timeSpent += v.timeSpent;
ret.totalCount += v.totalCount;
ret.totalTime += v.totalTime;
activeChildFound = true;
} else {
const c = this.totalCount(stepStack.slice(0, start + 1).map(e=>e.step).concat([v.step.childs!.byuuid[childUuid]]));
ret.totalCount += c.totalCount;
ret.totalTime += c.totalTime;
if (!activeChildFound) {
ret.imagePosition += c.totalCount;
ret.timeSpent += c.totalTime;
}
}
}
// account for foreach
ret.imagePosition += ret.totalCount * doneForeachCount;
ret.timeSpent += ret.totalTime * doneForeachCount;
ret.totalCount *= foreachCount;
ret.totalTime *= foreachCount;
// account for repeat
ret.imagePosition += ret.totalCount * doneCount;
ret.timeSpent += ret.totalTime * doneCount;
ret.totalCount *= loopCount;
ret.totalTime *= loopCount;
return ret;
}
// Ensure foreach is set according to definition
getNextStep():Array<SequenceWithStatus>|undefined {
const stepStack: Array<{step: SequenceStep, status: SequenceStepStatus}>= [];
// Pop the head if false
const enter= ()=>
{
const current = stepStack[stepStack.length - 1];
for(let incLoop = 0; (!this.statusDone(current)) && incLoop <= 1; ++incLoop) {
for(let incForeach = 0; incForeach <= 1; ++incForeach) {
if (current.step.foreach) {
if (current.status.finishedForeach === null) {
current.status.finishedForeach = {};
}
if (current.status.currentForeach === null
|| Object.prototype.hasOwnProperty.call(current.status.finishedForeach, current.status.currentForeach))
{
let newForeach = this.getNextForeach(current);
if (current.status.currentForeach !== null) {
current.status.execUuid = this.uuid();
}
current.status.currentForeach = newForeach;
if (newForeach === null) {
// Foreach is exhausted
break;
}
}
}
if (current.step.childs) {
// step has childs. Must go down
// Iterate all childs. If they are all done, the exec is finished
// Restart from activeChild
const childUuid = current.status.activeChild;
let toTry = childUuid
? [childUuid, ...current.step.childs.list.filter(e=>e!==childUuid)]
: current.step.childs.list;
for(const childUid of toTry) {
stepStack.push({
step: current.step.childs.byuuid[childUid],
status: this.getOrCreateExecution(childUid, current.status)
});
if (enter()) {
current.status.activeChild = childUid;
return true;
}
if (current.status.activeChild !== undefined) {
delete current.status.activeChild;
}
}
} else {
return true;
}
// Arrive here when current foreach is exhausted (child became so)
if (incForeach === 0) {
if (current.status.currentForeach !== null) {
if (!current.status.finishedForeach) {
current.status.finishedForeach = {};
}
current.status.finishedForeach[current.status.currentForeach] = true;
}
}
}
if (incLoop === 0) {
current.status.finishedLoopCount = (current.status.finishedLoopCount || 0) + 1;
// Looping is still possible. Start a new foreach loop
current.status.currentForeach = current.step.foreach ? current.step.foreach.list[0] : null;
current.status.finishedForeach = current.step.foreach ? {} : null;
current.status.execUuid = this.uuid();
}
}
// Pop the entry
stepStack.splice(stepStack.length - 1, 1);
return false;
}
stepStack.push({step: this.sequence.root, status: this.getOrCreateExecution("root")});
if (!enter()) {
return undefined;
}
return stepStack;
}
getStepParameters(step: {step: SequenceStep, status: SequenceStepStatus}):SequenceStepParameters
{
const {foreach, childs, repeat, ...ret} = {...step.step};
if (foreach && step.status.currentForeach && hasKey(foreach.byuuid, step.status.currentForeach)) {
const p : keyof SequenceStepParameters = foreach.param;
ret[p] = foreach.byuuid[step.status.currentForeach][p] as any;
}
// Handle dither once here
if (ret.dithering && ret.dithering.once) {
if (step.status.finishedLoopCount) {
delete ret.dithering;
}
}
if (ret.focuser && ret.focuser.once) {
if (step.status.finishedLoopCount) {
delete ret.focuser;
}
}
return ret;
}
getParameters(steps: Array<SequenceWithStatus>):SequenceStepParameters {
const param:SequenceStepParameters = {};
let ditheringStepId: number|undefined;
let focuserStepId: number|undefined;
for(let i = 0; i < steps.length; ++i) {
const o = steps[i];
const stepParams = this.getStepParameters(o);
if (Object.prototype.hasOwnProperty.call(stepParams, 'dithering')) {
ditheringStepId = i;
}
if (Object.prototype.hasOwnProperty.call(stepParams, 'focuser')) {
focuserStepId = i;
}
Object.assign(param, stepParams);
}
if (ditheringStepId !== undefined && param.dithering?.once) {
// We want loopCount == 0 + activeChild = firstChild + activeForeach = first
let first = true;
for(let i = ditheringStepId; i < steps.length; ++i) {
const o = steps[i];
if (o.status.finishedLoopCount) {
first = false;
break;
}
if (o.step.childs && o.status.activeChild !== o.step.childs.list[0]) {
first = false;
break;
}
if (o.step.foreach && o.status.currentForeach !== null && o.status.currentForeach !== o.step.foreach.list[0]) {
first = false;
break;
}
}
if (!first) {
delete param.dithering;
}
}
if (focuserStepId !== undefined && param.focuser?.once) {
// We want loopCount == 0 + activeChild = firstChild + activeForeach = first
let first = true;
for(let i = focuserStepId; i < steps.length; ++i) {
const o = steps[i];
if (o.status.finishedLoopCount) {
first = false;
break;
}
if (o.step.childs && o.status.activeChild !== o.step.childs.list[0]) {
first = false;
break;
}
if (o.step.foreach && o.status.currentForeach !== null && o.status.currentForeach !== o.step.foreach.list[0]) {
first = false;
break;
}
}
if (!first) {
delete param.focuser;
}
}
return param;
}
/** Iterate over all actual exposure of the sequence */
scanParameters(cb: (param: SequenceStepParameters, count: number)=>(void)) {
const scanStepParameters = (
cur: SequenceStepParameters,
iteration:number,
step: SequenceStep)=>
{
const {foreach, childs, repeat, ...ret} = {...step};
const scanNode=()=>{
cur = {...cur, ...ret};
if (childs && childs.list.length) {
for(const uid of childs.list) {
scanStepParameters(cur, iteration * (repeat||1), childs.byuuid[uid]);
}
} else {
cb(cur, iteration * (repeat || 1));
}
}
if (foreach) {
cur={...cur};
const p : keyof SequenceStepParameters = foreach.param;
console.log('Iterating ', foreach);
for(const foreachUid of foreach.list) {
cur[p] = foreach.byuuid[foreachUid][p] as any;
scanNode();
}
} else {
scanNode();
}
}
scanStepParameters({}, 1, this.sequence.root);
}
};