Skip to content

Commit

Permalink
React pendant (#2518)
Browse files Browse the repository at this point in the history
Replaced old Angular pendant with one written in React.
  • Loading branch information
breiler authored May 12, 2024
1 parent 963da8a commit ba8754e
Show file tree
Hide file tree
Showing 160 changed files with 5,985 additions and 29,401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public void unsetGcodeFile() throws Exception {

initGcodeParser();
this.gcodeFile = null;
this.gcodeStream = null;
this.processedGcodeFile = null;
}

Expand All @@ -378,6 +379,9 @@ private void processGcodeFile() throws Exception {

eventDispatcher.sendUGSEvent(new FileStateEvent(FileState.FILE_LOADING));
initializeProcessedLines(true, this.gcodeFile, this.gcp);
if (this.processedGcodeFile != null) {
gcodeStream = new GcodeStreamReader(this.processedGcodeFile, getCommandCreator());
}
eventDispatcher.sendUGSEvent(new FileStateEvent(FileState.FILE_LOADED));
}

Expand Down
6 changes: 3 additions & 3 deletions ugs-pendant/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.0</version>
<configuration>
<workingDirectory>${project.basedir}/src/main/webapp</workingDirectory>
<workingDirectory>src/main/webapp</workingDirectory>
</configuration>
<executions>
<execution>
Expand All @@ -139,8 +139,8 @@
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v16.13.2</nodeVersion>
<npmVersion>8.1.2</npmVersion>
<nodeVersion>v20.12.2</nodeVersion>
<npmVersion>10.5.0</npmVersion>
</configuration>
</execution>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public List<PendantURLBean> start() {

contextHandlerCollection.addHandler(createResourceConfigHandler(new StaticConfig(), ""));
contextHandlerCollection.addHandler(createResourceConfigHandler(new AppV1Config(backendAPI, jogService), API_CONTEXT_PATH));
contextHandlerCollection.addHandler(createResourceConfigHandler(new StaticConfig(), "/*"));
contextHandlerCollection.addHandler(createWebSocketHandler(WEBSOCKET_CONTEXT_PATH));

try {
Expand Down Expand Up @@ -139,7 +140,7 @@ public List<PendantURLBean> getUrlList() {
String url = "http://" + hostAddress + ":" + port;
ByteArrayOutputStream bout = QRCode.from(url).to(ImageType.PNG).stream();
out.add(new PendantURLBean(url, bout.toByteArray()));
LOG.info("Listening on: " + url);
LOG.info(() -> "Listening on: " + url);
}
}
}
Expand Down Expand Up @@ -167,11 +168,9 @@ public BackendAPI getBackendAPI() {

@Override
public void UGSEvent(UGSEvent evt) {
if (evt instanceof SettingChangedEvent) {
if (backendAPI.getSettings().getPendantPort() != port && isStarted()) {
stop();
start();
}
if (evt instanceof SettingChangedEvent && (backendAPI.getSettings().getPendantPort() != port && isStarted())) {
stop();
start();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private static String getMimeType(String path) {
mimeType = "text/html";
} else if (path.endsWith(".css")) {
mimeType = "text/css";
} else if (path.endsWith(".ttf")) {
mimeType = "font/ttf";
}
return mimeType;
}
Expand All @@ -51,8 +53,14 @@ public Response getIndex() throws Exception {
}

@GET
@Path("{path:.*\\.(jpg|gif|html|js|css|ico)$}")
public Response getStaticResource(@PathParam("path") String path) throws Exception {
@Path("{path:(jog|macros|run)$}")
public Response getSubPage() throws Exception {
return getStaticResource("");
}

@GET
@Path("{path:.*\\.(jpg|gif|html|js|css|ico|ttf)$}")
public Response getStaticResource(@PathParam("path") String path) {
if (path.equalsIgnoreCase("")) {
path = "index.html";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.willwinder.universalgcodesender.pendantui.v1.model;

public record FileStatus(String fileName, long rowCount, long completedRowCount, long remainingRowCount,
long sendDuration, long sendRemainingDuration) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public class Settings implements Serializable {
private double jogStepSizeXY;
private UnitUtils.Units preferredUnits;
private double jogStepSizeZ;
private String port;
private String portRate;
private String firmwareVersion;
private boolean useZStepSize;

public void setJogFeedRate(double jogFeedRate) {
this.jogFeedRate = jogFeedRate;
Expand Down Expand Up @@ -42,4 +46,36 @@ public double getJogStepSizeZ() {
public void setJogStepSizeZ(double jogStepSizeZ) {
this.jogStepSizeZ = jogStepSizeZ;
}

public void setPort(String port) {
this.port = port;
}

public String getPort() {
return port;
}

public void setPortRate(String portRate) {
this.portRate = portRate;
}

public String getPortRate() {
return portRate;
}

public void setFirmwareVersion(String firmwareVersion) {
this.firmwareVersion = firmwareVersion;
}

public String getFirmwareVersion() {
return firmwareVersion;
}

public void setUseZStepSize(boolean useZStepSize) {
this.useZStepSize = useZStepSize;
}

public boolean isUseZStepSize() {
return useZStepSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,26 @@ This file is part of Universal Gcode Sender (UGS).
package com.willwinder.universalgcodesender.pendantui.v1.resources;

import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.pendantui.v1.model.FileStatus;
import com.willwinder.universalgcodesender.pendantui.v1.model.WorkspaceFileList;
import jakarta.inject.Inject;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataParam;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataParam;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Optional;

@Path("/files")
public class FilesResource {
Expand Down Expand Up @@ -98,4 +100,16 @@ public WorkspaceFileList getWorkspaceFileList() {
public void openWorkspaceFile(@QueryParam("file") String file) throws Exception {
backendAPI.openWorkspaceFile(file);
}

@GET
@Path("getFileStatus")
@Produces(MediaType.APPLICATION_JSON)
public FileStatus getFileStatus() {
return new FileStatus(Optional.ofNullable(backendAPI.getGcodeFile()).map(File::getAbsolutePath).orElse(""),
backendAPI.getNumRows(),
backendAPI.getNumCompletedRows(),
backendAPI.getNumRemainingRows(),
backendAPI.getSendDuration(),
backendAPI.getSendRemainingDuration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public Settings getSettings() {
response.setJogStepSizeXY(settings.getManualModeStepSize());
response.setJogStepSizeZ(settings.getZJogStepSize());
response.setPreferredUnits(settings.getPreferredUnits());
response.setPort(settings.getPort());
response.setPortRate(settings.getPortRate());
response.setFirmwareVersion(settings.getFirmwareVersion());
response.setUseZStepSize(settings.useZStepSize());
return response;
}

Expand All @@ -54,6 +58,9 @@ public void setSettings(Settings settings) throws Exception {
backendSettings.setManualModeStepSize(settings.getJogStepSizeXY());
backendSettings.setZJogStepSize(settings.getJogStepSizeZ());
backendSettings.setPreferredUnits(settings.getPreferredUnits());
backendAPI.applySettings(backendSettings);
backendSettings.setPort(settings.getPort());
backendSettings.setPortRate(settings.getPortRate());
backendSettings.setFirmwareVersion(settings.getFirmwareVersion());
backendSettings.setUseZStepSize(settings.isUseZStepSize());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ This file is part of Universal Gcode Sender (UGS).

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.willwinder.universalgcodesender.listeners.ControllerStatus;
import com.willwinder.universalgcodesender.listeners.ControllerStatusBuilder;
import com.willwinder.universalgcodesender.listeners.UGSEventListener;
import com.willwinder.universalgcodesender.model.UGSEvent;
import com.willwinder.universalgcodesender.model.UnitUtils;
import com.willwinder.universalgcodesender.model.events.ControllerStatusEvent;
import com.willwinder.universalgcodesender.pendantui.BackendProvider;
import com.willwinder.universalgcodesender.pendantui.v1.model.Event;
import com.willwinder.universalgcodesender.utils.Settings;
import jakarta.websocket.ClientEndpoint;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
Expand Down Expand Up @@ -53,6 +58,13 @@ public EventsSocket() {
}
}

private static ControllerStatus convertToPreferredUnits(ControllerStatus controllerStatusEvent, UnitUtils.Units units) {
return ControllerStatusBuilder.newInstance(controllerStatusEvent)
.setMachineCoord(controllerStatusEvent.getMachineCoord().getPositionIn(units))
.setWorkCoord(controllerStatusEvent.getWorkCoord().getPositionIn(units))
.build();
}

@OnOpen
public void onWebSocketConnect(Session session) {
sessions.put(session.getId(), session);
Expand All @@ -68,13 +80,13 @@ public void onWebSocketClose(Session session) {
@OnError
public void onWebSocketError(Session session, Throwable cause) {
sessions.remove(session.getId());
LOGGER.log(Level.WARNING, "WebSocket Closed: " + session.getId(), cause);
LOGGER.log(Level.WARNING, cause, () -> "WebSocket Closed: " + session.getId());
}

@Override
public void UGSEvent(UGSEvent evt) {
try {
String data = gson.toJson(new Event(evt)).replaceAll(":NaN", ":null");
String data = getEventAsJsonString(evt);
sessions.values().forEach(session -> {
try {
session.getBasicRemote().sendText(data);
Expand All @@ -86,4 +98,15 @@ public void UGSEvent(UGSEvent evt) {
e.printStackTrace();
}
}

private String getEventAsJsonString(UGSEvent evt) {
if (evt instanceof ControllerStatusEvent controllerStatusEvent) {
Settings settings = BackendProvider.getBackendAPI().getSettings();
ControllerStatus currentStatus = convertToPreferredUnits(controllerStatusEvent.getStatus(), settings.getPreferredUnits());
ControllerStatus previousStatus = convertToPreferredUnits(controllerStatusEvent.getPreviousStatus(), settings.getPreferredUnits());
return gson.toJson(new Event(new ControllerStatusEvent(currentStatus, previousStatus))).replace(":NaN", ":null");
}

return gson.toJson(new Event(evt));
}
}
11 changes: 0 additions & 11 deletions ugs-pendant/src/main/webapp/.browserslistrc

This file was deleted.

13 changes: 0 additions & 13 deletions ugs-pendant/src/main/webapp/.editorconfig

This file was deleted.

18 changes: 18 additions & 0 deletions ugs-pendant/src/main/webapp/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
59 changes: 20 additions & 39 deletions ugs-pendant/src/main/webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# compiled output
/dist
/tmp
/out-tsc
node_modules
dist
dist-ssr
*.local

# dependencies
/node_modules

# profiling files
chrome-profiler-events.json
speed-measure-plugin.json

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
# Editor directories and files
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings

# System Files
.idea
.DS_Store
Thumbs.db
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
node/*
Loading

0 comments on commit ba8754e

Please sign in to comment.