-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-state-data-object.ts
157 lines (141 loc) · 4.19 KB
/
get-state-data-object.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
/**
* The {@link GetStateDataObject} class is part of the {@link GetStateData}
* class, which is kind of an object representation of the `/GetState.csv`
* API endpoint response of the ProCon.IP pool controller.
* @packageDocumentation
*/
/**
* An object representation of a single CSV response column _(ignoring the first
* row of the raw input!)_.
*/
export class GetStateDataObject {
/**
* Making {@link GetStateDataObject} objects extensible, also allows accessing
* object keys using string variables.
*/
[key: string]: any; // eslint-disable-line no-undef
/**
* Object id aka column index.
*/
public id!: number;
/**
* Object label.
*/
public label!: string;
/**
* Raw object input value.
*/
public raw!: number;
/**
* Object value offset.
*/
public offset!: number;
/**
* Object value gain.
*/
public gain!: number;
/**
* Plain (calculated) object value.
*/
public value!: string | number;
/**
* Object display value.
*/
public displayValue!: string;
/**
* Object unit.
*/
public unit!: string;
/**
* Object instance category string.
*/
public category!: string;
/**
* Sub-index for each category.
*
* Starts counting from `0` at the first object of the instances category.
* Used to determine e.g. the relay IDs.
*/
public categoryId!: number;
/**
* Indicates whether the object is considered to be active.
*
* Indeed this only means the name is not '_n.a._'.
*/
public active!: boolean;
/**
* Passthru all parameters to {@link GetStateDataObject.set}.
*
* @param index Column id/index
* @param name Column or data portion name
* @param unit Column or data portion unit (if applicable in any way)
* @param offset Column value offset
* @param gain Column value gain
* @param measure Column value raw measurement
*/
public constructor(index: number, name: string, unit: string, offset: string, gain: string, measure: string) {
this.set(index, name, unit, offset, gain, measure);
}
/**
* Set object values based on the raw input values.
*
* The input values correspond to the data rows of the represented column
* (except the `index` paramter which indeed is the column id/index itself).
*
* @param index Column id/index
* @param name Column or data portion name
* @param unit Column or data portion unit (if applicable in any way)
* @param offset Column value offset
* @param gain Column value gain
* @param measure Column value raw measurement
*/
public set(index: number, name: string, unit: string, offset: string, gain: string, measure: string): void {
// Set basic object values.
this.id = index;
this.label = name;
this.displayValue = '';
this.unit = unit;
this.offset = Number(offset);
this.gain = Number(gain);
this.raw = Number(measure);
this.value = this.offset + this.gain * this.raw;
this.category = this.category === undefined ? 'none' : this.category;
this.categoryId = this.categoryId === undefined ? 0 : this.categoryId;
this.active = name !== 'n.a.'; // Mark object as active if it is not labeled with 'n.a.'.
// Set display value according to the object unit.
switch (this.unit) {
case 'C':
case 'F':
this.displayValue = `${Number(this.value).toFixed(2)} °${this.unit}`;
break;
case 'h':
/* tslint:disable: no-bitwise */
this.displayValue =
(Number(this.value) >> 8 < 10 ? 0 : '') +
'' +
(Number(this.value) >> 8) +
':' +
((Number(this.value) & 0xff) < 10 ? 0 : '') +
'' +
(Number(this.value) & 0xff);
/* tslint:enable: no-bitwise */
break;
// case "pH":
// this.displayValue = `${this.unit} ${this.value}`;
// break;
case '--':
this.displayValue = String(this.value);
break;
default:
this.displayValue = `${Number(this.value).toFixed(2)} ${this.unit}`;
}
}
/**
* Iterate all fields of this object.
*
* @param callback A user-defined callback.
*/
public forFields(callback: (field: string) => any): void {
Object.keys(this).forEach(callback);
}
}