From 2ef59454ba8ce07db3c735c00db59ef4cc547fe6 Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Tue, 2 Aug 2022 23:26:27 +0200 Subject: [PATCH 1/4] refactor: change remoteMode to instancesDB in config.json Signed-off-by: Stefan Dej --- .env.development.local.example | 4 +- public/config.json | 15 +++++- src/App.vue | 2 +- src/components/TheSelectPrinterDialog.vue | 53 +++++++++++-------- src/components/mixins/base.ts | 4 +- .../settings/SettingsRemotePrintersTab.vue | 8 ++- src/components/ui/PrinterSelector.vue | 5 +- src/locales/en.json | 4 +- src/main.ts | 2 +- src/store/actions.ts | 22 +++++--- src/store/gui/actions.ts | 3 +- src/store/gui/remoteprinters/actions.ts | 8 +-- src/store/index.ts | 11 +--- src/store/mutations.ts | 4 +- src/store/types.ts | 4 +- 15 files changed, 89 insertions(+), 60 deletions(-) diff --git a/.env.development.local.example b/.env.development.local.example index 75e02c500..58126c3a1 100644 --- a/.env.development.local.example +++ b/.env.development.local.example @@ -7,8 +7,8 @@ VUE_APP_PORT=7125 # reconnect interval in ms VUE_APP_RECONNECT_INTERVAL=5000 -# remote mode -VUE_APP_REMOTE_MODE=false +# where should mainsail read the instances (moonraker, browser or json) +VUE_APP_INSTANCES_DB="moonraker" # defaults for multi language tests VUE_APP_I18N_LOCALE=en diff --git a/public/config.json b/public/config.json index d2a269291..08f6e1ee1 100644 --- a/public/config.json +++ b/public/config.json @@ -1,8 +1,19 @@ { "hostname": null, "port": null, - "remoteMode": false, + "instancesDB": "moonraker", "instances": [ - + { + "hostname": "192.168.0.210", + "port": 7125 + }, + { + "hostname": "192.168.0.211", + "port": 7125 + }, + { + "hostname": "192.168.0.212", + "port": 7125 + } ] } diff --git a/src/App.vue b/src/App.vue index 4aa8e4cfe..45bd13d51 100644 --- a/src/App.vue +++ b/src/App.vue @@ -40,7 +40,7 @@ - + diff --git a/src/components/TheSelectPrinterDialog.vue b/src/components/TheSelectPrinterDialog.vue index 34c43913e..a7c19f6b2 100644 --- a/src/components/TheSelectPrinterDialog.vue +++ b/src/components/TheSelectPrinterDialog.vue @@ -173,31 +173,40 @@ - - -

- {{ $t('SelectPrinterDialog.Hello') }} -

+ + +

- {{ $t('SelectPrinterDialog.RememberToAdd', { cors: currentUrl }) }} -

-

- {{ $t('SelectPrinterDialog.YouCanFindMore') }} -
- - https://docs.mainsail.xyz/remotemode - - . + {{ $t('SelectPrinterDialog.PleaseAddYourPrintersToConfigJson') }}

- - - - {{ $t('SelectPrinterDialog.AddPrinter') }} - - - @@ -254,7 +263,7 @@ export default class TheSelectPrinterDialog extends Mixins(BaseMixin) { } get canAddPrinters() { - return this.$store.state.configInstances.length === 0 + return this.instancesDB !== 'json' } get protocol() { diff --git a/src/components/mixins/base.ts b/src/components/mixins/base.ts index c1e486863..3c9863431 100644 --- a/src/components/mixins/base.ts +++ b/src/components/mixins/base.ts @@ -11,8 +11,8 @@ export default class BaseMixin extends Vue { return this.$store.getters['socket/getHostUrl'] } - get remoteMode() { - return this.$store.state.remoteMode + get instancesDB() { + return this.$store.state.instancesDB ?? 'moonraker' } get socketIsConnected(): boolean { diff --git a/src/components/settings/SettingsRemotePrintersTab.vue b/src/components/settings/SettingsRemotePrintersTab.vue index 28c3e4e21..be8c806e6 100644 --- a/src/components/settings/SettingsRemotePrintersTab.vue +++ b/src/components/settings/SettingsRemotePrintersTab.vue @@ -3,6 +3,9 @@

{{ $t('Settings.RemotePrintersTab.RemotePrinters') }}

+ + {{ $t('Settings.RemotePrintersTab.UseConfigJson') }} +
1) || (!this.remoteMode && this.countPrinters) + return ( + (this.instancesDB !== 'moonraker' && this.countPrinters > 1) || + (this.instancesDB === 'moonraker' && this.countPrinters) + ) } get printers() { diff --git a/src/locales/en.json b/src/locales/en.json index 5e3993527..930d72010 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -654,6 +654,7 @@ "HostnameInvalid": "invalid Hostname/IP", "HostnameIp": "Hostname/IP", "HostnameRequired": "Hostname is required", + "PleaseAddYourPrintersToConfigJson": "Please add your Printers to your config.json.", "Port": "Port", "PortRequired": "Port is required", "RememberToAdd": "Please remember to add '{cors}' in moonraker.conf within 'cors_domains'.", @@ -834,7 +835,8 @@ "Hostname": "Hostname", "Port": "Port", "RemotePrinters": "Printers", - "UpdatePrinter": "Update Printer" + "UpdatePrinter": "Update Printer", + "UseConfigJson": "InstanceDB = JSON detected. Please use the config.json to modify the printers list." }, "TimelapseTab": { "Autorender": "Autorender", diff --git a/src/main.ts b/src/main.ts index 738efd42d..452fd1ccc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -89,7 +89,7 @@ const initLoad = async () => { const url = store.getters['socket/getWebsocketUrl'] Vue.use(WebSocketPlugin, { url, store }) - if (!store?.state?.remoteMode) Vue.$socket.connect() + if (store?.state?.instancesDB === 'moonraker') Vue.$socket.connect() } initLoad() diff --git a/src/store/actions.ts b/src/store/actions.ts index 087371ab4..966101e52 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -8,8 +8,6 @@ export const actions: ActionTree = { }, changePrinter({ dispatch, getters, state }, payload) { - const remoteMode = state.remoteMode - dispatch('files/reset') dispatch('gui/reset') dispatch('printer/reset') @@ -32,11 +30,21 @@ export const actions: ActionTree = { * This function will parse the config.json content and config mainsail */ importConfigJson({ commit, dispatch }, payload: ConfigJson) { - const remoteMode = payload.remoteMode ?? false - if (remoteMode) { - commit('setRemoteMode', true) - - if ('instances' in payload && Array.isArray(payload.instances) && payload.instances.length) { + type RootStateInstancesDbType = 'moonraker' | 'browser' | 'json' + let instancesDB: RootStateInstancesDbType = payload.instancesDB ?? 'moonraker' + if (document.location.hostname === 'my.mainsail.xyz') instancesDB = 'browser' + if (import.meta.env.VUE_APP_INSTANCES_DB) + instancesDB = import.meta.env.VUE_APP_INSTANCES_DB as RootStateInstancesDbType + + if (instancesDB !== 'moonraker') { + commit('setInstancesDB', instancesDB) + + if ( + instancesDB === 'json' && + 'instances' in payload && + Array.isArray(payload.instances) && + payload.instances.length + ) { commit('setConfigInstances', payload.instances) } diff --git a/src/store/gui/actions.ts b/src/store/gui/actions.ts index 3904606c4..aab4627f2 100644 --- a/src/store/gui/actions.ts +++ b/src/store/gui/actions.ts @@ -26,7 +26,8 @@ export const actions: ActionTree = { const mainsailUrl = baseUrl + '?namespace=mainsail' if ('remoteprinters' in payload.value) { - if (!rootState.remoteMode) dispatch('remoteprinters/initStore', payload.value.remoteprinters.printers) + if (rootState.instancesDB === 'moonraker') + dispatch('remoteprinters/initStore', payload.value.remoteprinters.printers) delete payload.value.remoteprinters } diff --git a/src/store/gui/remoteprinters/actions.ts b/src/store/gui/remoteprinters/actions.ts index 452b1ebd1..93d9abf5f 100644 --- a/src/store/gui/remoteprinters/actions.ts +++ b/src/store/gui/remoteprinters/actions.ts @@ -15,7 +15,7 @@ export const actions: ActionTree = { initFromLocalstorage({ dispatch, rootState }) { let value = rootState.configInstances ?? [] - if (value.length === 0) value = JSON.parse(localStorage.getItem('printers') ?? '{}') + if (rootState.instancesDB === 'moonraker') value = JSON.parse(localStorage.getItem('printers') ?? '{}') if (Array.isArray(value)) { const printers: any = {} @@ -47,7 +47,7 @@ export const actions: ActionTree = { }, upload({ state, rootState }, id) { - if (rootState.remoteMode) { + if (rootState.instancesDB === 'browser') { const printers: any[] = [] Object.keys(state.printers).forEach((id: string) => { @@ -59,7 +59,7 @@ export const actions: ActionTree = { }) localStorage.setItem('printers', JSON.stringify(printers)) - } else if (id in state.printers) { + } else if (rootState.instancesDB === 'moonraker' && id in state.printers) { const value = { hostname: state.printers[id].hostname, port: state.printers[id].port, @@ -112,7 +112,7 @@ export const actions: ActionTree = { commit('delete', id) dispatch('farm/unregisterPrinter', id, { root: true }) - if (rootState.remoteMode) dispatch('upload') + if (rootState.instancesDB === 'browser') dispatch('upload') else { Vue.$socket.emit('server.database.delete_item', { namespace: 'mainsail', diff --git a/src/store/index.ts b/src/store/index.ts index 8820032b4..5ff2eee83 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -18,20 +18,11 @@ import { gcodeviewer } from '@/store/gcodeviewer' Vue.use(Vuex) export const getDefaultState = (): RootState => { - let remoteMode = false - - if ( - document.location.hostname === 'my.mainsail.xyz' || - String(import.meta.env.VUE_APP_REMOTE_MODE).toLowerCase() === 'true' || - String(import.meta.env.VUE_APP_REMOTE_MODE) === '1' - ) - remoteMode = true - return { packageVersion: (import.meta.env.PACKAGE_VERSION as string) || '0.0.0', debugMode: (import.meta.env.VUE_APP_DEBUG_MODE as boolean) || false, naviDrawer: null, - remoteMode, + instancesDB: 'moonraker', configInstances: [], } } diff --git a/src/store/mutations.ts b/src/store/mutations.ts index 8827a6122..1186708d7 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -7,8 +7,8 @@ export const mutations: MutationTree = { Vue.set(state, 'naviDrawer', payload) }, - setRemoteMode(state, payload) { - Vue.set(state, 'remoteMode', payload) + setInstancesDB(state, payload) { + Vue.set(state, 'instancesDB', payload) }, setConfigInstances(state, payload) { diff --git a/src/store/types.ts b/src/store/types.ts index 8a8d7ba08..1c48ecfb7 100644 --- a/src/store/types.ts +++ b/src/store/types.ts @@ -8,7 +8,7 @@ export interface RootState { packageVersion: string debugMode: boolean naviDrawer: boolean | null - remoteMode: boolean + instancesDB: 'moonraker' | 'browser' | 'json' configInstances: ConfigJsonInstance[] socket?: SocketState @@ -27,7 +27,7 @@ export interface RootStateDependency { export interface ConfigJson { hostname?: string | null port?: string | number | null - remoteMode?: boolean + instancesDB?: 'moonraker' | 'browser' | 'json' instances?: ConfigJsonInstance[] } From 8b217b39dd3df3befff1332415fa91c66a2a4bfe Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Thu, 4 Aug 2022 08:54:05 +0200 Subject: [PATCH 2/4] refactor: cleanup config.json --- public/config.json | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/public/config.json b/public/config.json index 08f6e1ee1..e782b5c4b 100644 --- a/public/config.json +++ b/public/config.json @@ -2,18 +2,5 @@ "hostname": null, "port": null, "instancesDB": "moonraker", - "instances": [ - { - "hostname": "192.168.0.210", - "port": 7125 - }, - { - "hostname": "192.168.0.211", - "port": 7125 - }, - { - "hostname": "192.168.0.212", - "port": 7125 - } - ] + "instances": [] } From 01370a51999d0e2ae325e104561dc1835e1db7d6 Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Thu, 4 Aug 2022 08:58:16 +0200 Subject: [PATCH 3/4] refactor: rewrite locale Co-authored-by: th33xitus --- .env.development.local.example | 2 +- src/components/TheSelectPrinterDialog.vue | 2 +- src/locales/en.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.env.development.local.example b/.env.development.local.example index 58126c3a1..a818e9f67 100644 --- a/.env.development.local.example +++ b/.env.development.local.example @@ -7,7 +7,7 @@ VUE_APP_PORT=7125 # reconnect interval in ms VUE_APP_RECONNECT_INTERVAL=5000 -# where should mainsail read the instances (moonraker, browser or json) +# where should mainsail read the instances from (moonraker, browser or json) VUE_APP_INSTANCES_DB="moonraker" # defaults for multi language tests diff --git a/src/components/TheSelectPrinterDialog.vue b/src/components/TheSelectPrinterDialog.vue index a7c19f6b2..8e82c03c6 100644 --- a/src/components/TheSelectPrinterDialog.vue +++ b/src/components/TheSelectPrinterDialog.vue @@ -203,7 +203,7 @@

- {{ $t('SelectPrinterDialog.PleaseAddYourPrintersToConfigJson') }} + {{ $t('SelectPrinterDialog.AddPrintersToJson') }}

diff --git a/src/locales/en.json b/src/locales/en.json index 930d72010..3bfac7a9c 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -654,7 +654,7 @@ "HostnameInvalid": "invalid Hostname/IP", "HostnameIp": "Hostname/IP", "HostnameRequired": "Hostname is required", - "PleaseAddYourPrintersToConfigJson": "Please add your Printers to your config.json.", + "AddPrintersToJson": "Please add the Printers to the config.json.", "Port": "Port", "PortRequired": "Port is required", "RememberToAdd": "Please remember to add '{cors}' in moonraker.conf within 'cors_domains'.", From 8549e5f6b1a33ebd1a58cfed8d92686ce25be384 Mon Sep 17 00:00:00 2001 From: Stefan Dej Date: Sat, 6 Aug 2022 21:37:36 +0200 Subject: [PATCH 4/4] locale(de): add de locale Signed-off-by: Stefan Dej --- src/locales/de.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/locales/de.json b/src/locales/de.json index ff8b5004d..afa2e89e8 100644 --- a/src/locales/de.json +++ b/src/locales/de.json @@ -663,6 +663,7 @@ }, "SelectPrinterDialog": { "AddPrinter": "Drucker hinzufügen", + "AddPrintersToJson": "Bitte füge einen Drucker in der config.json hinzu.", "CannotConnectTo": "Kann keine Verbindung zu {host} herstellen.", "ChangePrinter": "Drucker ändern", "Connecting": "Verbinde zu {host}", @@ -852,7 +853,8 @@ "Hostname": "Hostname", "Port": "Port", "RemotePrinters": "Remote Drucker", - "UpdatePrinter": "Drucker aktualisieren" + "UpdatePrinter": "Drucker aktualisieren", + "UseConfigJson": "InstanceDB = JSON erkannt. Bitte bearbeite die config.json um die Druckerliste zu modifizieren." }, "TimelapseTab": { "Autorender": "Autorender",