Migration to AGL gerrit (update go import)
[src/xds/xds-server.git] / lib / xsapiv1 / events.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package xsapiv1
19
20 import (
21         "encoding/json"
22         "fmt"
23 )
24
25 // EventRegisterArgs Parameters (json format) of /events/register command
26 type EventRegisterArgs struct {
27         Name   string `json:"name"`
28         Filter string `json:"filter"`
29 }
30
31 // EventUnRegisterArgs Parameters of /events/unregister command
32 type EventUnRegisterArgs struct {
33         Name string `json:"name"`
34         ID   int    `json:"id"`
35 }
36
37 // EventMsg Message send
38 type EventMsg struct {
39         Time          string      `json:"time"`
40         FromSessionID string      `json:"sessionID"` // Session ID of client who produce this event
41         Type          string      `json:"type"`
42         Data          interface{} `json:"data"` // Data
43 }
44
45 // EventEvent Event send in WS when an internal event (eg. Syncthing event is received)
46 const (
47         // EventTypePrefix Used as event prefix
48         EventTypePrefix = "event:" // following by event type
49
50         // Supported Events type
51         EVTAll               = EventTypePrefix + "all"
52         EVTFolderChange      = EventTypePrefix + "folder-change"       // type EventMsg with Data type xsapiv1.FolderConfig
53         EVTFolderStateChange = EventTypePrefix + "folder-state-change" // type EventMsg with Data type xsapiv1.FolderConfig
54         EVTSDKInstall        = EventTypePrefix + "sdk-install"         // type EventMsg with Data type xsapiv1.SDKManagementMsg
55         EVTSDKRemove         = EventTypePrefix + "sdk-remove"          // type EventMsg with Data type xsapiv1.SDKManagementMsg
56         EVTSDKStateChange    = EventTypePrefix + "sdk-state-change"    // type EventMsg with Data type xsapiv1.SDK
57 )
58
59 // EVTAllList List of all supported events
60 var EVTAllList = []string{
61         EVTFolderChange,
62         EVTFolderStateChange,
63         EVTSDKInstall,
64         EVTSDKRemove,
65         EVTSDKStateChange,
66 }
67
68 // DecodeFolderConfig Helper to decode Data field type FolderConfig
69 func (e *EventMsg) DecodeFolderConfig() (FolderConfig, error) {
70         var err error
71         f := FolderConfig{}
72         switch e.Type {
73         case EVTFolderChange, EVTFolderStateChange:
74                 d := []byte{}
75                 d, err = json.Marshal(e.Data)
76                 if err == nil {
77                         err = json.Unmarshal(d, &f)
78                 }
79         default:
80                 err = fmt.Errorf("Invalid type")
81         }
82         return f, err
83 }