-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScopeTrackCounter.ts
78 lines (67 loc) · 2.11 KB
/
ScopeTrackCounter.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
import IndiManager from "./IndiManager";
import { IndiConnection } from "./Indi";
// Count the duration of scope is tracking
export default class ScopeTrackCounter {
private readonly indiManager: IndiManager;
private readonly scope: string;
private previousDuration: number;
private currentState: boolean;
private currentStart: number;
private started:boolean = false;
constructor(indiManager: IndiManager, scope: string) {
this.indiManager = indiManager;
this.scope = scope;
this.currentState = false;
this.currentStart = 0;
this.previousDuration = 0;
}
private getState(cnx?: IndiConnection) {
if (cnx === undefined) {
return false;
}
const propState = cnx.getDevice(this.scope).getVector("TELESCOPE_TRACK_STATE").getPropertyValueIfExists("TRACK_ON");
return "On" === propState;
}
private setState(newState: boolean) {
if (newState === this.currentState) {
return;
}
const now = new Date().getTime();
if (!newState) {
this.previousDuration += now - this.currentStart;
}
this.currentStart = now;
this.currentState = newState;
}
private check = (cnx?: IndiConnection) => {
const newState = this.getState(cnx);
this.setState(newState);
}
public start() {
if (this.started) {
this.stop();
}
this.started = true;
this.currentState = false;
this.currentStart = 0;
this.previousDuration = 0;
this.check(this.indiManager.connection);
this.indiManager.addConnectionListener(this.check);
}
public stop() {
if (!this.started) {
return;
}
this.started = false;
this.setState(false);
this.indiManager.removeConnectionListener(this.check);
}
public getElapsed(): number {
let ret = this.previousDuration;
if (this.currentState) {
const now = new Date().getTime();
ret += (now - this.currentStart);
}
return ret;
}
}