forked from mcuadros/OctoPrint-TFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (137 loc) · 3.14 KB
/
main.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"github.com/minkshaman/OctoPrint-TFT/ui"
"github.com/gotk3/gotk3/gtk"
"gopkg.in/yaml.v1"
)
const (
EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH"
EnvResolution = "OCTOPRINT_TFT_RESOLUTION"
EnvBaseURL = "OCTOPRINT_HOST"
EnvAPIKey = "OCTOPRINT_APIKEY"
EnvConfigFile = "OCTOPRINT_CONFIG_FILE"
)
var (
BaseURL string
APIKey string
ConfigFile string
Resolution string
)
func init() {
ui.StylePath = os.Getenv(EnvStylePath)
Resolution = os.Getenv(EnvResolution)
ConfigFile = os.Getenv(EnvConfigFile)
if ConfigFile == "" {
ConfigFile = findConfigFile()
}
cfg := readConfig(ConfigFile)
BaseURL = os.Getenv(EnvBaseURL)
if BaseURL == "" {
BaseURL = fmt.Sprintf("http://%s:%d", cfg.Server.Host, cfg.Server.Port)
ui.Logger.Infof("Using %q as server address", BaseURL)
}
APIKey = os.Getenv(EnvAPIKey)
if APIKey == "" {
APIKey = cfg.API.Key
if cfg.API.Key != "" {
ui.Logger.Infof("Found API key at %q file", ConfigFile)
}
}
}
func main() {
gtk.Init(nil)
settings, _ := gtk.SettingsGetDefault()
settings.SetProperty("gtk-application-prefer-dark-theme", true)
width, height := getSize()
_ = ui.New(BaseURL, APIKey, width, height)
gtk.Main()
}
var (
configLocation = ".octoprint/config.yaml"
homeOctoPi = "/home/pi/"
)
type config struct {
// API Settings.
API struct {
// Key is the current API key needed for accessing the API.
Key string
}
// Server settings.
Server struct {
// Hosts define the host to which to bind the server, defaults to "0.0.0.0".
Host string
// Port define the port to which to bind the server, defaults to 5000.
Port int
}
}
func readConfig(configFile string) *config {
cfg := &config{}
if configFile == "" {
return cfg
}
ui.Logger.Infof("OctoPrint's config file found: %q", configFile)
data, err := ioutil.ReadFile(configFile)
if err != nil {
ui.Logger.Fatal(err)
return cfg
}
if err := yaml.Unmarshal([]byte(data), cfg); err != nil {
ui.Logger.Fatalf("Error decoding YAML config file %q: %s", configFile, err)
return cfg
}
if cfg.Server.Host == "" {
cfg.Server.Host = "localhost"
}
if cfg.Server.Port == 0 {
cfg.Server.Port = 5000
}
return cfg
}
func findConfigFile() string {
if file := doFindConfigFile(homeOctoPi); file != "" {
return file
}
usr, err := user.Current()
if err != nil {
return ""
}
return doFindConfigFile(usr.HomeDir)
}
func doFindConfigFile(home string) string {
path := filepath.Join(home, configLocation)
if _, err := os.Stat(path); err == nil {
return path
}
return ""
}
func getSize() (width, height int) {
if Resolution == "" {
return
}
parts := strings.SplitN(Resolution, "x", 2)
if len(parts) != 2 {
ui.Logger.Fatalf("Malformed %s variable: %q", EnvResolution, Resolution)
return
}
var err error
width, err = strconv.Atoi(parts[0])
if err != nil {
ui.Logger.Fatalf("Malformed %s variable: %q, %s",
EnvResolution, Resolution, err)
return
}
height, err = strconv.Atoi(parts[1])
if err != nil {
ui.Logger.Fatalf("Malformed %s variable: %q, %s",
EnvResolution, Resolution, err)
return
}
return
}