diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-12-16 00:10:14 +0100 |
---|---|---|
committer | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2017-12-16 00:10:14 +0100 |
commit | 285332c351777b74abca638b8b2a2cde3c68edc6 (patch) | |
tree | ce52d6d347f23a944520a6b239f8099de75e1a41 /lib/xsapiv1 | |
parent | 963931e04d44a5b40d38817163f52f56241a9f33 (diff) |
Fixed and improved events management.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/xsapiv1')
-rw-r--r-- | lib/xsapiv1/events.go | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/lib/xsapiv1/events.go b/lib/xsapiv1/events.go index e19eb82..1552579 100644 --- a/lib/xsapiv1/events.go +++ b/lib/xsapiv1/events.go @@ -17,6 +17,11 @@ package xsapiv1 +import ( + "encoding/json" + "fmt" +) + // EventRegisterArgs Parameters (json format) of /events/register command type EventRegisterArgs struct { Name string `json:"name"` @@ -31,9 +36,10 @@ type EventUnRegisterArgs struct { // EventMsg Message send type EventMsg struct { - Time string `json:"time"` - Type string `json:"type"` - Folder FolderConfig `json:"folder"` + Time string `json:"time"` + FromSessionID string `json:"sessionID"` // Session ID of client who produce this event + Type string `json:"type"` + Data interface{} `json:"data"` // Data } // EventEvent Event send in WS when an internal event (eg. Syncthing event is received) @@ -46,3 +52,26 @@ const ( EVTFolderChange = EventTypePrefix + "folder-change" // type EventMsg with Data type xsapiv1.??? EVTFolderStateChange = EventTypePrefix + "folder-state-change" // type EventMsg with Data type xsapiv1.??? ) + +// EVTAllList List of all supported events +var EVTAllList = []string{ + EVTFolderChange, + EVTFolderStateChange, +} + +// DecodeFolderConfig Helper to decode Data field type FolderConfig +func (e *EventMsg) DecodeFolderConfig() (FolderConfig, error) { + var err error + f := FolderConfig{} + switch e.Type { + case EVTFolderChange, EVTFolderStateChange: + d := []byte{} + d, err = json.Marshal(e.Data) + if err == nil { + err = json.Unmarshal(d, &f) + } + default: + err = fmt.Errorf("Invalid type") + } + return f, err +} |