Skip to content

Commit e4e2154

Browse files
Add logging for simulator actions (#1021)
1 parent 97faa9b commit e4e2154

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

src/device/simulator.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ export class SimulatorDeviceConnection
163163
}
164164
case "request_flash": {
165165
this.emit(EVENT_REQUEST_FLASH);
166+
this.logging.event({
167+
type: "sim-user-start",
168+
});
166169
break;
167170
}
168171
case "state_change": {
@@ -229,11 +232,21 @@ export class SimulatorDeviceConnection
229232

230233
constructor(
231234
private logging: Logging,
232-
private iframe: () => HTMLIFrameElement | null
235+
private iframe: () => HTMLIFrameElement | null,
236+
private sensorsLogged: Record<string, boolean> = {}
233237
) {
234238
super();
235239
}
236240

241+
private logSensor(sensorId: string): void {
242+
if (!this.sensorsLogged[sensorId]) {
243+
this.logging.event({
244+
type: `sim-user-${sensorId}`,
245+
});
246+
this.sensorsLogged[sensorId] = true;
247+
}
248+
}
249+
237250
async initialize(): Promise<void> {
238251
window.addEventListener("message", this.messageListener);
239252
this.setStatus(ConnectionStatus.NOT_CONNECTED);
@@ -286,11 +299,13 @@ export class SimulatorDeviceConnection
286299
}
287300

288301
radioSend(message: string) {
302+
const kind = "radio_input";
289303
const data = new TextEncoder().encode(message);
290304
const prefixed = new Uint8Array(3 + data.length);
291305
prefixed.set([1, 0, 1]);
292306
prefixed.set(data, 3);
293-
this.postMessage("radio_input", { data: prefixed });
307+
this.postMessage(kind, { data: prefixed });
308+
this.logSensor(kind);
294309
}
295310

296311
setSimulatorValue = async (
@@ -314,6 +329,7 @@ export class SimulatorDeviceConnection
314329
id,
315330
value,
316331
});
332+
this.logSensor(id);
317333
};
318334

319335
stop = async (): Promise<void> => {
@@ -323,14 +339,23 @@ export class SimulatorDeviceConnection
323339
reset = async (): Promise<void> => {
324340
this.postMessage("reset", {});
325341
this.notifyResetComms();
342+
this.logging.event({
343+
type: "sim-user-reset",
344+
});
326345
};
327346

328347
mute = async (): Promise<void> => {
329348
this.postMessage("mute", {});
349+
this.logging.event({
350+
type: "sim-user-mute",
351+
});
330352
};
331353

332354
unmute = async (): Promise<void> => {
333355
this.postMessage("unmute", {});
356+
this.logging.event({
357+
type: "sim-user-unmute",
358+
});
334359
};
335360

336361
private setStatus(newStatus: ConnectionStatus) {

src/simulator/DataLoggingModule.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ReactNode, useCallback, useMemo } from "react";
2222
import { RiDownload2Line, RiErrorWarningLine } from "react-icons/ri";
2323
import { FormattedMessage } from "react-intl";
2424
import { DataLog } from "../device/simulator";
25+
import { useLogging } from "../logging/logging-hooks";
2526
import { useDataLog } from "./data-logging-hooks";
2627
import { useAutoScrollToBottom } from "./scroll-hooks";
2728

@@ -53,12 +54,16 @@ const DataLoggingModule = ({
5354
};
5455
}, [untruncatedDataLog]);
5556
const [ref, handleScroll] = useAutoScrollToBottom(truncatedDataLog);
57+
const logging = useLogging();
5658
const handleSaveLog = useCallback(() => {
5759
const blob = new Blob([toCsv(untruncatedDataLog)], {
5860
type: "text/csv;charset=utf-8",
5961
});
6062
saveAs(blob, "simulated-log-data.csv");
61-
}, [untruncatedDataLog]);
63+
logging.event({
64+
type: "sim-user-data-log-saved",
65+
});
66+
}, [logging, untruncatedDataLog]);
6267
if (minimised) {
6368
return (
6469
<HStack spacing={3}>

src/simulator/SimulatorActionBar.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "../device/device-hooks";
2020
import { EVENT_REQUEST_FLASH } from "../device/simulator";
2121
import { useFileSystem } from "../fs/fs-hooks";
22+
import { useLogging } from "../logging/logging-hooks";
2223
import { RunningStatus } from "./Simulator";
2324

2425
interface SimulatorActionBarProps extends BoxProps {
@@ -34,6 +35,7 @@ const SimulatorActionBar = ({
3435
const device = useSimulator();
3536
const fs = useFileSystem();
3637
const intl = useIntl();
38+
const logging = useLogging();
3739
const [isMuted, setIsMuted] = useState<boolean>(false);
3840
const syncStatus = useSyncStatus();
3941
const handlePlay = useCallback(async () => {
@@ -43,13 +45,21 @@ const SimulatorActionBar = ({
4345
});
4446
onRunningChange(RunningStatus.RUNNING);
4547
}, [device, fs, onRunningChange]);
46-
const handleStop = useCallback(() => {
47-
device.stop();
48-
onRunningChange(RunningStatus.STOPPED);
49-
}, [device, onRunningChange]);
48+
const handleStop = useCallback(
49+
(source: "user" | "code") => {
50+
device.stop();
51+
onRunningChange(RunningStatus.STOPPED);
52+
if (source === "user") {
53+
logging.event({
54+
type: "sim-user-stopped",
55+
});
56+
}
57+
},
58+
[device, logging, onRunningChange]
59+
);
5060
useEffect(() => {
5161
if (syncStatus === SyncStatus.OUT_OF_SYNC) {
52-
handleStop();
62+
handleStop("code");
5363
}
5464
}, [handleStop, syncStatus]);
5565
const handleMuteUnmute = useCallback(() => {
@@ -72,7 +82,7 @@ const SimulatorActionBar = ({
7282
<IconButton
7383
size={size}
7484
variant="outline"
75-
onClick={handleStop}
85+
onClick={() => handleStop("user")}
7686
icon={<RiStopFill />}
7787
aria-label={intl.formatMessage({ id: "simulator-stop" })}
7888
disabled={running === RunningStatus.STOPPED}

0 commit comments

Comments
 (0)