2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 // EventRegisterArgs is the parameters (json format) of /events/register command
26 type EventRegisterArgs struct {
27 Name string `json:"name"`
28 ProjectID string `json:"filterProjectID"`
31 // EventUnRegisterArgs is the parameters (json format) of /events/unregister command
32 type EventUnRegisterArgs struct {
33 Name string `json:"name"`
37 // Events Type definitions
39 // EventTypePrefix Used as event prefix
40 EventTypePrefix = "event:" // following by event type
42 // Supported Events type
43 EVTAll = EventTypePrefix + "all"
44 EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type xaapiv1.ServerCfg
45 EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type xaapiv1.ProjectConfig
46 EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type xaapiv1.ProjectConfig
47 EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig
48 EVTSDKAdd = EventTypePrefix + "sdk-add" // type EventMsg with Data type xaapiv1.SDK
49 EVTSDKRemove = EventTypePrefix + "sdk-remove" // type EventMsg with Data type xaapiv1.SDK
50 EVTSDKManagement = EventTypePrefix + "sdk-management" // type EventMsg with Data type xaapiv1.SDKManagementMsg
51 EVTSDKStateChange = EventTypePrefix + "sdk-state-change" // type EventMsg with Data type xaapiv1.SDK
54 // EVTAllList List of all supported events
55 var EVTAllList = []string{
66 // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
67 type EventMsg struct {
68 Time string `json:"time"` // Timestamp
69 FromSessionID string `json:"sessionID"` // Session ID of client who produce this event
70 Type string `json:"type"` // Data type
71 Data interface{} `json:"data"` // Data
74 // DecodeServerCfg Helper to decode Data field type ServerCfg
75 func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) {
77 if e.Type != EVTServerConfig {
78 return p, fmt.Errorf("Invalid type")
80 d, err := json.Marshal(e.Data)
82 err = json.Unmarshal(d, &p)
87 // DecodeProjectConfig Helper to decode Data field type ProjectConfig
88 func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
92 case EVTProjectAdd, EVTProjectChange, EVTProjectDelete:
94 d, err = json.Marshal(e.Data)
96 err = json.Unmarshal(d, &p)
99 err = fmt.Errorf("Invalid type")
104 // DecodeSDKMgtMsg Helper to decode Data field type SDKManagementMsg
105 func (e *EventMsg) DecodeSDKMgtMsg() (SDKManagementMsg, error) {
107 s := SDKManagementMsg{}
108 if e.Type != EVTSDKManagement {
109 return s, fmt.Errorf("Invalid type")
112 d, err = json.Marshal(e.Data)
114 err = json.Unmarshal(d, &s)
119 // DecodeSDKEvent Helper to decode Data field type SDK
120 func (e *EventMsg) DecodeSDKEvent() (SDK, error) {
124 case EVTSDKAdd, EVTSDKRemove, EVTSDKStateChange:
126 d, err = json.Marshal(e.Data)
128 err = json.Unmarshal(d, &s)
131 err = fmt.Errorf("Invalid type")