-
-
Notifications
You must be signed in to change notification settings - Fork 670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Push: add custom messenger (BC) #17211
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package push | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/evcc-io/evcc/api" | ||
"github.com/evcc-io/evcc/provider" | ||
"github.com/evcc-io/evcc/util" | ||
) | ||
|
||
func init() { | ||
registry.AddCtx(api.Custom, NewConfigurableFromConfig) | ||
} | ||
|
||
// NewConfigurableFromConfig creates Messenger from config | ||
func NewConfigurableFromConfig(ctx context.Context, other map[string]interface{}) (Messenger, error) { | ||
var cc struct { | ||
Send provider.Config | ||
Encoding string | ||
} | ||
|
||
if err := util.DecodeOther(other, &cc); err != nil { | ||
return nil, err | ||
} | ||
|
||
send, err := provider.NewStringSetterFromConfig(ctx, "send", cc.Send) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewConfigurable(send, cc.Encoding) | ||
} | ||
|
||
// NewConfigurable creates a new Messenger | ||
func NewConfigurable(send func(string) error, encoding string) (*Push, error) { | ||
m := &Push{ | ||
log: util.NewLogger("push"), | ||
send: send, | ||
encoding: strings.ToLower(encoding), | ||
} | ||
return m, nil | ||
} | ||
|
||
// Push is a configurable Messenger implementation | ||
type Push struct { | ||
log *util.Logger | ||
send func(string) error | ||
encoding string | ||
} | ||
|
||
// Send implements the Messenger interface | ||
func (m *Push) Send(title, msg string) { | ||
var res string | ||
|
||
switch m.encoding { | ||
case "json": | ||
b, _ := json.Marshal(struct { | ||
Title string `json:"title"` | ||
Msg string `json:"msg"` | ||
andig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}{ | ||
Title: title, | ||
Msg: msg, | ||
}) | ||
res = string(b) | ||
case "csv": | ||
res = title + "," + msg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is too simplistic, especially when |
||
case "tsv": | ||
res = title + "\t" + msg | ||
case "title": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this type does not "fit" into the list: json, csv, tsv are formats (so kind of an encoding), "title" and "message" (the default) are more filters. Wdyt to restrict encoding to "json" and "string" (or "raw") and have some second options as to whether to include the title or not ? (usually if you only use a single service endpoint and you don't need the title, you just don't define it in |
||
res = title | ||
default: | ||
res = msg | ||
} | ||
|
||
if err := m.send(res); err != nil { | ||
m.log.ERROR.Printf("send: %v", err) | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would check if the title is empty or not defined in the config, and omit the title when empty.
This is a common use case for machine-readable payloads like JSON.
You could also allow the msg to be specified in JSON directly, to not impose your custom schema { msg: ..., title: ... } to the user. See #17278 for a proposal.