-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.go
More file actions
300 lines (265 loc) · 8.42 KB
/
Copy pathcamera.go
File metadata and controls
300 lines (265 loc) · 8.42 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
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
package gogige
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/aaronmurniadi/gogige/genapi"
"github.com/aaronmurniadi/gogige/gvcp"
)
// Camera is a connected GigE Vision device with GenICam feature access.
type Camera struct {
IP string
gvcp *gvcp.GVCP
nodes *genapi.NodeMap
log Logger
mu sync.Mutex
closed bool
}
// New builds a Camera from an already-connected GVCP client and NodeMap.
func New(ip string, g *gvcp.GVCP, nodes *genapi.NodeMap, log Logger) *Camera {
if log == nil {
log = NopLogger{}
}
return &Camera{IP: ip, gvcp: g, nodes: nodes, log: log}
}
// Connect opens GVCP to the camera at ip, takes control, and loads GenICam XML.
func Connect(ip string) (*Camera, error) {
if ip == "" {
return nil, errors.New("gige: ip address must not be empty")
}
g, err := gvcp.DialGVCP(ip, 2*time.Second)
if err != nil {
return nil, err
}
if err := g.TakeControl(); err != nil {
_ = g.Close()
return nil, fmt.Errorf("gige: take control: %w", err)
}
xmlData, err := genapi.FetchXML(g)
if err != nil {
_ = g.LeaveControl()
_ = g.Close()
return nil, fmt.Errorf("gige: fetch XML: %w", err)
}
nm, err := genapi.ParseNodeMap(xmlData, g)
if err != nil {
_ = g.LeaveControl()
_ = g.Close()
return nil, err
}
// Refresh CCP after potentially long XML fetch (heartbeat window).
if err := g.TakeControl(); err != nil {
_ = g.Close()
return nil, fmt.Errorf("gige: re-take control: %w", err)
}
return New(ip, g, nm, NopLogger{}), nil
}
// Close releases control and the GVCP socket.
func (c *Camera) Close() {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
c.closed = true
if c.gvcp == nil {
return
}
_ = c.gvcp.LeaveControl()
_ = c.gvcp.Close()
c.gvcp = nil
}
// GVCP returns the underlying control client.
func (c *Camera) GVCP() *gvcp.GVCP {
if c == nil {
return nil
}
return c.gvcp
}
// Logger returns the camera logger (never nil).
func (c *Camera) Logger() Logger {
if c == nil || c.log == nil {
return NopLogger{}
}
return c.log
}
// NodeMap returns the loaded GenICam map.
func (c *Camera) NodeMap() *genapi.NodeMap { return c.nodes }
// Has reports whether a GenICam feature exists (gvcp.Commander).
func (c *Camera) Has(name string) bool {
return c != nil && c.nodes != nil && c.nodes.Has(name)
}
// SetInteger sets an integer GenICam feature.
func (c *Camera) SetInteger(name string, v int64) error {
return c.nodes.SetInteger(name, v)
}
// Integer reads the current value of an integer GenICam feature.
func (c *Camera) Integer(name string) (int64, error) {
return c.nodes.ReadInteger(name)
}
// SetBoolean sets a boolean GenICam feature.
func (c *Camera) SetBoolean(name string, v bool) error {
return c.nodes.SetBoolean(name, v)
}
// Boolean reads the current value of a boolean GenICam feature.
func (c *Camera) Boolean(name string) (bool, error) {
return c.nodes.ReadBoolean(name)
}
// SetFloat sets a float GenICam feature.
func (c *Camera) SetFloat(name string, v float64) error {
return c.nodes.SetFloat(name, v)
}
// Float reads the current value of a float GenICam feature.
func (c *Camera) Float(name string) (float64, error) {
if c == nil || c.nodes == nil {
return 0, errors.New("gige: camera not connected")
}
return c.nodes.ReadFloat(name)
}
// SetString sets a string or enumeration GenICam feature.
func (c *Camera) SetString(name, v string) error {
return c.nodes.SetString(name, v)
}
// SetEnum sets an enumeration GenICam feature.
func (c *Camera) SetEnum(name, value string) error {
return c.nodes.SetString(name, value)
}
// Enum reads the current enumeration symbol of an enumeration GenICam feature.
func (c *Camera) Enum(name string) (string, error) {
if c == nil || c.nodes == nil {
return "", errors.New("gige: camera not connected")
}
return c.nodes.CurrentEnum(name)
}
// String reads the current string value of a string GenICam feature.
func (c *Camera) String(name string) (string, error) {
if c == nil || c.nodes == nil {
return "", errors.New("gige: camera not connected")
}
return c.nodes.ReadString(name)
}
// ExecuteCommand executes a GenICam command node.
func (c *Camera) ExecuteCommand(name string) error {
return c.nodes.Execute(name)
}
// Execute implements gvcp.Commander.
func (c *Camera) Execute(name string) error { return c.ExecuteCommand(name) }
// Features returns a Features view backed by this Camera, so callers that
// already hold a *Camera can use the same feature accessors as a Device.
func (c *Camera) Features() Features { return cameraFeatures{c: c} }
// GrabSample opens a transient GVSP stream on the camera, picks one frame, and
// returns it as a Sample with a JPEG for the requested component. comp of
// ComponentUnknown selects ComponentColor. Closes the stream before returning.
// Prefer StartStream for continuous capture; this is for one-off grabs from a
// Camera you already hold.
func (c *Camera) GrabSample(ctx context.Context, comp Component) (Sample, error) {
if c == nil {
return Sample{PackCount: -1}, errors.New("gige: nil camera")
}
s := NewFromCamera(c)
if comp != ComponentUnknown {
s.component = comp
}
if err := s.Open(c.IP); err != nil {
return Sample{PackCount: -1}, err
}
defer s.Close()
return s.Grab(ctx)
}
// GrabAllSamples opens a transient GVSP stream on the camera, picks one frame,
// and returns a Sample for every BSCF component (color, depth, mono, …).
func (c *Camera) GrabAllSamples(ctx context.Context) ([]Sample, error) {
if c == nil {
return nil, errors.New("gige: nil camera")
}
s := NewFromCamera(c)
if err := s.Open(c.IP); err != nil {
return nil, err
}
defer s.Close()
return s.GrabAll(ctx)
}
// GrabComponents opens a transient GVSP stream on the camera and returns one
// frame's BSCF components without JPEG encoding (raw Data, dimensions, pixel
// format).
func (c *Camera) GrabComponents(ctx context.Context) ([]Sample, error) {
if c == nil {
return nil, errors.New("gige: nil camera")
}
s := NewFromCamera(c)
if err := s.Open(c.IP); err != nil {
return nil, err
}
defer s.Close()
return s.GrabComponents(ctx)
}
// GrabJPEG opens a transient GVSP stream on the camera, picks one frame, and
// returns the JPEG bytes for the requested component (default color).
func (c *Camera) GrabJPEG(ctx context.Context, comp Component) ([]byte, error) {
sample, err := c.GrabSample(ctx, comp)
if err != nil {
return nil, err
}
if len(sample.JPEG) == 0 {
return nil, errors.New("gige: grab: empty frame")
}
return sample.JPEG, nil
}
// The following *Feature getters/setters are the pre-Phase-4 names, kept as
// thin aliases so existing code keeps compiling. Prefer the short forms
// (SetInteger, Integer, SetEnum, Enum, SetBool, Bool, SetFloat, Float,
// SetString, String) documented on Camera.
// SetBooleanFeature sets a boolean GenICam feature (alias for SetBoolean).
func (c *Camera) SetBooleanFeature(name string, v bool) error {
return c.SetBoolean(name, v)
}
// BooleanFeature reads a boolean GenICam feature (alias for Boolean).
func (c *Camera) BooleanFeature(name string) (bool, error) {
return c.Boolean(name)
}
// SetIntFeature sets an integer GenICam feature (alias for SetInteger).
func (c *Camera) SetIntFeature(name string, v int64) error {
return c.SetInteger(name, v)
}
// SetFloatFeature sets a float GenICam feature (alias for SetFloat).
func (c *Camera) SetFloatFeature(name string, v float64) error {
return c.SetFloat(name, v)
}
// SetStringFeature sets a string or enumeration GenICam feature (alias for
// SetString/SetEnum).
func (c *Camera) SetStringFeature(name, v string) error {
return c.SetString(name, v)
}
// ApplyControlPair sets one GenICam feature from "Name=value" string.
func ApplyControlPair(c *Camera, pair string) error {
eq := strings.IndexByte(pair, '=')
if eq <= 0 || eq == len(pair)-1 {
return fmt.Errorf("gige: bad control pair %q", pair)
}
feature := pair[:eq]
val := pair[eq+1:]
var err error
switch strings.ToLower(val) {
case "true":
err = c.SetBooleanFeature(feature, true)
case "false":
err = c.SetBooleanFeature(feature, false)
default:
if i, perr := strconv.ParseInt(val, 10, 64); perr == nil && strconv.FormatInt(i, 10) == val {
err = c.SetIntFeature(feature, i)
} else if f, perr := strconv.ParseFloat(val, 64); perr == nil && strings.ContainsAny(val, ".eE") {
err = c.SetFloatFeature(feature, f)
} else {
err = c.SetStringFeature(feature, val)
}
}
if err != nil {
return fmt.Errorf("gige: control %s: %w", pair, err)
}
return nil
}
var _ gvcp.Commander = (*Camera)(nil)