dahuarpc

package
v0.0.0-...-72e33f6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package dahuarpc is a client library for Dahua's RPC API.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLoginUserOrPasswordNotValid = newLoginError("User or password not valid")
	ErrLoginUserNotValid           = newLoginError("User not valid")
	ErrLoginPasswordNotValid       = newLoginError("Password not valid")
	ErrLoginInBlackList            = newLoginError("User in blackList")
	ErrLoginHasBeedUsed            = newLoginError("User has be used")
	ErrLoginHasBeenLocked          = newLoginError("User locked")
)
View Source
var (
	ErrClientClosed = errors.New("client closed")
)
View Source
var (
	ErrRequestFailed = errors.New("request failed")
)

Functions

func Cookie(session string) string

Cookie creates a session cookie.

func DoRaw

func DoRaw(ctx context.Context, rb RequestBuilder, httpClient *http.Client, urL string) (io.ReadCloser, error)

DoRaw executes the RPC request.

func ExtractFilePathTags

func ExtractFilePathTags(filePath string) []string

ExtractFilePathTags extracts tags from the file path. Tags are strings surrounded by brackets

func GetCurrentTime

func GetCurrentTime(ctx context.Context, c Conn) (string, error)

func KeepAlive

func KeepAlive(ctx context.Context, c Conn) (int, error)

func LoadFileURL

func LoadFileURL(u *url.URL, filePath string) string

LoadFileURL is the HTTP URL for accessing files. The file path must be absolute.

func Login

func Login(ctx context.Context, conn ConnLogin, username, password string) error

func LoginURL

func LoginURL(u *url.URL) string

LoginURL is the HTTP RPC API URL for login.

func Logout

func Logout(ctx context.Context, c Conn) (bool, error)

func SecondLogin

func SecondLogin(ctx context.Context, c Conn, username, password, loginType, authorityType string) error

func URL

func URL(u *url.URL) string

URL is the HTTP RPC API URL.

Types

type AuthParam

type AuthParam struct {
	Encryption string `json:"encryption"`
	Random     string `json:"random"`
	Realm      string `json:"realm"`
}

func (AuthParam) HashPassword

func (a AuthParam) HashPassword(username, password string) string

HashPassword runs the hashing algorithm for the password.

type Cache

type Cache map[string]Response[json.RawMessage]

Cache caches RPC calls.

func NewCache

func NewCache() Cache

func (Cache) Send

func (c Cache) Send(ctx context.Context, conn Conn, key string, rb RequestBuilder) (Response[json.RawMessage], error)

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(httpClient *http.Client, u *url.URL, username, password string, configFuncs ...ConfigFunc) Client

func (Client) Close

func (c Client) Close(ctx context.Context) error

Close closes the connection.

func (Client) CloseNoWait

func (c Client) CloseNoWait(ctx context.Context) error

CloseNoWait closes the connection without waiting for it to finish closing.

func (Client) Do

func (Client) Session

func (c Client) Session(ctx context.Context) string

func (Client) State

func (c Client) State(ctx context.Context) ClientState

type ClientState

type ClientState struct {
	State     State
	LastID    int
	Error     error
	LastLogin time.Time
	LastRPC   time.Time
}

type Config

type Config struct {
	// contains filtered or unexported fields
}

type ConfigFunc

type ConfigFunc func(c *Config)

func WithContext

func WithContext(ctx context.Context) ConfigFunc

func WithOnError

func WithOnError(fn func(err error)) ConfigFunc

type Conn

type Conn interface {
	Do(ctx context.Context, rb RequestBuilder) (io.ReadCloser, error)
}

Conn is the base connection.

type ConnLogin

type ConnLogin interface {
	Conn
	SetSession(session string)
}

ConnLogin is a connection used to login.

type ConnSession

type ConnSession interface {
	Conn
	Session(ctx context.Context) string
}

ConnSession is a connection with a session.

type ErrorType

type ErrorType string
var (
	ErrorTypeInvalidSession    ErrorType = "InvalidSession"
	ErrorTypeInvalidRequest    ErrorType = "InvalidRequest"
	ErrorTypeMethodNotFound    ErrorType = "MethodNotFound"
	ErrorTypeInterfaceNotFound ErrorType = "InterfaceNotFound"
	ErrorTypeNoData            ErrorType = "NoData"
	ErrorTypeUnknown           ErrorType = "Unknown"
)

type FileClient

type FileClient struct {
	// contains filtered or unexported fields
}

