-
Notifications
You must be signed in to change notification settings - Fork 2
/
Camera.ts
949 lines (832 loc) · 39.8 KB
/
Camera.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
import CancellationToken from 'cancellationtoken';
import MemoryStreams from 'memory-streams';
import Log from './Log';
import { ExpressApplication, AppContext } from "./ModuleBase";
import {CameraStatus, CameraDeviceSettings, BackofficeStatus, Sequence, ImageStatus, CameraShoot} from './shared/BackOfficeStatus';
import JsonProxy from './shared/JsonProxy';
import { Vector } from './Indi';
import {Task, createTask} from "./Task.js";
import {timestampToEpoch} from "./Indi";
import {IdGenerator} from "./IdGenerator";
import * as Obj from "./shared/Obj";
import {Comparator} from './shared/Comparator';
import * as RequestHandler from "./RequestHandler";
import * as BackOfficeAPI from "./shared/BackOfficeAPI";
import ConfigStore from './ConfigStore';
import { Pipe } from './SystemPromise';
import * as fs from 'fs';
type ScopeState = "light"|"dark"|"flat";
const logger = Log.logger(__filename);
const stateByFrameType :{[id:string]:ScopeState}= {
FRAME_BIAS:"dark",
FRAME_DARK:"dark",
FRAME_FLAT:"flat",
}
const coverMessageByFrameType = {
"light":"Uncover scope",
"dark": "Cover scope",
"flat": "Switch scope to flat field",
}
const dirComparator = Comparator.when<BackOfficeAPI.ImageFileInfo>(
(e)=>!e.name.startsWith('.'),
Comparator.ordering().applyTo<BackOfficeAPI.ImageFileInfo>(e=>e.name)
);
const fileComparator = Comparator.when<BackOfficeAPI.ImageFileInfo>(
(e)=>!e.name.startsWith('.'),
Comparator.ordering().applyTo<BackOfficeAPI.ImageFileInfo>(e=>(-(e.time||0)))
);
export default class Camera
implements RequestHandler.APIAppProvider<BackOfficeAPI.CameraAPI>
{
appStateManager: JsonProxy<BackofficeStatus>;
shootPromises: {[camId: string]:Task<BackOfficeAPI.ShootResult>};
streamPromises: {[camId: string]:Task<void>};
currentStatus: CameraStatus;
context: AppContext;
get indiManager() { return this.context.indiManager };
get imageProcessor() { return this.context.imageProcessor };
imageIdGenerator = new IdGenerator();
previousImages: any;
fakeImageId: number = 0;
constructor(app:ExpressApplication, appStateManager:JsonProxy<BackofficeStatus>, context:AppContext) {
this.appStateManager = appStateManager;
this.appStateManager.getTarget().camera = {
status: "idle",
currentImagingSetup: null,
defaultImageLoadingPath: process.env.HOME || null,
currentStreams: {},
// Device => duration
currentShoots: {
},
// List of finished images
images: {
list: [],
// detail of each image
byuuid: {
// 0: {
// path: "/path/to/file.fits",
// device: "device"
// }
}
},
lastByDevices: {},
dynStateByDevices: {},
configuration: {
preferedImagingSetup: null,
}
};
// Device => promise
this.shootPromises = {};
this.streamPromises = {};
this.currentStatus = this.appStateManager.getTarget().camera;
this.context = context;
this.imageIdGenerator = new IdGenerator();
this.previousImages = {};
new ConfigStore(appStateManager, 'camera', ['camera', 'configuration'], {
fakeImagePath: null,
fakeImages: null,
defaultImagePath: process.env.HOME,
defaultImagePrefix: 'IMAGE_XXX'
}, {
fakeImagePath: "/home/ludovic/Astronomie/home/photos/2015/2015-08-09/photos/2015-08-09/",
fakeImages: [
"Single_Bin1x1_1s_2015-08-09_23-40-17.fit", "Single_Bin1x1_1s_2015-08-09_23-44-44.fit", "Single_Bin1x1_30s_2015-08-09_23-47-04.fit", "Single_Bin2x2_2s_2015-08-09_23-28-37.fit", "Single_M27_Bin1x1_2s_2015-08-10_03-40-12.fit",
"Single_Bin1x1_1s_2015-08-09_23-41-16.fit", "Single_Bin1x1_30s_2015-08-09_23-42-37.fit", "Single_Bin1x1_5s_2015-08-09_23-41-47.fit", "Single_Bin2x2_2s_2015-08-09_23-29-41.fit", "Single_M27_G_Bin1x1_2s_2015-08-10_03-46-49.fit",
"Single_Bin1x1_1s_2015-08-09_23-44-21.fit", "Single_Bin1x1_30s_2015-08-09_23-45-37.fit", "Single_Bin2x2_2s_2015-08-09_23-27-56.fit", "Single_M27_Bin1x1_1s_2015-08-10_03-39-51.fit"
],
defaultImagePath: process.env.HOME,
defaultImagePrefix: 'IMAGE_XXX'
});
context.imagingSetupManager.createPreferredImagingSelector({
currentPath: [ 'camera', 'currentImagingSetup' ],
preferedPath: [ 'camera', 'configuration', 'preferedImagingSetup' ],
read: ()=> ({
prefered: this.currentStatus.configuration.preferedImagingSetup,
current: this.currentStatus.currentImagingSetup,
}),
set: (s:{prefered?: string|null|undefined, current?: string|null|undefined})=>{
if (s.prefered !== undefined) {
this.currentStatus.configuration.preferedImagingSetup = s.prefered;
}
if (s.current !== undefined) {
this.currentStatus.currentImagingSetup = s.current;
}
}
});
// Update configuration/dyn states
this.appStateManager.addSynchronizer(
[ 'indiManager', 'availableCameras' ],
()=> {
const dynStateRoot = this.currentStatus.dynStateByDevices;
for(const o of this.indiManager.currentStatus.availableCameras) {
if (!Obj.hasKey(dynStateRoot, o)) {
dynStateRoot[o] = {}
}
}
},
true);
// Update shoots
this.appStateManager.addSynchronizer(
[
[
[
'indiManager', 'deviceTree', null, 'CCD_EXPOSURE',
[
['childs', 'CCD_EXPOSURE_VALUE', '$_'],
['$state']
]
],
[
'indiManager', 'availableCameras'
]
]
], this.updateRunningShoots.bind(this), true);
this.appStateManager.addSynchronizer(
[
[
[ 'indiManager', 'deviceTree', null, 'CCD_FILE_PATH', '$rev' ],
[ 'indiManager', 'availableCameras']
]
], this.updateDoneImages.bind(this), true
);
this.updateDoneImages();
}
getImageByUuid(uuid:string): ImageStatus|undefined {
if (Obj.hasKey(this.currentStatus.images.byuuid, uuid)) {
return this.currentStatus.images.byuuid[uuid];
}
return undefined;
}
updateDoneImages()
{
var indiManager = this.appStateManager.getTarget().indiManager;
// Ensure that the CCD_FILE_PATH property is set for all devices
var found:{[deviceId:string]:string} = {};
for(var device of indiManager.availableCameras)
{
var rev, value;
try {
var dtree = indiManager.deviceTree[device];
if ("CCD_FILE_PATH" in dtree) {
rev = dtree.CCD_FILE_PATH.$rev;
var timestamp = dtree.CCD_FILE_PATH.$timestamp;
value = dtree.CCD_FILE_PATH.childs.FILE_PATH.$_;
if (value === undefined) {
continue;
}
// Ignore CCD_FILE_PATH that are from before connection
// Some buggy drivers send spurious CCD_FILE_PATH message, not distinguishable from new shoot
if (!Object.prototype.hasOwnProperty.call(dtree, "CONNECTION")) {
continue;
}
var age = timestampToEpoch(timestamp) - timestampToEpoch(dtree.CONNECTION.$timestamp);
if (age <= 2) {
logger.warn('Ignored CCD_FILE_PATH from before last connection event', {age, vector: dtree.CCD_FILE_PATH});
continue;
}
} else {
continue;
}
} catch(e) {
logger.error('Error in CCD_FILE_PATH handling', {device}, e);
continue;
}
var stamp = rev + ":" + value;
found[device] = stamp;
if (!Object.prototype.hasOwnProperty.call(this.previousImages, device))
{
this.previousImages[device] = stamp;
} else {
if (this.previousImages[device] != stamp) {
logger.debug('changed value', {device, old: this.previousImages[device], new: stamp});
this.previousImages[device] = stamp;
if (value != '') {
let currentShoot;
if (Object.prototype.hasOwnProperty.call(this.currentStatus.currentShoots, device)) {
currentShoot = this.currentStatus.currentShoots[device];
} else {
currentShoot = undefined;
}
if (currentShoot != undefined && currentShoot.managed) {
logger.debug('Image will be result of our action.', {value})
} else {
logger.info('New external image', {value});
var newUuid = this.imageIdGenerator.next();
this.currentStatus.images.list.push(newUuid);
this.currentStatus.images.byuuid[newUuid] = {
path: value,
device: device
};
}
}
}
}
}
for(var o of Object.keys(this.previousImages))
{
if (!found[o]) {
logger.debug('No more looking for shoot',{device: o});
delete(this.previousImages[o]);
}
}
}
updateRunningShoots()
{
var indiManager = this.appStateManager.getTarget().indiManager;
var connectedDevices = [];
for(var deviceId of Object.keys(indiManager.deviceTree).sort()) {
var device = indiManager.deviceTree[deviceId];
var status, exposure;
try {
status = device.CCD_EXPOSURE.$state;
exposure = device.CCD_EXPOSURE.childs.CCD_EXPOSURE_VALUE.$_;
exposure = parseFloat(exposure);
} catch(e) {
continue;
}
connectedDevices.push(deviceId);
let currentShoot;
if (Object.prototype.hasOwnProperty.call(this.currentStatus.currentShoots, deviceId)) {
currentShoot = this.currentStatus.currentShoots[deviceId];
} else {
currentShoot = undefined;
}
if (status == 'Busy' && exposure > 0) {
// Create a shoot
if (currentShoot === undefined) {
currentShoot= {
exposure: exposure,
expLeft: exposure,
status: 'External' as 'External',
managed: false,
};
this.currentStatus.currentShoots[deviceId] = currentShoot;
if (Obj.hasKey(this.currentStatus.dynStateByDevices, deviceId)) {
this.currentStatus.dynStateByDevices[deviceId].spyRecommanded = true;
}
} else {
if (exposure > 0 || currentShoot.expLeft) {
currentShoot.expLeft = exposure;
}
if (exposure > currentShoot.exposure) {
currentShoot.exposure = exposure;
}
}
} else {
// Destroy the shoot, if not managed
if (currentShoot !== undefined) {
if (currentShoot.managed) {
if (status !== 'Busy') exposure = 0;
if (exposure > 0 || currentShoot.expLeft) {
currentShoot.expLeft = exposure;
}
} else {
delete this.currentStatus.currentShoots[deviceId];
}
}
}
}
// destroy shoots of disconnected devices
for(var k of Object.keys(this.currentStatus.currentShoots))
{
if (connectedDevices.indexOf(k) == -1) {
delete this.currentStatus.currentShoots[k];
}
}
}
setCurrentImagingSetup=async (ct: CancellationToken, message:{imagingSetup:null|string})=>{
if (message.imagingSetup !== null && !this.context.imagingSetupManager.getImagingSetupInstance(message.imagingSetup).exists()) {
throw new Error("invalid imaging setup");
}
this.currentStatus.currentImagingSetup = message.imagingSetup;
}
setDefaultImageLoadingPath=async (ct: CancellationToken, message:{defaultImageLoadingPath:null|string})=>{
this.currentStatus.defaultImageLoadingPath = message.defaultImageLoadingPath;
}
getImageFiles= async (ct: CancellationToken, payload: { path: string; }) => {
const basePath = payload.path + (payload.path.endsWith('/') ? "" : "/");
const itemsNames = await fs.promises.readdir(basePath, {withFileTypes: false});
const directories:Array<BackOfficeAPI.ImageFileInfo> = [];
const images:Array<BackOfficeAPI.ImageFileInfo> = [];
const imageRegex = /\.(fits|fit|cr2|jpeg|jpg)$/i;
// Put the directories above
for(const p of itemsNames) {
ct.throwIfCancelled();
try {
const stat = await fs.promises.lstat(basePath + p);
if (stat.isDirectory()) {
directories.push({
name: p,
type: "dir",
time: null,
});
} else if (stat.isFile() && imageRegex.test(p)) {
images.push({
name: p,
type: "image",
time: stat.mtimeMs,
});
}
} catch(e) {
logger.warn(`Unable to stat ${p}`, e);
}
}
directories.sort(dirComparator.apply);
// Sort files by modification time (desc)
images.sort(fileComparator.apply);
return [...images, ...directories];
};
getCropAdjustment(device:any)
{
const frameVec = device.getVector('CCD_FRAME');
if (!frameVec.exists()) {
return {};
}
const ccdVec = device.getVector('CCD_INFO');
if (!ccdVec.exists()) {
return {};
}
const crop = {
x: parseFloat(frameVec.getPropertyValue('X')),
y: parseFloat(frameVec.getPropertyValue('Y')),
w: parseFloat(frameVec.getPropertyValue('WIDTH')),
h: parseFloat(frameVec.getPropertyValue('HEIGHT'))
}
const max = {
w: parseFloat(ccdVec.getPropertyValue('CCD_MAX_X')),
h: parseFloat(ccdVec.getPropertyValue('CCD_MAX_Y')),
}
const binningVec = device.getVector('CCD_BINNING');
const bin = binningVec.exists() ?
{
x:parseFloat(binningVec.getPropertyValue('HOR_BIN')),
y:parseFloat(binningVec.getPropertyValue('VER_BIN'))
}
: {x: 1, y: 1};
logger.debug('Crop status', {crop, max, bin});
if (crop.x || crop.y || crop.w != max.w || crop.h != max.h) {
return {
X: "0",
Y: "0",
WIDTH: "" + Math.floor(max.w),
HEIGHT: "" + Math.floor(max.h)
};
}
return {};
}
private resolveImagingSetup(imagingSetup: string|null) {
const imagingSetupInstance = this.context.imagingSetupManager.getImagingSetupInstance(imagingSetup);
if (!imagingSetupInstance.exists()) {
throw new Error("Invalid imaging setup: " + imagingSetup);
}
const device = imagingSetupInstance.config().cameraDevice;
if (device === null) {
throw new Error("No camera configured");
}
if (this.indiManager.currentStatus.availableCameras.indexOf(device) === -1) {
throw new Error("Camera not found");
}
return {imagingSetupInstance, device};
}
private async applyShootSettings(task: Task<any>, device: string, currentShootSettings: CameraDeviceSettings) {
var exposure = currentShootSettings.exposure;
if (exposure === null || exposure === undefined) {
exposure = 0.1;
}
currentShootSettings.exposure = exposure;
// Set the binning - if prop is present only
if (currentShootSettings.bin !== null
&& currentShootSettings.bin !== undefined
&& this.indiManager.getValidConnection().getDevice(device).getVector('CCD_BINNING').exists())
{
task.cancellation.throwIfCancelled();
logger.debug('Bin upgrade', {device});
await this.indiManager.setParam(task.cancellation, device, 'CCD_BINNING', {
HOR_BIN: '' + currentShootSettings.bin!,
VER_BIN: '' + currentShootSettings.bin!
});
}
// Reset the frame size - if prop is present only
if (Object.keys(this.getCropAdjustment(this.indiManager.getValidConnection().getDevice(device))).length != 0) {
task.cancellation.throwIfCancelled();
logger.debug('set crop', {device});
await this.indiManager.setParam(task.cancellation, device, 'CCD_FRAME', this.getCropAdjustment(this.indiManager.getValidConnection().getDevice(device)), true);
}
// Set the iso
if (currentShootSettings.iso !== null
&& currentShootSettings.iso !== undefined
&& this.indiManager.getValidConnection().getDevice(device).getVector('CCD_ISO').exists()) {
task.cancellation.throwIfCancelled();
logger.debug('set iso', {device});
await this.indiManager.setParam(task.cancellation, device, 'CCD_ISO',
// FIXME : support cb for setParam
(vector:Vector) => {
const vec = vector.getExistingVectorInTree();
const v = currentShootSettings.iso;
var childToSet = undefined;
for(var id of vec.childNames)
{
var child = vec.childs[id];
if (child.$label == v) {
childToSet = id;
break;
}
}
if (childToSet === undefined) throw new Error("Unsupported iso value: " + v);
logger.debug('found iso', {device, childToSet});
return ({[childToSet]: 'On'});
}
);
}
// Make sure continuous loop is not set (this prevents gathering the photo in deterministic way without timeout)
if (this.indiManager.getValidConnection().getDevice(device).getVector('CCD_FAST_TOGGLE').exists()) {
await this.indiManager.setParam(task.cancellation, device, 'CCD_FAST_TOGGLE',
(vec:Vector) => {
if (vec.getPropertyValueIfExists('INDI_DISABLED') !== 'On') {
logger.debug('want CCD_FAST_TOGGLE', {device});
return {
INDI_DISABLED: 'On'
}
} else {
return ({});
}
});
}
}
// Return a promise to shoot at the given camera (where)
async doShoot(cancellation: CancellationToken, imagingSetup:string, settingsProvider?:(s:CameraDeviceSettings)=>CameraDeviceSettings):Promise<BackOfficeAPI.ShootResult>
{
// On veut un objet de controle qui comporte à la fois la promesse et la possibilité de faire cancel
var ccdFilePathInitRevId:any;
let shootResult:BackOfficeAPI.ShootResult;
const {imagingSetupInstance, device} = this.resolveImagingSetup(imagingSetup);
if (Object.prototype.hasOwnProperty.call(this.currentStatus.currentShoots, device)) {
throw new Error("Shoot already started for " + device);
}
if (Object.prototype.hasOwnProperty.call(this.currentStatus.currentStreams, device)) {
throw new Error("Stream is already started for " + device);
}
let settings = Object.assign({}, imagingSetupInstance.config().cameraSettings);
if (settingsProvider !== undefined) {
settings = settingsProvider(settings);
}
this.currentStatus.currentShoots[device] = Object.assign({
status: 'init' as "init",
managed: true,
path: this.currentStatus.configuration.defaultImagePath || process.env.HOME,
prefix: this.currentStatus.configuration.defaultImagePrefix || 'IMAGE_XXX',
expLeft: settings.exposure,
}, settings);
return await createTask<BackOfficeAPI.ShootResult>(cancellation, async (task)=>{
this.shootPromises[device] = task;
try {
const currentShootSettings = this.currentStatus.currentShoots[device];
logger.info('Starting shoot', {device, settings: currentShootSettings});
await this.applyShootSettings(task, device, currentShootSettings);
task.cancellation.throwIfCancelled();
await this.indiManager.setParam(task.cancellation, device, 'UPLOAD_SETTINGS',
(vec:Vector)=> {
const ret = {};
if (vec.getPropertyValueIfExists('UPLOAD_DIR') !== currentShootSettings.path
|| vec.getPropertyValueIfExists('UPLOAD_PREFIX') !== currentShootSettings.prefix)
{
logger.debug('adjusting UPLOAD_DIR/UPLOAD_PREFIX', {device});
return {
UPLOAD_DIR: currentShootSettings.path,
UPLOAD_PREFIX: currentShootSettings.prefix
}
} else {
return {}
}
});
// Set the upload mode to at least upload_client
task.cancellation.throwIfCancelled();
await this.indiManager.setParam(task.cancellation, device, 'UPLOAD_MODE',
(vec:Vector) => {
if (vec.getPropertyValueIfExists('UPLOAD_CLIENT') == 'On') {
logger.debug('want upload_client', {device});
return {
UPLOAD_BOTH: 'On'
}
} else {
return ({});
}
});
logger.debug('wait readiness of CCD_FILE_PATH', {device});
await this.indiManager.waitForVectors(task.cancellation, device, ['CCD_FILE_PATH']);
const connection = this.indiManager.connection;
if (connection == undefined) {
throw "Indi server not connected";
}
ccdFilePathInitRevId = connection.getDevice(device).getVector("CCD_FILE_PATH").getRev();
var expVector = connection.getDevice(device).getVector("CCD_EXPOSURE");
task.cancellation.throwIfCancelled();
logger.debug('starting exposure', {device});
expVector.setValues([{name: 'CCD_EXPOSURE_VALUE', value: '' + currentShootSettings.exposure! }]);
const doneWithExposure = task.cancellation.onCancelled(() => {
// FIXME: we must wait, otherwise a new shoot can begin while these are still occuring.
var expVector = connection.getDevice(device).getVector("CCD_ABORT_EXPOSURE");
expVector.setValues([{name: 'ABORT', value: 'On'}]);
var uploadModeVector = connection.getDevice(device).getVector("UPLOAD_MODE");
uploadModeVector.setValues([{name: 'UPLOAD_CLIENT', value: 'On'}]);
});
let nextLog = new Date().getTime() + currentShootSettings.exposure * 1000 + 5000;
try {
// Make this uninterruptible
await connection.wait(CancellationToken.CONTINUE, () => {
logger.debug('Checking for exposure end', {device});
var value = expVector.getPropertyValue("CCD_EXPOSURE_VALUE");
var state = expVector.getState();
if (value != "0") {
currentShootSettings.status = 'Exposing';
} else if (state == "Busy" && currentShootSettings.status == 'Exposing') {
currentShootSettings.status = 'Downloading';
}
if (state === "Busy") {
if (new Date().getTime() > nextLog) {
logger.info('Still waiting exposure', {device, settings: currentShootSettings});
nextLog = new Date().getTime() + 5000;
}
return false;
}
if (state !== "Ok" && state !== "Idle") {
logger.warn('Wrong exposure state', {device, state});
throw new Error("Exposure failed");
}
logger.debug('Exposure done', {device});
return true;
});
} finally {
doneWithExposure();
}
task.cancellation.throwIfCancelled();
if (ccdFilePathInitRevId === connection.getDevice(device).getVector("CCD_FILE_PATH").getRev())
{
throw new Error("CCD_FILE_PATH was not updated");
}
var value = connection.getDevice(device).getVector("CCD_FILE_PATH").getPropertyValue("FILE_PATH");
logger.debug('Finished image acquisistion', {device, value});
if (this.currentStatus.configuration.fakeImages != null) {
var examples = this.currentStatus.configuration.fakeImages;
value = examples[(this.fakeImageId++)%examples.length];
if (this.currentStatus.configuration.fakeImagePath != null) {
value = this.currentStatus.configuration.fakeImagePath + value;
}
logger.warn('Using fake image', {device, value});
}
this.currentStatus.lastByDevices[device] = value;
var newUuid = this.imageIdGenerator.next();
this.currentStatus.images.list.push(newUuid);
this.currentStatus.images.byuuid[newUuid] = {
path: value,
device: device
};
shootResult = ({path: value, device, uuid: newUuid});
// Remove UPLOAD_MODE
// FIXME: this should be in a finally !
await this.indiManager.setParam(task.cancellation, device, 'UPLOAD_MODE',
(vec:Vector) => {
if (vec.getPropertyValueIfExists('UPLOAD_CLIENT') != 'On') {
logger.debug('set back upload_client', {device});
return {
UPLOAD_CLIENT: 'On'
}
} else {
return ({});
}
});
logger.info('Image acquisistion done', shootResult);
return shootResult;
} finally {
logger.debug('Doing cleanup', {device});
delete this.shootPromises[device];
delete this.currentStatus.currentShoots[device];
}
});
}
async doLoopExposure(cancellation: CancellationToken, imagingSetup:string, settingsProvider?:(s:CameraDeviceSettings)=>CameraDeviceSettings) {
const {imagingSetupInstance, device} = this.resolveImagingSetup(imagingSetup);
let settings = Object.assign({}, imagingSetupInstance.config().cameraSettings);
if (settingsProvider !== undefined) {
settings = settingsProvider(settings);
}
return await createTask<void>(cancellation, async (task)=>{
logger.info('Starting shoot', {device, settings: settings});
this.currentStatus.currentShoots[device] = Object.assign({
status: 'init' as "init",
managed: true,
path: null,
prefix: null,
expLeft: settings.exposure,
}, settings);
const currentShootSettings = this.currentStatus.currentShoots[device];
await this.applyShootSettings(task, device, settings);
task.cancellation.throwIfCancelled();
const connection = this.indiManager.connection;
if (connection == undefined) {
throw "Indi server not connected";
}
var expVector = connection.getDevice(device).getVector("CCD_EXPOSURE");
task.cancellation.throwIfCancelled();
logger.debug('starting loop exposure', {device});
expVector.setValues([{name: 'CCD_EXPOSURE_VALUE', value: '' + settings.exposure! }]);
const doneWithExposure = task.cancellation.onCancelled(() => {
// FIXME: we must wait, otherwise a new shoot can begin while these are still occuring.
var expVector = connection.getDevice(device).getVector("CCD_ABORT_EXPOSURE");
expVector.setValues([{name: 'ABORT', value: 'On'}]);
});
let nextLog = new Date().getTime() + settings.exposure * 1000 + 5000;
try {
// Make this uninterruptible
await connection.wait(CancellationToken.CONTINUE, () => {
logger.debug('Checking for exposure end', {device});
var value = expVector.getPropertyValue("CCD_EXPOSURE_VALUE");
var state = expVector.getState();
if (value != "0") {
currentShootSettings.status = 'Exposing';
} else if (state == "Busy" && currentShootSettings.status == 'Exposing') {
currentShootSettings.status = 'Downloading';
}
if (state === "Busy") {
if (new Date().getTime() > nextLog) {
logger.info('Still waiting exposure', {device, settings});
nextLog = new Date().getTime() + 5000;
}
return false;
}
if (state !== "Ok" && state !== "Idle") {
logger.warn('Wrong exposure state', {device, state});
throw new Error("Exposure failed");
}
logger.debug('Exposure done', {device});
return true;
});
} finally {
delete this.currentStatus.currentShoots[device];
doneWithExposure();
}
task.cancellation.throwIfCancelled();
logger.debug('Finished loop image acquisistion', {device});
});
}
async loopExposure(cancellation: CancellationToken, imagingSetup:string) {
while(true) {
cancellation.throwIfCancelled();
await this.doLoopExposure(cancellation, imagingSetup);
}
}
// Return a promise to stream the given camera
async doStream(cancellation: CancellationToken, imagingSetup:string, loopExposure: boolean):Promise<void>
{
const {imagingSetupInstance, device} = this.resolveImagingSetup(imagingSetup);
if (Object.prototype.hasOwnProperty.call(this.currentStatus.currentShoots, device)
&& this.currentStatus.currentShoots[device].managed) {
throw new Error("Shoot already started for " + device);
}
if (Object.prototype.hasOwnProperty.call(this.currentStatus.currentStreams, device)) {
throw new Error("Stream is already started for " + device);
}
return await createTask<void>(cancellation, async (task)=>{
this.streamPromises[device] = task;
delete this.currentStatus.currentShoots[device];
try {
this.currentStatus.currentStreams[device] = {
streamId: null,
streamDetails: null,
serial: null,
autoexp: null,
subframe: null,
frameSize: null,
};
task.cancellation.throwIfCancelled();
logger.info('Starting passive streaming', {device});
// Set the upload mode to at least upload_client
await this.indiManager.setParam(task.cancellation, device, 'UPLOAD_MODE',
(vec:Vector) => {
if (vec.getPropertyValueIfExists('UPLOAD_CLIENT') === 'Off') {
logger.debug('want upload_client for stream', {device});
return {
UPLOAD_CLIENT: 'On'
}
} else {
return ({});
}
});
task.cancellation.throwIfCancelled();
let loopExposureTask;
if (loopExposure) {
loopExposureTask = createTask(task.cancellation, async (loopTask)=>{
try {
await this.loopExposure(loopTask.cancellation, imagingSetup)
} catch(e) {
if (e instanceof CancellationToken.CancellationError) {
return;
}
// This is fatal... Propagate
logger.warn('loop exposure failed', e);
task.cancel(e);
}
});
}
const unregister = task.cancellation.onCancelled(()=>{
});
try {
const onJson = (e:string)=> {
if (e) {
let val;
try {
val = JSON.parse(e);
} catch(e) {
logger.warn('json parse error in streamer', {device}, e);
return;
}
logger.debug('decoded from streamer', {device, json: val});
const target = this.currentStatus.currentStreams[device];
if (val.serial) {
target.serial = val.serial;
}
if (val.streamId) {
target.streamId = val.streamId;
}
if (val.streamDetails) {
target.streamDetails = val.streamDetails;
}
if (val.frameSize) {
target.frameSize = val.frameSize;
} else {
target.frameSize = null;
}
if (val.subframe) {
target.subframe = val.subframe;
} else {
target.subframe = null;
}
}
}
const addr = this.indiManager.getIndiServerAddr();
await Pipe(task.cancellation,
{
command: ["./fitsviewer/streamer", addr.host, "" + addr.port, device, "CCD1"]
},
new MemoryStreams.ReadableStream(""),
onJson
);
} finally {
unregister();
if (loopExposureTask) {
loopExposureTask?.cancel();
try {
await loopExposureTask
} catch(e) {
logger.info("Loop exposure task finished", e);
}
}
}
} finally {
logger.debug('Streamer doing cleanup', {device});
delete this.streamPromises[device];
delete this.currentStatus.currentStreams[device];
}
});
}
stream = async (ct: CancellationToken, message: {loopExposure: boolean})=>{
if (this.currentStatus.currentImagingSetup === null) {
throw new Error("No imaging setup selected");
}
return await this.doStream(ct, this.currentStatus.currentImagingSetup, message.loopExposure);
}
shoot = async (ct: CancellationToken, message:{})=>{
if (this.currentStatus.currentImagingSetup === null) {
throw new Error("No imaging setup selected");
}
return await this.doShoot(ct, this.currentStatus.currentImagingSetup);
}
abort = async (ct: CancellationToken, message:{})=>{
if (this.currentStatus.currentImagingSetup === null) {
throw new Error("No imaging setup selected");
}
const {imagingSetupInstance, device} = this.resolveImagingSetup(this.currentStatus.currentImagingSetup);
let sthCanceled = false;
if (Object.prototype.hasOwnProperty.call(this.streamPromises, device)) {
this.streamPromises[device].cancel();
sthCanceled = true;
}
if (Object.prototype.hasOwnProperty.call(this.shootPromises, device)) {
this.shootPromises[device].cancel();
sthCanceled = true;
}
if (!sthCanceled) {
throw new Error("Shoot not initiated on our side. Not aborting");
}
}
getAPI() {
return {
shoot: this.shoot,
stream: this.stream,
abort: this.abort,
setCurrentImagingSetup: this.setCurrentImagingSetup,
setDefaultImageLoadingPath: this.setDefaultImageLoadingPath,
getImageFiles: this.getImageFiles,
}
}
}