From 9756183d3b2f0db749bb07c47e2eda919680a8d6 Mon Sep 17 00:00:00 2001 From: Cedric Verstraeten Date: Thu, 16 Jan 2025 21:46:02 +0100 Subject: [PATCH 1/3] upgrade onvif dependency --- machinery/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machinery/go.mod b/machinery/go.mod index b059ebb5..9445c87e 100644 --- a/machinery/go.mod +++ b/machinery/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.5.3 github.com/kellydunn/golang-geo v0.7.0 github.com/kerberos-io/joy4 v1.0.64 - github.com/kerberos-io/onvif v0.0.16 + github.com/kerberos-io/onvif v0.0.17 github.com/minio/minio-go/v6 v6.0.57 github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/pion/rtp v1.8.10 From c4577e94b1d7a29bf6860ccb22dddf184a29e5ce Mon Sep 17 00:00:00 2001 From: Cedric Verstraeten Date: Thu, 16 Jan 2025 21:46:19 +0100 Subject: [PATCH 2/3] improve closing of responses --- machinery/src/onvif/main.go | 66 +++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/machinery/src/onvif/main.go b/machinery/src/onvif/main.go index efc3abbc..45623d77 100644 --- a/machinery/src/onvif/main.go +++ b/machinery/src/onvif/main.go @@ -222,10 +222,10 @@ func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, var b []byte if resp != nil { b, err = io.ReadAll(resp.Body) + resp.Body.Close() // Ensure the response body is closed if err != nil { log.Log.Error("onvif.ConnectToOnvifDevice(): " + err.Error()) } - resp.Body.Close() } stringBody := string(b) decodedXML, et, err := getXMLNode(stringBody, "GetCapabilitiesResponse") @@ -252,10 +252,10 @@ func GetTokenFromProfile(device *onvif.Device, profileId int) (xsdonvif.Referenc // Get Profiles resp, err := device.CallMethod(media.GetProfiles{}) if err == nil { - defer resp.Body.Close() b, err := io.ReadAll(resp.Body) if err == nil { stringBody := string(b) + resp.Body.Close() // Ensure the response body is closed decodedXML, et, err := getXMLNode(stringBody, "GetProfilesResponse") if err != nil { log.Log.Debug("onvif.GetTokenFromProfile(): " + err.Error()) @@ -288,21 +288,19 @@ func GetPTZConfigurationsFromDevice(device *onvif.Device) (ptz.GetConfigurations var b []byte if resp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } if err == nil { - if err == nil { - stringBody := string(b) - decodedXML, et, err := getXMLNode(stringBody, "GetConfigurationsResponse") - if err != nil { + stringBody := string(b) + decodedXML, et, err := getXMLNode(stringBody, "GetConfigurationsResponse") + if err != nil { + log.Log.Debug("onvif.GetPTZConfigurationsFromDevice(): " + err.Error()) + return configurations, err + } else { + if err := decodedXML.DecodeElement(&configurations, et); err != nil { log.Log.Debug("onvif.GetPTZConfigurationsFromDevice(): " + err.Error()) return configurations, err - } else { - if err := decodedXML.DecodeElement(&configurations, et); err != nil { - log.Log.Debug("onvif.GetPTZConfigurationsFromDevice(): " + err.Error()) - return configurations, err - } } } } @@ -360,7 +358,7 @@ func GetPosition(device *onvif.Device, token xsdonvif.ReferenceToken) (xsdonvif. var b []byte if resp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } if err == nil { @@ -794,7 +792,7 @@ func GetPresetsFromDevice(device *onvif.Device) ([]models.OnvifActionPreset, err var b []byte if resp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } if err == nil { stringBody := string(b) @@ -845,7 +843,7 @@ func GoToPresetFromDevice(device *onvif.Device, presetName string) error { var b []byte if resp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } if err == nil { stringBody := string(b) @@ -1004,7 +1002,7 @@ func CreatePullPointSubscription(dev *onvif.Device) (string, error) { var b2 []byte if resp != nil { b2, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed if err == nil { stringBody := string(b2) decodedXML, et, err := getXMLNode(stringBody, "CreatePullPointSubscriptionResponse") @@ -1023,19 +1021,25 @@ func CreatePullPointSubscription(dev *onvif.Device) (string, error) { } func UnsubscribePullPoint(dev *onvif.Device, pullPointAddress string) error { + // Unsubscribe from the device unsubscribe := event.Unsubscribe{} requestBody, err := xml.Marshal(unsubscribe) if err != nil { log.Log.Error("onvif.main.UnsubscribePullPoint(): " + err.Error()) } + res, err := dev.SendSoap(pullPointAddress, string(requestBody)) if err != nil { log.Log.Error("onvif.main.UnsubscribePullPoint(): " + err.Error()) } if res != nil { - _, err := io.ReadAll(res.Body) - res.Body.Close() + b, err := io.ReadAll(res.Body) + res.Body.Close() // Ensure the response body is closed + if err == nil { + stringBody := string(b) + log.Log.Debug("onvif.main.UnsubscribePullPoint(): " + stringBody) + } if err != nil { log.Log.Error("onvif.main.UnsubscribePullPoint(): " + err.Error()) } @@ -1099,7 +1103,7 @@ func GetEventMessages(dev *onvif.Device, pullPointAddress string) ([]ONVIFEvents var pullMessagesResponse event.PullMessagesResponse if res != nil { bs, err := io.ReadAll(res.Body) - res.Body.Close() + res.Body.Close() // Ensure the response body is closed if err == nil { stringBody := string(bs) decodedXML, et, err := getXMLNode(stringBody, "PullMessagesResponse") @@ -1208,7 +1212,7 @@ func GetDigitalInputs(dev *onvif.Device) (device.GetDigitalInputsResponse, error resp, err := dev.CallMethod(deviceio.GetDigitalInputs{}) if resp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } if err == nil { @@ -1240,21 +1244,19 @@ func GetRelayOutputs(dev *onvif.Device) (device.GetRelayOutputsResponse, error) var b []byte if resp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } if err == nil { - if err == nil { - stringBody := string(b) - decodedXML, et, err := getXMLNode(stringBody, "GetRelayOutputsResponse") - if err != nil { - log.Log.Error("onvif.main.GetRelayOutputs(): " + err.Error()) + stringBody := string(b) + decodedXML, et, err := getXMLNode(stringBody, "GetRelayOutputsResponse") + if err != nil { + log.Log.Error("onvif.main.GetRelayOutputs(): " + err.Error()) + return relayoutputs, err + } else { + if err := decodedXML.DecodeElement(&relayoutputs, et); err != nil { + log.Log.Debug("onvif.main.GetRelayOutputs(): " + err.Error()) return relayoutputs, err - } else { - if err := decodedXML.DecodeElement(&relayoutputs, et); err != nil { - log.Log.Debug("onvif.main.GetRelayOutputs(): " + err.Error()) - return relayoutputs, err - } } } } @@ -1283,7 +1285,7 @@ func TriggerRelayOutput(dev *onvif.Device, output string) (err error) { var b []byte if errResp != nil { b, err = io.ReadAll(resp.Body) - resp.Body.Close() + resp.Body.Close() // Ensure the response body is closed } stringBody := string(b) if err == nil && resp.StatusCode == 200 { From 666ff202ad73de3ebd111a15851cee8669f2aeb6 Mon Sep 17 00:00:00 2001 From: Cedric Verstraeten Date: Thu, 16 Jan 2025 21:49:27 +0100 Subject: [PATCH 3/3] update go.sum --- machinery/go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/machinery/go.sum b/machinery/go.sum index 6356c8d7..d85ca66d 100644 --- a/machinery/go.sum +++ b/machinery/go.sum @@ -298,8 +298,8 @@ github.com/kellydunn/golang-geo v0.7.0 h1:A5j0/BvNgGwY6Yb6inXQxzYwlPHc6WVZR+Mrar github.com/kellydunn/golang-geo v0.7.0/go.mod h1:YYlQPJ+DPEzrHx8kT3oPHC/NjyvCCXE+IuKGKdrjrcU= github.com/kerberos-io/joy4 v1.0.64 h1:gTUSotHSOhp9mNqEecgq88tQHvpj7TjmrvPUsPm0idg= github.com/kerberos-io/joy4 v1.0.64/go.mod h1:nZp4AjvKvTOXRrmDyAIOw+Da+JA5OcSo/JundGfOlFU= -github.com/kerberos-io/onvif v0.0.16 h1:fSiyc5ja6Q6ux6Jscvv4TVDPeK8O9x8WT1fG7YsbC8Q= -github.com/kerberos-io/onvif v0.0.16/go.mod h1:P1kUcCfeotJSlL1jwGseH6NSnCwWiuJLl3gAzafnLbA= +github.com/kerberos-io/onvif v0.0.17 h1:BK91WK+cO4lPvhVTh0dPtALRxjGIlFdXiA3O9tvq2UQ= +github.com/kerberos-io/onvif v0.0.17/go.mod h1:P1kUcCfeotJSlL1jwGseH6NSnCwWiuJLl3gAzafnLbA= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g=