2 * Copyright (C) 2017 "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
50 // EVTAllList List of all supported events
51 var EVTAllList = []string{
58 // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
59 type EventMsg struct {
60 Time string `json:"time"` // Timestamp
61 FromSessionID string `json:"sessionID"` // Session ID of client that emits this event
62 Type string `json:"type"` // Data type
63 Data interface{} `json:"data"` // Data
66 // DecodeServerCfg Helper to decode Data field type ServerCfg
67 func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) {
69 if e.Type != EVTServerConfig {
70 return p, fmt.Errorf("Invalid type")
72 d, err := json.Marshal(e.Data)
74 err = json.Unmarshal(d, &p)
79 // DecodeProjectConfig Helper to decode Data field type ProjectConfig
80 func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
84 case EVTProjectAdd, EVTProjectChange, EVTProjectDelete:
86 d, err = json.Marshal(e.Data)
88 err = json.Unmarshal(d, &p)
91 err = fmt.Errorf("Invalid type")