Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add default moonraker instances to config.json #695

Merged
merged 5 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ export default class App extends Mixins(BaseMixin) {
return this.$store.getters['getTitle']
}

get remoteMode(): boolean {
return this.$store.state.socket.remoteMode ?? false
}

get mainBackground(): string {
return this.$store.getters['files/getMainBackground']
}
Expand Down
8 changes: 6 additions & 2 deletions src/components/TheSelectPrinterDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
</v-icon>
</v-col>
<v-col>{{ getPrinterName(printer.id) }}</v-col>
<v-col class="col-auto pa-0">
<v-col v-if="canAddPrinters" class="col-auto pa-0">
<v-btn
tile
text
Expand Down Expand Up @@ -190,7 +190,7 @@
</p>
</v-col>
</v-row>
<v-row>
<v-row v-if="canAddPrinters">
<v-col class="text-center mt-0">
<v-btn text color="primary" @click="dialogAddPrinter.bool = true">
{{ $t('SelectPrinterDialog.AddPrinter') }}
Expand Down Expand Up @@ -252,6 +252,10 @@ export default class TheSelectPrinterDialog extends Mixins(BaseMixin) {
return this.$store.getters['gui/remoteprinters/getRemoteprinters'] ?? []
}

get canAddPrinters() {
return this.$store.state.configInstances.length === 0
}

