nordicopen / pyeasee

Easee EV charger API python library
MIT License
39 stars 11 forks source link

Help for SignalR / Websocket #39

Closed Newan closed 3 years ago

Newan commented 3 years ago

Hello,

thanks for pyeasse. I have a question about this, which is not a issues for pyeasee. I am programming an adapter for ioBroker. Rest also works wonderfully. But You have implemented in pyeasse also the realtime changes via signalr. I can establish the basic connection, but I don't get a status change of the chargers. Is there any documentation somewhere or can you briefly help me adapt this to NodeJs?

const signalR = require("@microsoft/signalr");

let loginToken = "token";
let connection = new signalR.HubConnectionBuilder()
    .withUrl("https://api.easee.cloud/hubs/chargers", { accessTokenFactory: () => loginToken })
    .build();
connection.on("ProductUpdate", data => {
    console.log("ProductUpdae");
    console.log(data);
});
connection.on("ChargerUpdate", data => {
    console.log("ChargerUpdate");
    console.log(data);
});
connection.start()
    .then(() => {
          // Here is my PROBLEM?
          connection.send("SubscribeWithCurrentState", (data) => {
          console.log('Debug inside');
          console.log(data);
      });
    });
olalid commented 3 years ago

Not sure how your code works so not sure if this is a valid comment, but the websocket must connect first before subscribe works, and you must supply a valid product ID to the call, i.e. you can not subscribe generally to get all updates for everything, only to a specific product updates.

olalid commented 3 years ago

Hint btw, you do not need both ProductUpdate and ChargerUpdate. ProductUpdate is sufficient.

Newan commented 3 years ago

ok thanks. you send it like:

self.sr_connection.send("SubscribeWithCurrentState", [id, True])

it the seond part an array or how can i translat it?

can i send it in JSON
{ id: "4711"}

or how?

olalid commented 3 years ago

In the lib we use (https://github.com/mandrewcito/signalrcore) this is a list of all arguments that should be passed to the method (i.e. SubscribeWithCurrentState in this case). So I would suggest you read the documentation for the lib you are using to find out how to pass a list of arguments. The 2 arguments is supposed to be "product serial number" and True, id is just the variable name and is not part of what needs to be passed to the method.

Newan commented 3 years ago

Hi, thank you for the advice to rtfm ☺️, but I may have expressed myself ambiguously. I understood how to use signalr, but I miss a documentation about the commands that can be used for the easee API. Can you give me a hint on where to find those commands?

The NodeJS script runs now and work:

const signalR = require("@microsoft/signalr");

let loginToken = "<authToken>";
let connection = new signalR.HubConnectionBuilder()
    .withUrl("https://api.easee.cloud/hubs/chargers", { accessTokenFactory: () => loginToken })
    .build();

connection.on("ProductUpdate", data => {
    console.log("ProductUpdae");
    console.log(data);
});
connection.start()
    .then((data) => {
          // Here is my PROBLEM?
          connection.send("SubscribeWithCurrentState", "<SerialId>", true).then((data) => {
            console.log(data);
          });
    });
olalid commented 3 years ago

The only info about signalr that I know of exists is this: https://www.notion.so/Developer-documentation-96beaa49e5a64d5fa18d9c801a7dfc28 The command you have just sent is the only existing command over signalr as far as I know. So you listen for updates over signalr, and use the REST API for control. And the REST API you can find here: https://api.easee.cloud/index.html