Fix serial port persistence

This commit is contained in:
GHOSCHT 2021-10-28 18:55:56 +02:00
parent 6090269cd9
commit 43725faea4
4 changed files with 27 additions and 8 deletions

View file

@ -66,6 +66,7 @@
"electron-squirrel-startup": "1.0.0", "electron-squirrel-startup": "1.0.0",
"electron-store": "^8.0.1", "electron-store": "^8.0.1",
"fs-extra": "10.0.0", "fs-extra": "10.0.0",
"lodash": "^4.17.21",
"react": "17.0.2", "react": "17.0.2",
"react-dom": "17.0.2", "react-dom": "17.0.2",
"react-redux": "7.2.5", "react-redux": "7.2.5",

View file

@ -6,6 +6,7 @@ import SerialPort from "serialport";
import { connect, disconnect } from "./redux/actions/asyncSerialConnectionActions"; import { connect, disconnect } from "./redux/actions/asyncSerialConnectionActions";
import { setSerialPort } from "./redux/actions/serialConnectionActions"; import { setSerialPort } from "./redux/actions/serialConnectionActions";
import KnobSection from "./Components/KnobSection"; import KnobSection from "./Components/KnobSection";
import { electronStore } from "./redux/store/index";
const GlobalStyle = createGlobalStyle` const GlobalStyle = createGlobalStyle`
html { html {
@ -58,6 +59,14 @@ const App = () => {
> >
disconnect disconnect
</button> </button>
<button
type="button"
onClick={() => {
electronStore.clear();
}}
>
reset storage
</button>
<Select <Select
value={portSelectionValue} value={portSelectionValue}
onChange={handlePortChange} onChange={handlePortChange}

View file

@ -21,29 +21,29 @@ const KnobSection = () => {
}; };
useEffect(() => { useEffect(() => {
if (serialConnection.portController !== null) { if (serialConnection.portController !== null && serialConnection.portController !== undefined) {
serialConnection.portController.parser.on("data", SerialDataListener); serialConnection.portController.parser.on("data", SerialDataListener);
} }
return () => { return () => {
if (serialConnection.portController !== null) { if (serialConnection.portController !== null && serialConnection.portController !== undefined) {
serialConnection.portController.parser.removeListener("data", SerialDataListener); serialConnection.portController.parser.removeListener("data", SerialDataListener);
} }
}; };
}, [serialConnection]); }, [serialConnection]);
const sendIncreaseHandler = (index: number) => { const sendIncreaseHandler = (index: number) => {
if (serialConnection.portController !== null && serialConnection.portController.port !== null) { if (serialConnection.portController !== null && serialConnection.portController.port !== null && serialConnection.portController !== undefined) {
serialConnection.portController.port.write(`${index}i`); serialConnection.portController.port.write(`${index}i`);
} }
}; };
const sendDecreaseHandler = (index: number) => { const sendDecreaseHandler = (index: number) => {
if (serialConnection.portController !== null && serialConnection.portController.port !== null) { if (serialConnection.portController !== null && serialConnection.portController.port !== null && serialConnection.portController !== undefined) {
serialConnection.portController.port.write(`${index}d`); serialConnection.portController.port.write(`${index}d`);
} }
}; };
const sendToggleHandler = (index: number) => { const sendToggleHandler = (index: number) => {
if (serialConnection.portController !== null && serialConnection.portController.port !== null) { if (serialConnection.portController !== null && serialConnection.portController.port !== null && serialConnection.portController !== undefined) {
serialConnection.portController.port.write(`${index}t`); serialConnection.portController.port.write(`${index}t`);
} }
}; };

View file

@ -1,10 +1,11 @@
import thunk from "redux-thunk"; import thunk from "redux-thunk";
import { createStore, applyMiddleware } from "redux"; import { createStore, applyMiddleware } from "redux";
import { RootAction, RootState } from "typesafe-actions"; import { RootAction, RootState } from "typesafe-actions";
import { persistStore, persistReducer } from "redux-persist"; import { persistStore, persistReducer, createTransform } from "redux-persist";
import createElectronStorage from "redux-persist-electron-storage"; import createElectronStorage from "redux-persist-electron-storage";
import ElectronStore from "electron-store"; import ElectronStore from "electron-store";
import omit from "lodash/omit";
import composeEnhancers from "./utils"; import composeEnhancers from "./utils";
import rootReducer from "./root-reducer"; import rootReducer from "./root-reducer";
@ -12,18 +13,26 @@ const middlewares = [thunk];
const enhancer = composeEnhancers(applyMiddleware(...middlewares)); const enhancer = composeEnhancers(applyMiddleware(...middlewares));
const electronStore = new ElectronStore(); export const electronStore = new ElectronStore();
createElectronStorage({ createElectronStorage({
electronStore, electronStore,
}); });
const blacklistPaths = ["serialConnection.portController", "serialConnection.status"];
const persistConfig = { const persistConfig = {
key: "root", key: "root",
whitelist: ["port"],
storage: createElectronStorage({ storage: createElectronStorage({
electronStore, electronStore,
}), }),
blacklist: blacklistPaths.filter((a) => !a.includes(".")),
transforms: [
// nested blacklist-paths require a custom transform to be applied
createTransform((inboundState: object, key) => {
const blacklistPathsForKey = blacklistPaths.filter((path) => path.startsWith(`${String(key)}.`)).map((path) => path.substr(String(key).length + 1));
return omit(inboundState, ...blacklistPathsForKey);
}, null),
],
}; };
export const persistedReducer = persistReducer(persistConfig, rootReducer); export const persistedReducer = persistReducer(persistConfig, rootReducer);