-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.ts
208 lines (182 loc) · 5.8 KB
/
utils.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
import { ChartDataset, ChartOptions } from "chart.js";
import { ChartJSOrUndefined } from "react-chartjs-2/dist/types";
export interface PluggableMonitorSetting {
// The setting identifier
readonly id?: string;
// A human-readable label of the setting (to be displayed on the GUI)
readonly label?: string;
// The setting type (at the moment only "enum" is avaiable)
readonly type?: string;
// The values allowed on "enum" types
readonly values?: string[];
// The selected value
selectedValue: string;
}
type PluggableMonitorSettings = Record<"baudrate", PluggableMonitorSetting>;
export type EOL = "" | "\n" | "\r" | "\r\n";
export function isEOL(str: any): str is EOL {
const eol = ["", "\n", "\r", "\r\n"];
return typeof str === "string" && eol.includes(str);
}
interface MonitorModelState {
autoscroll: boolean;
timestamp: boolean;
lineEnding: EOL;
interpolate: boolean;
darkTheme: boolean;
wsPort: number;
serialPort: string;
connected: boolean;
generate?: boolean;
dataPointThreshold: number;
}
export interface MonitorSettings {
pluggableMonitorSettings: PluggableMonitorSettings;
monitorUISettings: Partial<MonitorModelState>;
}
export namespace PluggableMonitor {
export namespace Protocol {
export enum ClientCommand {
SEND_MESSAGE = "SEND_MESSAGE",
CHANGE_SETTINGS = "CHANGE_SETTINGS",
}
export enum MiddlewareCommand {
ON_SETTINGS_DID_CHANGE = "ON_SETTINGS_DID_CHANGE",
}
export type ClientCommandMessage = {
command: ClientCommand;
data: Partial<MonitorSettings> | string;
};
type MiddlewareCommandMessage = {
command: MiddlewareCommand;
data: Partial<MonitorSettings>;
};
type DataMessage = string[];
export type Message =
| ClientCommandMessage
| MiddlewareCommandMessage
| DataMessage;
export function isClientCommandMessage(
message: Message
): message is ClientCommandMessage {
return (
!Array.isArray(message) &&
typeof message.command === "string" &&
Object.keys(ClientCommand).includes(message.command)
);
}
export function isMiddlewareCommandMessage(
message: Message
): message is MiddlewareCommandMessage {
return (
!Array.isArray(message) &&
typeof message.command === "string" &&
Object.keys(MiddlewareCommand).includes(message.command)
);
}
export function isDataMessage(message: Message): message is DataMessage {
return Array.isArray(message);
}
}
}
const lineColors = [
"#0072B2",
"#D55E00",
"#009E73",
"#E69F00",
"#CC79A7",
"#56B4E9",
"#F0E442",
"#95A5A6",
];
let existingDatasetsMap: {
[key: string]: ChartDataset<"line">;
} = {};
export const resetExistingDatasetsMap = () => {
existingDatasetsMap = {};
};
export const resetDatapointCounter = () => {
datapointCounter = 0;
};
export let datapointCounter = 0;
export const addDataPoints = (
parsedMessages: {
datasetNames: string[];
parsedLines: { [key: string]: number }[];
},
chart: ChartJSOrUndefined,
opts: ChartOptions<"line">,
cubicInterpolationMode: "default" | "monotone",
dataPointThreshold: number,
setForceUpdate: React.Dispatch<any>
) => {
if (!chart) {
return;
}
// if the chart has been crated, can add data to it
if (chart && chart.data.datasets) {
const { datasetNames, parsedLines } = parsedMessages;
const existingDatasetNames = Object.keys(existingDatasetsMap);
// add missing datasets to the chart
existingDatasetNames.length < 8 &&
datasetNames.forEach((datasetName) => {
if (
!existingDatasetNames.includes(datasetName) &&
existingDatasetNames.length < 8
) {
const newDataset = {
data: [],
label: datasetName,
borderColor: lineColors[existingDatasetNames.length],
backgroundColor: lineColors[existingDatasetNames.length],
borderWidth: 1,
pointRadius: 0,
cubicInterpolationMode,
};
existingDatasetsMap[datasetName] = newDataset;
chart.data.datasets.push(newDataset);
existingDatasetNames.push(datasetName);
// used to force a re-render in the parent component
setForceUpdate(existingDatasetNames.length);
}
});
// iterate every parsedLine, adding each variable to the corrisponding variable in the dataset
// if a dataset has not variable in the line, fill it with and empty value
parsedLines.forEach((parsedLine) => {
const xAxis =
opts.scales!.x?.type === "realtime" ? Date.now() : datapointCounter++;
// add empty values to datasets that are missing in the parsedLine
Object.keys(existingDatasetsMap).forEach((datasetName) => {
const newPoint =
datasetName in parsedLine
? {
x: xAxis,
y: parsedLine[datasetName],
}
: null;
newPoint && existingDatasetsMap[datasetName].data.push(newPoint);
});
});
const oldDataValue = datapointCounter - dataPointThreshold;
for (let s = 0; s < chart.data.datasets.length; s++) {
const dataset = chart.data.datasets[s];
let delCount = 0;
for (let i = 0; i < dataset.data.length; i++) {
if (dataset.data[i] && (dataset.data[i] as any).x < oldDataValue) {
delCount++;
} else {
dataset.data.splice(0, delCount);
break; // go to the next dataset
}
// purge the data if we need to remove all points
if (dataset.data.length === delCount) {
// remove the whole dataset from the chart and the map
delete existingDatasetsMap[dataset.label!];
chart.data.datasets.splice(s, 1);
setForceUpdate(-1);
}
}
}
chart.update();
}
};