-
Notifications
You must be signed in to change notification settings - Fork 3
/
daemon.go
324 lines (288 loc) · 9.75 KB
/
daemon.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/leoleovich/3djuggler/gcodefeeder"
"github.com/leoleovich/3djuggler/juggler"
log "github.com/sirupsen/logrus"
)
type Daemon struct {
config *Config
jobfile string
job *juggler.Job
ie *InternEndpoint
feeder *gcodefeeder.Feeder
statusChan chan juggler.JobStatus
}
func (daemon *Daemon) Start() {
var err error
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
http.HandleFunc("/info", daemon.InfoHandler)
http.HandleFunc("/start", daemon.StartHandler)
http.HandleFunc("/pause", daemon.PauseHandler)
http.HandleFunc("/reschedule", daemon.RescheduleHandler)
http.HandleFunc("/cancel", daemon.CancelHandler)
http.HandleFunc("/version", daemon.VersionHandler)
go func() { log.Fatal(http.ListenAndServe(daemon.config.Listen, nil)) }()
log.Debug("Started http server on ", daemon.config.Listen)
daemon.statusChan = make(chan juggler.JobStatus, 10)
var oldStatus juggler.JobStatus
if err := daemon.ie.reschedule(); err != nil {
log.Error("reschedule failed: ", err)
}
for range time.Tick(pollingInterval) {
select {
case daemon.job.Status = <-daemon.statusChan:
log.Debugf("Assigning status '%s'", daemon.job.Status)
if err := daemon.ie.reportJobStatusChange(daemon.job); err != nil {
log.Error("Can't report it to intern: ", err)
}
default:
log.Debug("No status updates")
}
log.Infof("My status is: '%s'", daemon.job.Status)
if err = daemon.ie.reportStat(); err != nil {
log.Error(err)
}
switch daemon.job.Status {
case juggler.StatusWaitingJob, juggler.StatusButtonTimeout:
daemon.job.ID = 0
if err = daemon.ie.nextJob(); err != nil {
log.Error(err)
break
}
daemon.job.ID = daemon.ie.job.ID
daemon.job.Filename = daemon.ie.job.Filename
daemon.job.FileContent = daemon.ie.job.FileContent
daemon.job.Progress = daemon.ie.job.Progress
daemon.job.Owner = daemon.ie.job.Owner
daemon.job.Color = daemon.ie.job.Color
daemon.job.Fetched = time.Now()
daemon.job.Scheduled = time.Now().Add(waitingForButtonInterval)
daemon.UpdateStatus(juggler.StatusWaitingButton)
fallthrough
case juggler.StatusWaitingButton:
log.Info("Job ", daemon.job.ID, " is waiting")
err = daemon.ie.getJob(daemon.job.ID)
if err != nil {
log.Error("Can't get job status from intern: ", err)
} else {
log.Info("Job status on intern: ", daemon.ie.job.Status)
}
if err == nil && daemon.ie.job.Status == juggler.StatusCancelling {
log.Info("The job is cancelling")
daemon.UpdateStatus(juggler.StatusCancelling)
break
}
if daemon.job.Scheduled.After(time.Now()) {
log.Info("Waiting ", daemon.job.Scheduled.Unix()-time.Now().Unix(), " more seconds for somebody to press the button")
} else {
log.Warning("Nobody pressed the button on time")
log.Warning("Timeout while waiting for a job. Switching back to ", daemon.job.Status)
daemon.UpdateStatus(juggler.StatusButtonTimeout)
}
case juggler.StatusSending:
if oldStatus != juggler.StatusWaitingButton && oldStatus != juggler.StatusPaused {
log.Warningf("Forbidden status change sequence, from %s to %s. Ignoring", oldStatus, daemon.job.Status)
continue
}
log.Info("Sending to printer")
log.Debug("FileSize: ", len(daemon.job.FileContent))
err := os.WriteFile(daemon.jobfile, []byte(daemon.job.FileContent), 0644)
if err != nil {
log.Error(err)
break
}
daemon.feeder, err = gcodefeeder.NewFeeder(
daemon.config.Serial,
daemon.jobfile,
)
if err != nil {
log.Error("Failed to create Feeder: ", err)
break
}
daemon.UpdateStatus(juggler.StatusPrinting)
go func() {
if err = daemon.feeder.Feed(); err != nil {
log.Error(err)
}
}()
case juggler.StatusPrinting:
log.Infof("Job %d is currently printing", daemon.job.ID)
// Check status from intern
err = daemon.ie.getJob(daemon.job.ID)
if err != nil {
log.Error("Can't report status to intern: ", err)
}
if err == nil && daemon.ie.job.Status == juggler.StatusCancelling {
log.Info("Cancelling the job")
daemon.UpdateStatus(juggler.StatusCancelling)
break
}
daemon.job.Progress = float64(daemon.feeder.Progress())
daemon.job.FeederStatus = daemon.feeder.Status()
switch daemon.job.FeederStatus {
case gcodefeeder.Printing:
// We need to update percentage of print
if err := daemon.ie.reportJobStatusChange(daemon.job); err != nil {
log.Error("Can't report it to intern: ", err)
}
case gcodefeeder.Finished:
daemon.UpdateStatus(juggler.StatusFinished)
case gcodefeeder.Error:
daemon.UpdateStatus(juggler.StatusCancelling)
case gcodefeeder.ManuallyPaused, gcodefeeder.FSensorBusy, gcodefeeder.MMUBusy:
daemon.UpdateStatus(juggler.StatusPaused)
default:
log.Warning("Printing. Feeder status is: ", daemon.feeder.Status())
}
case juggler.StatusPaused:
daemon.job.FeederStatus = daemon.feeder.Status()
log.Infof("Job %d is currently paused", daemon.job.ID)
switch daemon.job.FeederStatus {
case gcodefeeder.Printing:
daemon.UpdateStatus(juggler.StatusPrinting)
case gcodefeeder.Error:
daemon.UpdateStatus(juggler.StatusCancelling)
default:
log.Warning("Paused. Feeder status is: ", daemon.feeder.Status())
}
case juggler.StatusCancelling:
fallthrough
case juggler.StatusFinished:
if daemon.feeder != nil && daemon.feeder.Status() != gcodefeeder.Finished {
log.Info("Stopping feeder")
daemon.feeder.Cancel()
}
log.Info("Deleting from intern")
err = daemon.ie.deleteJob(daemon.job)
if err != nil {
log.Error(err)
}
daemon.UpdateStatus(juggler.StatusWaitingJob)
default:
log.Error("Job ", daemon.job, " is in a weird state")
}
oldStatus = daemon.job.Status
}
}
func (daemon *Daemon) UpdateStatus(status juggler.JobStatus) {
select {
case daemon.statusChan <- status:
log.Debugf("Requesting status change to: '%s'", status)
default:
log.Error("Unable to request status change. statusChan is full")
}
}
// InfoHandler gives provides with json containing job status and some other important fields
func (daemon *Daemon) InfoHandler(w http.ResponseWriter, _ *http.Request) {
log.Infof("Received info handler request")
// Add headers to allow AJAX
juggler.SetHeaders(w)
job := &juggler.Job{
ID: daemon.job.ID,
Owner: daemon.job.Owner,
Filename: daemon.job.Filename,
Progress: daemon.job.Progress,
Status: daemon.job.Status,
Color: daemon.job.Color,
Fetched: daemon.job.Fetched,
Scheduled: daemon.job.Scheduled,
PrinterName: daemon.config.InternEndpoint.PrinterName,
}
b, err := json.Marshal(job)
if err != nil {
log.Errorf("Failed to respond on /info request: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(b))
}
// StartHandler acknowledged start of the job
func (daemon *Daemon) StartHandler(w http.ResponseWriter, _ *http.Request) {
log.Infof("Received start handler request")
// Add headers to allow AJAX
juggler.SetHeaders(w)
if daemon.job.Status == juggler.StatusWaitingButton {
// Initial start
daemon.UpdateStatus(juggler.StatusSending)
for daemon.job.Status != juggler.StatusSending {
log.Infof("Waiting for %s status to be set", juggler.StatusSending)
time.Sleep(1 * time.Second)
}
return
} else if daemon.job.Status == juggler.StatusPaused {
// Unpause
daemon.feeder.Start()
daemon.UpdateStatus(juggler.StatusPrinting)
for daemon.job.Status != juggler.StatusPrinting {
log.Infof("Waiting for %s status to be set", juggler.StatusPrinting)
time.Sleep(1 * time.Second)
}
return
}
errS := fmt.Sprintf("Ignore buttonpress in '%v' status", daemon.job.Status)
log.Info(errS)
http.Error(w, errS, http.StatusBadRequest)
}
// RescheduleHandler resets the time when the job will start
func (daemon *Daemon) RescheduleHandler(w http.ResponseWriter, _ *http.Request) {
log.Infof("Received reschedule handler request")
// Add headers to allow AJAX
juggler.SetHeaders(w)
if daemon.job.Status != juggler.StatusWaitingButton {
errS := fmt.Sprintf("Ignore reschedule in '%v' status", daemon.job.Status)
log.Info(errS)
http.Error(w, errS, http.StatusBadRequest)
return
}
daemon.job.Fetched = time.Now()
daemon.job.Scheduled = time.Now().Add(waitingForButtonInterval)
}
// CancelHandler cancels job execution
func (daemon *Daemon) CancelHandler(w http.ResponseWriter, _ *http.Request) {
log.Infof("Received cancel handler request")
// Add headers to allow AJAX
juggler.SetHeaders(w)
if daemon.job.ID == 0 {
errS := "Ignore cancel, no job scheduled"
log.Info(errS)
http.Error(w, errS, http.StatusBadRequest)
return
}
daemon.job.Scheduled = time.Time{}
daemon.UpdateStatus(juggler.StatusCancelling)
for daemon.job.Status != juggler.StatusCancelling {
log.Infof("Waiting for %s status to be set", juggler.StatusCancelling)
time.Sleep(1 * time.Second)
}
}
// CancelHandler cancels job execution
func (daemon *Daemon) PauseHandler(w http.ResponseWriter, _ *http.Request) {
log.Infof("Received pause handler request")
// Add headers to allow AJAX
juggler.SetHeaders(w)
if daemon.job.Status != juggler.StatusPrinting {
errS := "Ignore pause, not printing"
log.Info(errS)
http.Error(w, errS, http.StatusBadRequest)
return
}
daemon.feeder.Pause()
daemon.UpdateStatus(juggler.StatusPaused)
for daemon.job.Status != juggler.StatusPaused {
log.Infof("Waiting for %s status to be set", juggler.StatusPaused)
time.Sleep(1 * time.Second)
}
}
// VersionHandler cancels job execution
func (daemon *Daemon) VersionHandler(w http.ResponseWriter, _ *http.Request) {
log.Infof("Received version handler request")
// Add headers to allow AJAX
juggler.SetHeaders(w)
fmt.Fprint(w, gitCommit)
}