-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_events.go
90 lines (84 loc) · 3.1 KB
/
client_events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package model
import (
"context"
"encoding/json"
"fmt"
pkgmodel "github.com/c0dered273/automation-remote-controller/pkg/model"
"github.com/c0dered273/automation-remote-controller/pkg/proto"
"github.com/google/uuid"
"github.com/rs/zerolog"
)
// ClientEvents обеспечивает связь между пользователем telegram и конкретным клиентским приложением
// структура содержит каналы, которые привязаны к стриму подключенного клиентского приложения
type ClientEvents struct {
ctx context.Context
// Recv прием сообщений от клиентского приложения
Recv chan *Event
// Send отправка сообщений в клиентское приложение
Send chan *Event
// Err обработка ошибок, при появлении в канале объекта, клиентский стрим закрывается
Err chan error
// chatID идентификатор чата telegram, в который будут отправлены уведомления
chatID int64
// botNotify канал отправки сообщений непосредственно в чат пользователю
botNotify chan<- Notification
// IsNotify флаг показывает отправлять ли пользователю сообщения
IsNotify bool
logger zerolog.Logger
}
// SendAction отправить событие клиентскому приложению
func (e *ClientEvents) SendAction(a pkgmodel.ActionEvent) error {
payload, err := json.Marshal(a)
if err != nil {
return fmt.Errorf("client events: failed to marshal action, %w", err)
}
event := Event{
E: &proto.Event{
Id: uuid.NewString(),
Action: proto.Action_SWITCH,
Payload: payload,
},
Err: nil,
}
e.Send <- &event
return nil
}
// ContinuousReadAndNotify ожидает событие от клиентского приложения и передает его непосредственно в чат пользователю
func (e *ClientEvents) ContinuousReadAndNotify() {
go func() {
for {
select {
case <-e.ctx.Done():
e.Err <- e.ctx.Err()
return
case recv := <-e.Recv:
if !e.IsNotify {
continue
}
switch recv.E.Action {
case proto.Action_NOTIFICATION:
var notifyEvent pkgmodel.NotifyEvent
err := json.Unmarshal(recv.E.Payload, ¬ifyEvent)
if err != nil {
e.Err <- fmt.Errorf("client events: failed unmarshal event, %w", err)
return
}
e.botNotify <- NewNotification(e.chatID, notifyEvent.Text)
}
}
}
}()
}
// NewClientEvents создает настроенную структуру ClientEvents
func NewClientEvents(ctx context.Context, chatID int64, botNotify chan<- Notification, isNotify bool, logger zerolog.Logger) *ClientEvents {
return &ClientEvents{
ctx: ctx,
Recv: make(chan *Event),
Send: make(chan *Event),
Err: make(chan error),
chatID: chatID,
botNotify: botNotify,
IsNotify: isNotify,
logger: logger,
}
}