-
Notifications
You must be signed in to change notification settings - Fork 9
/
job.go
123 lines (98 loc) · 3.21 KB
/
job.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
package octoprint
import (
"bytes"
"encoding/json"
"io"
)
const JobTool = "/api/job"
var JobToolErrors = statusMapping{
409: "Printer is not operational or the current print job state does not match the preconditions for the command.",
}
// JobRequest retrieve information about the current job (if there is one).
type JobRequest struct{}
// Do sends an API request and returns the API response.
func (cmd *JobRequest) Do(c *Client) (*JobResponse, error) {
b, err := c.doJSONRequest("GET", JobTool, nil, nil)
if err != nil {
return nil, err
}
r := &JobResponse{}
if err := json.Unmarshal(b, r); err != nil {
return nil, err
}
return r, err
}
// StartRequest starts the print of the currently selected file.
type StartRequest struct{}
// Do sends an API request and returns an error if any.
func (cmd *StartRequest) Do(c *Client) error {
payload := map[string]string{"command": "start"}
b := bytes.NewBuffer(nil)
if err := json.NewEncoder(b).Encode(payload); err != nil {
return err
}
_, err := c.doJSONRequest("POST", JobTool, b, JobToolErrors)
return err
}
// CancelRequest cancels the current print job.
type CancelRequest struct{}
// Do sends an API request and returns an error if any.
func (cmd *CancelRequest) Do(c *Client) error {
payload := map[string]string{"command": "cancel"}
b := bytes.NewBuffer(nil)
if err := json.NewEncoder(b).Encode(payload); err != nil {
return err
}
_, err := c.doJSONRequest("POST", JobTool, b, JobToolErrors)
return err
}
// RestartRequest restart the print of the currently selected file from the
// beginning. There must be an active print job for this to work and the print
// job must currently be paused
type RestartRequest struct{}
// Do sends an API request and returns an error if any.
func (cmd *RestartRequest) Do(c *Client) error {
payload := map[string]string{"command": "restart"}
b := bytes.NewBuffer(nil)
if err := json.NewEncoder(b).Encode(payload); err != nil {
return err
}
_, err := c.doJSONRequest("POST", JobTool, b, JobToolErrors)
return err
}
type PauseAction string
const (
// Pause the current job if it’s printing, does nothing if it’s already paused.
Pause PauseAction = "pause"
// Resume the current job if it’s paused, does nothing if it’s printing.
Resume PauseAction = "resume"
// Toggle the pause state of the job, pausing it if it’s printing and
// resuming it if it’s currently paused.
Toggle PauseAction = "toggle"
)
// PauseRequest pauses/resumes/toggles the current print job.
type PauseRequest struct {
// Action specifies which action to take.
// In order to stay backwards compatible to earlier iterations of this API,
// the default action to take if no action parameter is supplied is to
// toggle the print job status.
Action PauseAction `json:"action"`
}
// Do sends an API request and returns an error if any.
func (cmd *PauseRequest) Do(c *Client) error {
b := bytes.NewBuffer(nil)
if err := cmd.encode(b); err != nil {
return err
}
_, err := c.doJSONRequest("POST", JobTool, b, JobToolErrors)
return err
}
func (cmd *PauseRequest) encode(w io.Writer) error {
return json.NewEncoder(w).Encode(struct {
Command string `json:"command"`
PauseRequest
}{
Command: "pause",
PauseRequest: *cmd,
})
}