Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

help needed with WebSocketClient coding in a QA


Question

Posted (edited)

Anyone that can give some assistance with coding WebSockets in a QA??

 

Trying to build a really simple QA to collect data from my “Tibber Pulse”. The intention is to capture live energy consumption and put the code in a Energy Meter QA. In the Tibber iPhone app I get updated values every 10’s second from my Tibber Pulse and to get similar in a Fibaro HC3 QA would be ideal.

 

Have never worked with WebSockets in the past so have no clue to how to do this…. But as a general concept it looks doable, but my attempt so far have only failed.

 

General simple idea is to create a QA (Energy Meter type) with WebSocket code that listens to data from my “Tibber Pulse” and simply store the data received to the QA with self:updateProperty("value", TibberDataRecieved).

 

There are some information in regards to WebSockets on the Tibber development pages:

Please login or register to see this link.

Please login or register to see this link.

 

Subscriptions

GraphQL Subscriptions is the mechanism that is used for streaming data. It is basically a layer built on top of standard web sockets. A client starts a subscription by issuing a subscription query:

Please login or register to see this code.

The server will then push results conforming to this query over the socket that is kept open between client and server.

GraphQL Subscriptions Endpoint

Please login or register to see this code.

Subscribing to data

While queries and mutations are typically handled over HTTP some data are better suited to handled as streams over a persistent connection (web socket). Subscriptions follows the same semantics as queries and mutations - the shape of what is returned/sent from the server is determined by the client when invoking the api:

Please login or register to see this code.

Since subscriptions are handled over web sockets they can be a bit more challenging to handle from the client’s perspective. Many GraphQL client libraries provide good abstractions for this though. Note that the maximum number of open websockets is limited to two.

Streaming live data from Tibber Pulse

Python example to get live streaming data from your Tibber Pulse over GraphQl. This also works for any other type of smart energy meter supported by Tibber, such as Watty. Requires ‘Python GraphQL Client’ package: pip install python-graphql-client

Please login or register to see this code.

 

Tibber GraphiQL example from (

Please login or register to see this link.

)

Please login or register to see this code.

 

Edited by JcBorgs
Added more information

17 answers to this question

Recommended Posts

  • 0
Posted

My advice would be to code it in Lua off-line on a PC/Mac so that you can debug, step through, the code and get an understanding what's going on.

