Skip to content

Commit 9acf6ba

Browse files
authored
fix(Spoolman): add version check (#1634)
Signed-off-by: Pedro Lamas <[email protected]>
1 parent cb7b92b commit 9acf6ba

File tree

5 files changed

+108
-31
lines changed

5 files changed

+108
-31
lines changed

src/api/socketActions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,19 @@ export const SocketActions = {
854854
)
855855
},
856856

857+
async serverSpoolmanProxyGetInfo () {
858+
baseEmit(
859+
'server.spoolman.proxy', {
860+
params: {
861+
request_method: 'GET',
862+
path: '/v1/info',
863+
use_v2_response: true
864+
},
865+
dispatch: 'spoolman/onInfo'
866+
}
867+
)
868+
},
869+
857870
async serverSpoolmanProxyGetSettingCurrency () {
858871
baseEmit(
859872
'server.spoolman.proxy', {

src/store/spoolman/actions.ts

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { ActionTree } from 'vuex'
22
import type {
33
Spool,
4+
SpoolmanInfo,
45
SpoolmanProxyResponse,
6+
SpoolmanProxyResponseV2,
57
SpoolmanState,
68
WebsocketBasePayload,
79
WebsocketFilamentPayload,
@@ -12,9 +14,30 @@ import type { RootState } from '../types'
1214
import { SocketActions } from '@/api/socketActions'
1315
import { consola } from 'consola'
1416
import { EventBus } from '@/eventBus'
17+
import { gte, valid } from 'semver'
1518

1619
const logPrefix = '[SPOOLMAN]'
1720

21+
const payloadAsSpoolmanProxyResponseV2 = <T>(payload: SpoolmanProxyResponse<T>): SpoolmanProxyResponseV2<T> => {
22+
if (
23+
payload != null &&
24+
typeof payload === 'object' &&
25+
'error' in payload &&
26+
'response' in payload
27+
) {
28+
if (payload.error != null) {
29+
EventBus.$emit(typeof payload.error === 'string' ? payload.error : payload.error.message, { type: 'error' })
30+
}
31+
32+
return payload
33+
}
34+
35+
return {
36+
error: null,
37+
response: payload
38+
}
39+
}
40+
1841
export const actions: ActionTree<SpoolmanState, RootState> = {
1942
/**
2043
* Reset our store
@@ -29,7 +52,7 @@ export const actions: ActionTree<SpoolmanState, RootState> = {
2952
async init () {
3053
SocketActions.serverSpoolmanGetSpoolId()
3154
SocketActions.serverSpoolmanProxyGetAvailableSpools()
32-
SocketActions.serverSpoolmanProxyGetSettingCurrency()
55+
SocketActions.serverSpoolmanProxyGetInfo()
3356
},
3457

3558
async onActiveSpool ({ commit }, payload) {
@@ -111,39 +134,55 @@ export const actions: ActionTree<SpoolmanState, RootState> = {
111134
commit('setAvailableSpools', spools)
112135
},
113136

137+
async onStatusChanged ({ commit, dispatch }, payload: boolean) {
138+
if (payload) {
139+
// refresh data, connected state will be set on data retrieval
140+
dispatch('init')
141+
} else {
142+
commit('setConnected', payload)
143+
}
144+
},
145+
114146
async onAvailableSpools ({ commit, dispatch }, payload: SpoolmanProxyResponse<Spool[]>) {
115-
if ('error' in payload && 'response' in payload) {
116-
if (payload.error != null) {
117-
EventBus.$emit(typeof payload.error === 'string' ? payload.error : payload.error.message, { type: 'error' })
118-
return
119-
}
147+
payload = payloadAsSpoolmanProxyResponseV2(payload)
120148

121-
payload = payload.response
149+
if (payload.error != null) {
150+
return
122151
}
123152

124-
commit('setAvailableSpools', [...payload])
153+
commit('setAvailableSpools', [...payload.response])
154+
125155
commit('setConnected', true)
156+
126157
dispatch('initializeWebsocketConnection')
127158
},
128159

129-
async onStatusChanged ({ commit, dispatch }, payload) {
130-
if (payload) {
131-
// refresh data, connected state will be set on data retrieval
132-
dispatch('init')
133-
} else commit('setConnected', payload)
160+
async onInfo ({ state, commit }, payload: SpoolmanProxyResponse<SpoolmanInfo>) {
161+
payload = payloadAsSpoolmanProxyResponseV2(payload)
162+
163+
if (payload.error != null) {
164+
return
165+
}
166+
167+
commit('setInfo', payload.response)
168+
169+
if (
170+
state.info &&
171+
valid(state.info.version) &&
172+
gte(state.info.version, '0.16.0')
173+
) {
174+
SocketActions.serverSpoolmanProxyGetSettingCurrency()
175+
}
134176
},
135177

136178
async onSettingCurrency ({ commit }, payload: SpoolmanProxyResponse<{ value: string }>) {
137-
if ('error' in payload && 'response' in payload) {
138-
if (payload.error != null) {
139-
EventBus.$emit(typeof payload.error === 'string' ? payload.error : payload.error.message, { type: 'error' })
140-
return
141-
}
179+
payload = payloadAsSpoolmanProxyResponseV2(payload)
142180

143-
payload = payload.response
181+
if (payload.error != null) {
182+
return
144183
}
145184

146-
commit('setCurrency', payload)
185+
commit('setCurrency', payload.response)
147186
},
148187

149188
async initializeWebsocketConnection ({ state, rootState, dispatch }) {

src/store/spoolman/mutations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { MutationTree } from 'vuex'
22
import { defaultState } from './state'
33
import type {
44
Spool,
5+
SpoolmanInfo,
56
SpoolmanState,
67
SpoolSelectionDialogState
78
} from '@/store/spoolman/types'
@@ -32,6 +33,10 @@ export const mutations: MutationTree<SpoolmanState> = {
3233
state.dialog = payload
3334
},
3435

36+
setInfo (state, payload: SpoolmanInfo) {
37+
state.info = payload
38+
},
39+
3540
setCurrency (state, payload: { value: string }) {
3641
state.currency = payload.value.replace(/^"|"$/g, '')
3742
},

src/store/spoolman/state.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import type { SpoolmanState } from '@/store/spoolman/types'
22

33
export const defaultState = (): SpoolmanState => {
44
return {
5+
info: null,
56
availableSpools: [],
6-
activeSpool: undefined,
7-
currency: undefined,
7+
activeSpool: null,
8+
currency: null,
89
connected: false,
910
dialog: {
1011
show: false

src/store/spoolman/types.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ export interface Spool {
4444
}
4545

4646
export interface SpoolmanState {
47+
info: SpoolmanInfo | null;
4748
availableSpools: Spool[];
48-
activeSpool?: number;
49-
currency?: string;
49+
activeSpool: number | null;
50+
currency: string | null;
5051
connected: boolean;
5152
dialog: SpoolSelectionDialogState;
5253
socket?: WebSocket;
@@ -87,12 +88,30 @@ export interface MacroWithSpoolId extends Macro {
8788
}
8889
}
8990

90-
export type SpoolmanProxyResponse<T> = T | {
91-
response: T,
92-
error: null
93-
} | {
94-
response: null,
91+
export interface SpoolmanInfo {
92+
version: string;
93+
debug_mode: boolean;
94+
automatic_backups: boolean;
95+
data_dir: string;
96+
logs_dir: string;
97+
backups_dir: string;
98+
db_type: string;
99+
git_commit: string;
100+
build_date: Date;
101+
}
102+
103+
export interface SpoolmanProxyResponseV2Success<T> {
104+
response: T;
105+
error: null;
106+
}
107+
108+
export interface SpoolmanProxyResponseV2Error {
109+
response: null;
95110
error: string | {
96-
message: string
97-
}
111+
message: string;
112+
};
98113
}
114+
115+
export type SpoolmanProxyResponseV2<T> = SpoolmanProxyResponseV2Success<T> | SpoolmanProxyResponseV2Error
116+
117+
export type SpoolmanProxyResponse<T> = T | SpoolmanProxyResponseV2<T>

0 commit comments

Comments
 (0)