From bd3ab9cc97eede6ef1b4be07c55b98a22b2b07e4 Mon Sep 17 00:00:00 2001 From: Ondrej Sery Date: Wed, 6 Sep 2023 11:26:08 +0200 Subject: [PATCH] Add device set-system-factory-default command. --- cmd/gonvif/device/cmd.go | 1 + .../device/set-system-factory-default.go | 46 +++++++++++++++++++ pkg/gonvif/client.go | 30 ++++++++---- 3 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 cmd/gonvif/device/set-system-factory-default.go diff --git a/cmd/gonvif/device/cmd.go b/cmd/gonvif/device/cmd.go index f09bdeb..26a3eae 100644 --- a/cmd/gonvif/device/cmd.go +++ b/cmd/gonvif/device/cmd.go @@ -20,6 +20,7 @@ func init() { getDeviceInformation, getNetworkInterfaces, getServices, + setSystemFactoryDefault, systemReboot, ) } diff --git a/cmd/gonvif/device/set-system-factory-default.go b/cmd/gonvif/device/set-system-factory-default.go new file mode 100644 index 0000000..0fb5849 --- /dev/null +++ b/cmd/gonvif/device/set-system-factory-default.go @@ -0,0 +1,46 @@ +package device + +import ( + "github.com/spf13/cobra" + + "github.com/eyetowers/gonvif/cmd/gonvif/root" + "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver10/device/wsdl" + "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver10/schema" +) + +var ( + hard bool +) + +var setSystemFactoryDefault = &cobra.Command{ + Use: "set-system-factory-default", + Short: "Set Onvif device system factory default", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + client, err := ServiceClient(root.URL, root.Username, root.Password, root.Verbose) + if err != nil { + return err + } + return runSetSystemFactoryDefault(client, hard) + }, +} + +func init() { + setSystemFactoryDefault.Flags().BoolVarP(&hard, "hard", "", false, + "Require a hard factory reset which will also reset camera network settings and thus its reachability.") +} + +func runSetSystemFactoryDefault(client wsdl.Device, hard bool) error { + reset := schema.FactoryDefaultTypeSoft + if hard { + reset = schema.FactoryDefaultTypeHard + } + + resp, err := client.SetSystemFactoryDefault(&wsdl.SetSystemFactoryDefault{ + FactoryDefault: &reset, + }) + if err != nil { + return err + } + return root.OutputJSON(resp) +} diff --git a/pkg/gonvif/client.go b/pkg/gonvif/client.go index 898f5e9..64b37a1 100644 --- a/pkg/gonvif/client.go +++ b/pkg/gonvif/client.go @@ -211,16 +211,30 @@ func AuthorizedSOAPClient(serviceURL, username, password string, verbose bool) * func logResponse(resp *http.Response) { log.Printf("<-- %d %s", resp.StatusCode, resp.Request.URL) - defer resp.Body.Close() - body, _ := io.ReadAll(resp.Body) - log.Printf("BODY:\n%s", string(body)) - resp.Body = io.NopCloser(bytes.NewReader(body)) + resp.Body = logBodyAndHeaders(resp.Body, resp.Header) } func logRequest(req *http.Request) { log.Printf("--> %s %s", req.Method, req.URL) - defer req.Body.Close() - body, _ := io.ReadAll(req.Body) - log.Printf("BODY:\n%s", string(body)) - req.Body = io.NopCloser(bytes.NewReader(body)) + req.Body = logBodyAndHeaders(req.Body, req.Header) +} + +func logBodyAndHeaders(body io.ReadCloser, headers http.Header) io.ReadCloser { + defer body.Close() + bytes, copy := readAndDuplicate(body) + log.Printf("HEADERS:\n") + for k, vals := range headers { + for _, val := range vals { + log.Printf("%s: %s\n", k, val) + } + } + log.Printf("BODY:\n%s", string(bytes)) + return copy +} + +func readAndDuplicate(body io.ReadCloser) ([]byte, io.ReadCloser) { + defer body.Close() + buf, _ := io.ReadAll(body) + + return buf, io.NopCloser(bytes.NewReader(buf)) }