Reworked SDKs events (introduced sdk-state-change)
[src/xds/xds-agent.git] / lib / xaapiv1 / events.go
index 12c8cb2..6520057 100644 (file)
@@ -1,3 +1,20 @@
+/*
+ * Copyright (C) 2017-2018 "IoT.bzh"
+ * Author Sebastien Douheret <sebastien@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package xaapiv1
 
 import (
@@ -23,11 +40,15 @@ const (
        EventTypePrefix = "event:" // following by event type
 
        // Supported Events type
-       EVTAll           = EventTypePrefix + "all"
-       EVTServerConfig  = EventTypePrefix + "server-config"        // type EventMsg with Data type xaapiv1.ServerCfg
-       EVTProjectAdd    = EventTypePrefix + "project-add"          // type EventMsg with Data type xaapiv1.ProjectConfig
-       EVTProjectDelete = EventTypePrefix + "project-delete"       // type EventMsg with Data type xaapiv1.ProjectConfig
-       EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig
+       EVTAll            = EventTypePrefix + "all"
+       EVTServerConfig   = EventTypePrefix + "server-config"        // type EventMsg with Data type xaapiv1.ServerCfg
+       EVTProjectAdd     = EventTypePrefix + "project-add"          // type EventMsg with Data type xaapiv1.ProjectConfig
+       EVTProjectDelete  = EventTypePrefix + "project-delete"       // type EventMsg with Data type xaapiv1.ProjectConfig
+       EVTProjectChange  = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig
+       EVTSDKAdd         = EventTypePrefix + "sdk-add"              // type EventMsg with Data type xaapiv1.SDK
+       EVTSDKRemove      = EventTypePrefix + "sdk-remove"           // type EventMsg with Data type xaapiv1.SDK
+       EVTSDKManagement  = EventTypePrefix + "sdk-management"       // type EventMsg with Data type xaapiv1.SDKManagementMsg
+       EVTSDKStateChange = EventTypePrefix + "sdk-state-change"     // type EventMsg with Data type xaapiv1.SDK
 )
 
 // EVTAllList List of all supported events
@@ -36,12 +57,16 @@ var EVTAllList = []string{
        EVTProjectAdd,
        EVTProjectDelete,
        EVTProjectChange,
+       EVTSDKAdd,
+       EVTSDKRemove,
+       EVTSDKManagement,
+       EVTSDKStateChange,
 }
 
 // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
 type EventMsg struct {
        Time          string      `json:"time"`      // Timestamp
-       FromSessionID string      `json:"sessionID"` // Session ID of client that emits this event
+       FromSessionID string      `json:"sessionID"` // Session ID of client who produce this event
        Type          string      `json:"type"`      // Data type
        Data          interface{} `json:"data"`      // Data
 }
@@ -75,3 +100,35 @@ func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
        }
        return p, err
 }
+
+// DecodeSDKMgtMsg Helper to decode Data field type SDKManagementMsg
+func (e *EventMsg) DecodeSDKMgtMsg() (SDKManagementMsg, error) {
+       var err error
+       s := SDKManagementMsg{}
+       if e.Type != EVTSDKManagement {
+               return s, fmt.Errorf("Invalid type")
+       }
+       d := []byte{}
+       d, err = json.Marshal(e.Data)
+       if err == nil {
+               err = json.Unmarshal(d, &s)
+       }
+       return s, err
+}
+
+// DecodeSDKEvent Helper to decode Data field type SDK
+func (e *EventMsg) DecodeSDKEvent() (SDK, error) {
+       var err error
+       s := SDK{}
+       switch e.Type {
+       case EVTSDKAdd, EVTSDKRemove, EVTSDKStateChange:
+               d := []byte{}
+               d, err = json.Marshal(e.Data)
+               if err == nil {
+                       err = json.Unmarshal(d, &s)
+               }
+       default:
+               err = fmt.Errorf("Invalid type")
+       }
+       return s, err
+}