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"
45 EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type xaapiv1.ServerCfg
46 EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type xaapiv1.ProjectConfig
47 EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type xaapiv1.ProjectConfig
48 EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig
50 EVTSDKAdd = EventTypePrefix + "sdk-add" // type EventMsg with Data type xaapiv1.SDK
51 EVTSDKRemove = EventTypePrefix + "sdk-remove" // type EventMsg with Data type xaapiv1.SDK
52 EVTSDKManagement = EventTypePrefix + "sdk-management" // type EventMsg with Data type xaapiv1.SDKManagementMsg
53 EVTSDKStateChange = EventTypePrefix + "sdk-state-change" // type EventMsg with Data type xaapiv1.SDK
55 EVTTargetAdd = EventTypePrefix + "target-add" // type EventMsg with Data type xaapiv1.TargetConfig
56 EVTTargetRemove = EventTypePrefix + "target-remove" // type EventMsg with Data type xaapiv1.TargetConfig
57 EVTTargetStateChange = EventTypePrefix + "target-state-change" // type EventMsg with Data type xaapiv1.TargetConfig
59 EVTTargetTerminalAdd = EventTypePrefix + "target-terminal-add" // type EventMsg with Data type xaapiv1.TerminalConfig
60 EVTTargetTerminalRemove = EventTypePrefix + "target-terminal-remove" // type EventMsg with Data type xaapiv1.TerminalConfig
61 EVTTargetTerminalStateChange = EventTypePrefix + "target-terminal-state-change" // type EventMsg with Data type xaapiv1.TerminalConfig
64 // EVTAllList List of all supported events
65 var EVTAllList = []string{
78 EVTTargetTerminalRemove,
79 EVTTargetTerminalStateChange,
82 // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
83 type EventMsg struct {
84 Time string `json:"time"` // Timestamp
85 FromSessionID string `json:"sessionID"` // Session ID of client who produce this event
86 Type string `json:"type"` // Data type
87 Data interface{} `json:"data"` // Data
90 // DecodeServerCfg Helper to decode Data field type ServerCfg
91 func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) {
93 if e.Type != EVTServerConfig {
94 return p, fmt.Errorf("Invalid type")
96 d, err := json.Marshal(e.Data)
98 err = json.Unmarshal(d, &p)
103 // DecodeProjectConfig Helper to decode Data field type ProjectConfig
104 func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
108 case EVTProjectAdd, EVTProjectChange, EVTProjectDelete:
110 d, err = json.Marshal(e.Data)
112 err = json.Unmarshal(d, &p)
115 err = fmt.Errorf("Invalid type")
120 // DecodeSDKMgtMsg Helper to decode Data field type SDKManagementMsg
121 func (e *EventMsg) DecodeSDKMgtMsg() (SDKManagementMsg, error) {
123 s := SDKManagementMsg{}
124 if e.Type != EVTSDKManagement {
125 return s, fmt.Errorf("Invalid type")
128 d, err = json.Marshal(e.Data)
130 err = json.Unmarshal(d, &s)
135 // DecodeSDKEvent Helper to decode Data field type SDK
136 func (e *EventMsg) DecodeSDKEvent() (SDK, error) {
140 case EVTSDKAdd, EVTSDKRemove, EVTSDKStateChange:
142 d, err = json.Marshal(e.Data)
144 err = json.Unmarshal(d, &s)
147 err = fmt.Errorf("Invalid type")
152 // DecodeTargetEvent Helper to decode Data field type TargetConfig
153 func (e *EventMsg) DecodeTargetEvent() (TargetConfig, error) {
157 case EVTTargetAdd, EVTTargetRemove, EVTTargetStateChange:
159 d, err = json.Marshal(e.Data)
161 err = json.Unmarshal(d, &p)
164 err = fmt.Errorf("Invalid type")
169 // DecodeTerminalEvent Helper to decode Data field type TerminalConfig
170 func (e *EventMsg) DecodeTerminalEvent() (TerminalConfig, error) {
172 p := TerminalConfig{}
174 case EVTTargetTerminalAdd, EVTTargetTerminalRemove, EVTTargetTerminalStateChange:
176 d, err = json.Marshal(e.Data)
178 err = json.Unmarshal(d, &p)
181 err = fmt.Errorf("Invalid type")