get protocol() {
return this.$store.state.socket.protocol
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/mixins/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class BaseMixin extends Vue {
}

get remoteMode() {
return this.$store.state.socket.remoteMode
return this.$store.state.remoteMode
}

get socketIsConnected(): boolean {
Expand Down
9 changes: 7 additions & 2 deletions src/components/settings/SettingsRemotePrintersTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:title="formatPrinterName(printer)"
:loading="printer.socket.isConnecting"
:icon="printer.socket.isConnected ? mdiCheckboxMarkedCircle : mdiCancel">
<v-btn small outlined @click="editPrinter(printer)">
<v-btn small outlined :disabled="!canAddPrinters" @click="editPrinter(printer)">
<v-icon left small>{{ mdiPencil }}</v-icon>
{{ $t('Settings.Edit') }}
</v-btn>
Expand All @@ -18,14 +18,15 @@
outlined
class="ml-3 minwidth-0 px-2"
color="error"
:disabled="!canAddPrinters"
@click="delPrinter(printer.id)">
<v-icon small>{{ mdiDelete }}</v-icon>
</v-btn>
</settings-row>
</div>
</v-card-text>
<v-card-actions class="d-flex justify-end">
<v-btn text color="primary" @click="createPrinter">
<v-btn text color="primary" :disabled="!canAddPrinters" @click="createPrinter">
{{ $t('Settings.RemotePrintersTab.AddPrinter') }}
</v-btn>
</v-card-actions>
Expand Down Expand Up @@ -112,6 +113,10 @@ export default class SettingsRemotePrintersTab extends Mixins(BaseMixin) {
return this.$store.getters['gui/remoteprinters/getRemoteprinters'] ?? []
}

get canAddPrinters() {
return this.$store.state.configInstances.length === 0
}

get protocol() {
return this.$store.state.socket.protocol ?? 'ws'
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ Vue.component('EChart', ECharts)
//load config.json and init vue
fetch('/config.json')
.then((res) => res.json())
.then((file) => {
store.commit('socket/setData', file)
.then(async (file) => {
await store.dispatch('importConfigJson', file)

const url = store.getters['socket/getWebsocketUrl']
Vue.use(WebSocketPlugin, {
url: url,
store: store,
})

if (!store?.state?.socket?.remoteMode) Vue.$socket.connect()
if (!store?.state?.remoteMode) Vue.$socket.connect()

new Vue({
vuetify,
Expand Down
23 changes: 20 additions & 3 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import router from '@/plugins/router'
import { ActionTree } from 'vuex'
import { RootState } from './types'
import { ConfigJson, RootState } from './types'
import { v4 as uuidv4 } from 'uuid'

export const actions: ActionTree<RootState, RootState> = {
switchToDashboard() {
router.push('/')
},

changePrinter({ dispatch, getters, state }, payload) {
const remoteMode = state.socket?.remoteMode
const remoteMode = state.remoteMode

dispatch('files/reset')
dispatch('gui/reset')
Expand All @@ -21,11 +22,27 @@ export const actions: ActionTree<RootState, RootState> = {
dispatch('socket/setSocket', {
hostname: printerSocket.hostname,
port: printerSocket.port,
remoteMode: remoteMode,
})
},

setNaviDrawer({ commit }, payload) {
commit('setNaviDrawer', payload)
},

/**
* This function will parse the config.json content and config mainsail
* @param commit - vuex commit
* @param dispatch - vuex dispatch
* @param payload - content of config.json as a object
*/
importConfigJson({ commit, dispatch }, payload: ConfigJson) {
const remoteMode = 'remoteMode' in payload ? payload.remoteMode : false
if (remoteMode) {
commit('setRemoteMode', true)

if ('instances' in payload && Array.isArray(payload.instances) && payload.instances.length) {
commit('setConfigInstances', payload.instances)
}
}
},
}
3 changes: 1 addition & 2 deletions src/store/gui/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const actions: ActionTree<GuiState, RootState> = {
const mainsailUrl = baseUrl + '?namespace=mainsail'

if ('remoteprinters' in payload.value) {
if (!rootState.socket?.remoteMode)
dispatch('remoteprinters/initStore', payload.value.remoteprinters.printers)
if (!rootState.remoteMode) dispatch('remoteprinters/initStore', payload.value.remoteprinters.printers)
delete payload.value.remoteprinters
}

Expand Down
9 changes: 5 additions & 4 deletions src/store/gui/remoteprinters/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export const actions: ActionTree<GuiRemoteprintersState, RootState> = {
commit('reset')
},

initFromLocalstorage({ dispatch }) {
const value = JSON.parse(localStorage.getItem('printers') ?? '{}')
initFromLocalstorage({ dispatch, rootState }) {
let value = rootState.configInstances ?? []
if (value.length === 0) value = JSON.parse(localStorage.getItem('printers') ?? '{}')
if (Array.isArray(value)) {
const printers: any = {}

Expand Down Expand Up @@ -46,7 +47,7 @@ export const actions: ActionTree<GuiRemoteprintersState, RootState> = {
},

upload({ state, rootState }, id) {
if (rootState.socket?.remoteMode) {
if (rootState.remoteMode) {
const printers: any[] = []

Object.keys(state.printers).forEach((id: string) => {
Expand Down Expand Up @@ -111,7 +112,7 @@ export const actions: ActionTree<GuiRemoteprintersState, RootState> = {
commit('delete', id)
dispatch('farm/unregisterPrinter', id, { root: true })

if (rootState.socket?.remoteMode) dispatch('upload')
if (rootState.remoteMode) dispatch('upload')
else {
Vue.$socket.emit('server.database.delete_item', {
namespace: 'mainsail',
Expand Down
13 changes: 12 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Vuex from 'vuex'
import { actions } from '@/store/actions'
import { mutations } from '@/store/mutations'
import { getters } from '@/store/getters'
import { RootState } from './types'
import { ConfigJsonInstance, RootState } from './types'

// load modules
import { socket } from '@/store/socket'
Expand All @@ -18,10 +18,21 @@ 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,
configInstances: [],
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ export const mutations: MutationTree<RootState> = {
setNaviDrawer(state, payload) {
Vue.set(state, 'naviDrawer', payload)
},

setRemoteMode(state, payload) {
Vue.set(state, 'remoteMode', payload)
},

setConfigInstances(state, payload) {
Vue.set(state, 'configInstances', payload)
},
}
1 change: 1 addition & 0 deletions src/store/socket/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from 'vue'
import { ActionTree } from 'vuex'
import { SocketState } from '@/store/socket/types'
import { RootState } from '@/store/types'
import { v4 as uuidv4 } from 'uuid'

export const actions: ActionTree<SocketState, RootState> = {
reset({ commit }) {
Expand Down
22 changes: 3 additions & 19 deletions src/store/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,11 @@ import { getters } from '@/store/socket/getters'
import { RootState } from '@/store/types'

export const getDefaultState = (): SocketState => {
let remoteMode: boolean
let hostname: string
let port: number

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
hostname = ''
port = 7125
} else {
remoteMode = false
hostname = (import.meta.env.VUE_APP_HOSTNAME as string) || window.location.hostname
const defaultPort = window.location.port || (window.location.protocol === 'https:' ? 443 : 80)
port = import.meta.env.VUE_APP_PORT ? Number(import.meta.env.VUE_APP_PORT) : Number(defaultPort)
}
const hostname = (import.meta.env.VUE_APP_HOSTNAME as string) || window.location.hostname
const defaultPort = window.location.port || (window.location.protocol === 'https:' ? 443 : 80)
const port = import.meta.env.VUE_APP_PORT ? Number(import.meta.env.VUE_APP_PORT) : Number(defaultPort)

return {
remoteMode,
hostname,
port,
protocol: document.location.protocol === 'https:' ? 'wss' : 'ws',
Expand Down
1 change: 0 additions & 1 deletion src/store/socket/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export interface SocketState {
remoteMode: boolean
hostname: string
port: number
protocol: string
Expand Down
12 changes: 12 additions & 0 deletions src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface RootState {
packageVersion: string
debugMode: boolean
naviDrawer: boolean | null
remoteMode: boolean
configInstances: ConfigJsonInstance[]

socket?: SocketState
gui?: GuiState
Expand All @@ -21,3 +23,13 @@ export interface RootStateDependency {
installedVersion: string
neededVersion: string
}

export interface ConfigJson {
remoteMode?: boolean
instances?: ConfigJsonInstance[]
}

export interface ConfigJsonInstance {
hostname: string
port?: number
}