1
0
Fork 0
hats/pkg/amcrest/api.go

192 lines
4.8 KiB
Go
Raw Permalink Normal View History

2023-12-14 16:07:25 +00:00
package amcrest
import (
"errors"
"fmt"
2023-12-14 21:25:16 +00:00
"io"
"net/http"
2023-12-14 16:07:25 +00:00
"strconv"
"strings"
2023-12-14 21:25:16 +00:00
"time"
2023-12-14 16:07:25 +00:00
"code.jhot.me/jhot/hats/internal/util"
"github.com/go-resty/resty/v2"
2023-12-14 21:25:16 +00:00
"github.com/icholy/digest"
"github.com/samber/lo"
2023-12-14 16:07:25 +00:00
)
type AmcrestClient struct {
2023-12-14 21:25:16 +00:00
rawClient *http.Client
2023-12-14 16:07:25 +00:00
restClient *resty.Client
2023-12-14 21:25:16 +00:00
host string
user string
pass string
2023-12-14 16:07:25 +00:00
}
func New(host string, username string, password string) *AmcrestClient {
return &AmcrestClient{
2023-12-14 21:25:16 +00:00
rawClient: &http.Client{Timeout: 30 * time.Second, Transport: &digest.Transport{
Username: username,
Password: password,
}},
2023-12-14 16:07:25 +00:00
restClient: resty.New().SetBaseURL(host).SetDigestAuth(username, password),
2023-12-14 21:25:16 +00:00
host: host,
user: username,
pass: password,
2023-12-14 16:07:25 +00:00
}
}
type GetSnapshotResponse struct {
MimeType string
Data []byte
}
// Get a snapshot from the specified channel
//
// Parameter
//
// channel int (optional): the channel index (starts at 1)
func (c *AmcrestClient) GetSnapshot(channel ...int) (GetSnapshotResponse, error) {
2023-12-14 18:27:30 +00:00
req := c.restClient.R()
2023-12-14 16:07:25 +00:00
if len(channel) > 0 && channel[0] != 0 {
2023-12-14 18:27:30 +00:00
req.SetQueryParam("channel", fmt.Sprintf("%d", channel[0]))
2023-12-14 16:07:25 +00:00
}
2023-12-14 18:27:30 +00:00
resp, err := util.CheckSuccess(req.Get("cgi-bin/snapshot.cgi"))
2023-12-14 16:07:25 +00:00
if err != nil {
return GetSnapshotResponse{}, err
}
return GetSnapshotResponse{
MimeType: resp.Header().Get("content-type"),
Data: resp.Body(),
}, nil
}
type ConfigValue struct {
// Category[Channel].FormatType[FormatEncodeType].Param=Value
Category string
Channel int // The channel index (starts at 0)
FormatType string
FormatEncodeType int
Param string
Value any
}
func (c ConfigValue) ToParamString() string {
return fmt.Sprintf("%s[%d].%s[%d].%s", c.Category, c.Channel, c.FormatType, c.FormatEncodeType, c.Param)
}
// Parse config values from a string
//
// input: a string like "Category[Channel].FormatType[FormatEncodeType].Param.Name=Value"
func ConfigValueFromString(input string) (ConfigValue, error) {
output := ConfigValue{}
values := strings.SplitN(input, "=", 2)
if len(values) > 1 {
output.Value = values[1]
}
sections := strings.SplitN(values[0], ".", 3)
for i, section := range sections {
if i < 2 {
parts := strings.Split(section, "[")
var val int64 = 0
if len(parts) > 1 {
var err error
val, err = strconv.ParseInt(strings.TrimSuffix(parts[1], "]"), 10, 64)
if err != nil {
return output, fmt.Errorf("error parsing integer from string: %w", err)
}
}
switch i {
case 0:
output.Category = parts[0]
output.Channel = int(val)
case 1:
output.FormatType = parts[0]
output.FormatEncodeType = int(val)
}
} else {
output.Param = section
}
}
return output, nil
}
func (c *AmcrestClient) SetConfig(inputs ...ConfigValue) error {
if len(inputs) == 0 {
return errors.New("no inputs provided")
}
2023-12-14 21:25:16 +00:00
// Have to use net/http so the raw query can be set without url encoding the value(s)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/cgi-bin/configManager.cgi", c.host), nil)
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
formattedInputs := lo.Map[ConfigValue, string](inputs, func(input ConfigValue, _ int) string {
return fmt.Sprintf("%s=%v", input.ToParamString(), input.Value)
})
req.URL.RawQuery = fmt.Sprintf("action=setConfig&%s", strings.Join(formattedInputs, "&"))
resp, err := c.rawClient.Do(req)
if err != nil {
return fmt.Errorf("error making request: %w", err)
}
2023-12-14 16:07:25 +00:00
2023-12-14 21:25:16 +00:00
defer resp.Body.Close()
if resp.StatusCode > 299 {
body, _ := io.ReadAll(resp.Body)
err = fmt.Errorf("%d status code received: %s", resp.StatusCode, string(body))
2023-12-14 16:07:25 +00:00
}
return err
}
func (c *AmcrestClient) GetEncodingConfigCapabilities() ([]ConfigValue, error) {
req := c.restClient.R().SetQueryParam("action", "getConfigCaps")
resp, err := util.CheckSuccess(req.Get("cgi-bin/encode.cgi"))
values := []ConfigValue{}
if err != nil {
return values, err
}
for _, line := range strings.Split(string(resp.Body()), "\n") {
val, err := ConfigValueFromString(line)
if err != nil {
return values, fmt.Errorf("error parsing line: %w", err)
}
values = append(values, val)
}
return values, nil
}
func (c *AmcrestClient) GetConfig(configName string) ([]ConfigValue, error) {
resp, err := util.CheckSuccess(c.restClient.R().
SetQueryParam("action", "getConfig").SetQueryParam("name", configName).
Get("cgi-bin/configManager.cgi"))
values := []ConfigValue{}
if err != nil {
return values, err
}
for _, line := range strings.Split(strings.ReplaceAll(string(resp.Body()), "\r\n", "\n"), "\n") {
if line == "" {
continue
}
val, err := ConfigValueFromString(strings.TrimPrefix(line, "table."))
if err != nil {
return values, fmt.Errorf("error parsing line: %w", err)
}
values = append(values, val)
}
return values, nil
}