84e62c1a624ed446b7406c3ec4d18e150aca6621
[src/xds/xds-server.git] / lib / xsapiv1 / events.go
1 /*
2  * Copyright (C) 2017 "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 )
57
58 // EVTAllList List of all supported events
59 var EVTAllList = []string{
60         EVTFolderChange,
61         EVTFolderStateChange,
62         EVTSDKInstall,
63         EVTSDKRemove,
64 }
65
66 // DecodeFolderConfig Helper to decode Data field type FolderConfig
67 func (e *EventMsg) DecodeFolderConfig() (FolderConfig, error) {
68         var err error
69         f := FolderConfig{}
70         switch e.Type {
71         case EVTFolderChange, EVTFolderStateChange:
72                 d := []byte{}
73                 d, err = json.Marshal(e.Data)
74                 if err == nil {
75                         err = json.Unmarshal(d, &f)
76                 }
77         default:
78                 err = fmt.Errorf("Invalid type")
79         }
80         return f, err
81 }