84a364c421581d1636e1dac3c598c8a993be2270
[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         EVTSDKAdd            = EventTypePrefix + "sdk-add"             // type EventMsg with Data type xsapiv1.SDK
55         EVTSDKRemove         = EventTypePrefix + "sdk-remove"          // type EventMsg with Data type xsapiv1.SDK
56         EVTSDKManagement     = EventTypePrefix + "sdk-management"      // type EventMsg with Data type xsapiv1.SDKManagementMsg
57         EVTSDKStateChange    = EventTypePrefix + "sdk-state-change"    // type EventMsg with Data type xsapiv1.SDK
58 )
59
60 // EVTAllList List of all supported events
61 var EVTAllList = []string{
62         EVTFolderChange,
63         EVTFolderStateChange,
64         EVTSDKAdd,
65         EVTSDKRemove,
66         EVTSDKManagement,
67         EVTSDKStateChange,
68 }
69
70 // DecodeFolderConfig Helper to decode Data field type FolderConfig
71 func (e *EventMsg) DecodeFolderConfig() (FolderConfig, error) {
72         var err error
73         f := FolderConfig{}
74         switch e.Type {
75         case EVTFolderChange, EVTFolderStateChange:
76                 d := []byte{}
77                 d, err = json.Marshal(e.Data)
78                 if err == nil {
79                         err = json.Unmarshal(d, &f)
80                 }
81         default:
82                 err = fmt.Errorf("Invalid type")
83         }
84         return f, err
85 }
86
87 // DecodeSDKEvent Helper to decode Data field type SDK
88 func (e *EventMsg) DecodeSDKEvent() (SDK, error) {
89         var err error
90         s := SDK{}
91         switch e.Type {
92         case EVTSDKAdd, EVTSDKRemove, EVTSDKStateChange:
93                 d := []byte{}
94                 d, err = json.Marshal(e.Data)
95                 if err == nil {
96                         err = json.Unmarshal(d, &s)
97                 }
98         default:
99                 err = fmt.Errorf("Invalid type")
100         }
101         return s, err
102 }