2020-07-13 05:25:48 +00:00
|
|
|
package bbgo
|
|
|
|
|
2022-06-09 07:49:13 +00:00
|
|
|
import (
|
2022-08-03 06:10:56 +00:00
|
|
|
"bytes"
|
|
|
|
|
2022-06-09 07:49:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-06-13 03:20:29 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2022-06-09 07:49:13 +00:00
|
|
|
)
|
2022-06-04 17:09:31 +00:00
|
|
|
|
2022-06-19 04:29:36 +00:00
|
|
|
var Notification = &Notifiability{
|
|
|
|
SymbolChannelRouter: NewPatternChannelRouter(nil),
|
|
|
|
SessionChannelRouter: NewPatternChannelRouter(nil),
|
|
|
|
ObjectChannelRouter: NewObjectChannelRouter(),
|
|
|
|
}
|
|
|
|
|
|
|
|
func Notify(obj interface{}, args ...interface{}) {
|
|
|
|
Notification.Notify(obj, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotifyTo(channel string, obj interface{}, args ...interface{}) {
|
|
|
|
Notification.NotifyTo(channel, obj, args...)
|
|
|
|
}
|
|
|
|
|
2022-08-03 06:10:56 +00:00
|
|
|
func SendPhoto(buffer *bytes.Buffer) {
|
|
|
|
Notification.SendPhoto(buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendPhotoTo(channel string, buffer *bytes.Buffer) {
|
|
|
|
Notification.SendPhotoTo(channel, buffer)
|
|
|
|
}
|
|
|
|
|
2020-07-16 07:36:02 +00:00
|
|
|
type Notifier interface {
|
2021-05-12 04:37:48 +00:00
|
|
|
NotifyTo(channel string, obj interface{}, args ...interface{})
|
|
|
|
Notify(obj interface{}, args ...interface{})
|
2022-08-03 06:10:56 +00:00
|
|
|
SendPhotoTo(channel string, buffer *bytes.Buffer)
|
|
|
|
SendPhoto(buffer *bytes.Buffer)
|
2020-07-16 07:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NullNotifier struct{}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *NullNotifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {}
|
2020-10-22 02:47:54 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *NullNotifier) Notify(obj interface{}, args ...interface{}) {}
|
2020-10-22 02:54:03 +00:00
|
|
|
|
2022-08-03 06:10:56 +00:00
|
|
|
func (n *NullNotifier) SendPhoto(buffer *bytes.Buffer) {}
|
|
|
|
|
|
|
|
func (n *NullNotifier) SendPhotoTo(channel string, buffer *bytes.Buffer) {}
|
|
|
|
|
2020-10-22 02:54:03 +00:00
|
|
|
type Notifiability struct {
|
2020-10-27 01:24:59 +00:00
|
|
|
notifiers []Notifier
|
2021-03-16 10:39:18 +00:00
|
|
|
SessionChannelRouter *PatternChannelRouter `json:"-"`
|
|
|
|
SymbolChannelRouter *PatternChannelRouter `json:"-"`
|
|
|
|
ObjectChannelRouter *ObjectChannelRouter `json:"-"`
|
2020-10-27 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
// RouteSymbol routes symbol name to channel
|
2020-10-27 01:24:59 +00:00
|
|
|
func (m *Notifiability) RouteSymbol(symbol string) (channel string, ok bool) {
|
2020-11-17 00:19:22 +00:00
|
|
|
if m.SymbolChannelRouter != nil {
|
|
|
|
return m.SymbolChannelRouter.Route(symbol)
|
|
|
|
}
|
|
|
|
return "", false
|
2020-10-27 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 00:19:22 +00:00
|
|
|
// RouteSession routes Session name to channel
|
2020-10-27 01:24:59 +00:00
|
|
|
func (m *Notifiability) RouteSession(session string) (channel string, ok bool) {
|
2020-11-17 00:19:22 +00:00
|
|
|
if m.SessionChannelRouter != nil {
|
|
|
|
return m.SessionChannelRouter.Route(session)
|
|
|
|
}
|
|
|
|
return "", false
|
2020-10-27 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:24:27 +00:00
|
|
|
// RouteObject routes object to channel
|
2020-10-27 01:24:59 +00:00
|
|
|
func (m *Notifiability) RouteObject(obj interface{}) (channel string, ok bool) {
|
2020-11-17 00:19:22 +00:00
|
|
|
if m.ObjectChannelRouter != nil {
|
|
|
|
return m.ObjectChannelRouter.Route(obj)
|
|
|
|
}
|
|
|
|
return "", false
|
2020-10-22 02:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 21:24:27 +00:00
|
|
|
// AddNotifier adds the notifier that implements the Notifier interface.
|
2020-10-22 02:54:03 +00:00
|
|
|
func (m *Notifiability) AddNotifier(notifier Notifier) {
|
|
|
|
m.notifiers = append(m.notifiers, notifier)
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (m *Notifiability) Notify(obj interface{}, args ...interface{}) {
|
2022-06-04 17:09:31 +00:00
|
|
|
if str, ok := obj.(string); ok {
|
2022-06-13 03:20:29 +00:00
|
|
|
simpleArgs := util.FilterSimpleArgs(args)
|
2022-06-09 04:31:50 +00:00
|
|
|
logrus.Infof(str, simpleArgs...)
|
2022-06-04 17:09:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:54:03 +00:00
|
|
|
for _, n := range m.notifiers {
|
2021-05-12 04:37:48 +00:00
|
|
|
n.Notify(obj, args...)
|
2020-10-22 06:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (m *Notifiability) NotifyTo(channel string, obj interface{}, args ...interface{}) {
|
2020-10-22 06:45:05 +00:00
|
|
|
for _, n := range m.notifiers {
|
2021-05-12 04:37:48 +00:00
|
|
|
n.NotifyTo(channel, obj, args...)
|
2020-10-22 02:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-03 06:10:56 +00:00
|
|
|
|
|
|
|
func (m *Notifiability) SendPhoto(buffer *bytes.Buffer) {
|
|
|
|
for _, n := range m.notifiers {
|
|
|
|
n.SendPhoto(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Notifiability) SendPhotoTo(channel string, buffer *bytes.Buffer) {
|
|
|
|
for _, n := range m.notifiers {
|
|
|
|
n.SendPhotoTo(channel, buffer)
|
|
|
|
}
|
|
|
|
}
|