FileClient handles file access to prevent "Resource is limited, open video failed!" errors. The client limits the number of concurrent requests and makes sure the body is completely drained.

func NewFileClient

func NewFileClient(client *http.Client, concurrent int) FileClient

func (FileClient) Close

func (c FileClient) Close()

func (FileClient) Do

func (c FileClient) Do(ctx context.Context, urL, cookie string) (io.ReadCloser, error)

type Integer

type Integer int64

Integer is for types that are supposed to be integers but for some reason the device returns a float.

func (Integer) Integer

func (s Integer) Integer() int64

func (*Integer) UnmarshalJSON

func (s *Integer) UnmarshalJSON(data []byte) error

type LoginError

type LoginError struct {
	Message string
}

func (*LoginError) Error

func (e *LoginError) Error() string

type Request

type Request struct {
	ID      int
	Session string
	Method  string
	Params  any
	Object  int64
	// contains filtered or unexported fields
}

func (Request) MarshalJSON

func (r Request) MarshalJSON() ([]byte, error)

type RequestBuilder

type RequestBuilder struct {
	Login   bool
	Request Request
}

func New

func New(method string) RequestBuilder

func NewLogin

func NewLogin(method string) RequestBuilder

func (RequestBuilder) ID

func (rb RequestBuilder) ID(id int) RequestBuilder

func (RequestBuilder) Object

func (rb RequestBuilder) Object(object int64) RequestBuilder

func (RequestBuilder) Option

func (rb RequestBuilder) Option(key string, value any) RequestBuilder

func (RequestBuilder) Params

func (rb RequestBuilder) Params(params any) RequestBuilder

func (RequestBuilder) Session

func (rb RequestBuilder) Session(session string) RequestBuilder

type Response

type Response[T any] struct {
	ID      int             `json:"id"`
	Session ResponseSession `json:"session"`
	Error   *ResponseError  `json:"error"`
	Params  T               `json:"params"`
	Result  ResponseResult  `json:"result"`
}

func FirstLogin

func FirstLogin(ctx context.Context, c Conn, username string) (Response[AuthParam], error)

func Send

func Send[T any](ctx context.Context, c Conn, rb RequestBuilder) (Response[T], error)

Send sends the RPC request and checks the response's error field.

func SendRaw

func SendRaw[T any](ctx context.Context, c Conn, rb RequestBuilder) (Response[T], error)

SendRaw sends the RPC request.

type ResponseError

type ResponseError struct {
	Method  string
	Code    int
	Message string
	Type    ErrorType
}

func (*ResponseError) Error

func (r *ResponseError) Error() string

func (*ResponseError) UnmarshalJSON

func (r *ResponseError) UnmarshalJSON(data []byte) error

type ResponseResult

type ResponseResult int64

func (ResponseResult) Bool

func (s ResponseResult) Bool() bool

func (ResponseResult) Integer

func (s ResponseResult) Integer() int64

func (*ResponseResult) UnmarshalJSON

func (s *ResponseResult) UnmarshalJSON(data []byte) error

type ResponseSession

type ResponseSession string

func (ResponseSession) String

func (s ResponseSession) String() string

func (*ResponseSession) UnmarshalJSON

func (s *ResponseSession) UnmarshalJSON(data []byte) error

type State

type State int

State is the connection state.

const (
	StateLogout State = iota
	StateLogin
	StateError
	StateClosed
)

func (State) String

func (s State) String() string

type TimeSection

type TimeSection struct {
	Number int
	Start  time.Duration
	End    time.Duration
}

func DefaultTimeSection

func DefaultTimeSection() TimeSection

func NewTimeSectionFromRange

func NewTimeSectionFromRange(number int, start, end time.Time) TimeSection

func NewTimeSectionFromString

func NewTimeSectionFromString(s string) (TimeSection, error)

NewTimeSectionFromString (e.g. "1 08:01:45-16:16:22").

func (TimeSection) MarshalJSON

func (s TimeSection) MarshalJSON() ([]byte, error)

func (TimeSection) String

func (s TimeSection) String() string

func (*TimeSection) UnmarshalJSON

func (s *TimeSection) UnmarshalJSON(data []byte) error

type Timestamp

type Timestamp string

func NewTimestamp

func NewTimestamp(date time.Time, deviceLocation *time.Location) Timestamp

func (Timestamp) Parse

func (t Timestamp) Parse(deviceLocation *time.Location) (time.Time, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL