-
-
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
Plugin parameter and result chaining #7836
Conversation
The syntax for the examples has changed. See #7836 (comment) for current syntax. Examples: I can get the climate status of my car via MQTT. The value is send as plain string and needs to be converted to a boolean. I can't use jq, since that needs a JSON String. With this change, I can configure the go plugin to do the conversation. The MQTT value is added as the parameter vehicles:
- type: custom
title: MG5
name: ev1
climater:
source: go
vm: shared
script: |
remoteClimateState != "off"
param:
- name: remoteClimateState
type: string
config:
source: mqtt
topic: saic/EMAIL/vehicles/VIN/climate/remoteClimateState I have a go-eCharger, but I can't use the native plug-in, since I can't reach the charger directly from evcc. Both can reach the same MQTT Server though. To chargers:
- name: goe1
type: custom
enable:
source: go
vm: shared
script: |
ret := 1
if enable {
ret = 2
}
ret
result:
- name: enable
type: int
config:
source: mqtt
topic: go-eCharger/XXX/frc/set
phases1p3p:
source: go
vm: shared
script: |
ret := 1
if phases == 3 {
ret = 2
}
ret
result:
- name: enable
type: int
config:
source: mqtt
topic: go-eCharger/XXX/psm/set |
Really cool PR! There was a coding style discussion some time ago suggesting to avoid the if err == nil pattern where suitable. Since we're touching large parts here, maybe we should do so where it fits and doesn't make things more complicated. |
The error handling discussion is presumably this one: #6879 Correct? |
Yes, thanks for digging that out. Here's one example of a converted setter without named returns: // NewIntSetterFromConfig creates a IntSetter from config
func NewIntSetterFromConfig(param string, config Config) (func(int64) error, error) {
factory, err := registry.Get(config.Source)
if err != nil {
return nil, err
}
provider, err := factory(config.Other)
if err != nil {
return nil, err
}
prov, ok := provider.(SetIntProvider)
if !ok {
return nil, fmt.Errorf("invalid plugin source for type int: %s", config.Source)
}
return prov.IntSetter(param), nil
} /cc @panzerdev |
Changed param -> in and result -> out. Examples from above would now be: vehicles:
- type: custom
title: MG5
name: ev1
climater:
source: go
vm: shared
script: |
remoteClimateState != "off"
in:
- name: remoteClimateState
type: string
config:
source: mqtt
topic: saic/EMAIL/vehicles/VIN/climate/remoteClimateState chargers:
- name: goe1
type: custom
enable:
source: go
vm: shared
script: |
ret := 1
if enable {
ret = 2
}
ret
out:
- name: enable
type: int
config:
source: mqtt
topic: go-eCharger/XXX/frc/set
phases1p3p:
source: go
vm: shared
script: |
ret := 1
if phases == 3 {
ret = 2
}
ret
out:
- name: enable
type: int
config:
source: mqtt
topic: go-eCharger/XXX/psm/set Has a better feeling for me, what about you? |
How does this behave, when the source in the param is giving an Error? Is an Error handling in the script possible? |
Think so. Seems clearer that the top level plugin is not the end of the chain. |
No- errors are always handled pre/post plugin and break the chain. I'd say that's required if we don't want to have different kinds of plugins (which we don't). |
No custom error handling is possible, the error will bubble up to the top level plugin. In other words, the top level plugin only succeeds if all Unfortunately it's a bit different for If you have ideas on how to improve that, I'm all open for it. |
Thats just how it is.
That's still not handling. Toplevel logic will only ever be called if all plugins have their data ready? The script never sees the errors. Which is totally fine. |
Yes, top-level logic is only called if all in-plugins have delivered data. |
I have extracted the initialization code, so that the in/out plugins are only initialized once. Currently all shared code is in the javascript.go file, I will extract that into it's own file, but wanted to have minimal changes for better review ability first. Haven't done any other cleanup yet. |
f5e606f
to
d6f3e05
Compare
I did some additional cleanup, don't know if that is the right direction though. Everything is in own commits, so I can go back very easily. |
I did a bunch of simplifications and style changes, please check if I broke anything. Last piece i'm confused about is the use of generics. I get the otto vs reflect-ness of the change but do we really need to expose this? Shouldn't it be possible to use the duck-typed In other words: we should return the concrete types, not the intermediate internal values. This removes the need for the |
Thanks for the code improvements, looks really better now. I have compiled it and will test it tomorrow. Will also look into the open review comments. |
I haven't found a way to do so, without moving all the type switches into the go/javascript implementations again, which duplicates essentially the same code. But maybe there is a way, to do that cleanly. Currently we have only two plugins, that support these in/out parameter, but there might be more (thinking about the script plugin, which could set environment variables with in or something like that), and I wanted to have as less code duplication as possible to support such future extensions. |
Tested it today, everything seems to work as expected, so thus would be ready from my point of view. |
Ich häng noch an der gleichen Stelle. Beispiel go: |
Ich bekomme es nicht besser hin. Habe alles mögliche versucht, aber ich bekomme die Typkonvertierung immer nur an der Stelle hin. Die einzige alternative die ich im Moment sehe, wäre das aus dem transformation.go rauszunehmen und in die beiden Implementierungen rein zu-copy-pasten. Aber das finde ich auch nicht wirklich schön. Vielleicht kommt dir ja noch eine Idee... |
Ich habs versuche, aber ungetestet. Letzter Commit, nur für Go umgesetzt. Magst mal drauf schauen? Wenns nix taugt gerne git revert. |
They have already been converted to the correct type before
So, das ist jetzt übers Wochenende bei mir ohne Fehler gelaufen. Habe heute morgen noch ein bisschen refactored. Ist jetzt aus meiner Sicht fertig. |
Richtig genialer PR, Glückwunsch und Danke! |
/cc @VolkerK62 damit kann man schön rumspielen und Daten von a nach b schieben, konvertieren etc :) |
@tisoft hast Du Dich egtl. schon bei Slack angemeldet? |
Jetzt, ja. 😎 |
Allows to add additional
in
andout
options to a Javascript or Go Plugin.Fixes #5692
Replacement for #5730