8 "github.com/gin-gonic/gin"
9 common "github.com/iotbzh/xds-common/golib"
10 "github.com/iotbzh/xds-server/lib/xsapiv1"
13 // eventsList Registering for events that will be send over a WS
14 func (s *APIService) eventsList(c *gin.Context) {
18 // eventsRegister Registering for events that will be send over a WS
19 func (s *APIService) eventsRegister(c *gin.Context) {
20 var args xsapiv1.EventRegisterArgs
22 if c.BindJSON(&args) != nil {
23 common.APIError(c, "Invalid arguments")
27 sess := s.sessions.Get(c)
29 common.APIError(c, "Unknown sessions")
33 evType := strings.TrimPrefix(xsapiv1.EVTFolderStateChange, xsapiv1.EventTypePrefix)
34 if args.Name != evType {
35 common.APIError(c, "Unsupported event name")
39 /* XXX - to be removed if no plan to support "generic" event
40 var cbFunc st.EventsCB
41 cbFunc = func(ev st.Event, data *st.EventsCBData) {
43 evid, _ := strconv.Atoi((*data)["id"].(string))
44 ssid := (*data)["sid"].(string)
45 so := s.sessions.IOSocketGet(ssid)
47 s.log.Infof("Event %s not emitted - sid: %s", ev.Type, ssid)
49 // Consider that client disconnected, so unregister this event
50 s.mfolders.SThg.Events.UnRegister(ev.Type, evid)
60 if err := (*so).Emit(EVTAll, msg); err != nil {
61 s.log.Errorf("WS Emit Event : %v", err)
64 if err := (*so).Emit(EventTypePrefix+ev.Type, msg); err != nil {
65 s.log.Errorf("WS Emit Event : %v", err)
69 data := make(st.EventsCBData)
72 id, err := s.mfolders.SThg.Events.Register(args.Name, cbFunc, args.ProjectID, &data)
75 var cbFunc FolderEventCB
76 cbFunc = func(cfg *xsapiv1.FolderConfig, data *FolderEventCBData) {
77 ssid := (*data)["sid"].(string)
78 so := s.sessions.IOSocketGet(ssid)
80 //s.log.Infof("Event %s not emitted - sid: %s", ev.Type, ssid)
82 // Consider that client disconnected, so unregister this event
83 // SEB FIXMEs.mfolders.RegisterEventChange(ev.Type)
87 msg := xsapiv1.EventMsg{
88 Time: time.Now().String(),
93 s.Log.Debugf("WS Emit %s - Status=%10s, IsInSync=%6v, ID=%s",
94 xsapiv1.EventTypePrefix+evType, cfg.Status, cfg.IsInSync, cfg.ID)
96 if err := (*so).Emit(xsapiv1.EventTypePrefix+evType, msg); err != nil {
97 s.Log.Errorf("WS Emit Folder StateChanged event : %v", err)
100 data := make(FolderEventCBData)
101 data["sid"] = sess.ID
103 prjID, err := s.mfolders.ResolveID(args.ProjectID)
105 common.APIError(c, err.Error())
108 if err = s.mfolders.RegisterEventChange(prjID, &cbFunc, &data); err != nil {
109 common.APIError(c, err.Error())
113 c.JSON(http.StatusOK, gin.H{"status": "OK"})
116 // eventsRegister Registering for events that will be send over a WS
117 func (s *APIService) eventsUnRegister(c *gin.Context) {
118 var args xsapiv1.EventUnRegisterArgs
120 if c.BindJSON(&args) != nil || args.Name == "" || args.ID < 0 {
121 common.APIError(c, "Invalid arguments")
125 if err := s.mfolders.SThg.Events.UnRegister(args.Name, args.ID); err != nil {
126 common.APIError(c, err.Error())
129 c.JSON(http.StatusOK, gin.H{"status": "OK"})
131 common.APIError(c, "Not implemented yet")