WIP
This commit is contained in:
+36
-4
@@ -1,3 +1,4 @@
|
||||
import { connType } from '@core/slices/appSlice';
|
||||
import {
|
||||
addChannel,
|
||||
addMessage,
|
||||
@@ -16,6 +17,7 @@ import {
|
||||
IHTTPConnection,
|
||||
ISerialConnection,
|
||||
Protobuf,
|
||||
SettingsManager,
|
||||
Types,
|
||||
} from '@meshtastic/meshtasticjs';
|
||||
|
||||
@@ -34,14 +36,42 @@ export const connectionUrl = state.hostOverrideEnabled
|
||||
export const ble = new IBLEConnection();
|
||||
export const serial = new ISerialConnection();
|
||||
|
||||
export const setConnection = (conn: connectionType): void => {
|
||||
export const setConnection = async (conn: connType): Promise<void> => {
|
||||
await connection.disconnect();
|
||||
cleanupListeners();
|
||||
connection = conn;
|
||||
|
||||
switch (conn) {
|
||||
case connType.HTTP:
|
||||
connection = new IHTTPConnection();
|
||||
break;
|
||||
case connType.BLE:
|
||||
connection = new IBLEConnection();
|
||||
break;
|
||||
case connType.SERIAL:
|
||||
connection = new ISerialConnection();
|
||||
break;
|
||||
}
|
||||
registerListeners();
|
||||
const connectionParams = store.getState().app.connectionParams;
|
||||
switch (conn) {
|
||||
case connType.HTTP:
|
||||
await connection.connect(connectionParams.HTTP);
|
||||
break;
|
||||
case connType.BLE:
|
||||
await connection.connect(
|
||||
// @ts-ignore tmp
|
||||
connectionParams.BLE,
|
||||
);
|
||||
break;
|
||||
case connType.SERIAL:
|
||||
await connection.connect(
|
||||
// @ts-ignore tmp
|
||||
connectionParams.SERIAL,
|
||||
);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
const cleanupListeners = (): void => {
|
||||
export const cleanupListeners = (): void => {
|
||||
connection.onDeviceStatus.cancelAll();
|
||||
connection.onMyNodeInfo.cancelAll();
|
||||
connection.onUserPacket.cancelAll();
|
||||
@@ -53,6 +83,8 @@ const cleanupListeners = (): void => {
|
||||
};
|
||||
|
||||
const registerListeners = (): void => {
|
||||
SettingsManager.debugMode = Protobuf.LogRecord_Level.TRACE;
|
||||
|
||||
connection.onDeviceStatus.subscribe((status) => {
|
||||
store.dispatch(setDeviceStatus(status));
|
||||
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
import type { Types } from '@meshtastic/meshtasticjs';
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
|
||||
export type currentPageName = 'messages' | 'settings';
|
||||
|
||||
export enum connType {
|
||||
HTTP,
|
||||
BLE,
|
||||
SERIAL,
|
||||
}
|
||||
|
||||
interface AppState {
|
||||
mobileNavOpen: boolean;
|
||||
connectionModalOpen: boolean;
|
||||
darkMode: boolean;
|
||||
currentPage: currentPageName;
|
||||
connType: connType;
|
||||
connectionParams: {
|
||||
BLE: Types.BLEConnectionParameters;
|
||||
HTTP: Types.HTTPConnectionParameters;
|
||||
SERIAL: Types.SerialConnectionParameters;
|
||||
};
|
||||
}
|
||||
|
||||
const initialState: AppState = {
|
||||
@@ -15,6 +28,17 @@ const initialState: AppState = {
|
||||
connectionModalOpen: true,
|
||||
darkMode: localStorage.getItem('darkMode') === 'true' ?? false,
|
||||
currentPage: 'messages',
|
||||
connType: connType.HTTP,
|
||||
connectionParams: {
|
||||
BLE: {},
|
||||
HTTP: {
|
||||
address: 'http://meshtastic.local/',
|
||||
tls: false,
|
||||
receiveBatchRequests: false,
|
||||
fetchInterval: 2000,
|
||||
},
|
||||
SERIAL: {},
|
||||
},
|
||||
};
|
||||
|
||||
export const appSlice = createSlice({
|
||||
@@ -40,6 +64,28 @@ export const appSlice = createSlice({
|
||||
setCurrentPage(state, action: PayloadAction<currentPageName>) {
|
||||
state.currentPage = action.payload;
|
||||
},
|
||||
setConnType(state, action: PayloadAction<connType>) {
|
||||
state.connType = action.payload;
|
||||
},
|
||||
setConnectionParams(
|
||||
state,
|
||||
action: PayloadAction<{
|
||||
type: connType;
|
||||
params: Types.ConnectionParameters;
|
||||
}>,
|
||||
) {
|
||||
switch (action.payload.type) {
|
||||
case connType.BLE:
|
||||
state.connectionParams.BLE = action.payload.params;
|
||||
break;
|
||||
case connType.HTTP:
|
||||
state.connectionParams.HTTP = action.payload.params;
|
||||
break;
|
||||
case connType.SERIAL:
|
||||
state.connectionParams.SERIAL = action.payload.params;
|
||||
break;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -50,6 +96,8 @@ export const {
|
||||
closeConnectionModal,
|
||||
setDarkModeEnabled,
|
||||
setCurrentPage,
|
||||
setConnType,
|
||||
setConnectionParams,
|
||||
} = appSlice.actions;
|
||||
|
||||
export default appSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user