Skip to content

Commit

Permalink
[tmp] stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Hal-9k1 committed Feb 9, 2025
1 parent 6a84569 commit 3417470
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 36 deletions.
7 changes: 4 additions & 3 deletions src/common/AppConsoleMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { randomUUID } from 'crypto';

/**
* A type of AppConsoleMessage.
*/
Expand Down Expand Up @@ -31,6 +29,9 @@ export default class AppConsoleMessage {
constructor(type: MessageType, text: string) {
this.type = type;
this.text = text;
this.uuid = randomUUID();
this.uuid = '';
for (let i = 0; i < 8; i++) {

Check failure on line 33 in src/common/AppConsoleMessage.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Unary operator '++' used
this.uuid += `${Math.floor(Math.random() * 10000000)}-`;
}
}
}
56 changes: 44 additions & 12 deletions src/common/IpcEventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export type RendererChannels =
| 'renderer-post-console'
| 'renderer-file-control'
| 'renderer-quit-request';

/**
* IPC event channels used for communication from the renderer to the main process.
*/
export type MainChannels =
| 'main-file-control'
| 'main-quit'
| 'main-update-robot-mode'
| 'main-connection-config'
| 'main-robot-input';

/**
Expand Down Expand Up @@ -52,6 +54,7 @@ export interface RendererInitData {
*/
showDirtyUploadWarning: boolean;
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process wants to
* try saving the code and needs to ask the renderer process for the content of the editor.
Expand All @@ -67,6 +70,7 @@ export interface RendererFileControlPromptSaveData {
*/
forceDialog: boolean;
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process wants to
* try loading a file into the editor and needs to ask the renderer process if this is ok (if there
Expand All @@ -78,6 +82,7 @@ export interface RendererFileControlPromptLoadData {
*/
type: 'promptLoad';
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process has
* successfully saved the code and the renderer should clear the dirty editor indicator.
Expand All @@ -88,6 +93,7 @@ export interface RendererFileControlSaveData {
*/
type: 'didSave';
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process has
* successfully loaded code and the renderer should store the retrieved content in the editor.
Expand All @@ -107,6 +113,7 @@ export interface RendererFileControlOpenData {
*/
isCleanFile: boolean;
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process has
* successfully saved or loaded code and the renderer should update the path shown in the editor.
Expand All @@ -121,6 +128,7 @@ export interface RendererFileControlPathData {
*/
path: string;
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process detects
* external changes to the currently open file and the renderer should set the dirty editor
Expand All @@ -132,6 +140,7 @@ export interface RendererFileControlExtChangeData {
*/
type: 'didExternalChange';
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process wants to
* upload code from the last saved file to the robot and needs to ask the renderer process to notify
Expand All @@ -143,6 +152,7 @@ export interface RendererFileControlPromptUploadData {
*/
type: 'promptUpload';
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process wants to
* download code from the robot into the editor and needs to ask the renderer if this is ok (if
Expand All @@ -154,13 +164,15 @@ export interface RendererFileControlPromptDownloadData {
*/
type: 'promptDownload';
}

/**
* Data for a specialization of the renderer-file-control event, sent when the main process wants to
* close the file open in the editor.
*/
interface RendererFileControlPromptCreateNewFile {
type: 'promptCreateNewFile';
}

/**
* Data for the renderer-file-control event sent by the main process to request or submit
* information related to the code file and editor.
Expand All @@ -175,26 +187,31 @@ export type RendererFileControlData =
| RendererFileControlPromptUploadData
| RendererFileControlPromptDownloadData
| RendererFileControlPromptCreateNewFile;

/**
* Data for the renderer-post-console event sent by the main process to add a console message to the
* AppConsole.
*/
export type RendererPostConsoleData = AppConsoleMessage;

/**
* Data for the renderer-battery-update event sent by the main process to update the robot's battery
* voltage.
*/
export type RendererBatteryUpdateData = number;

/**
* Data for the renderer-latency-update event sent by the main process to update the displayed robot
* connection latency.
*/
export type RendererLatencyUpdateData = number;

/**
* Data for the renderer-devices-update event sent by the main process to update the state of
* connected lowcar devices.
*/
export type RendererDevicesUpdateData = DeviceInfoState[];

/**
* Data for a specialization of the main-file-control event, sent by the renderer to
* initiate/respond to a request to save the code.
Expand All @@ -214,6 +231,7 @@ export interface MainFileControlSaveData {
*/
content: string;
}

/**
* Data for a specialization of the main-file-control event, sent by the renderer to
* initiate/authorize a request to load code into the editor.
Expand All @@ -224,6 +242,7 @@ export interface MainFileControlLoadData {
*/
type: 'load';
}

/**
* Data for a specialization of the main-file-control event, sent by the renderer to
* initiate/respond to a request to upload the last opened file to the robot.
Expand All @@ -238,6 +257,7 @@ export interface MainFileControlUploadData {
*/
robotSSHAddress: string;
}

/**
* Data for a specialization of the main-file-control event, sent by the renderer to
* initiate/respond to a request to download the code on the robot into the editor.
Expand All @@ -252,6 +272,7 @@ export interface MainFileControlDownloadData {
*/
robotSSHAddress: string;
}

/**
* Data for a specialization of the main-file-control event, sent by the renderer to
* inititate/respond to a request to clear the save path (so the next request to save must prompt
Expand All @@ -263,6 +284,7 @@ export interface MainFileControlClearSavePathData {
*/
type: 'clearSavePath';
}

/**
* Data for the main-file-control event sent by the renderer process to submit information related
* to the code file and editor.
Expand All @@ -273,34 +295,44 @@ export type MainFileControlData =
| MainFileControlUploadData
| MainFileControlDownloadData
| MainFileControlClearSavePathData;

/**
* Data for the main-quit event sent by the renderer both to authorize a request to quit and to send
* updated configuration data that should be saved before the program closes.
*/
export interface MainQuitData {
/**
* The IP address used to communicate with the robot's runtime, to be saved to persistent config.
*/
robotIPAddress: string;
/**
* The IP address of the field controller, to be saved to persistent config.
*/
fieldIPAddress: string;
/**
* The field station number to connect to, to be saved to persistent config.
*/
fieldStationNumber: string;
/**
* Whether the user should be warned when uploading code with unsaved changes in the editor (since
* these won't be uploaded), to be saved to persistent config.
*/
showDirtyUploadWarning: boolean;
}

/**
* Data for the main-update-robot-mode event sent by the renderer to stop the robot or start it with
* a specified opmode.
*/
export type MainUpdateRobotModeData = RobotRunMode;

/**
* Data for the main-connection-config event sent by the renderer to notify the main process of a
* change in the Runtime or field connection config.
*/
export interface MainConnectionConfigData {
/**
* The IP address used to communicate with the robot's runtime.
*/
robotIPAddress: string;
/**
* The IP address of the field controller.
*/
fieldIPAddress: string;
/**
* The field station number to connect to.
*/
fieldStationNumber: string;
}

/**
* Data for the main-robot-input event sent by the renderer with gamepad or keyboard inputs bound
* for the robot.
Expand Down
59 changes: 45 additions & 14 deletions src/main/MainApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ function addRendererListener(
func: (data: MainUpdateRobotModeData) => void,
): void;

/**
* Adds a listener for the main-connection-config IPC event fired by the renderer.
* @param channel - the event channel to listen to
* @param func - the listener to attach
*/
function addRendererListener(
channel: 'main-connection-config',
func: (data: MainConnectionConfigData) => void,
): void;

/**
* Adds a listener for the main-robot-input IPC event fired by the renderer.
* @param channel - the event channel to listen to
Expand Down Expand Up @@ -146,6 +156,12 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
*/
#suppressNetworkErrors: boolean;

/**
* Whether error messages relating to the PDB will be suppressed. Used so faulty PDBs do not flood
* the console.
*/
#suppressPdbErrors: boolean;

/**
* Persistent configuration loaded when MainApp is constructed and saved when the main window is
* closed.
Expand All @@ -172,6 +188,7 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
this.#watchDebounce = true;
this.#preventQuit = true;
this.#suppressNetworkErrors = false;
this.#suppressPdbErrors = false;
this.#codeTransfer = new CodeTransfer(
REMOTE_CODE_PATH,
ROBOT_SSH_PORT,
Expand Down Expand Up @@ -219,6 +236,9 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
'main-update-robot-mode',
this.#runtimeComms.sendRunMode.bind(this.#runtimeComms),
);
addRendererListener('main-connection-config', (data) => {
this.#runtimeComms.setRobotIp(data.robotIPAddress);
});
addRendererListener(
'main-robot-input',
this.#runtimeComms.sendInputs.bind(this.#runtimeComms),
Expand Down Expand Up @@ -277,23 +297,30 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {
(state) => state.id.split('_')[0] === DeviceType.PDB.toString(),
);
if (pdbs.length !== 1) {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
'dawn-err',
'Cannot read battery voltage. Not exactly one PDB is connected to the robot.',
),
);
if (!this.#suppressPdbErrors) {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
'dawn-err',
'Cannot read battery voltage. Not exactly one PDB is connected to the robot.',
),
);
this.#suppressPdbErrors = true;
}
} else if (!('v_batt' in pdbs[0]) || Number.isNaN(Number(pdbs[0].v_batt))) {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
'dawn-err',
'PDB does not have v_batt property or it is not a number.',
),
);
if (!this.#suppressPdbErrors) {
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage(
'dawn-err',
'PDB does not have v_batt property or it is not a number.',
),
);
this.#suppressPdbErrors = true;
}
} else {
this.#sendToRenderer('renderer-battery-update', Number(pdbs[0].v_batt));
this.#suppressPdbErrors = false;
}
}

Expand Down Expand Up @@ -323,6 +350,10 @@ export default class MainApp implements MenuHandler, RuntimeCommsListener {

onRuntimeDisconnect() {
if (!this.#suppressNetworkErrors) {
this.#sendToRenderer(

Check failure on line 353 in src/main/MainApp.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

Replace `⏎········'renderer-latency-update',⏎········-1⏎······` with `'renderer-latency-update',·-1`
'renderer-latency-update',
-1
);
this.#sendToRenderer(
'renderer-post-console',
new AppConsoleMessage('dawn-info', 'Disconnected from robot.'),
Expand Down
4 changes: 4 additions & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function sendMessage(
channel: 'main-robot-input',
data: MainRobotInputData,
): void;
function sendMessage(
channel: 'main-connection-config',
data: MainRobotInputData,
): void;
function sendMessage(channel: MainChannels, data?: any): void {
ipcRenderer.send(channel, data);
}
Expand Down
Loading

0 comments on commit 3417470

Please sign in to comment.