Skip to content

Commit e5e7120

Browse files
authored
Merge pull request #21 from golift/dn2_updates
Allow cameras case insensitive, fix lint, update deps.
2 parents 87b66c0 + 0f29869 commit e5e7120

File tree

11 files changed

+81
-56
lines changed

11 files changed

+81
-56
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
GOLANGCI_ARGS=-D exhaustivestruct
1+
GOLANGCI_ARGS=-D exhaustivestruct,varnamelen,interfacer,maligned,scopelint,golint
22

33
all:
44
@echo "try: make test"

cameras.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ func (c *Cameras) ByName(name string) *Camera {
3939
}
4040
}
4141

42+
// Try again, case-insensitive.
43+
for _, cam := range c.cameras {
44+
if strings.EqualFold(cam.Name, name) {
45+
return cam
46+
}
47+
}
48+
4249
return nil
4350
}
4451

@@ -76,7 +83,7 @@ func (c *Camera) StreamVideo(ops *VidOps, length time.Duration, maxsize int64) (
7683
// SaveVideo saves a segment of video from a camera to a file using FFMPEG.
7784
func (c *Camera) SaveVideo(ops *VidOps, length time.Duration, maxsize int64, outputFile string) error {
7885
if _, err := os.Stat(outputFile); !os.IsNotExist(err) {
79-
return ErrorPathExists
86+
return ErrPathExists
8087
}
8188

8289
f := ffmpeg.Get(&ffmpeg.Config{
@@ -182,7 +189,7 @@ func (c *Camera) GetJPEG(ops *VidOps) (image.Image, error) {
182189
// VidOps defines the image size. ops.FPS is ignored.
183190
func (c *Camera) SaveJPEG(ops *VidOps, path string) error {
184191
if _, err := os.Stat(path); !os.IsNotExist(err) {
185-
return ErrorPathExists
192+
return ErrPathExists
186193
}
187194

188195
jpgImage, err := c.GetJPEG(ops)

cameras_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestAll(t *testing.T) {
4545
assert.EqualValues(2, len(cams), "the data contains two cameras, two cameras must be returned")
4646
}
4747

48-
func TestByNum(t *testing.T) { //nolint:dupl
48+
func TestByNum(t *testing.T) {
4949
t.Parallel()
5050
assert := assert.New(t)
5151

@@ -69,7 +69,7 @@ func TestByNum(t *testing.T) { //nolint:dupl
6969
assert.Nil(server.Cameras.ByNum(99), "a non-existent camera must return nil")
7070
}
7171

72-
func TestByName(t *testing.T) { //nolint:dupl
72+
func TestByName(t *testing.T) {
7373
t.Parallel()
7474
assert := assert.New(t)
7575

@@ -91,6 +91,13 @@ func TestByName(t *testing.T) { //nolint:dupl
9191
cam := server.Cameras.ByName("Porch")
9292
assert.EqualValues(1, cam.Number, "camera 1 is Porch in the test data")
9393
assert.Nil(server.Cameras.ByName("not here"), "a non-existent camera must return nil")
94+
95+
cam = server.Cameras.ByName("porch2")
96+
assert.Nil(cam, "there is no camera named porch2")
97+
98+
cam = server.Cameras.ByName("porch")
99+
assert.EqualValues(1, cam.Number, "camera 1 is Porch in the test data")
100+
assert.Nil(server.Cameras.ByName("not here"), "a non-existent camera must return nil")
94101
}
95102

96103
/* Having a comment at the end of the file like this allows commenting the whole file easily. */

events.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,30 +273,30 @@ func (e *Events) serverRefresh() {
273273
func (e *Events) UnmarshalEvent(text string) Event { // nolint:funlen,cyclop
274274
var (
275275
err error
276-
parts = strings.SplitN(text, " ", 4)
276+
parts = strings.SplitN(text, " ", 4) //nolint:gomnd
277277
newEvent = Event{Msg: parts[3], ID: -1, Time: time.Now()}
278278
// Parse the time stamp; append the Offset from ++systemInfo to get the right time-location.
279279
eventTime = fmt.Sprintf("%v%+03.0f", parts[0], e.server.Info.GmtOffset.Hours())
280280
)
281281

282282
if newEvent.When, err = time.ParseInLocation(EventTimeFormat+"-07", eventTime, time.Local); err != nil {
283283
newEvent.When = time.Now()
284-
newEvent.Errors = append(newEvent.Errors, ErrorDateParseFail)
284+
newEvent.Errors = append(newEvent.Errors, ErrDateParseFail)
285285
}
286286

287287
// Parse the ID
288288
if newEvent.ID, err = strconv.Atoi(parts[1]); err != nil {
289289
newEvent.ID = BadID
290-
newEvent.Errors = append(newEvent.Errors, ErrorIDParseFail)
290+
newEvent.Errors = append(newEvent.Errors, ErrIDParseFail)
291291
}
292292

293293
// Parse the camera number.
294294
parts[2] = strings.TrimPrefix(parts[2], "CAM")
295295
if parts[2] != "X" {
296296
if cameraNum, err := strconv.Atoi(parts[2]); err != nil {
297-
newEvent.Errors = append(newEvent.Errors, ErrorCAMParseFail)
297+
newEvent.Errors = append(newEvent.Errors, ErrCAMParseFail)
298298
} else if newEvent.Camera = e.server.Cameras.ByNum(cameraNum); newEvent.Camera == nil {
299-
newEvent.Errors = append(newEvent.Errors, ErrorCAMMissing)
299+
newEvent.Errors = append(newEvent.Errors, ErrCAMMissing)
300300
}
301301
}
302302

@@ -306,7 +306,7 @@ func (e *Events) UnmarshalEvent(text string) Event { // nolint:funlen,cyclop
306306
newEvent.Type = EventType(parts[0])
307307
// Check if the type we just converted is a known event.
308308
if name := EventName(newEvent.Type); name == "" {
309-
newEvent.Errors = append(newEvent.Errors, ErrorUnknownEvent)
309+
newEvent.Errors = append(newEvent.Errors, ErrUnknownEvent)
310310
newEvent.Type = EventUnknownEvent
311311
}
312312

@@ -370,7 +370,7 @@ func (e *Event) eventChans(chans map[EventType][]chan Event) {
370370
// scanLinesCR is a custom bufio.Scanner to read SecuritySpy eventStream.
371371
func scanLinesCR(data []byte, atEOF bool) (advance int, token []byte, err error) {
372372
if atEOF && len(data) == 0 {
373-
return 0, nil, ErrorDisconnect
373+
return 0, nil, ErrDisconnect
374374
}
375375

376376
if i := bytes.IndexByte(data, '\r'); i >= 0 {

events_types.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import (
99

1010
// This is a list of errors returned by the Events methods.
1111
var (
12-
// ErrorUnknownEvent never really returns, but will fire if SecuritySpy
12+
// ErrUnknownEvent never really returns, but will fire if SecuritySpy
1313
// adds new events this library doesn't know about.
14-
ErrorUnknownEvent = fmt.Errorf("unknown event")
15-
// ErrorCAMParseFail will return if the camera number in an event stream does not exist.
14+
ErrUnknownEvent = fmt.Errorf("unknown event")
15+
// ErrCAMParseFail will return if the camera number in an event stream does not exist.
1616
// If you see this, run Refresh() more often, or fix your flaky camera connection.
17-
ErrorCAMParseFail = fmt.Errorf("CAM parse failed")
18-
// ErrorIDParseFail will return if the camera number provided by the event stream is not a number.
17+
ErrCAMParseFail = fmt.Errorf("CAM parse failed")
18+
// ErrIDParseFail will return if the camera number provided by the event stream is not a number.
1919
// This should never happen, but future versions of SecuritySpy could trigger this if formats change.
20-
ErrorIDParseFail = fmt.Errorf("ID parse failed")
21-
// ErrorCAMMissing like the errors above should never return.
20+
ErrIDParseFail = fmt.Errorf("ID parse failed")
21+
// ErrCAMMissing like the errors above should never return.
2222
// This is triggered by a corrupted event format.
23-
ErrorCAMMissing = fmt.Errorf("camera number missing")
24-
// ErrorDateParseFail will only trigger if the time stamp format for events changes.
25-
ErrorDateParseFail = fmt.Errorf("timestamp parse failed")
26-
// ErrorDisconnect becomes the msg in a custom event when the SecSpy event stream is disconnected.
27-
ErrorDisconnect = fmt.Errorf("server connection closed")
23+
ErrCAMMissing = fmt.Errorf("camera number missing")
24+
// ErrDateParseFail will only trigger if the time stamp format for events changes.
25+
ErrDateParseFail = fmt.Errorf("timestamp parse failed")
26+
// ErrDisconnect becomes the msg in a custom event when the SecSpy event stream is disconnected.
27+
ErrDisconnect = fmt.Errorf("server connection closed")
2828
)
2929

3030
const (

files.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ const (
2727

2828
// Errors returned by the Files type methods.
2929
var (
30-
// ErrorPathExists returns when a requested write path already exists.
31-
ErrorPathExists = fmt.Errorf("cannot overwrite existing path")
32-
33-
// ErrorInvalidName returns when requesting a file download and the filename is invalid.
34-
ErrorInvalidName = fmt.Errorf("invalid file name")
30+
// ErrPathExists returns when a requested write path already exists.
31+
ErrPathExists = fmt.Errorf("cannot overwrite existing path")
32+
// ErrInvalidName returns when requesting a file download and the filename is invalid.
33+
ErrInvalidName = fmt.Errorf("invalid file name")
3534
)
3635

3736
// Files powers the Files interface.
@@ -110,13 +109,13 @@ func (f *Files) GetFile(name string) (*File, error) {
110109
}
111110

112111
if fileExtSplit := strings.Split(name, "."); len(fileExtSplit) != fileParts {
113-
return file, ErrorInvalidName
112+
return file, ErrInvalidName
114113
} else if nameDateSplit := strings.Split(fileExtSplit[0], " "); len(fileExtSplit) < fileParts {
115-
return file, ErrorInvalidName
114+
return file, ErrInvalidName
116115
} else if file.Updated, err = time.Parse(FileDateFormat, nameDateSplit[0]); err != nil {
117-
return file, ErrorInvalidName
116+
return file, ErrInvalidName
118117
} else if file.Camera = f.server.Cameras.ByName(nameDateSplit[len(nameDateSplit)-1]); file.Camera == nil {
119-
return file, ErrorCAMMissing
118+
return file, ErrCAMMissing
120119
} else if file.Link.Type = "video/quicktime"; fileExtSplit[1] == "jpg" {
121120
file.Link.Type = "image/jpeg"
122121
}
@@ -134,7 +133,7 @@ func (f *Files) GetFile(name string) (*File, error) {
134133
// Returns an error if path exists.
135134
func (f *File) Save(path string) (int64, error) {
136135
if _, err := os.Stat(path); !os.IsNotExist(err) {
137-
return 0, ErrorPathExists
136+
return 0, ErrPathExists
138137
}
139138

140139
body, err := f.Get(true)

go.mod

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
module golift.io/securityspy
22

3-
go 1.16
3+
go 1.17
44

55
require (
6-
github.com/davecgh/go-spew v1.1.1 // indirect
7-
github.com/golang/mock v1.5.0
6+
github.com/golang/mock v1.6.0
87
github.com/stretchr/testify v1.6.2-0.20201103103935-92707c0b2d50
98
golift.io/ffmpeg v1.0.1
10-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.0 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1115
)

go.sum

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
12
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
5-
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
3+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
4+
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
65
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
76
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
87
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
98
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
109
github.com/stretchr/testify v1.6.2-0.20201103103935-92707c0b2d50 h1:aQdElrdadJZjGar4PipPBSpVh3yyDIuDSaM5PbMn6o8=
1110
github.com/stretchr/testify v1.6.2-0.20201103103935-92707c0b2d50/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
1212
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1313
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
14-
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
14+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
1515
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1616
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
17+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
1718
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1820
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
1921
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
24+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
2026
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
27+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
28+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2129
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
30+
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
2231
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2332
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
33+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2434
golift.io/ffmpeg v1.0.1 h1:uZZ/HEH9bydD6vjiGdwso80U8Wd3F6o/s4HN/de+N78=
2535
golift.io/ffmpeg v1.0.1/go.mod h1:CtNdNCyVbHuMhA4RPR27UVG5ukLl1vlZjZpgNWhg+VQ=
2636
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2737
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2839
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
29-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
30-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

ptz.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88
)
99

1010
var (
11-
// ErrorPTZNotOK is returned for any command that has a successful web request,
11+
// ErrPTZNotOK is returned for any command that has a successful web request,
1212
// but the reply does not end with the word OK.
13-
ErrorPTZNotOK = fmt.Errorf("PTZ command not OK")
14-
15-
// ErrorPTZRange returns when a PTZ preset outside of 1-8 is provided.
16-
ErrorPTZRange = fmt.Errorf("PTZ preset out of range 1-8")
13+
ErrPTZNotOK = fmt.Errorf("PTZ command not OK")
14+
// ErrPTZRange returns when a PTZ preset outside of 1-8 is provided.
15+
ErrPTZRange = fmt.Errorf("PTZ preset out of range 1-8")
1716
)
1817

1918
// PTZ are what "things" a camera can do. Use the bound methods to interact
@@ -163,7 +162,7 @@ func (z *PTZ) Preset(preset PTZpreset) error {
163162
case PTZpreset8:
164163
return z.ptzReq(ptzCommandSavePreset8)
165164
default:
166-
return ErrorPTZRange
165+
return ErrPTZRange
167166
}
168167
}
169168

@@ -187,7 +186,7 @@ func (z *PTZ) PresetSave(preset PTZpreset) error {
187186
case PTZpreset8:
188187
return z.ptzReq(ptzCommandPreset8)
189188
default:
190-
return ErrorPTZRange
189+
return ErrPTZRange
191190
}
192191
}
193192

securityspy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func TestUnmarshalXMLDuration(t *testing.T) {
170170
for _, val := range good {
171171
assert.Nil(xml.Unmarshal([]byte("<tag>"+val+"</tag>"), &bit), "unmarshalling must not produce an error")
172172
assert.Equal(val, bit.Val, "the value was not unmarshalled correctly")
173-
num, err := strconv.ParseFloat(val, 10)
173+
num, err := strconv.ParseFloat(val, 64)
174174
assert.Nil(err, "must not be an error parsing test numbers")
175175
assert.Equal(num, bit.Seconds(), "the value was not unmarshalled correctly")
176176
assert.Equal(val, bit.Val, "the value was not unmarshalled correctly")

0 commit comments

Comments
 (0)