-
-
Notifications
You must be signed in to change notification settings - Fork 678
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2fdd2a5
Push: add custom messenger
andig c5627fc
wip
andig ea3b20a
Merge branch 'master' into feat/custom-messenger
naltatis b5e2a9e
Update push/push.go
andig 03f693e
Update push/push.go
andig 0e22eae
Use encoding/csv
andig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package push | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/csv" | ||
"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 | ||
} | ||
|
||
func (m *Push) csv(separator rune, title, msg string) string { | ||
var b bytes.Buffer | ||
ww := csv.NewWriter(&b) | ||
ww.Comma = separator | ||
_ = ww.Write([]string{title, msg}) | ||
ww.Flush() | ||
return b.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,omitempty"` | ||
Msg string `json:"msg"` | ||
}{ | ||
Title: title, | ||
Msg: msg, | ||
}) | ||
res = string(b) | ||
case "csv": | ||
res = m.csv(',', title, msg) | ||
case "tsv": | ||
res = m.csv('\t', title, msg) | ||
case "title": | ||
res = title | ||
default: | ||
res = msg | ||
} | ||
|
||
if err := m.send(res); err != nil { | ||
m.log.ERROR.Printf("send: %v", err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
events:
...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.
It doesn't, but it allows returning the title instead of the message for simple command line scripts. It doesn't hurt anyway.
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 still don't buy the argument it doesn't hurt anyway, but your mileage may vary :-)