-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTriggerExecuter.ts
240 lines (215 loc) · 7.77 KB
/
TriggerExecuter.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
import CancellationToken from 'cancellationtoken';
import Log from './Log';
import * as Obj from './shared/Obj';
import ConfigStore from './ConfigStore';
import { BackofficeStatus, TriggerConfig, TriggerExecuterStatus, ToolConfig } from './shared/BackOfficeStatus';
import JsonProxy from './shared/JsonProxy';
import { AppContext } from './ModuleBase';
const logger = Log.logger(__filename);
// Connect to a JSONProxy and react to state change
// Example trigger:
// {
//
// desc: 'Autoconnect CCD Simulator'
// type: 'indi-new-property',
// device: 'CCD Simulator',
// property:'Connection',
// value: 'Disconnect'
// trigger: switchToValue('Connect')
// }
//
//
class IndiNewProperty
{
private readonly key: string;
readonly params: TriggerConfig;
private readonly context: AppContext;
private last: string[];
constructor(key:string, params: TriggerConfig, context: AppContext)
{
this.key = key;
this.params = params;
this.context = context;
this.last = [];
}
private getProperties=(newState: BackofficeStatus)=>{
if (newState.indiManager === undefined) return undefined;
var device = newState.indiManager.deviceTree[this.params.device];
if (device === undefined) return undefined;
var vector = device[this.params.vector];
if (vector === undefined) return undefined;
if (Array.isArray(this.params.property)) {
var result = [];
for(var i = 0; i < this.params.property.length; ++i)
{
var property = vector.childs[this.params.property[i]];
if (property === undefined) return undefined;
result[i] = property;
}
return result;
} else {
var property = vector.childs[this.params.property];
if (property === undefined) return undefined;
return [property];
}
}
private getCurrentValue=(newState: BackofficeStatus):string[]=> {
var prop = this.getProperties(newState);
if (prop === undefined) return [];
var result = [];
for(var i = 0 ; i < prop.length; ++i) {
result[i] = prop[i].$_;
}
return result;
}
private getTargetProperty=(i:number)=> {
if (Array.isArray(this.params.property)) {
return this.params.property[i];
} else if (i == 0) {
return this.params.property;
} else {
throw new Error("Wrong trigger properties");
}
}
private getTargetValue=(i:number)=> {
if (Array.isArray(this.params.property)) {
if (!Array.isArray(this.params.value)) {
throw new Error("Array/simple mismatch for trigger properties");
}
return this.params.value[i];
} else if (i == 0) {
if (Array.isArray(this.params.value)) {
throw new Error("Array/simple mismatch for trigger properties");
}
return this.params.value;
} else {
throw new Error("Wrong trigger properties");
}
}
private action=(newState: BackofficeStatus, oldValues: string[], newValues:string[])=> {
if (oldValues.length == 0) {
var toSet: {[id:string]:string} = {};
var changeRequired = false;
for(var i = 0 ; i < newValues.length; ++i) {
var targetValue = this.getTargetValue(i);
if (targetValue !== newValues[i]) {
changeRequired = true;
toSet[this.getTargetProperty(i)] = targetValue;
}
}
if (changeRequired) {
(async ()=> {
try {
await this.context.indiManager.setParam(
CancellationToken.CONTINUE,
this.params.device,
this.params.vector,
toSet
);
} catch(e) {
logger.error('Trigger failed' , {key: this.key}, e);
}
})();
}
}
}
check(newState: BackofficeStatus)
{
var newValue = this.getCurrentValue(newState);
if (!Obj.deepEqual(this.last, newValue)) {
logger.debug('Activating trigger' , {key: this.key});
var previousValue = this.last;
this.last = newValue;
this.action(newState, previousValue, newValue);
}
}
};
export default class TriggerExecuter
{
private jsonProxy: JsonProxy<BackofficeStatus>;
private instanciatedTriggers: {[id:string]:IndiNewProperty};
private context: AppContext;
config: { [id: string]: TriggerConfig; };
constructor(jsonProxy:JsonProxy<BackofficeStatus>, context:AppContext) {
this.jsonProxy = jsonProxy;
this.instanciatedTriggers = {};
this.context = context;
jsonProxy.getTarget().triggerExecuter = {
triggers: {}
};
this.config = jsonProxy.getTarget().triggerExecuter.triggers;
jsonProxy.addSynchronizer(['triggerExecuter'/*, 'triggers'*/],
this.syncTriggers.bind(this),
true);
new ConfigStore<TriggerExecuterStatus['triggers']>(jsonProxy, 'triggerExecuter', ['triggerExecuter', 'triggers'], {
}, {
autoconnect_CCDSimulator: {
desc:'testautoconnect',
device: 'CCD Simulator',
vector:'CONNECTION',
property: 'CONNECT',
value: 'On'
},
"gps_autoset_lat": {
"desc": "Push local coordinates to Mount",
"device": "Mount",
"vector": "GEOGRAPHIC_COORD",
"property": ["LONG", "LAT"],
"value": ["1.4", "48.0833"]
}
});
/* this.triggers.push(new IndiNewProperty({
desc:'testautoconnect',
device: 'CCD Simulator',
vector:'CONNECTION',
property: 'CONNECT',
value: 'On'
}, context));
*/
this.jsonProxy.addListener(this.listener);
this.listener();
}
private createTrigger=(key:string, params: TriggerConfig)=>{
logger.debug('Instanciating trigger' , {key, params});
var result = new IndiNewProperty(key, params, this.context);
try {
result.check(this.jsonProxy.getTarget());
} catch(e) {
logger.debug('Error in trigger' , {key, params}, e);
}
return result;
}
private syncTriggers=()=>
{
logger.debug('Syncing triggers');
for(const ikey of Object.keys(this.config))
{
const wantedParams = this.config[ikey];
if (!Obj.hasKey(this.instanciatedTriggers, ikey)) {
this.instanciatedTriggers[ikey] = this.createTrigger(ikey, Obj.deepCopy(wantedParams));
} else {
var existing = this.instanciatedTriggers[ikey];
if (!Obj.deepEqual(existing.params, wantedParams)) {
this.instanciatedTriggers[ikey] = this.createTrigger(ikey, Obj.deepCopy(wantedParams));
}
}
}
for(const ikey of Object.keys(this.instanciatedTriggers))
{
if (!Obj.hasKey(this.config, ikey)) {
delete this.instanciatedTriggers[ikey];
}
}
}
private listener=()=>
{
var state = this.jsonProxy.getTarget();
for(var key of Object.keys(this.instanciatedTriggers)) {
try {
this.instanciatedTriggers[key].check(state);
} catch(e) {
logger.error('Error in trigger', {key}, e);
}
}
}
};