-
Notifications
You must be signed in to change notification settings - Fork 2
/
FilterWheel.ts
311 lines (278 loc) · 11.5 KB
/
FilterWheel.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
import CancellationToken from 'cancellationtoken';
import Log from './Log';
import { ExpressApplication, AppContext } from "./ModuleBase";
import { BackofficeStatus, FilterWheelStatus} from './shared/BackOfficeStatus';
import JsonProxy, { TriggeredWildcard, NoWildcard } from './shared/JsonProxy';
import { hasKey } from './shared/Obj';
import { Task } from "./Task.js";
import * as Obj from "./shared/Obj";
import * as RequestHandler from "./RequestHandler";
import * as BackOfficeAPI from "./shared/BackOfficeAPI";
const logger = Log.logger(__filename);
export default class FilterWheel
implements RequestHandler.APIAppProvider<BackOfficeAPI.FilterWheelAPI>
{
appStateManager: JsonProxy<BackofficeStatus>;
switchPromises: {[filterWheelId: string]:Task<void>};
currentStatus: FilterWheelStatus;
context: AppContext;
get indiManager() { return this.context.indiManager };
constructor(app:ExpressApplication, appStateManager:JsonProxy<BackofficeStatus>, context:AppContext) {
this.appStateManager = appStateManager;
this.appStateManager.getTarget().filterWheel = {
dynStateByDevices: {},
};
// Device => promise
this.switchPromises = {};
this.currentStatus = this.appStateManager.getTarget().filterWheel;
this.context = context;
// Update dynState for each filterwheels
this.appStateManager.addSynchronizer(
[ 'indiManager', 'availableFilterWheels' ],
()=> {
const dynStateRoot = this.currentStatus.dynStateByDevices;
for(const o of this.indiManager.currentStatus.availableFilterWheels) {
if (!Obj.hasKey(dynStateRoot, o)) {
dynStateRoot[o] = {
targetFilterPos: null,
currentFilterPos: null,
filterIds: [],
}
}
}
},
true);
// Compute available filter list
this.appStateManager.addSynchronizer(
[
[
[ 'indiManager', 'availableFilterWheels' ],
[ 'filterWheel', 'dynStateByDevices', null ],
[ 'indiManager', 'deviceTree', null, 'FILTER_NAME' ],
[ 'indiManager', 'deviceTree', null, 'FILTER_SLOT', 'childs', 'FILTER_SLOT_VALUE', [['$min'],['$max']] ],
]
],
this.updateFilterIdsForSomeFilterWheels,
true,
true
)
// Compute current filter
this.appStateManager.addSynchronizer(
[
[
[ 'indiManager', 'deviceTree', null, 'FILTER_SLOT', 'childs', 'FILTER_SLOT_VALUE', '$_' ],
[ 'filterWheel', 'dynStateByDevices', null ],
]
],
this.updateCurrentFilterPosForSomeFilterWheels,
true,
true,
);
}
private updateCurrentFilterPos=(deviceId:string)=> {
if (!hasKey(this.currentStatus.dynStateByDevices, deviceId)) {
return null;
}
let pos:number|null = null;
try {
const device = this.indiManager.getValidConnection().getDevice(deviceId);
const slotVec = device.getVector('FILTER_SLOT').getVectorInTree();
if (slotVec !== null) {
pos = parseInt(slotVec.childs.FILTER_SLOT_VALUE.$_, 10);
if (isNaN(pos)) {
pos = null;
}
}
} catch(e) {
}
this.currentStatus.dynStateByDevices[deviceId].currentFilterPos = pos;
}
private updateCurrentFilterPosForSomeFilterWheels=(wildcards:TriggeredWildcard)=> {
const idList = wildcards[NoWildcard]
? Object.keys(this.currentStatus.dynStateByDevices)
: Object.keys(wildcards);
for(const id of idList) {
this.updateCurrentFilterPos(id);
}
}
// Un filter id est soit un chiffre (parseInt) soit #filterId
private computeFilterIds= (deviceId:string)=> {
const ret: string[] = [];
const device = this.indiManager.getValidConnection().getDevice(deviceId);
const nameVec = device.getVector('FILTER_NAME').getVectorInTree();
if (nameVec != null) {
const occ : {[id:string]:number} = {};
for(const id of nameVec.childNames) {
const value = nameVec.childs[id].$_;
const filterId = "#" + value.replace(/#/g, '_');
if (!hasKey(occ,filterId)) {
occ[filterId] = 1;
ret.push(filterId);
} else {
occ[filterId]++;
ret.push(filterId + ' #' + occ[filterId]);
}
}
} else {
// Just trust the slots
const slotVec = device.getVector('FILTER_SLOT').getVectorInTree();
if (slotVec !== null && slotVec.childs['FILTER_SLOT_VALUE']) {
const value = slotVec.childs['FILTER_SLOT_VALUE']
const min = parseInt(value.$min!);
const max = parseInt(value.$max!);
if (!isNaN(min) && !isNaN(max)) {
for(let i = min; i < max; ++i) {
ret.push("" + i);
}
}
}
}
return ret;
}
private updateFilterIds=(fwId: string)=> {
if (!hasKey(this.currentStatus.dynStateByDevices, fwId)) {
return null;
}
let ret:string[] = [];
try {
ret = this.computeFilterIds(fwId);
} catch(e) {
}
this.currentStatus.dynStateByDevices[fwId].filterIds = ret;
}
updateFilterIdsForSomeFilterWheels=(wildcards:TriggeredWildcard)=> {
const idList = wildcards[NoWildcard]
? Object.keys(this.currentStatus.dynStateByDevices)
: Object.keys(wildcards);
for(const id of idList) {
this.updateFilterIds(id);
}
}
public getFilterId(fwId: string, no: number) {
try {
const ids = this.computeFilterIds(fwId);
if (no >= 1 && no <= ids.length) {
return ids[no-1];
}
return "" + no;
} catch(e) {
return "" + no;
}
}
abortFilterChange= async(ct:CancellationToken, payload:any)=>{}
private needConfirmation(fwId:string) {
if (this.isManualFilterIndiDriver(fwId)) {
return true;
}
const devConf = this.indiManager.currentStatus.configuration.indiServer.devices;
if (!hasKey(devConf, fwId)) {
return false;
}
return !!devConf[fwId].options.confirmFilterChange;
}
private isManualFilterIndiDriver(fwId:string) {
const driver = this.indiManager.getValidConnection().getDevice(fwId).getVector("DRIVER_INFO").getPropertyValueIfExists("DRIVER_EXEC");
return driver === "indi_manual_wheel";
}
// Operation can be canceled by user
changeFilter= async(ct:CancellationToken, payload: {filterWheelDeviceId: string, filterNumber?: number, filterId?: string, force?: boolean})=>{
const filterWheelDeviceId = payload.filterWheelDeviceId;
const checkFilterWheel=(force?: boolean)=>{
if (!hasKey(this.currentStatus.dynStateByDevices, filterWheelDeviceId)) {
throw new Error("Device not available");
}
let filterPos:number;
if (payload.filterId !== undefined) {
let i = this.currentStatus.dynStateByDevices[filterWheelDeviceId].filterIds.indexOf(payload.filterId);
if (i === -1) {
throw new Error("Unknown filter");
}
filterPos = i + 1;
} else if (payload.filterNumber !== undefined) {
// FIXME: check bounds but indi SHOULD explicitely reject invalid values
filterPos = payload.filterNumber!;
} else {
throw new Error("No filter provided");
}
if (this.currentStatus.dynStateByDevices[filterWheelDeviceId].targetFilterPos !== null) {
throw new Error("Filter wheel busy");
}
if ((!(payload.force || force))
&& this.currentStatus.dynStateByDevices[filterWheelDeviceId].currentFilterPos === filterPos)
{
logger.debug('FilterWheel already at pos', {filterWheelDeviceId, filterPos});
return undefined;
}
return filterPos;
}
logger.info('FilterWheel move', {filterWheelDeviceId, payload});
let filterPos:number|undefined;
if ((filterPos = checkFilterWheel()) === undefined) {
return false;
}
let confirmed: boolean;
if (this.needConfirmation(filterWheelDeviceId)) {
logger.info('Asking confirmation for filter change', {filterWheelDeviceId});
const manualDriver = this.isManualFilterIndiDriver(filterWheelDeviceId);
const filterTitle = this.currentStatus.dynStateByDevices[filterWheelDeviceId].filterIds[filterPos - 1];
const ready = await this.context.notification.dialog(ct,
(manualDriver
? "Move filter of "
: "Confirm filter change of ")
+ filterWheelDeviceId + " to: " + filterTitle,
[
{
title: "ok",
value: true,
},
{
title: "pause",
value: false,
}
]);
if (!ready) {
logger.info('User canceled filter change', {filterWheelDeviceId});
throw new CancellationToken.CancellationError("User canceled");
}
// For manual filter wheel, issue a sync
if (manualDriver) {
logger.debug('Syncing manual filter wheel', {filterWheelDeviceId});
await this.indiManager.setParam(ct,
filterWheelDeviceId,
"SYNC_FILTER",
{"TARGET_FILTER": "" + filterPos},
true,
false
// FIXME: cancelator
);
return true;
}
if ((filterPos = checkFilterWheel(true)) === undefined) {
return false;
}
}
try {
// record target pos in dynState
this.currentStatus.dynStateByDevices[filterWheelDeviceId].targetFilterPos = filterPos;
logger.info('Moving FilterWheel', {filterWheelDeviceId, filterPos});
await this.indiManager.setParam(ct,
filterWheelDeviceId,
'FILTER_SLOT',
{'FILTER_SLOT_VALUE': "" + filterPos},
true,
false
// FIXME: cancelator
);
logger.info('Done moving filterWheel', {filterWheelDeviceId, filterPos});
} finally {
this.currentStatus.dynStateByDevices[filterWheelDeviceId].targetFilterPos = null;
}
return true;
}
getAPI() {
return {
abortFilterChange: this.abortFilterChange,
changeFilter: this.changeFilter,
}
}
}