-
Notifications
You must be signed in to change notification settings - Fork 11
/
parse_client.go
116 lines (91 loc) · 1.92 KB
/
parse_client.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
package fanuc
import (
"strings"
)
type GetFunc = func(device, string) (string, error)
type ParseClient struct {
GetFunc
io []IO
}
func (c *ParseClient) Errors() ([]Error, error) {
body, err := c.GetFunc(MD, "errall.ls")
if err != nil {
return nil, err
}
return parseErrors(body)
}
func (c *ParseClient) Frames() ([]Frame, error) {
body, err := c.GetFunc(MD, "frame.dg")
if err != nil {
return nil, err
}
return parseFrames(body)
}
func (c *ParseClient) cacheIO() error {
body, err := c.GetFunc(MD, "iostate.dg")
if err != nil {
return err
}
io, err := parseIO(body)
if err != nil {
return err
}
c.io = io
return nil
}
func contains(t Type, types []Type) bool {
for _, i := range types {
if i == t {
return true
}
}
return false
}
func (c *ParseClient) IO(types ...Type) ([]IO, error) {
if c.io == nil {
err := c.cacheIO()
if err != nil {
return nil, err
}
}
// Return all IO if no types provided
if len(types) == 0 {
return c.io, nil
}
var result []IO
for _, port := range c.io {
if contains(port.Type, types) {
result = append(result, port)
}
}
return result, nil
}
func (c *ParseClient) NumericRegisters() ([]NumericRegister, error) {
body, err := c.GetFunc(MD, "numreg.va")
if err != nil {
return nil, err
}
return parseNumericRegisters(body)
}
func (c *ParseClient) PositionRegisters() ([]PositionRegister, error) {
body, err := c.GetFunc(MD, "posreg.va")
if err != nil {
return nil, err
}
return parsePositionRegisters(body)
}
func (c *ParseClient) TPPrograms() ([]string, error) {
body, err := c.GetFunc(MD, "index_tp.htm")
if err != nil {
return nil, err
}
return parseTPPrograms(body)
}
func (c *ParseClient) TPPositions(programName string) ([]Position, error) {
lsName := strings.Replace(strings.ToLower(programName), ".tp", ".ls", -1)
body, err := c.GetFunc(MD, lsName)
if err != nil {
return nil, err
}
return parseTPPositions(body)
}