Skip to content

Commit 6a253dc

Browse files
committed
refactor: handle undefined config items
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 604445d commit 6a253dc

File tree

11 files changed

+159
-147
lines changed

11 files changed

+159
-147
lines changed

src/components/settings/macros/MacroCategorySettings.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
</template>
7878

7979
<template
80-
v-if="macro.config.description && macro.config.description !== 'G-Code macro'"
80+
v-if="macro.config?.description && macro.config.description !== 'G-Code macro'"
8181
#sub-title
8282
>
8383
<span class="ml-1 mr-2">

src/components/settings/macros/MacroSettingsDialog.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<app-dialog
33
v-model="open"
44
:title="macro.name.toUpperCase()"
5-
:sub-title="macro.config.description"
5+
:sub-title="macro.config?.description"
66
max-width="480"
77
@save="handleSave"
88
>

src/components/widgets/macros/Macros.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
v-for="macro in category.macros"
4444
:key="`category-${macro.name}`"
4545
top
46-
:disabled="!macro.config.description || macro.config.description === 'G-Code macro'"
46+
:disabled="!macro.config?.description || macro.config.description === 'G-Code macro'"
4747
>
4848
<template #activator="{ on, attrs }">
4949
<macro-btn
@@ -57,7 +57,7 @@
5757
{{ macro.alias || macro.name }}
5858
</macro-btn>
5959
</template>
60-
<span>{{ macro.config.description }}</span>
60+
<span>{{ macro.config?.description }}</span>
6161
</v-tooltip>
6262
</v-expansion-panel-content>
6363
</v-expansion-panel>

