Skip to content
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

Add javascript support to the http plugin #1944

Merged
merged 10 commits into from
Nov 28, 2021

Conversation

DerAndereAndi
Copy link
Contributor

This allows to run javascript code on the data returned by the HTTP call and optionally processed via jq

This allows e.g. the senec device to be configured without using external scripts that runs curl and python

The data is provided to the script env via an input variable

This allows to run javascript code on the data returned by the HTTP call and optionally processed via jq

This allows e.g. the senec device to be configured without using external scripts that runs curl and python

The data is provided to the script env via an input variable
@DerAndereAndi DerAndereAndi added the enhancement New feature or request label Nov 27, 2021
@DerAndereAndi DerAndereAndi marked this pull request as draft November 27, 2021 19:26
@DerAndereAndi
Copy link
Contributor Author

This PR allows the Senec device to be setup like this:

- name: grid
  type: custom
  power:
    source: http
    uri: http://192.168.xxx.xxx/lala.cgi
    method: POST
    headers:
    - content-type: application/json
    body: '{"ENERGY":{"GUI_GRID_POW":""}}'
    jq: .ENERGY.GUI_GRID_POW | sub("fl_"; "0x")
    script: |
      var s = input >> 31 ? -1 : 1;var e = (input >> 23) & 0xFF;s * (input & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, (e - 127))
    scale: -1

instead of

- type: custom
  power:
    source: script
    cmd: >
      /bin/bash -c "set +H; curl --data '{\"ENERGY\":{\"GUI_GRID_POW\":\"\"}}' --header \"Content-Type: application/json\" --request POST http://192.0.2.2/lala.cgi | jq .ENERGY.GUI_GRID_POW | python3 -c 'import struct;print(struct.unpack(\"!f\",bytes.fromhex(input()[4:12]))[0])'"
    timeout: 5s
    scale: -1

Making the configuration independent of the system evcc is running on.

@andig
Copy link
Member

andig commented Nov 27, 2021

Whoah… das schau ich mir morgen an 👍🏻

@andig
Copy link
Member

andig commented Nov 27, 2021

Was tut das sub bei jq?

@DerAndereAndi
Copy link
Contributor Author

Ersetzt fl_ mit 0x im Resultat.

@andig
Copy link
Member

andig commented Nov 28, 2021

Ich meine jetzt verstanden zu haben, dass es sich um eine HEX zu Foat32 Umwandlung handelt. Die könnte ggf auch die entsprechenden Funktionen aus Modbus nutzen: https://github.com/evcc-io/evcc/blob/master/util/modbus/modbus.go#L267

@DerAndereAndi
Copy link
Contributor Author

Das ist in dem Beispiel richtig. Aber dazu müsste man dann eine Property decode zur Verfügung stellen und da eine Auswahl an solchen Funktionen anbieten. Könnte man zusätzlich machen. Aber ich denke mit der script Funktion erreicht man auch zusätzlich noch eine höhere Flexibilität und Host-Platform Unabhängigkeit für z.b. decodings oder Fälle an die wir momentan nicht denken können.

@DerAndereAndi
Copy link
Contributor Author

Mit decode würde das Beispiel jetzt noch einfacher, aber denke JS script ist weiterhin eine gute Option um es drin zu lassen:

- name: grid
  type: custom
  power:
    source: http
    uri: http://192.168.xxx.xxx/lala.cgi
    method: POST
    headers:
    - content-type: application/json
    body: '{"ENERGY":{"GUI_GRID_POW":""}}'
    jq: .ENERGY.GUI_GRID_POW | sub("fl_"; "")
    decode: float32

@DerAndereAndi
Copy link
Contributor Author

decode erwartet momentan einen hex String. Frage wäre hier ob man da auch etwas anderes berücksichtigen sollte. Und wenn ja, was.

@andig
Copy link
Member

andig commented Nov 28, 2021

decode erwartet momentan einen hex String. Frage wäre hier ob man da auch etwas anderes berücksichtigen sollte. Und wenn ja, was.

Das müssten sicher 2 Operationen sein. Hex auspacken und Wert interpretieren. Evtl decode als Liste?

@DerAndereAndi
Copy link
Contributor Author

Liste finde ich merkwürdig, habs mal mit unpack separiert. Daraus wird dann:

- name: grid
  type: custom
  power:
    source: http
    uri: http://192.168.xxx.xxx/lala.cgi
    method: POST
    headers:
    - content-type: application/json
    body: '{"ENERGY":{"GUI_GRID_POW":""}}'
    jq: .ENERGY.GUI_GRID_POW | sub("fl_"; "")
    unpack: hex
    decode: float32

provider/http.go Outdated Show resolved Hide resolved
provider/http.go Outdated Show resolved Hide resolved
provider/http.go Outdated Show resolved Hide resolved
provider/http.go Outdated Show resolved Hide resolved
@andig
Copy link
Member

andig commented Nov 28, 2021

LGTM

@DerAndereAndi DerAndereAndi marked this pull request as ready for review November 28, 2021 19:39
@DerAndereAndi DerAndereAndi merged commit 0b248d5 into master Nov 28, 2021
@DerAndereAndi DerAndereAndi deleted the feature/http-plugin-javascript branch November 28, 2021 21:23
dontbyte pushed a commit to dontbyte/evcc that referenced this pull request Aug 2, 2022
This allows to run javascript code on the data returned by the HTTP call, optionally processed via jq and decoded, e.g. hex values into ints and floats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants