|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + |
| 12 | + device "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver10/device/wsdl" |
| 13 | + analytics "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver20/analytics/wsdl" |
| 14 | + imaging "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver20/imaging/wsdl" |
| 15 | + media2 "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver20/media/wsdl" |
| 16 | + ptz "github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver20/ptz/wsdl" |
| 17 | + "github.com/hooklift/gowsdl/soap" |
| 18 | + "github.com/motemen/go-loghttp" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + ErrServiceNotSupported = errors.New("onvif service not supported") |
| 23 | + |
| 24 | + verboseHTTPClient = &http.Client{ |
| 25 | + Transport: &loghttp.Transport{ |
| 26 | + LogResponse: logResponse, |
| 27 | + LogRequest: logRequest, |
| 28 | + }, |
| 29 | + } |
| 30 | +) |
| 31 | + |
| 32 | +type Client interface { |
| 33 | + Analytics() (analytics.AnalyticsEnginePort, error) |
| 34 | + Device() (device.Device, error) |
| 35 | + Imaging() (imaging.ImagingPort, error) |
| 36 | + Media2() (media2.Media2, error) |
| 37 | + PTZ() (ptz.PTZ, error) |
| 38 | +} |
| 39 | + |
| 40 | +type impl struct { |
| 41 | + analytics analytics.AnalyticsEnginePort |
| 42 | + device device.Device |
| 43 | + imaging imaging.ImagingPort |
| 44 | + media2 media2.Media2 |
| 45 | + ptz ptz.PTZ |
| 46 | +} |
| 47 | + |
| 48 | +func New(baseURL, username, password string, verbose bool) (Client, error) { |
| 49 | + soapClient, err := serviceSOAPClient(baseURL, "onvif/device_service", username, password, verbose) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + d := device.NewDevice(soapClient) |
| 54 | + resp, err := d.GetServices(&device.GetServices{}) |
| 55 | + if err != nil { |
| 56 | + return nil, fmt.Errorf("listing available Onvif services: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + var result impl |
| 60 | + for _, svc := range resp.Service { |
| 61 | + svcClient, err := serviceSOAPClient(baseURL, svc.XAddr, username, password, verbose) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + if svc.Namespace == "http://www.onvif.org/ver20/analytics/wsdl" { |
| 66 | + result.analytics = analytics.NewAnalyticsEnginePort(svcClient) |
| 67 | + } |
| 68 | + if svc.Namespace == "http://www.onvif.org/ver10/device/wsdl" { |
| 69 | + result.device = device.NewDevice(svcClient) |
| 70 | + } |
| 71 | + if svc.Namespace == "http://www.onvif.org/ver20/imaging/wsdl" { |
| 72 | + result.imaging = imaging.NewImagingPort(svcClient) |
| 73 | + } |
| 74 | + if svc.Namespace == "http://www.onvif.org/ver20/media/wsdl" { |
| 75 | + result.media2 = media2.NewMedia2(svcClient) |
| 76 | + } |
| 77 | + if svc.Namespace == "http://www.onvif.org/ver20/ptz/wsdl" { |
| 78 | + result.ptz = ptz.NewPTZ(svcClient) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return &result, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (c *impl) Analytics() (analytics.AnalyticsEnginePort, error) { |
| 86 | + if c.analytics == nil { |
| 87 | + return nil, ErrServiceNotSupported |
| 88 | + } |
| 89 | + return c.analytics, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (c *impl) Device() (device.Device, error) { |
| 93 | + if c.device == nil { |
| 94 | + return nil, ErrServiceNotSupported |
| 95 | + } |
| 96 | + return c.device, nil |
| 97 | +} |
| 98 | + |
| 99 | +func (c *impl) Imaging() (imaging.ImagingPort, error) { |
| 100 | + if c.imaging == nil { |
| 101 | + return nil, ErrServiceNotSupported |
| 102 | + } |
| 103 | + return c.imaging, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (c *impl) Media2() (media2.Media2, error) { |
| 107 | + if c.media2 == nil { |
| 108 | + return nil, ErrServiceNotSupported |
| 109 | + } |
| 110 | + return c.media2, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (c *impl) PTZ() (ptz.PTZ, error) { |
| 114 | + if c.ptz == nil { |
| 115 | + return nil, ErrServiceNotSupported |
| 116 | + } |
| 117 | + return c.ptz, nil |
| 118 | +} |
| 119 | + |
| 120 | +func serviceURL(baseURL, suffix string) (string, error) { |
| 121 | + base, err := url.Parse(baseURL) |
| 122 | + if err != nil { |
| 123 | + return "", fmt.Errorf("malformed base URL: %w", err) |
| 124 | + } |
| 125 | + u, err := url.Parse(suffix) |
| 126 | + if err != nil { |
| 127 | + return "", fmt.Errorf("malformed service suffix URL: %w", err) |
| 128 | + } |
| 129 | + return base.ResolveReference(u).String(), nil |
| 130 | +} |
| 131 | + |
| 132 | +func sanitizeServiceURL(baseURL, advertisedURL string) (string, error) { |
| 133 | + u, err := url.Parse(advertisedURL) |
| 134 | + if err != nil { |
| 135 | + return "", fmt.Errorf("malformed service advertised URL: %w", err) |
| 136 | + } |
| 137 | + return serviceURL(baseURL, u.Path) |
| 138 | +} |
| 139 | + |
| 140 | +func serviceSOAPClient(baseURL, advertisedURL, username, password string, verbose bool) (*soap.Client, error) { |
| 141 | + u, err := sanitizeServiceURL(baseURL, advertisedURL) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + return AuthorizedSOAPClient(u, username, password, verbose), nil |
| 146 | +} |
| 147 | + |
| 148 | +func AuthorizedSOAPClient(serviceURL, username, password string, verbose bool) *soap.Client { |
| 149 | + httpClient := http.DefaultClient |
| 150 | + if verbose { |
| 151 | + httpClient = verboseHTTPClient |
| 152 | + } |
| 153 | + client := soap.NewClient(serviceURL, soap.WithHTTPClient(httpClient)) |
| 154 | + client.SetHeaders(soap.NewSecurity(username, password)) |
| 155 | + return client |
| 156 | +} |
| 157 | + |
| 158 | +func logResponse(resp *http.Response) { |
| 159 | + log.Printf("<-- %d %s", resp.StatusCode, resp.Request.URL) |
| 160 | + defer resp.Body.Close() |
| 161 | + body, _ := io.ReadAll(resp.Body) |
| 162 | + log.Printf("BODY:\n%s", string(body)) |
| 163 | + resp.Body = io.NopCloser(bytes.NewReader(body)) |
| 164 | +} |
| 165 | + |
| 166 | +func logRequest(req *http.Request) { |
| 167 | + log.Printf("--> %s %s", req.Method, req.URL) |
| 168 | + defer req.Body.Close() |
| 169 | + body, _ := io.ReadAll(req.Body) |
| 170 | + log.Printf("BODY:\n%s", string(body)) |
| 171 | + req.Body = io.NopCloser(bytes.NewReader(body)) |
| 172 | +} |
0 commit comments