-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfi9800p.go
123 lines (102 loc) · 3.77 KB
/
fi9800p.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 foscam
import (
"encoding/xml"
"fmt"
"github.com/google/go-querystring/query"
)
// fi9800p is a camera Foscam FI9800P.
// We don't need to export this struct since we are using an interface factory.
// see the file foscam.go for more details
type fi9800p struct {
Client HTTPClient `url:"-"`
// The go-querystring values may change from camera to camera, so we can't
// use Config (from foscam.go) directly here.
URL string `url:"-"`
User string `url:"usr"`
Password string `url:"pwd"`
}
type fi9800pMotion struct {
XMLName xml.Name `xml:"CGI_Result" url:"-"`
IsEnable uint8 `xml:"isEnable" url:"isEnable"`
Linkage uint8 `xml:"linkage" url:"linkage"`
SnapInterval uint8 `xml:"snapInterval" url:"snapInterval"`
Sensitivity uint8 `xml:"sensitivity" url:"sensitivity"`
TriggerInterval uint8 `xml:"triggerInterval" url:"triggerInterval"`
IsMovAlarmEnable uint8 `xml:"isMovAlarmEnable" url:"isMovAlarmEnable"`
IsPirAlarmEnable uint8 `xml:"isPirAlarmEnable" url:"isPirAlarmEnable"`
Schedule0 uint64 `xml:"schedule0" url:"schedule0"`
Schedule1 uint64 `xml:"schedule1" url:"schedule1"`
Schedule2 uint64 `xml:"schedule2" url:"schedule2"`
Schedule3 uint64 `xml:"schedule3" url:"schedule3"`
Schedule4 uint64 `xml:"schedule4" url:"schedule4"`
Schedule5 uint64 `xml:"schedule5" url:"schedule5"`
Schedule6 uint64 `xml:"schedule6" url:"schedule6"`
Area0 uint16 `xml:"area0" url:"area0"`
Area1 uint16 `xml:"area1" url:"area1"`
Area2 uint16 `xml:"area2" url:"area2"`
Area3 uint16 `xml:"area3" url:"area3"`
Area4 uint16 `xml:"area4" url:"area4"`
Area5 uint16 `xml:"area5" url:"area5"`
Area6 uint16 `xml:"area6" url:"area6"`
Area7 uint16 `xml:"area7" url:"area7"`
Area8 uint16 `xml:"area8" url:"area8"`
Area9 uint16 `xml:"area9" url:"area9"`
}
type fi9800pResponse struct {
XMLName xml.Name `xml:"CGI_Result"`
Result int `xml:"result"`
}
// updateMotionDetect updates the motion detection configuration.
func (c *fi9800p) updateMotionDetect(mc fi9800pMotion) error {
// Construct the URL
qc, _ := query.Values(c) // Credentials
qm, _ := query.Values(mc) // Motion Config
url := fmt.Sprintf("%s/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&%s&%s", c.URL, qm.Encode(), qc.Encode())
b, err := getRequest(c.Client, url)
if err != nil {
return err
}
var mr fi9800pResponse
if err := xml.Unmarshal(b, &mr); err != nil {
return err
}
if mr.Result != 0 {
return &BadResponseError{Want: 0, Got: mr.Result}
}
return nil
}
// GetMotionDetect retrieves the motion detection configuration.
func (c *fi9800p) GetMotionDetect() (fi9800pMotion, error) {
var mc fi9800pMotion
// Construct the URL
q, _ := query.Values(c)
url := fmt.Sprintf("%s/cgi-bin/CGIProxy.fcgi?cmd=getMotionDetectConfig&%s", c.URL, q.Encode())
b, err := getRequest(c.Client, url)
if err != nil {
return mc, err
}
if err = xml.Unmarshal(b, &mc); err != nil {
return mc, err
}
return mc, nil
}
// ChangeMotionStatus enables/disables the camera motion detection.
func (c *fi9800p) ChangeMotionStatus(enable bool) error {
mc, err := c.GetMotionDetect()
if err != nil {
return err
}
// If the camera status is already the desired, don't send the request
if mc.IsEnable == b2u(enable) {
return nil
}
mc.IsEnable = b2u(enable)
return c.updateMotionDetect(mc)
}
// SnapPicture takes a snapshot and returns the picture in a byte slice.
func (c *fi9800p) SnapPicture() ([]byte, error) {
// Construct the URL
q, _ := query.Values(c)
url := fmt.Sprintf("%s/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&%s", c.URL, q.Encode())
return getSnap(c.Client, url)
}