src/components/widgets/outputs/OutputFan.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class OutputFan extends Mixins(StateMixin, BrowserMixin) {
5858
5959
get value () {
6060
if (!this.fan.speed) return 0
61-
const speed = this.fan.speed / (this.fan.config.max_power || 1)
61+
const speed = this.fan.speed / (this.fan.config?.max_power || 1)
6262
return Math.round(speed * 100)
6363
}
6464

src/components/widgets/outputs/OutputLed.vue

+20-18
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,31 @@ export default class OutputLed extends Mixins(StateMixin) {
5353
get supportedChannels (): string {
5454
const { type, config } = this.led
5555
56-
if ('color_order' in config) {
57-
const colorOrder = Array.isArray(config.color_order)
58-
? config.color_order[0]
59-
: config.color_order
60-
61-
if (typeof colorOrder === 'string') {
62-
return colorOrder
56+
if (config) {
57+
if ('color_order' in config) {
58+
const colorOrder = Array.isArray(config.color_order)
59+
? config.color_order[0]
60+
: config.color_order
61+
62+
if (typeof colorOrder === 'string') {
63+
return colorOrder
64+
}
6365
}
64-
}
6566
66-
switch (type) {
67-
case 'dotstar':
68-
return 'RGB'
67+
switch (type) {
68+
case 'dotstar':
69+
return 'RGB'
6970
70-
case 'led': {
71-
const channels = []
71+
case 'led': {
72+
const channels = []
7273
73-
if ('red_pin' in config) channels.push('R')
74-
if ('green_pin' in config) channels.push('G')
75-
if ('blue_pin' in config) channels.push('B')
76-
if ('white_pin' in config) channels.push('W')
74+
if ('red_pin' in config) channels.push('R')
75+
if ('green_pin' in config) channels.push('G')
76+
if ('blue_pin' in config) channels.push('B')
77+
if ('white_pin' in config) channels.push('W')
7778
78-
return channels.join('')
79+
return channels.join('')
80+
}
7981
}
8082
}
8183

src/components/widgets/toolhead/ExtruderStepperSync.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
]"
1414
:disabled="!klippyReady || printerPrinting"
1515
:loading="hasWait(`${$waits.onSyncExtruder}${extruderStepper.name}`)"
16-
:reset-value="extruderStepper.config.extruder"
16+
:reset-value="extruderStepper.config?.extruder"
1717
item-value="key"
1818
item-text="name"
1919
@change="sendSyncExtruderMotion"

src/components/widgets/toolhead/ToolheadCard.vue

+11-12
Original file line numberDiff line numberDiff line change
@@ -168,49 +168,48 @@ export default class ToolheadCard extends Mixins(StateMixin, ToolheadMixin) {
168168
}
169169
170170
get printerSupportsQuadGantryLevel (): boolean {
171-
return 'quad_gantry_level' in this.printerSettings
171+
return this.printerSettings.quad_gantry_level != null
172172
}
173173
174174
get printerSupportsZTiltAdjust (): boolean {
175175
return (
176-
'z_tilt' in this.printerSettings ||
176+
this.printerSettings.z_tilt != null ||
177177
(
178178
this.klippyApp.isKalico &&
179-
'z_tilt_ng' in this.printerSettings
179+
this.printerSettings.z_tilt_ng != null
180180
)
181181
)
182182
}
183183
184184
get printerSupportsBedScrewsAdjust (): boolean {
185-
return 'bed_screws' in this.printerSettings
185+
return this.printerSettings.bed_screws != null
186186
}
187187
188188
get printerSupportsBedScrewsCalculate (): boolean {
189-
return 'screws_tilt_adjust' in this.printerSettings
189+
return this.printerSettings.screws_tilt_adjust != null
190190
}
191191
192192
get printerSupportsBedTiltCalibrate (): boolean {
193-
return 'bed_tilt' in this.printerSettings
193+
return this.printerSettings.bed_tilt != null
194194
}
195195
196196
get printerSupportsDeltaCalibrate (): boolean {
197-
return 'delta_calibrate' in this.printerSettings
197+
return this.printerSettings.delta_calibrate != null
198198
}
199199
200200
get printerSupportsProbeCalibrate (): boolean {
201201
return (
202-
'probe' in this.printerSettings ||
203-
'bltouch' in this.printerSettings ||
204-
'smart_effector' in this.printerSettings ||
202+
this.printerSettings.probe != null ||
203+
this.printerSettings.bltouch != null ||
204+
this.printerSettings.smart_effector != null ||
205205
Object.keys(this.printerSettings)
206206
.some(x => x.startsWith('probe_eddy_current '))
207207
)
208208
}
209209
210210
get printerSupportsZEndstopCalibrate (): boolean {
211211
return (
212-
'stepper_z' in this.printerSettings &&
213-
'z_position_endstop' in this.printerSettings.stepper_z
212+
this.printerSettings.stepper_z?.position_endstop != null
214213
)
215214
}
216215

src/store/chart_helpers.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,28 @@ export const handleSystemStatsChange = (payload: KlipperPrinterState, state: Roo
119119
*/
120120
export const handleAddChartEntry = (retention: number, state: RootState, commit: Commit, getters: any) => {
121121
const configureChartEntry = () => {
122-
const date = new Date()
123-
const r: ChartData = {
124-
date
122+
const chartData: ChartData = {
123+
date: new Date()
125124
}
126125

127126
const keys: string[] = getters.getChartableSensors
128127

129128
keys.forEach((key) => {
130-
const temp = state.printer.printer[key].temperature
131-
const target = state.printer.printer[key].target
132-
const power = state.printer.printer[key].power
133-
const speed = state.printer.printer[key].speed
134-
r[key] = temp
135-
if (target != null) r[`${key}#target`] = target
136-
if (power != null) r[`${key}#power`] = power
137-
if (speed != null) r[`${key}#speed`] = speed
129+
const sensor = state.printer.printer[key]
130+
131+
if (sensor != null) {
132+
const temp = sensor.temperature
133+
const target = sensor.target
134+
const power = sensor.power
135+
const speed = sensor.speed
136+
chartData[key] = temp
137+
if (target != null) chartData[`${key}#target`] = target
138+
if (power != null) chartData[`${key}#power`] = power
139+
if (speed != null) chartData[`${key}#speed`] = speed
140+
}
138141
})
139142

140-
return r
143+
return chartData
141144
}
142145

143146
if (state.charts.ready) {

src/store/macros/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface Macro {
1515
assignTo?: string;
1616
disabledWhilePrinting?: boolean;
1717
color?: string;
18-
config: KlipperPrinterGcodeMacroSettings;
18+
config?: KlipperPrinterGcodeMacroSettings;
1919
order?: number;
2020
variables?: Record<string, unknown>
2121
}

src/store/printer/getters.ts

+56-48
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,22 @@ export const getters = {
325325
* Return MCU's and their state
326326
*/
327327
getMcus: (state) => {
328-
const mcus = Object.keys(state.printer)
329-
.filter(key => key.startsWith('mcu'))
328+
const mcus: MCU[] = []
329+
330+
const mcuKeys = Object.keys(state.printer)
331+
.filter(key => key === 'mcu' || key.startsWith('mcu '))
330332
.sort()
331-
.map((key): MCU => ({
333+
334+
for (const key of mcuKeys) {
335+
const config = state.printer.configfile.settings[key.toLowerCase()]
336+
337+
mcus.push({
332338
name: key,
333339
prettyName: Vue.$filters.prettyCase(key),
334340
...state.printer[key],
335-
config: state.printer.configfile.settings[key.toLowerCase()]
336-
}))
341+
config,
342+
})
343+
}
337344

338345
return mcus
339346
},
@@ -408,23 +415,23 @@ export const getters = {
408415
getSteppers: (state): Stepper[] => {
409416
const steppers: Stepper[] = []
410417

411-
const stepperKeys: string[] = state.printer.motion_report?.steppers ?? []
418+
const stepperKeys = state.printer.motion_report?.steppers ?? []
412419

413420
for (const item of stepperKeys) {
414421
const name = item.startsWith('stepper_')
415422
? item.substring(8)
416423
: item.split(' ', 2).pop() || ''
417424

418425
const e = state.printer[item]
419-
const c = state.printer.configfile.settings[item.toLowerCase()]
426+
const config = state.printer.configfile.settings[item.toLowerCase()]
420427

421428
steppers.push({
422429
name,
423430
prettyName: Vue.$filters.prettyCase(name),
424431
key: item,
425432
enabled: state.printer.stepper_enable?.steppers[item],
426433
...e,
427-
config: { ...c }
434+
config
428435
})
429436
}
430437

@@ -515,45 +522,46 @@ export const getters = {
515522
* Return available heaters
516523
*/
517524
getHeaters: (state): Heater[] => {
518-
const heaters = state.printer.heaters?.available_heaters ?? []
519-
if (
520-
heaters.length
521-
) {
522-
const r: Heater[] = []
523-
heaters.forEach((e: string) => {
524-
const heater = state.printer[e]
525-
if (heater && Object.keys(heater).length > 0) {
526-
const config = state.printer.configfile.settings[e.toLowerCase()]
527-
// Some heater items may have a prefix determining type.
528-
// Check for these and split as necessary.
529-
const keys = [
530-
'heater_generic'
531-
]
532-
533-
const [type, nameFromSplit] = e.split(' ', 2)
534-
const name = nameFromSplit && keys.includes(type)
535-
? nameFromSplit
536-
: e
537-
538-
const color = Vue.$colorset.next(getKlipperType(e), e)
539-
const prettyName = Vue.$filters.prettyCase(name)
540-
541-
r.push({
542-
...heater,
543-
name,
544-
color,
545-
prettyName,
546-
key: e,
547-
minTemp: config?.min_temp ?? 0,
548-
maxTemp: config?.max_temp ?? 500,
549-
config: { ...config }
550-
})
551-
}
552-
})
525+
const heaters: Heater[] = []
526+
527+
const heaterKeys = state.printer.heaters?.available_heaters ?? []
553528

554-
return r.sort((a, b) => a.name.localeCompare(b.name))
529+
for (const key of heaterKeys) {
530+
const heater = state.printer[key]
531+
532+
if (heater && Object.keys(heater).length > 0) {
533+
const config = state.printer.configfile.settings[key.toLowerCase()]
534+
535+
// Some heater items may have a prefix determining type.
536+
// Check for these and split as necessary.
537+
const prefixedTypes = [
538+
'heater_generic'
539+
]
540+
541+
const [type, nameFromSplit] = key.split(' ', 2)
542+
const name = nameFromSplit && prefixedTypes.includes(type)
543+
? nameFromSplit
544+
: key
545+
546+
const color = Vue.$colorset.next(getKlipperType(key), key)
547+
const prettyName = Vue.$filters.prettyCase(name)
548+
549+
heaters.push({
550+
...heater,
551+
name,
552+
type,
553+
color,
554+
prettyName,
555+
key,
556+
minTemp: config?.min_temp ?? 0,
557+
maxTemp: config?.max_temp ?? 500,
558+
config
559+
})
560+
}
555561
}
556-
return []
562+
563+
return heaters
564+
.sort((a, b) => a.name.localeCompare(b.name))
557565
},
558566

559567
getAllLeds: (_, getters) => {
@@ -871,7 +879,7 @@ export const getters = {
871879
break
872880
}
873881

874-
const name = config[`screw${index}_name`]
882+
const name = config[`screw${index}_name`] ?? ''
875883
const prettyName = name
876884
? Vue.$filters.prettyCase(name)
877885
: i18n.t('app.general.label.screw_number', { index: index + 1 }).toString()
@@ -907,8 +915,8 @@ export const getters = {
907915
break
908916
}
909917

910-
const coords = config[key]
911-
const name = config[`${key}_name`]
918+
const coords = config[key] ?? [0, 0]
919+
const name = config[`${key}_name`] ?? ''
912920
const prettyName = name
913921
? Vue.$filters.prettyCase(name)
914922
: i18n.t('app.general.label.screw_number', { index: index + 1 }).toString()

0 commit comments

Comments
 (0)