-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndiAutoGphotoSensorSize.ts
141 lines (114 loc) · 5 KB
/
IndiAutoGphotoSensorSize.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
import CancellationToken from 'cancellationtoken';
import Log from './Log';
import IndiManager from './IndiManager';
const logger = Log.logger(__filename);
// Evaluate f function, but if fail, return def
function noErr<T>(f:()=>T, def: T)
{
try {
return f();
} catch(e) {
return def;
}
}
const targetVector = 'CCD_INFO';
const defaultValues = {
CCD_MAX_X: "16000",
CCD_MAX_Y: "9000",
CCD_PIXEL_SIZE: "10",
CCD_PIXEL_SIZE_X: "10",
CCD_PIXEL_SIZE_Y: "10",
CCD_BITSPERPIXEL: "16"
}
export default class IndiAutoGphotoSensorSize {
// Device => connectattempted
memory:{[id:string]:boolean} = {};
private readonly indiManager: IndiManager;
constructor(indiManager:IndiManager) {
this.indiManager = indiManager;
// Change of the config flag for any will trigger recompute
indiManager.appStateManager.addSynchronizer(['indiManager', 'configuration', 'indiServer', 'devices', null, 'options', 'autoGphotoSensorSize'],
this.check, false);
// Any change of the connection status will trigger recompute
indiManager.appStateManager.addSynchronizer(['indiManager', 'deviceTree', null, 'CONNECTION', 'childs', 'CONNECT'],
this.check, false);
indiManager.appStateManager.addSynchronizer(['indiManager', 'deviceTree', null, targetVector],
this.check, false);
}
private check=()=>{
// Check all the devices with flag set to true
// if connection status is :
// - missing or idle (connected), clear the memory
// - busy, set the memory to done
// - idle disconnected, connect
// Remove the unknown devices from the memory
logger.debug('Recheck');
const configDevices = noErr(()=>this.indiManager.currentStatus.configuration.indiServer.devices, undefined) || {};
const configuredDevices:{[id:string]:boolean} = {};
for(let devId of Object.keys(configDevices))
{
const dev = configDevices[devId];
if (!noErr(()=>dev.options.autoGphotoSensorSize, undefined)) {
continue;
}
logger.debug('Configured device', {devId});
configuredDevices[devId] = true;
}
const unseenDevices = {...this.memory};
if (this.indiManager.connection) {
const c = this.indiManager.connection;
const deviceIds = c.getAvailableDeviceIdsWith([targetVector]);
logger.debug('valid devices', {deviceIds});
for(let devId of deviceIds) {
if (!Object.prototype.hasOwnProperty.call(configuredDevices, devId)) {
continue;
}
// Check the connection state
const connVector = c.getDevice(devId).getVector('CONNECTION');
if (!connVector.exists()) {
continue;
}
if (connVector.getState() === 'Busy') {
continue;
}
if (connVector.getPropertyValueIfExists('CONNECT') !== 'On') {
continue;
}
// Check the target vector
delete(unseenDevices[devId]);
const vector = c.getDevice(devId).getVector(targetVector);
if (vector.getState() === 'Busy') {
continue;
}
const values = Object.keys(defaultValues).sort().map(k=>{
const strValue = vector.getPropertyValueIfExists(k);
return strValue === null ? null : parseFloat(strValue);
});
logger.debug('Current values', {devId, values});
if (values.filter(e=>(e!==0 && e !== null)).length === 0) {
continue;
}
// Check the connection state of the device
// Take memory state from the state of the connection vector
const memoryState = Object.prototype.hasOwnProperty.call(this.memory, devId) ? this.memory[devId] : null;
logger.debug('Memory state ', {devId, memoryState});
if (memoryState === true) {
continue;
}
this.memory[devId] = true;
(async ()=> {
try {
logger.info('Starting', {devId, defaultValues});
await this.indiManager.setParam(CancellationToken.CONTINUE, devId, targetVector, defaultValues, true);
} catch(e) {
logger.warn('Ignoring set size error', {devId, defaultValues}, e);
}
})();
}
}
for(let devId of Object.keys(unseenDevices)) {
logger.debug('Forget', {devId});
delete this.memory[devId];
}
}
}