-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ func init() { | |
getEventBrokers, | ||
getEventProperties, | ||
getServiceCapabilities, | ||
streamEvents, | ||
) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |