/
SensorObject.ts
115 lines (109 loc) · 3.78 KB
/
SensorObject.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
import { Unit } from '../../utils';
import { NumberType, SerializableMember, SerializableObject } from '../decorators';
import { Orientation } from '../position';
import { SensorValue } from '../values';
import { DataObject } from './DataObject';
/**
* Sensor calibration data
*/
@SerializableObject()
// eslint-disable-next-line
export class SensorCalibrationData<T = SensorValue | Object | Orientation> {
@SerializableMember(() => Unit)
unit?: Unit;
@SerializableMember()
offset?: T;
@SerializableMember()
multiplier?: T;
}
/**
* A sensor object is a {@link DataObject} that is a sensor with a value.
*/
@SerializableObject()
// eslint-disable-next-line
export abstract class SensorObject<T = SensorValue | Object | Orientation> extends DataObject {
/**
* Value of the sensor
*/
@SerializableMember()
value: T;
/**
* Frequency of the sensor
*/
@SerializableMember({
numberType: NumberType.DECIMAL,
})
frequency: number;
/**
* Sensor calibration data
*/
@SerializableMember()
calibrationData?: SensorCalibrationData<T>;
constructor(uid?: string, value?: T, frequency?: number, displayName?: string) {
super(uid, displayName);
this.value = value ?? ({} as T);
this.frequency = frequency;
}
/**
* Get the sensor timestamp
* @returns {number} timestamp
*/
get timestamp(): number {
return this.value instanceof SensorValue || this.value instanceof Orientation
? this.value.timestamp
: this.createdTimestamp;
}
/**
* Raw value before calibration
* @returns {SensorValue | object | Orientation} raw sensor value
*/
get raw(): T {
if (this.calibrationData) {
if (this.value instanceof SensorValue) {
let result = this.value.clone() as SensorValue;
const offset = this.calibrationData.offset as unknown as SensorValue;
const multiplier = this.calibrationData.multiplier as unknown as SensorValue;
if (multiplier) {
result = result.divide(multiplier);
}
if (offset) {
result = result.sub(offset);
}
return result as unknown as T;
} else {
return this.value;
}
} else if (typeof this.value === 'number') {
const offset = this.calibrationData.offset as unknown as number;
const multiplier = this.calibrationData.multiplier as unknown as number;
return (this.value / (multiplier ?? 1) - (offset ?? 0)) as unknown as T;
} else {
return this.value;
}
}
set raw(value: T) {
if (this.calibrationData) {
if (value instanceof SensorValue) {
let result = value.clone() as SensorValue;
const offset = this.calibrationData.offset as unknown as SensorValue;
const multiplier = this.calibrationData.multiplier as unknown as SensorValue;
if (offset) {
result = result.add(offset);
}
if (multiplier) {
result = result.multiply(multiplier);
}
this.value = result as unknown as T;
} else {
this.value = value;
}
} else if (typeof value === 'number') {
const offset = this.calibrationData.offset as unknown as number;
const multiplier = this.calibrationData.multiplier as unknown as number;
this.value = ((value + (offset ?? 0)) * (multiplier ?? 1)) as unknown as T;
} else {
this.value = value;
}
}
}
export type SensorType = new (uid?: string) => SensorObject;