diff --git a/cmd/gonvif/events/cmd.go b/cmd/gonvif/events/cmd.go index b42fb6f..157f895 100644 --- a/cmd/gonvif/events/cmd.go +++ b/cmd/gonvif/events/cmd.go @@ -21,6 +21,7 @@ func init() { getEventBrokers, getEventProperties, getServiceCapabilities, + streamEvents, ) } diff --git a/cmd/gonvif/events/stream-events.go b/cmd/gonvif/events/stream-events.go new file mode 100644 index 0000000..b5e5953 --- /dev/null +++ b/cmd/gonvif/events/stream-events.go @@ -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 + } + } +}