Skip to content

Commit

Permalink
Add events stream-events command.
Browse files Browse the repository at this point in the history
  • Loading branch information
osery committed Apr 11, 2023
1 parent 6554b27 commit 44ab8b9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/gonvif/events/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
getEventBrokers,
getEventProperties,
getServiceCapabilities,
streamEvents,
)
}

Expand Down
51 changes: 51 additions & 0 deletions cmd/gonvif/events/stream-events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package events

import (
"github.com/spf13/cobra"

"github.com/eyetowers/gonvif/cmd/gonvif/root"
"github.com/eyetowers/gonvif/pkg/generated/onvif/www_onvif_org/ver10/events/wsdl"
"github.com/eyetowers/gonvif/pkg/gonvif"
)

var streamEvents = &cobra.Command{
Use: "stream-events",
Short: "Stream live Onvif events from the device.",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
onvif, err := gonvif.New(root.URL, root.Username, root.Password, root.Verbose)
if err != nil {
return err
}
return runStreamEvents(onvif)
},
}

func runStreamEvents(client gonvif.Client) error {
events, err := client.Events()
if err != nil {
return err
}
resp, err := events.CreatePullPointSubscription(&wsdl.CreatePullPointSubscription{})
if err != nil {
return err
}
subscription, err := client.Subscription(string(*resp.SubscriptionReference.Address))
if err != nil {
return err
}
return processEvents(subscription)
}

func processEvents(subscription wsdl.PullPointSubscription) error {
for {
resp, err := subscription.PullMessages(&wsdl.PullMessages{MessageLimit: 100})
if err != nil {
return err
}
err = root.OutputJSON(resp)
if err != nil {
return err
}
}
}

0 comments on commit 44ab8b9

Please sign in to comment.