-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.ts
87 lines (76 loc) · 2.31 KB
/
message.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
import { ENUMS } from "./enums.ts";
import { Field, FIELDS } from "./fields.ts";
import { MESSAGES } from "./messages.ts";
import { DataField } from "./data_field.ts";
import { DefinitionRecord } from "./definition_record.ts";
type Record = { [index: string]: number | string };
export class Message {
globalMsgNum: number;
name: string;
data: Record[] = [];
constructor(globalMsgNum: number, definitions: DefinitionRecord[]) {
this.globalMsgNum = globalMsgNum;
this.name = MESSAGES[this.globalMsgNum];
if (this.name !== undefined) {
const fields = FIELDS[this.globalMsgNum];
this.data = definitions
.map((definition: DefinitionRecord) => {
return this.makeMessage(fields, definition);
})
.flat();
}
}
makeMessage(
fields: { [index: string]: Field },
definition: DefinitionRecord,
): Record[] {
const finished: Record[] = [];
definition.valid().map((dataRecords: [number, DataField][]) => {
const obj: Record = {};
dataRecords.map((dataRecord: [number, DataField]) => {
const data = this.processValue(
fields[dataRecord[0]],
dataRecord[1].data,
);
obj[data[0]] = data[1];
});
finished.push(obj);
});
return finished;
}
// deno-lint-ignore no-explicit-any
processValue(field: Field, value: any): [string, number | string] {
if (field["type"].substring(0, 4) === "enum") {
value = ENUMS[field["type"]][value];
} else if (
field["type"] === "dateTime" ||
field["type"] === "localDateTime"
) {
const t = new Date(Date.UTC(1989, 11, 31, 0, 0, 0)).getTime() / 1000;
const d = new Date(0);
d.setUTCSeconds(value + t);
value = d.toISOString();
} else if (field["type"] === "coordinates") {
value *= 180.0 / 2 ** 31;
}
if (field["scale"] !== 0) {
if (Array.isArray(value)) {
value = value.map((val) => {
return (val * 1.0) / field["scale"];
});
} else {
value = (value * 1.0) / field["scale"];
}
}
if (field["offset"] !== 0) {
if (Array.isArray(value)) {
value = value.map((val) => {
return val - field["offset"];
});
} else {
value = value - field["offset"];
}
}
return [field["name"], value];
}
}