Skip to content

[Simulator] Initial data logging #922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _default_workflow: &default_workflow
- "CircleCI API"
- "AWS Web"
_defaults: &defaults
resource_class: large
working_directory: ~/repo
_docker_defaults: &docker_defaults
image: cimg/node:16.14
Expand Down
24 changes: 24 additions & 0 deletions lang/ui.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,26 @@
"defaultMessage": "Collapse {title} module",
"description": "Aria label for collapse simulator module button"
},
"simulator-data-logging-empty": {
"defaultMessage": "No log entries.",
"description": "Shown in the simulator Data logging table when there are no rows"
},
"simulator-data-logging-full": {
"defaultMessage": "Log full",
"description": "Shown below the Data logging table to warn that the log is full"
},
"simulator-data-logging-rows": {
"defaultMessage": "{count, plural, =0 {No rows logged} 1 {# row logged} other {# rows logged}}",
"description": "Indicator of the number of rows logged"
},
"simulator-data-logging-save-log": {
"defaultMessage": "Save log",
"description": "Action label to save the data log as a file from the Data logging simulator panel"
},
"simulator-data-logging-truncated": {
"defaultMessage": "Older rows not shown",
"description": "Text shown in the Data logging table to indicate that old rows have been omitted"
},
"simulator-expand": {
"defaultMessage": "Expand simulator",
"description": "Aria label for the expand simulator button"
Expand Down Expand Up @@ -831,6 +851,10 @@
"defaultMessage": "Light level",
"description": "Light level title"
},
"simulator-log": {
"defaultMessage": "Data log",
"description": "Data log title"
},
"simulator-loud": {
"defaultMessage": "Loud",
"description": "Sound level high threshold marker hover text"
Expand Down
24 changes: 24 additions & 0 deletions lang/ui.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,26 @@
"defaultMessage": "Collapse {title} module",
"description": "Aria label for collapse simulator module button"
},
"simulator-data-logging-empty": {
"defaultMessage": "No log entries.",
"description": "Shown in the simulator Data logging table when there are no rows"
},
"simulator-data-logging-full": {
"defaultMessage": "Log full",
"description": "Shown below the Data logging table to warn that the log is full"
},
"simulator-data-logging-rows": {
"defaultMessage": "{count, plural, =0 {No rows} 1 {# row} other {# rows}}",
"description": "Indicator of the number of rows logged"
},
"simulator-data-logging-save-log": {
"defaultMessage": "Save log",
"description": "Action label to save the data log as a file from the Data logging simulator panel"
},
"simulator-data-logging-truncated": {
"defaultMessage": "Older rows not shown",
"description": "Text shown in the Data logging table to indicate that old rows have been omitted"
},
"simulator-expand": {
"defaultMessage": "Expand simulator",
"description": "Aria label for the expand simulator button"
Expand Down Expand Up @@ -830,6 +850,10 @@
"defaultMessage": "Light level",
"description": "Light level title"
},
"simulator-log": {
"defaultMessage": "Data log",
"description": "Data log title"
},
"simulator-loud": {
"defaultMessage": "Loud",
"description": "Sound level high threshold marker hover text"
Expand Down
24 changes: 24 additions & 0 deletions lang/ui.lol.json
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,26 @@
"defaultMessage": "Collapse {title} module",
"description": "Aria label for collapse simulator module button"
},
"simulator-data-logging-empty": {
"defaultMessage": "No log entries.",
"description": "Shown in the simulator Data logging table when there are no rows"
},
"simulator-data-logging-full": {
"defaultMessage": "Log full",
"description": "Shown below the Data logging table to warn that the log is full"
},
"simulator-data-logging-rows": {
"defaultMessage": "{count, plural, =0 {No rows} 1 {# row} other {# rows}}",
"description": "Indicator of the number of rows logged"
},
"simulator-data-logging-save-log": {
"defaultMessage": "Save log",
"description": "Action label to save the data log as a file from the Data logging simulator panel"
},
"simulator-data-logging-truncated": {
"defaultMessage": "Older rows not shown",
"description": "Text shown in the Data logging table to indicate that old rows have been omitted"
},
"simulator-expand": {
"defaultMessage": "Expand simulator",
"description": "Aria label for the expand simulator button"
Expand Down Expand Up @@ -830,6 +850,10 @@
"defaultMessage": "Light level",
"description": "Light level title"
},
"simulator-log": {
"defaultMessage": "Data log",
"description": "Data log title"
},
"simulator-loud": {
"defaultMessage": "Loud",
"description": "Sound level high threshold marker hover text"
Expand Down
2 changes: 2 additions & 0 deletions src/common/use-raf-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import useIsUnmounted from "./use-is-unmounted";

/**
* State that rate limits changes with request animation frame.
*
* Note that if you rely on the previous state value you'll get a stale one.
*/
const useRafState = <S>(
initialState: S | (() => S)
Expand Down
46 changes: 45 additions & 1 deletion src/device/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "./device";

// Simulator-only events.
export const EVENT_LOG_DATA = "log_data";
export const EVENT_RADIO_DATA = "radio_data";
export const EVENT_RADIO_GROUP = "radio_group";
export const EVENT_RADIO_RESET = "radio_reset";
Expand Down Expand Up @@ -54,6 +55,11 @@ export interface EnumSensor {

export type Sensor = RangeSensor | EnumSensor;

export interface LogEntry {
headings?: string[];
data?: string[];
}

export interface SimulatorState {
radio: RadioState;

Expand Down Expand Up @@ -96,6 +102,21 @@ export type SensorStateKey = Extract<
| "buttonB"
>;

export interface DataLog {
headings: string[];
data: DataLogRow[];
}

export interface DataLogRow {
isHeading?: boolean;
data: string[];
}

const initialDataLog = (): DataLog => ({
headings: [],
data: [],
});

/**
* A simulated device.
*
Expand All @@ -108,13 +129,14 @@ export class SimulatorDeviceConnection
status: ConnectionStatus = ConnectionStatus.NO_AUTHORIZED_DEVICE;
state: SimulatorState | undefined;

log: DataLog = initialDataLog();

private messageListener = (event: MessageEvent) => {
const iframe = this.iframe();
if (!iframe || event.source !== iframe.contentWindow || !event.data.kind) {
// Not an event for us.
return;
}

switch (event.data.kind) {
case "ready": {
this.state = event.data.state;
Expand Down Expand Up @@ -150,6 +172,28 @@ export class SimulatorDeviceConnection
}
break;
}
case "log_output": {
const entry: LogEntry = event.data;
const result: DataLog = {
headings: entry.headings ?? this.log.headings,
data: this.log.data,
};
// The first row is all-time headings row so don't show the initial set.
if (entry.headings && this.log.data.length > 0) {
result.data.push({ isHeading: true, data: entry.headings });
}
if (entry.data) {
result.data.push({ data: entry.data });
}
this.log = result;
this.emit(EVENT_LOG_DATA, this.log);
break;
}
case "log_delete": {
this.log = initialDataLog();
this.emit(EVENT_LOG_DATA, this.log);
break;
}
case "serial_output": {
const text = event.data.data;
if (typeof text === "string") {
Expand Down
70 changes: 70 additions & 0 deletions src/messages/ui.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,70 @@
"value": " module"
}
],
"simulator-data-logging-empty": [
{
"type": 0,
"value": "No log entries."
}
],
"simulator-data-logging-full": [
{
"type": 0,
"value": "Log full"
}
],
"simulator-data-logging-rows": [
{
"offset": 0,
"options": {
"1": {
"value": [
{
"type": 7
},
{
"type": 0,
"value": " row logged"
}
]
},
"=0": {
"value": [
{
"type": 0,
"value": "No rows logged"
}
]
},
"other": {
"value": [
{
"type": 7
},
{
"type": 0,
"value": " rows logged"
}
]
}
},
"pluralType": "cardinal",
"type": 6,
"value": "count"
}
],
"simulator-data-logging-save-log": [
{
"type": 0,
"value": "Save log"
}
],
"simulator-data-logging-truncated": [
{
"type": 0,
"value": "Older rows not shown"
}
],
"simulator-expand": [
{
"type": 0,
Expand Down Expand Up @@ -1867,6 +1931,12 @@
"value": "Light level"
}
],
"simulator-log": [
{
"type": 0,
"value": "Data log"
}
],
"simulator-loud": [
{
"type": 0,
Expand Down
70 changes: 70 additions & 0 deletions src/messages/ui.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,70 @@
"value": " module"
}
],
"simulator-data-logging-empty": [
{
"type": 0,
"value": "No log entries."
}
],
"simulator-data-logging-full": [
{
"type": 0,
"value": "Log full"
}
],
"simulator-data-logging-rows": [
{
"offset": 0,
"options": {
"1": {
"value": [
{
"type": 7
},
{
"type": 0,
"value": " row"
}
]
},
"=0": {
"value": [
{
"type": 0,
"value": "No rows"
}
]
},
"other": {
"value": [
{
"type": 7
},
{
"type": 0,
"value": " rows"
}
]
}
},
"pluralType": "cardinal",
"type": 6,
"value": "count"
}
],
"simulator-data-logging-save-log": [
{
"type": 0,
"value": "Save log"
}
],
"simulator-data-logging-truncated": [
{
"type": 0,
"value": "Older rows not shown"
}
],
"simulator-expand": [
{
"type": 0,
Expand Down Expand Up @@ -1867,6 +1931,12 @@
"value": "Light level"
}
],
"simulator-log": [
{
"type": 0,
"value": "Data log"
}
],
"simulator-loud": [
{
"type": 0,
Expand Down
Loading