forked from Z-Bolt/OctoScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.go
More file actions
executable file
·275 lines (204 loc) · 7.5 KB
/
Copy pathtools.go
File metadata and controls
executable file
·275 lines (204 loc) · 7.5 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package utils
import (
"fmt"
"strconv"
"strings"
"github.com/gotk3/gotk3/gtk"
"github.com/Z-Bolt/OctoScreen/logger"
"github.com/Z-Bolt/OctoScreen/octoprintApis"
"github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
)
var cachedExtruderCount = -1
var cachedHasSharedNozzle = false
func getCachedPrinterProfileData(client *octoprintApis.Client) {
if cachedExtruderCount != -1 {
return
}
connectionResponse, err := (&octoprintApis.ConnectionRequest{}).Do(client)
if err != nil {
logger.LogError("Tools.setCachedPrinterProfileData()", "version.Get()", err)
return
}
printerProfile, err := (&octoprintApis.PrinterProfilesRequest{Id: connectionResponse.Current.PrinterProfile}).Do(client)
if err != nil {
logger.LogError("Tools.setCachedPrinterProfileData()", "Do(PrinterProfilesRequest)", err)
return
}
cachedExtruderCount = printerProfile.Extruder.Count
if cachedExtruderCount > 4 {
cachedExtruderCount = 4
}
cachedHasSharedNozzle = printerProfile.Extruder.HasSharedNozzle
}
func GetExtruderCount(client *octoprintApis.Client) int {
if cachedExtruderCount == -1 {
getCachedPrinterProfileData(client)
}
return cachedExtruderCount
}
func GetHotendCount(client *octoprintApis.Client) int {
if cachedExtruderCount == -1 {
getCachedPrinterProfileData(client)
}
if cachedHasSharedNozzle {
return 1
} else if cachedExtruderCount > 4 {
return 4
}
return cachedExtruderCount
}
func GetHasSharedNozzle(client *octoprintApis.Client) bool {
if cachedExtruderCount == -1 {
getCachedPrinterProfileData(client)
}
return cachedHasSharedNozzle
}
func GetDisplayNameForTool(toolName string) string {
// Since this is such a hack, lets add some bounds checking
if toolName == "" {
logger.Error("Tools..GetDisplayNameForTool() - toolName is empty")
return ""
}
lowerCaseName := strings.ToLower(toolName)
if strings.LastIndex(lowerCaseName, "tool") != 0 {
logger.Errorf("Tools.GetDisplayNameForTool() - toolName is invalid, value passed in was: %q", toolName)
return ""
}
if len(toolName) != 5 {
logger.Errorf("Tools.GetDisplayNameForTool() - toolName is invalid, value passed in was: %q", toolName)
return ""
}
toolIndexAsInt, _ := strconv.Atoi(string(toolName[4]))
displayName := toolName[0:4]
displayName = displayName + strconv.Itoa(toolIndexAsInt + 1)
return displayName
}
func GetToolTarget(client *octoprintApis.Client, tool string) (float64, error) {
logger.TraceEnter("Tools.GetToolTarget()")
fullStateRespone, err := (&octoprintApis.FullStateRequest{
Exclude: []string{"sd", "state"},
}).Do(client)
if err != nil {
logger.LogError("tools.GetToolTarget()", "Do(StateRequest)", err)
logger.TraceLeave("Tools.GetToolTarget()")
return -1, err
}
currentTemperatureData, ok := fullStateRespone.Temperature.CurrentTemperatureData[tool]
if !ok {
logger.TraceLeave("Tools.GetToolTarget()")
return -1, fmt.Errorf("unable to find tool %q", tool)
}
logger.TraceLeave("Tools.GetToolTarget()")
return currentTemperatureData.Target, nil
}
func SetToolTarget(client *octoprintApis.Client, tool string, target float64) error {
logger.TraceEnter("Tools.SetToolTarget()")
if tool == "bed" {
cmd := &octoprintApis.BedTargetRequest{Target: target}
logger.TraceLeave("Tools.SetToolTarget()")
return cmd.Do(client)
}
cmd := &octoprintApis.ToolTargetRequest{Targets: map[string]float64{tool: target}}
logger.TraceLeave("Tools.SetToolTarget()")
return cmd.Do(client)
}
func GetCurrentTemperatureData(client *octoprintApis.Client) (map[string]dataModels.TemperatureData, error) {
logger.TraceEnter("Tools.GetCurrentTemperatureData()")
temperatureDataResponse, err := (&octoprintApis.TemperatureDataRequest{}).Do(client)
if err != nil {
logger.LogError("tools.GetCurrentTemperatureData()", "Do(TemperatureDataRequest)", err)
logger.TraceLeave("Tools.GetCurrentTemperatureData()")
return nil, err
}
if temperatureDataResponse == nil {
logger.Error("tools.GetCurrentTemperatureData() - temperatureDataResponse is nil")
logger.TraceLeave("Tools.GetCurrentTemperatureData()")
return nil, err
}
// Can't test for temperatureDataResponse.TemperatureStateResponse == nil (type mismatch)
if temperatureDataResponse.TemperatureStateResponse.CurrentTemperatureData == nil {
logger.Error("tools.GetCurrentTemperatureData() - temperatureDataResponse.TemperatureStateResponse.CurrentTemperatureData is nil")
logger.TraceLeave("Tools.GetCurrentTemperatureData()")
return nil, err
}
logger.TraceLeave("Tools.GetCurrentTemperatureData()")
return temperatureDataResponse.TemperatureStateResponse.CurrentTemperatureData, nil
}
func CheckIfHotendTemperatureIsTooLow(client *octoprintApis.Client, extruderId, action string, parentWindow *gtk.Window) bool {
logger.TraceEnter("Tools.CheckIfHotendTemperatureIsTooLow()")
currentTemperatureData, err := GetCurrentTemperatureData(client)
if err != nil {
logger.LogError("tools.CurrentHotendTemperatureIsTooLow()", "GetCurrentTemperatureData()", err)
logger.TraceLeave("Tools.CheckIfHotendTemperatureIsTooLow()")
return true
}
temperatureData := currentTemperatureData[extruderId]
// If the temperature of the hotend is too low, display an error.
if HotendTemperatureIsTooLow(temperatureData, action, parentWindow) {
errorMessage := fmt.Sprintf(
"The temperature of the hotend is too low to %s.\n(the current temperature is only %.0f°C)\n\nPlease increase the temperature and try again.",
action,
temperatureData.Actual,
)
ErrorMessageDialogBox(parentWindow, errorMessage)
logger.TraceLeave("Tools.CheckIfHotendTemperatureIsTooLow()")
return true
}
logger.TraceLeave("Tools.CheckIfHotendTemperatureIsTooLow()")
return false
}
func GetToolheadFileName(hotendIndex, hotendCount int) string {
strImageFileName := ""
if hotendIndex == 1 && hotendCount == 1 {
strImageFileName = "toolhead.svg"
} else {
strImageFileName = fmt.Sprintf("toolhead-%d.svg", hotendIndex)
}
return strImageFileName
}
func GetExtruderFileName(hotendIndex, hotendCount int) string {
strImageFileName := ""
if hotendIndex == 1 && hotendCount == 1 {
strImageFileName = "extruder-typeB.svg"
} else {
strImageFileName = fmt.Sprintf("extruder-typeB-%d.svg", hotendIndex)
}
return strImageFileName
}
func GetHotendFileName(hotendIndex, hotendCount int) string {
strImageFileName := ""
if hotendIndex == 1 && hotendCount == 1 {
strImageFileName = "hotend.svg"
} else {
strImageFileName = fmt.Sprintf("hotend-%d.svg", hotendIndex)
}
return strImageFileName
}
func GetNozzleFileName(hotendIndex, hotendCount int) string {
strImageFileName := ""
if hotendIndex == 1 && hotendCount == 1 {
strImageFileName = "nozzle.svg"
} else {
strImageFileName = fmt.Sprintf("nozzle-%d.svg", hotendIndex)
}
return strImageFileName
}
func GetTemperatureDataString(temperatureData dataModels.TemperatureData) string {
return fmt.Sprintf("%.0f°C / %.0f°C", temperatureData.Actual, temperatureData.Target)
}
// TODO: maybe move HotendTemperatureIsTooLow into a hotend utils file?
const MIN_HOTEND_TEMPERATURE = 150.0
func HotendTemperatureIsTooLow(
temperatureData dataModels.TemperatureData,
action string,
parentWindow *gtk.Window,
) bool {
targetTemperature := temperatureData.Target
logger.Infof("tools.HotendTemperatureIsTooLow() - targetTemperature is %.2f", targetTemperature)
actualTemperature := temperatureData.Actual
logger.Infof("tools.HotendTemperatureIsTooLow() - actualTemperature is %.2f", actualTemperature)
if targetTemperature <= MIN_HOTEND_TEMPERATURE || actualTemperature <= MIN_HOTEND_TEMPERATURE {
return true
}
return false
}