Cleanup connection logic

This commit is contained in:
Sacha Weatherstone
2022-03-07 20:10:54 +11:00
parent 4513a5b5f3
commit 1202345d69
5 changed files with 70 additions and 87 deletions
+5 -6
View File
@@ -30,12 +30,9 @@ type connectionType = IBLEConnection | IHTTPConnection | ISerialConnection;
export let connection: connectionType = new IHTTPConnection();
const state = store.getState().meshtastic;
export const connectionUrl = state.hostOverrideEnabled
? state.hostOverride
: import.meta.env.PROD
? window.location.hostname
: (import.meta.env.VITE_PUBLIC_DEVICE_IP as string) ?? 'meshtastic.local';
const appState = store.getState().app;
export const connectionUrl = appState.connectionParams.HTTP.address;
export const setConnection = async (conn: connType): Promise<void> => {
await connection.disconnect();
@@ -53,6 +50,8 @@ export const setConnection = async (conn: connType): Promise<void> => {
}
registerListeners();
const connectionParams = store.getState().app.connectionParams;
console.log(connectionParams);
switch (conn) {
case connType.HTTP:
await connection.connect(connectionParams.HTTP);
+5 -1
View File
@@ -35,7 +35,11 @@ const initialState: AppState = {
connectionParams: {
BLE: {},
HTTP: {
address: 'http://meshtastic.local/',
address:
localStorage.getItem('connectionUrl') ?? import.meta.env.PROD
? window.location.hostname
: (import.meta.env.VITE_PUBLIC_DEVICE_IP as string) ??
'meshtastic.local',
tls: false,
receiveBatchRequests: false,
fetchInterval: 2000,
-23
View File
@@ -55,8 +55,6 @@ interface MeshtasticState {
ready: boolean;
nodes: Node[];
radio: Radio;
hostOverrideEnabled: boolean;
hostOverride: string;
chats: ChatEntries;
logs: Types.LogEventPacket[];
}
@@ -71,11 +69,6 @@ const initialState: MeshtasticState = {
preferences: Protobuf.RadioConfig_UserPreferences.create(),
hardware: Protobuf.MyNodeInfo.create(),
},
//todo implement
// connectionMethod: localStorage.getItem('connectionMethod'),
hostOverrideEnabled:
localStorage.getItem('hostOverrideEnabled') === 'true' ?? false,
hostOverride: localStorage.getItem('hostOverride') ?? '',
chats: {},
logs: [],
};
@@ -257,20 +250,6 @@ export const meshtasticSlice = createSlice({
node.lastHeard = action.payload.time;
}
},
setHostOverrideEnabled: (state, action: PayloadAction<boolean>) => {
state.hostOverrideEnabled = action.payload;
localStorage.setItem('hostOverrideEnabled', String(action.payload));
if (state.hostOverrideEnabled !== action.payload) {
// connection.disconnect();
}
},
setHostOverride: (state, action: PayloadAction<string>) => {
state.hostOverride = action.payload;
localStorage.setItem('hostOverride', action.payload);
if (state.hostOverride !== action.payload) {
// connection.disconnect();
}
},
addChat: (state, action: PayloadAction<number>) => {
state.chats[action.payload] = {
messages: [],
@@ -303,8 +282,6 @@ export const {
addMessage,
ackMessage,
updateLastInteraction,
setHostOverrideEnabled,
setHostOverride,
addChat,
resetState,
} = meshtasticSlice.actions;