summaryrefslogtreecommitdiffstats
path: root/lib/apiv1
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-08 17:37:56 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-08 17:37:56 +0100
commitc51d5034d527578da70bdd41b9ce13f28455c598 (patch)
tree78adcf6c861cbce0114cf2528a98e0bc603a06dd /lib/apiv1
parent0a5ab2ca1ab89e6e667b7f9c7bdffdff036ca9b7 (diff)
Added events decoder helpers.
Diffstat (limited to 'lib/apiv1')
-rw-r--r--lib/apiv1/events.go47
1 files changed, 41 insertions, 6 deletions
diff --git a/lib/apiv1/events.go b/lib/apiv1/events.go
index dde2710..cdd0889 100644
--- a/lib/apiv1/events.go
+++ b/lib/apiv1/events.go
@@ -1,5 +1,10 @@
package apiv1
+import (
+ "encoding/json"
+ "fmt"
+)
+
// EventRegisterArgs is the parameters (json format) of /events/register command
type EventRegisterArgs struct {
Name string `json:"name"`
@@ -12,17 +17,17 @@ type EventUnRegisterArgs struct {
ID int `json:"id"`
}
-// Events definitions
+// Events Type definitions
const (
// EventTypePrefix Used as event prefix
EventTypePrefix = "event:" // following by event type
// Supported Events type
EVTAll = EventTypePrefix + "all"
- EVTServerConfig = EventTypePrefix + "server-config" // data type apiv1.ServerCfg
- EVTProjectAdd = EventTypePrefix + "project-add" // data type apiv1.ProjectConfig
- EVTProjectDelete = EventTypePrefix + "project-delete" // data type apiv1.ProjectConfig
- EVTProjectChange = EventTypePrefix + "project-state-change" // data type apiv1.ProjectConfig
+ EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type apiv1.ServerCfg
+ EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type apiv1.ProjectConfig
+ EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type apiv1.ProjectConfig
+ EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type apiv1.ProjectConfig
)
// EVTAllList List of all supported events
@@ -33,9 +38,39 @@ var EVTAllList = []string{
EVTProjectChange,
}
-// EventMsg Message send
+// EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
type EventMsg struct {
Time string `json:"time"`
Type string `json:"type"`
Data interface{} `json:"data"`
}
+
+// DecodeServerCfg Helper to decode Data field type ServerCfg
+func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) {
+ p := ServerCfg{}
+ if e.Type != EVTServerConfig {
+ return p, fmt.Errorf("Invalid type")
+ }
+ d, err := json.Marshal(e.Data)
+ if err == nil {
+ err = json.Unmarshal(d, &p)
+ }
+ return p, err
+}
+
+// DecodeProjectConfig Helper to decode Data field type ProjectConfig
+func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
+ var err error
+ p := ProjectConfig{}
+ switch e.Type {
+ case EVTProjectAdd, EVTProjectChange, EVTProjectDelete:
+ d := []byte{}
+ d, err = json.Marshal(e.Data)
+ if err == nil {
+ err = json.Unmarshal(d, &p)
+ }
+ default:
+ err = fmt.Errorf("Invalid type")
+ }
+ return p, err
+}