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

HttpRequestTestWindow.go « ui - github.com/Z-Bolt/OctoScreen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4bc00baa4d9c3100a830dd94da7afb41f188d42 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package ui

import (
	"fmt"
	// "io/ioutil"
	// "os"
	// "os/user"
	// "path/filepath"
	// "strconv"
	// "strings"
	"time"

	"github.com/coreos/go-systemd/daemon"
	"github.com/gotk3/gotk3/gtk"

	"github.com/Z-Bolt/OctoScreen/logger"
	"github.com/Z-Bolt/OctoScreen/octoprintApis"
	// "github.com/Z-Bolt/OctoScreen/ui"
	"github.com/Z-Bolt/OctoScreen/utils"
)


type HttpRequestTestWindow struct {
	Window						*gtk.Window
	Label						*gtk.Label
	Client						*octoprintApis.Client
	BackgroundTask				*utils.BackgroundTask
	UpdateCount					int
}


func CreateHttpRequestTestWindow(
	endpoint string,
	key string,
	width int,
	height int,
) *HttpRequestTestWindow {
	logger.TraceEnter("CreateHttpRequestTestWindow()")


	instance := &HttpRequestTestWindow {
		Window: nil,
		Label: nil,
		Client: octoprintApis.NewClient(endpoint, key),
		BackgroundTask: nil,
		UpdateCount: 0,
	}

	instance.BackgroundTask = utils.CreateBackgroundTask(time.Second * 10, instance.updateTestWindow)

	instance.createWindow(width, height)
	instance.addControls()
	defer instance.Window.ShowAll()

	instance.sdNotify(daemon.SdNotifyReady)

	logger.TraceLeave("CreateHttpRequestTestWindow()")
	return instance
}


func (this *HttpRequestTestWindow) createWindow(
	width int,
	height int,
) {
	window, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
	if err != nil {
		logger.LogFatalError("createWindow()", "WindowNew()", err)
	}

	this.Window = window
	this.Window.SetTitle("HTTP Request Test")
	this.Window.SetDefaultSize(width, height)


	this.Window.Connect("show", this.BackgroundTask.Start)

	this.Window.Connect("destroy", func() {
		logger.Debug("destroy() callback was called")
		gtk.MainQuit()
	})
}


func (this *HttpRequestTestWindow) addControls() {
	// Create a new label widget to show in the window.
	label, err := gtk.LabelNew("")
	if err != nil {
		logger.LogFatalError("CreateHttpRequestTestWindow()", "LabelNew()", err)
	}

	this.Label = label
	this.Label.SetHAlign(gtk.ALIGN_START)
	this.Label.SetVAlign(gtk.ALIGN_START)
	this.Window.Add(this.Label)
}


func (this *HttpRequestTestWindow) updateTestWindow() {
	logger.TraceEnter("updateTestWindow()")

	this.UpdateCount++
	strUpdateCount := fmt.Sprintf("UpdateCount: %d", this.UpdateCount)
	logger.Debug(strUpdateCount)
	this.Label.SetLabel(strUpdateCount)


	//this.checkNotification()
	this.verifyConnection()

	logger.TraceLeave("updateTestWindow()")
}


func (this *HttpRequestTestWindow) verifyConnection() {
	logger.TraceEnter("verifyConnection()")

	this.sdNotify(daemon.SdNotifyWatchdog)

	connectionResponse, err := (&octoprintApis.ConnectionRequest{}).Do(this.Client)
	if err != nil {
		logger.LogError("verifyConnection()", "ConnectionRequest.Do()", err)
	} else {
		strCurrentState := string(connectionResponse.Current.State)
		logger.Debugf("verifyConnection() succeeded")
		logger.Debugf("connectionResponse.Current.State is %q", strCurrentState)
	}

	logger.TraceLeave("verifyConnection()")
}


func (this *HttpRequestTestWindow) sdNotify(state string) {
	logger.TraceEnter("sdNotify()")

	_, err := daemon.SdNotify(false, state)
	if err != nil {
		logger.Errorf("sdNotify()", "SdNotify()", err)
		logger.TraceLeave("sdNotify()")
		return
	}

	logger.TraceLeave("sdNotify()")
}