-
Notifications
You must be signed in to change notification settings - Fork 0
/
comtool.go
106 lines (92 loc) · 2.21 KB
/
comtool.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
// Package comtool provides the ability to set FANUC robot comments
// via an HTTP request to the KAREL ComSet utility.
package comtool
import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// FunctionCode is a type for the constants listed below which
// should be provided to Set. The constants correspond to
// the sFc parameter required by ComSet.
type FunctionCode int
const (
INVALID FunctionCode = 0
NUMREG FunctionCode = 1
// 2 -- set numreg
POSREG FunctionCode = 3
UALM FunctionCode = 4
// 5 -- set ualm_sev
RIN FunctionCode = 6
ROUT FunctionCode = 7
DIN FunctionCode = 8
DOUT FunctionCode = 9
GIN FunctionCode = 10
GOUT FunctionCode = 11
AIN FunctionCode = 12
AOUT FunctionCode = 13
SREG FunctionCode = 14
// 15 -- set SREG
// 16 ??
// 17 ??
// 18 ??
FLAG FunctionCode = 19
)
var codes = [...]string{
NUMREG: "R",
POSREG: "PR",
UALM: "UALM",
RIN: "RI",
ROUT: "RO",
DIN: "DI",
DOUT: "DO",
GIN: "GI",
GOUT: "GO",
AIN: "AI",
AOUT: "AO",
SREG: "SR",
FLAG: "F",
}
func (f FunctionCode) String() string {
s := codes[f]
if s != "" {
return s
}
return "functionCode(" + strconv.Itoa(int(f)) + ")"
}
var (
ErrForbidden = errForbidden()
ErrUnauthorized = errUnauthorized()
)
func errForbidden() error {
return errors.New("Forbidden. Please unlock KAREL via Setup > Host Comm > HTTP.")
}
func errUnauthorized() error {
return errors.New("Unauthorized. Please unlock KAREL via Setup > Host Comm > HTTP.")
}
// Set sets the comment for an item at the provided host.
func Set(code FunctionCode, id int, comment string, host string, timeout time.Duration) error {
url := fmt.Sprintf("http://%s/karel/ComSet?sComment=%s&sIndx=%d&sFc=%d", host, url.PathEscape(comment), id, code)
client := http.Client{
Timeout: time.Duration(timeout),
}
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusForbidden:
return ErrForbidden
case http.StatusUnauthorized:
return ErrUnauthorized
default:
return fmt.Errorf("Failed to set comment for %s[%d], '%s', at host %s: %d", code, id, comment, host, resp.StatusCode)
}
}
return nil
}