Skip to content

Commit

Permalink
Add device set-system-factory-default command.
Browse files Browse the repository at this point in the history
  • Loading branch information
osery committed Sep 6, 2023
1 parent c412a8d commit bd3ab9c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions cmd/gonvif/device/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
getDeviceInformation,
getNetworkInterfaces,
getServices,
setSystemFactoryDefault,
systemReboot,
)
}
Expand Down
46 changes: 46 additions & 0 deletions cmd/gonvif/device/set-system-factory-default.go
Original file line number Diff line number Diff line change
@@ -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)
}
30 changes: 22 additions & 8 deletions pkg/gonvif/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit bd3ab9c

Please sign in to comment.