diff --git a/machinery/docs/docs.go b/machinery/docs/docs.go index da8980b..e1d6ff1 100644 --- a/machinery/docs/docs.go +++ b/machinery/docs/docs.go @@ -605,12 +605,12 @@ const docTemplate = `{ "operationId": "verify-onvif", "parameters": [ { - "description": "Camera Config", - "name": "cameraConfig", + "description": "OnvifCredentials", + "name": "config", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/models.IPCamera" + "$ref": "#/definitions/models.OnvifCredentials" } } ], diff --git a/machinery/docs/swagger.json b/machinery/docs/swagger.json index d179660..a4e7d7f 100644 --- a/machinery/docs/swagger.json +++ b/machinery/docs/swagger.json @@ -597,12 +597,12 @@ "operationId": "verify-onvif", "parameters": [ { - "description": "Camera Config", - "name": "cameraConfig", + "description": "OnvifCredentials", + "name": "config", "in": "body", "required": true, "schema": { - "$ref": "#/definitions/models.IPCamera" + "$ref": "#/definitions/models.OnvifCredentials" } } ], diff --git a/machinery/docs/swagger.yaml b/machinery/docs/swagger.yaml index 7d5f488..6b5fa6b 100644 --- a/machinery/docs/swagger.yaml +++ b/machinery/docs/swagger.yaml @@ -705,12 +705,12 @@ paths: description: Will verify the ONVIF connectivity. operationId: verify-onvif parameters: - - description: Camera Config + - description: OnvifCredentials in: body - name: cameraConfig + name: config required: true schema: - $ref: '#/definitions/models.IPCamera' + $ref: '#/definitions/models.OnvifCredentials' responses: "200": description: OK diff --git a/machinery/src/cloud/Cloud.go b/machinery/src/cloud/Cloud.go index c4e1fca..c9b489e 100644 --- a/machinery/src/cloud/Cloud.go +++ b/machinery/src/cloud/Cloud.go @@ -235,7 +235,7 @@ func HandleHeartBeat(configuration *models.Configuration, communication *models. var pullPointAddress string if config.Capture.IPCamera.ONVIFXAddr != "" { cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { pullPointAddress, err = onvif.CreatePullPointSubscription(device) if err != nil { @@ -260,7 +260,7 @@ loop: var onvifEventsList []byte if config.Capture.IPCamera.ONVIFXAddr != "" { cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { configurations, err := onvif.GetPTZConfigurationsFromDevice(device) if err == nil { @@ -553,7 +553,7 @@ loop: if pullPointAddress != "" { cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { onvif.UnsubscribePullPoint(device, pullPointAddress) } diff --git a/machinery/src/onvif/main.go b/machinery/src/onvif/main.go index 514769b..ac7c945 100644 --- a/machinery/src/onvif/main.go +++ b/machinery/src/onvif/main.go @@ -54,7 +54,7 @@ func HandleONVIFActions(configuration *models.Configuration, communication *mode // Connect to Onvif device cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Get token from the first profile @@ -189,7 +189,7 @@ func HandleONVIFActions(configuration *models.Configuration, communication *mode log.Log.Debug("onvif.HandleONVIFActions(): finished") } -func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, error) { +func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, device.GetCapabilitiesResponse, error) { log.Log.Debug("onvif.ConnectToOnvifDevice(): started") dev, err := onvif.NewDevice(onvif.DeviceParams{ Xaddr: cameraConfiguration.ONVIFXAddr, @@ -197,6 +197,8 @@ func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, Password: cameraConfiguration.ONVIFPassword, AuthMode: "both", }) + + var capabilities device.GetCapabilitiesResponse if err != nil { log.Log.Debug("onvif.ConnectToOnvifDevice(): " + err.Error()) } else { @@ -220,7 +222,6 @@ func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, if err != nil { log.Log.Error("onvif.ConnectToOnvifDevice(): " + err.Error()) } else { - var capabilities device.GetCapabilitiesResponse if err := decodedXML.DecodeElement(&capabilities, et); err != nil { log.Log.Error("onvif.ConnectToOnvifDevice(): " + err.Error()) } else { @@ -231,7 +232,7 @@ func ConnectToOnvifDevice(cameraConfiguration *models.IPCamera) (*onvif.Device, log.Log.Info("onvif.ConnectToOnvifDevice(): successfully connected to device") } log.Log.Debug("onvif.ConnectToOnvifDevice(): finished") - return dev, err + return dev, capabilities, err } func GetTokenFromProfile(device *onvif.Device, profileId int) (xsdonvif.ReferenceToken, error) { @@ -302,7 +303,7 @@ func GetPositionFromDevice(configuration models.Configuration) (xsdonvif.PTZVect var position xsdonvif.PTZVector // Connect to Onvif device cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Get token from the first profile @@ -899,28 +900,52 @@ func GetPTZFunctionsFromDevice(configurations ptz.GetConfigurationsResponse) ([] // @in header // @name Authorization // @Tags onvif -// @Param cameraConfig body models.IPCamera true "Camera Config" +// @Param config body models.OnvifCredentials true "OnvifCredentials" // @Summary Will verify the ONVIF connectivity. // @Description Will verify the ONVIF connectivity. // @Success 200 {object} models.APIResponse func VerifyOnvifConnection(c *gin.Context) { - var cameraConfig models.IPCamera - err := c.BindJSON(&cameraConfig) - if err == nil { - device, err := ConnectToOnvifDevice(&cameraConfig) + var onvifCredentials models.OnvifCredentials + err := c.BindJSON(&onvifCredentials) + + if err == nil && onvifCredentials.ONVIFXAddr != "" { + + configuration := &models.Configuration{ + Config: models.Config{ + Capture: models.Capture{ + IPCamera: models.IPCamera{ + ONVIFXAddr: onvifCredentials.ONVIFXAddr, + ONVIFUsername: onvifCredentials.ONVIFUsername, + ONVIFPassword: onvifCredentials.ONVIFPassword, + }, + }, + }, + } + + cameraConfiguration := configuration.Config.Capture.IPCamera + device, capabilities, err := ConnectToOnvifDevice(&cameraConfiguration) if err == nil { - log.Log.Info("onvif.main.VerifyOnvifConnection(): successfully verified the ONVIF connection") - c.JSON(200, models.APIResponse{ - Data: device, - }) + // Get token from the first profile + token, err := GetTokenFromProfile(device, 0) + if err == nil { + c.JSON(200, gin.H{ + "device": device, + "capabilities": capabilities, + "token": token, + }) + } else { + c.JSON(400, gin.H{ + "data": "Something went wrong: " + err.Error(), + }) + } } else { - c.JSON(400, models.APIResponse{ - Message: "onvif.main.VerifyOnvifConnection(): s went wrong while verifying the ONVIF connection " + err.Error(), + c.JSON(400, gin.H{ + "data": "Something went wrong: " + err.Error(), }) } } else { - c.JSON(400, models.APIResponse{ - Message: "onvif.main.VerifyOnvifConnection(): s went wrong while receiving the config " + err.Error(), + c.JSON(400, gin.H{ + "data": "Something went wrong: " + err.Error(), }) } } @@ -1264,5 +1289,12 @@ func getXMLNode(xmlBody string, nodeName string) (*xml.Decoder, *xml.StartElemen } } } - return nil, nil, errors.New("getXMLNode(): " + err.Error()) + + // Check for authorisation error + // - The action requested requires authorization and the sender is not authorized + if strings.Contains(xmlBody, "not authorized") { + return nil, nil, errors.New("getXMLNode(): not authorized, make sure you have the correct credentials") + } else { + return nil, nil, errors.New("getXMLNode(): " + err.Error()) + } } diff --git a/machinery/src/routers/http/methods.go b/machinery/src/routers/http/methods.go index acd1ac4..0e31195 100644 --- a/machinery/src/routers/http/methods.go +++ b/machinery/src/routers/http/methods.go @@ -44,11 +44,21 @@ func LoginToOnvif(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, capabilities, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { - c.JSON(200, gin.H{ - "device": device, - }) + // Get token from the first profile + token, err := onvif.GetTokenFromProfile(device, 0) + if err == nil { + c.JSON(200, gin.H{ + "device": device, + "capabilities": capabilities, + "token": token, + }) + } else { + c.JSON(400, gin.H{ + "data": "Something went wrong: " + err.Error(), + }) + } } else { c.JSON(400, gin.H{ "data": "Something went wrong: " + err.Error(), @@ -88,10 +98,10 @@ func GetOnvifCapabilities(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + _, capabilities, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { c.JSON(200, gin.H{ - "capabilities": onvif.GetCapabilitiesFromDevice(device), + "capabilities": capabilities, }) } else { c.JSON(400, gin.H{ @@ -132,7 +142,7 @@ func DoOnvifPanTilt(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Get token from the first profile @@ -206,7 +216,7 @@ func DoOnvifZoom(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Get token from the first profile @@ -279,7 +289,7 @@ func GetOnvifPresets(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { presets, err := onvif.GetPresetsFromDevice(device) if err == nil { @@ -330,7 +340,7 @@ func GoToOnvifPreset(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { err := onvif.GoToPresetFromDevice(device, onvifPreset.Preset) if err == nil { @@ -385,7 +395,7 @@ func DoGetDigitalInputs(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + _, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Get the digital inputs and outputs from the device inputOutputs, err := onvif.GetInputOutputs() @@ -454,7 +464,7 @@ func DoGetRelayOutputs(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + _, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Get the digital inputs and outputs from the device inputOutputs, err := onvif.GetInputOutputs() @@ -527,7 +537,7 @@ func DoTriggerRelayOutput(c *gin.Context) { } cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { err := onvif.TriggerRelayOutput(device, output) if err == nil { diff --git a/machinery/src/routers/mqtt/main.go b/machinery/src/routers/mqtt/main.go index f7311b0..e2458f6 100644 --- a/machinery/src/routers/mqtt/main.go +++ b/machinery/src/routers/mqtt/main.go @@ -518,7 +518,7 @@ func HandleTriggerRelay(mqttClient mqtt.Client, hubKey string, payload models.Pa token := triggerRelayPayload.Token // Connect to Onvif device cameraConfiguration := configuration.Config.Capture.IPCamera - device, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) + device, _, err := onvif.ConnectToOnvifDevice(&cameraConfiguration) if err == nil { // Trigger relay output err := onvif.TriggerRelayOutput(device, token)