-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndiMessageQueue.ts
212 lines (188 loc) · 6.55 KB
/
IndiMessageQueue.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
import Log from './Log';
const logger = Log.logger(__filename);
type PendingMessage = {
// null means process asap
elapsingTime: number|null;
fantom?: boolean;
message: any;
}
// Schedules messages delivery and group similar messages into one single delivery
export class IndiMessageQueue {
// Queue contains immediate message (elapsingTime === null), then timed message in asc order
messageQueue: PendingMessage[] = [];
nextExpireTimer?: NodeJS.Timer;
nextExpireTime?: number;
messageProcessor: (message:any)=>(void);
nextTimer?: number = undefined;
nextTimerId?: NodeJS.Timeout = undefined;
nextTickPending: boolean = false;
disposed : boolean = false;
defaultExpiration: number;
constructor(defaultExpiration: number, messageProcessor: (message:any)=>(void))
{
this.defaultExpiration = defaultExpiration;
this.messageProcessor = messageProcessor;
}
private flush() {
if (this.disposed) {
return;
}
while (this.messageQueue.length) {
const head = this.messageQueue.splice(0, 1)[0];
if (head.fantom) {
continue;
}
if (head.elapsingTime !== null) {
// A message aggregation succeeded. Restart with the exact same message, but later
this.pushTimed({
elapsingTime: new Date().getTime() + this.defaultExpiration,
message: head.message,
fantom: true
});
}
try {
this.messageProcessor(head.message);
} catch(e) {
logger.error('indi message processing failed', e);
}
break;
}
if ((!this.disposed) && this.messageQueue.length) {
const when = this.messageQueue[0].elapsingTime;
if (when === null) {
this.programImmediate();
} else {
this.programNotAfter(when);
}
}
}
private programNotAfter(when: number) {
if (this.nextTickPending) {
return;
}
if (this.nextTimer !== undefined) {
if (this.nextTimer <= when) {
return;
}
clearTimeout(this.nextTimerId!);
this.nextTimerId = undefined;
this.nextTimer = undefined;
}
let delay = when - new Date().getTime();
if (delay < 0) {
delay = 0;
}
this.nextTimer = when;
this.nextTimerId = setTimeout(()=> {
this.nextTimer = undefined;
this.nextTimerId = undefined;
this.flush();
}, delay);
}
private programImmediate() {
if (this.nextTimerId !== undefined) {
clearTimeout(this.nextTimerId);
this.nextTimerId = undefined;
this.nextTimer = undefined;
}
if (!this.nextTickPending) {
this.nextTickPending = true;
process.nextTick(()=>{
this.nextTickPending = false;
this.flush()
});
}
}
private pushImmediate(entry: PendingMessage) {
let i = 0;
while( i < this.messageQueue.length && this.messageQueue[i].elapsingTime === null) {
i++;
}
this.messageQueue.splice(i, 0, entry);
}
private pushTimed(newEntry: PendingMessage) {
// Check we don't go backward in time
if (this.messageQueue.length) {
const lastEntry = this.messageQueue[this.messageQueue.length - 1];
if (lastEntry.elapsingTime !== null
&& lastEntry.elapsingTime > newEntry.elapsingTime!)
{
newEntry.elapsingTime = lastEntry.elapsingTime;
}
}
this.messageQueue.push(newEntry);
}
public dispose() {
if (this.nextTimerId !== undefined) {
clearTimeout(this.nextTimerId);
this.nextTimerId = undefined;
this.nextTimer = undefined;
}
this.messageQueue = []
this.disposed = true;
}
private isDelayable(message: any) {
return (message.$$ === 'setNumberVector'
&& message.$name === 'CCD_EXPOSURE'
&& message.$state === 'Busy');
}
private getPendingMessageId(message: any):number|undefined
{
for(let i = 0; i < this.messageQueue.length; ++i) {
const e = this.messageQueue[i];
if (e.elapsingTime === null) {
continue;
}
if (e.message.$$ === message.$$
&& e.message.$name === message.$name
&& e.message.$state === message.$state
&& e.message.$device === message.$device) {
return i;
}
}
return undefined;
}
public queue(message :any) {
if (!this.isDelayable(message)) {
for(let i = 0; i < this.messageQueue.length;)
{
if (this.messageQueue[i].fantom) {
this.messageQueue.splice(i, 1);
} else {
if (this.messageQueue[i].elapsingTime !== null) {
this.messageQueue[i].elapsingTime = null;
}
i++;
}
}
this.messageQueue.push({
elapsingTime: null,
message
});
this.programImmediate();
} else {
// Find in the queue a pending message with the same prop & co
const existingMessagePos = this.getPendingMessageId(message);
if (existingMessagePos !== undefined) {
this.messageQueue[existingMessagePos].message = message;
if (this.messageQueue[existingMessagePos].fantom) {
logger.debug('Throttling INDI messages', {op: message.$$, device: message.$device, name: message.$name});
this.messageQueue[existingMessagePos].fantom = false;
}
} else {
this.pushImmediate({
elapsingTime: null,
message
});
// Push a fantom entry, that will aggregate in between messages
const newEntry = {
elapsingTime : new Date().getTime() + this.defaultExpiration,
message: message,
fantom: true,
};
this.pushTimed(newEntry);
this.programImmediate();
}
}
}
};