github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/stream/handlers/event_handler.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package handlers 20 21 import ( 22 "context" 23 "encoding/json" 24 "go.uber.org/fx" 25 26 "github.com/e154/smart-home/common" 27 "github.com/e154/smart-home/common/events" 28 m "github.com/e154/smart-home/models" 29 "github.com/e154/smart-home/plugins/webpush" 30 "github.com/e154/smart-home/system/bus" 31 "github.com/e154/smart-home/system/stream" 32 ) 33 34 type EventHandler struct { 35 stream *stream.Stream 36 eventBus bus.Bus 37 } 38 39 func NewEventHandler(lc fx.Lifecycle, 40 stream *stream.Stream, 41 eventBus bus.Bus) *EventHandler { 42 handler := &EventHandler{ 43 stream: stream, 44 eventBus: eventBus, 45 } 46 47 lc.Append(fx.Hook{ 48 OnStart: func(ctx context.Context) (err error) { 49 return handler.Start(ctx) 50 }, 51 OnStop: func(ctx context.Context) (err error) { 52 return handler.Shutdown(ctx) 53 }, 54 }) 55 56 return handler 57 } 58 59 // Start ... 60 func (s *EventHandler) Start(_ context.Context) error { 61 s.stream.Subscribe("event_get_last_state", s.EventGetLastState) 62 s.stream.Subscribe("event_add_webpush_subscription", s.EventAddWebPushSubscription) 63 s.stream.Subscribe("event_get_webpush_public_key", s.EventGetWebPushPublicKey) 64 s.stream.Subscribe("event_get_user_devices", s.EventGetUserDevices) 65 s.stream.Subscribe("command_terminal", s.CommandTerminal) 66 s.stream.Subscribe("event_get_server_version", s.EventGetServerVersion) 67 return nil 68 } 69 70 // Shutdown ... 71 func (s *EventHandler) Shutdown(_ context.Context) error { 72 s.stream.UnSubscribe("event_get_last_state") 73 s.stream.UnSubscribe("event_add_webpush_subscription") 74 s.stream.UnSubscribe("event_get_webpush_public_key") 75 s.stream.UnSubscribe("event_get_user_devices") 76 s.stream.UnSubscribe("command_terminal") 77 s.stream.UnSubscribe("event_get_server_version") 78 return nil 79 } 80 81 func (s *EventHandler) EventGetWebPushPublicKey(client stream.IStreamClient, query string, body []byte) { 82 var userID int64 83 if user := client.GetUser(); user != nil { 84 userID = user.Id 85 } 86 87 s.eventBus.Publish(webpush.TopicPluginWebpush, webpush.EventGetWebPushPublicKey{ 88 UserID: userID, 89 SessionID: client.SessionID(), 90 }) 91 } 92 93 func (s *EventHandler) EventGetUserDevices(client stream.IStreamClient, query string, body []byte) { 94 var userID int64 95 if user := client.GetUser(); user != nil { 96 userID = user.Id 97 } 98 99 s.eventBus.Publish(webpush.TopicPluginWebpush, webpush.EventGetUserDevices{ 100 UserID: userID, 101 SessionID: client.SessionID(), 102 }) 103 } 104 105 func (s *EventHandler) EventAddWebPushSubscription(client stream.IStreamClient, query string, body []byte) { 106 var userID int64 107 if user := client.GetUser(); user != nil { 108 userID = user.Id 109 } 110 111 subscription := &m.Subscription{} 112 _ = json.Unmarshal(body, subscription) 113 s.eventBus.Publish(webpush.TopicPluginWebpush, webpush.EventAddWebPushSubscription{ 114 UserID: userID, 115 SessionID: client.SessionID(), 116 Subscription: subscription, 117 }) 118 } 119 120 func (s *EventHandler) EventGetLastState(client stream.IStreamClient, query string, body []byte) { 121 req := map[string]common.EntityId{} 122 _ = json.Unmarshal(body, &req) 123 id := req["entity_id"] 124 s.eventBus.Publish("system/entities/"+id.String(), events.EventGetLastState{ 125 EntityId: id, 126 }) 127 } 128 129 func (s *EventHandler) CommandTerminal(client stream.IStreamClient, query string, body []byte) { 130 s.eventBus.Publish("system/terminal", events.CommandTerminal{ 131 Common: events.Common{ 132 User: client.GetUser(), 133 SessionID: client.SessionID(), 134 }, 135 Text: string(body), 136 }) 137 } 138 139 func (s *EventHandler) EventGetServerVersion(client stream.IStreamClient, query string, body []byte) { 140 s.eventBus.Publish("system", events.EventGetServerVersion{ 141 Common: events.Common{ 142 User: client.GetUser(), 143 SessionID: client.SessionID(), 144 }, 145 }) 146 }