aboutsummaryrefslogtreecommitdiffstats
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
parent0a5ab2ca1ab89e6e667b7f9c7bdffdff036ca9b7 (diff)
Added events decoder helpers.
-rw-r--r--lib/agent/webserver.go2
-rw-r--r--lib/apiv1/events.go47
-rw-r--r--webapp/src/app/services/xdsagent.service.ts2
3 files changed, 43 insertions, 8 deletions
diff --git a/lib/agent/webserver.go b/lib/agent/webserver.go
index b75abd4..13572b4 100644
--- a/lib/agent/webserver.go
+++ b/lib/agent/webserver.go
@@ -233,8 +233,8 @@ func (s *WebServer) socketHandler(c *gin.Context) {
so.On("disconnection", func() {
s.Log.Debugf("WS disconnected (WSID=%s, SID=%s)", so.Id(), sess.ID)
- s.sessions.UpdateIOSocket(sess.ID, nil)
s.events.UnRegister(apiv1.EVTAll, sess.ID)
+ s.sessions.UpdateIOSocket(sess.ID, nil)
})
})
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
+}
diff --git a/webapp/src/app/services/xdsagent.service.ts b/webapp/src/app/services/xdsagent.service.ts
index 2ea2d5d..c4f2787 100644
--- a/webapp/src/app/services/xdsagent.service.ts
+++ b/webapp/src/app/services/xdsagent.service.ts
@@ -330,7 +330,7 @@ export class XDSAgentService {
private _RegisterEvents() {
// Register to all existing events
- this._post('/events/register', { "name": "all" })
+ this._post('/events/register', { "name": "event:all" })
.subscribe(
res => { },
error => {