(I'm myself amazed at people that can develop QAs on the HC3 with more than 50 lines of code ... it must be extremely frustrating...)

 

You could make your own emulated fibaro functions to make it similar to how it's coded on the HC3.

The trick is of course to get a web socket library that mimics the way it looks on the HC3.

My own HC3 emulator TQAE, has a websocket implementation that I believe is very similar to HC3 (I coded a web socket client for my Samsung TV and it worked when I moved it to the HC3)

You could try that if you want

 

 

  • 0
  • Inquirer
  • Posted

    @jgab

     

    We all have different challenges  and opinions, personally I think the HC3 environment works rather well to write code in, and also doing the majority of coding on a iPad..

     

    With WebSocket I am facing a big hurdle as I never written/used anything like that previously. Have no plan to make it big or complicated QA and would think that a simple code with:

    - WebSocket connect

    - WebSocket initiate the data subscription

    - WebSocket data received handle

    Would be enough for this to just establish a connection and start receive data. Would perhaps even be less than 50 lines of code?

     

    Could you perhaps share the Samsung TV websocket implementation you did so that I get view on how to actually work and implement a websocket function in the HC3_

     

    • 0
    Posted

    Please login or register to see this code.

     

    This is 100 lines of code. It's not the exact number of lines but being able to step through the code, set break-points, inspect return values on the fly. It's just the speed/productivity that gains an exponent...

    • 0
  • Inquirer
  • Posted

    @jgab

     

    Fully understand what you are pointing to with easier development process on a platform that are a bit more “developer” friendly compared to the HC3, it makes full sense to do larger development with the ability to do what you describe. Will most likely eventually move to a computer as both this Tibber Pulse QA and the Charge Amps QA I am working on are growing and becomes more complex. But for now a iPad Pro with physical keyboard works just fine.

     

    Thank you very much for sharing the Samsung WebSocket QA code, gives at least an insight how to start with working with WebSocket!

    • 0
    Posted

    Tried it before, didn't worked yet. There are too many issues to solve. How to handle the requestBody, headers and token. 

     

    Please login or register to see this code.

     

     

    • Like 1
    • 0
  • Inquirer
  • Posted

    @SmartHomeEddy

     

    Thank you very much, will try to pitch in and help or at least provide additional test data. But for sure it will be challenging and difficult to solve this.

    • 0
  • Inquirer
  • Posted (edited)

    @SmartHomeEddy

     

    Have looken into your code and as you said, it doesnt work. Have tried a couple of things but nothing really helps, it issues the connect command, the handleConnected is triggered so it seems to get some sort of connection initiated, but the imidiately triggers the handleDisconnect. For me it feels like the reason is that the Token isn’t included in the connect phase, but regardless of all the options I have tried nothing helps.

     

    Agree that the challenging part is to handle requestBody, Header and token. The documentation when it comes to webSocketClient commands on Fibaro’s web page are not providing much help there….

     

    @m.roszak You have insight on this? Trying to setup a WebSocketClient that requires “token” in the request but the documentation that you provide on the Fibaro page doesn’t show anything about how to implement this.

    Edited by JcBorgs
    • 0
    Posted

    We have prepared an option to add headers to websockets but it is not available yet, It will be from next version.
    Thats why it is not in docs.

    • 0
  • Inquirer
  • Posted
    1 hour ago, m.roszak said:

    We have prepared an option to add headers to websockets but it is not available yet, It will be from next version.
    Thats why it is not in docs.

    @m.roszakSounds good! and when  will that be released?

    • 0
    Posted

    Next beta version for HC3/HC3L/YH, no exact date yet :)

    • 0
  • Inquirer
  • Posted
    45 minutes ago, m.roszak said:

    Next beta version for HC3/HC3L/YH, no exact date yet :)

    @SmartHomeEddy

    This sound promising, right?

    Sounds at least to me that it is this that is missing today to be able to get WebSocketClient working with the Tibber Pulse? Or do you see additional issues?

    • 0
    Posted

    In the header the token can be included. There is also a requestBody where the query is handled. Hope the requestBody also gets a place in the WebSocket?

     

     

    • 0
  • Inquirer
  • Posted
    2 minutes ago, SmartHomeEddy said:

    In the header the token can be included. There is also a requestBody where the query is handled. Hope the requestBody also gets a place in the WebSocket?

     

     

    @m.roszak You have any input on this? Will it also be included in the updates in the new beta?

    • 0
    Posted

    I do not fully understood the question and I did consult this with developer, he is also not sure how to respond.

     

    The headary is set up when creating a connection:
    connect (url, headers)
    on such a created connection you can send a message:
    send (data)


    while handling incoming messages we can do by registering a handler:
    addEventListener ("dataReceived", function (data) ... end)

    as in 

    Please login or register to see this link.



    Regards,
    Michał 

    • 0
    Posted

    These are the variables for Tibber live:

    Please login or register to see this code.

     

    So in the (future) connect (url,headers) the above "url" and "headers" are handled. Then probably the above "data" variable can be handled with sent(data). 

    • 0
  • Inquirer
  • Posted
    On 4/28/2022 at 12:25 PM, m.roszak said:

    Next beta version for HC3/HC3L/YH, no exact date yet :)

    @m.roszakAny update on dates for the next Beta with the updated WebSocketClient features?

     

    • 0
  • Inquirer
  • Posted

    @m.roszak So new beta is now out with Header support in the WebSocketClient …

     

    Any documentation around how it is implemented and intended to be used?

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...