Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ControlPanel.go « ui - github.com/Z-Bolt/OctoScreen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 71fc839cac0c06b36d1ea767267b58948beef658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package ui

import (
	"strings"

	// "github.com/gotk3/gotk3/gtk"
	"github.com/Z-Bolt/OctoScreen/interfaces"
	"github.com/Z-Bolt/OctoScreen/logger"
	"github.com/Z-Bolt/OctoScreen/uiWidgets"
	"github.com/Z-Bolt/OctoScreen/octoprintApis"
	"github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
	// "github.com/Z-Bolt/OctoScreen/utils"
)

var controlPanelInstance *controlPanel

type controlPanel struct {
	CommonPanel
}

func ControlPanel(
	ui 				*UI,
	parentPanel		interfaces.IPanel,
) *controlPanel {
	if controlPanelInstance == nil {
		instance := &controlPanel {
			CommonPanel: NewCommonPanel(ui, parentPanel),
		}
		instance.initialize()
		controlPanelInstance = instance
	}

	return controlPanelInstance
}

func (this *controlPanel) initialize() {
	defer this.Initialize()

	for _, controlDefinition := range this.getDefaultControls() {
		icon := strings.ToLower(strings.Replace(controlDefinition.Name, " ", "-", -1))
		button := uiWidgets.CreateControlButton(this.UI.Client, this.UI.window, controlDefinition, icon)
		this.AddButton(button)
	}

	for _, controlDefinition := range this.getCustomControls() {
		button := uiWidgets.CreateControlButton(this.UI.Client, this.UI.window, controlDefinition, "custom-script")
		this.AddButton(button)
	}

	for _, commandDefinition := range this.getCommands() {
		button := uiWidgets.CreateCommandButton(this.UI.Client, this.UI.window, commandDefinition, "custom-script")
		this.AddButton(button)
	}
}

func (this *controlPanel) getDefaultControls() []*dataModels.ControlDefinition {
	var controlDefinitions = []*dataModels.ControlDefinition{{
		Name:    "Motor Off",
		Command: "M18",			// Disable all stepper motors immediately
	}, {
		Name:    "Motor On",
		Command: "M17",			// Enable all stepper motors
	}, {
		Name:    "Fan Off",
		Command: "M106 S0",		// Sets the fan speed to off
	}, {
		Name:    "Fan On",
		Command: "M106",		// Sets the fan speed to full
	}}

	return controlDefinitions
}

func (this *controlPanel) getCustomControls() []*dataModels.ControlDefinition {
	controlDefinitions := []*dataModels.ControlDefinition{}

	logger.Info("control.getCustomControl() - Retrieving custom controls")
	response, err := (&octoprintApis.CustomCommandsRequest{}).Do(this.UI.Client)
	if err != nil {
		logger.LogError("control.getCustomControl()", "Do(ControlDefinition)", err)
		return controlDefinitions
	}

	for _, control := range response.Controls {
		for _, childControl := range control.Children {
			if childControl.Command != "" || childControl.Script != "" || childControl.Commands != nil {
				controlDefinitions = append(controlDefinitions, childControl)
			}
		}
	}

	return controlDefinitions
}

func (this *controlPanel) getCommands() []*dataModels.CommandDefinition {
	logger.Info("Retrieving custom commands")
	response, err := (&octoprintApis.SystemCommandsRequest{}).Do(this.UI.Client)
	if err != nil {
		logger.LogError("control.getCommands()", "Do(SystemCommandsRequest)", err)
		return nil
	}

	return response.Custom
}