Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Previous section Next section Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Topic:
Could use a little help
This thread has 6 replies. Displaying all posts.
Post 1 made on Tuesday March 5, 2024 at 13:38
mpg7321
Regular Member
Joined:
Posts:
June 2020
111
Been trying to get feedback from my HUE system, heres the code that so far does not work, any help would be appreciated. Code runs once when page is being displayed.

var url = CF.widget("Hue_URL","HUE").label;
var socket = new TCPSocket();
socket.onConnect = function() {
prefix2 = 'GET /api/Y1Vpd5qwJVnjsRIdxFThJR0jsdcdaEvrQVe0NYM/lights'
postfix2 = ' HTTP/1.1\r\n'
try {
socket.write(prefix2+postfix2);
socket.write("HOST:+url+\r\n\r\n");
} catch(e) {
socket.close()
};
};
socket.onData = function() {
var info = socket.read();
var bright = info.split('"3":\{"state":\{"on":true,"bri":').pop().split(',"alert"')[0];
CF.widget("Hue_Level").label = bright;
};
Post 2 made on Tuesday March 5, 2024 at 16:03
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Before saying it does not work, please tell us what you have done to rule things out and provide a bit more details.

Have you used the Postman utility from a desktop to make the requests as you are doing here?

Does your label have a url like this, for example:
http[colon][slash][slash]myhost.net//api/Y1Vpd5qwJVnjsRIdxFThJR0jsdcdaEvrQVe0NYM/lights

or one that is simply the host name, for example:
myhost.net

Please also note that an http request's response comes with multiple lines with CRLF (\r\n) separators , not just the json markup. Your use of 'split' worries me and is very 'fragile' to say the least.

Have you debugged using try/catch to confirm the socekt is working? Also I don't see a socket.close call except in the 'catch' block. There also needs to be one after reading all data, otherwise you will run out of sockets.
Again, have you debugged and review the Diagnostics log for errors such as out of sockets?

Have you done System.print on the variable named 'info' as things come back?

Thanks,
L.
Lyndel McGee
Philips Pronto Addict/Beta Tester
OP | Post 3 made on Wednesday March 6, 2024 at 01:33
mpg7321
Regular Member
Joined:
Posts:
June 2020
111
I am trying to get and parse out the feedback from my Philipse Hue system. I have not tried Postman,
So I can do this command from a web browser and get data back,

http:[slash,slash]192.168.1.18:80/api/Y1Vpd5qwJVnjsRIdxFThJR0jxcrnZaEvrQVe0NYM/lights

I have tried to do an "info" dump and received no information. I did this by removing,
var bright = info.split('"3":\{"state":\{"on":true,"bri":').pop().split(',"alert"')[0];
and changed the last line to be
CF.widget("Hue_Level").label = info;

Right now I am not worried about parsing the data, work on that later.

This is an example I use in another program "Tablet Based" interface to get the feedback,
GET /api/Y1Vpd5qwJVnjsRIdxFThJR0jxcrnZaEvrQVe0NYM/lights HTTP/1.1\x0D\x0AHOST:{Set Hue IP}:80\x0D\x0A\x0D\x0A
Also I do not know where and how to do a "system print" for rdata..

I do have a close socket at the end, it just didn't realized it wasn't in the copy and paste.

Last edited by mpg7321 on March 6, 2024 13:13.
OP | Post 4 made on Wednesday March 6, 2024 at 13:35
mpg7321
Regular Member
Joined:
Posts:
June 2020
111
I got it working,

socket.onConnect = function()
{
socket.write('GET /api/Y1Vpd5qwJVnjsRIdxFThJR0jxcrnZaEvrQVe0NYM/lights/3 HTTP/1.0\r\n\r\n')
socket.close;
};
socket.onData = function() {
var info = socket.read();
var bright = info.split('"bri":').pop().split(',')[0];
CF.widget("Hue_Level").label = bright;
socket.close;
};
socket.onIOError = function(e) {
socket.close()
};
socket.connect('192.168.1.18', 80, 30);

what is the best way to grab "Parse" the string, return data being,

bla bla bla, "on": true,"bri":125, "alert": "select", bla bla bla.
my code above does work and it grabs only the number value, but what's a better way?

Last edited by mpg7321 on March 7, 2024 02:50.
Post 5 made on Thursday March 7, 2024 at 19:48
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
Use the Philips HTTP Library. There are examples in the latest DEV Guide.

This will take care of reading all the HTTP response lines and leave you with the response body, i.e. the JSON content. You can use a JSON library to do JSON.parse
on the response body to build a Javascript object structure.

I took a minified one years ago from the web and wrapped it as a PEP Library.

Check your RC Mail.

Last edited by Lyndel McGee on March 7, 2024 19:59.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 6 made on Saturday March 9, 2024 at 07:25
sebastian
Long Time Member
Joined:
Posts:
September 2003
93
I agree with Lyndel, use the HTTP library.
Depending on the kind of "post-processing" you're planning to do, adding a JSON library might be a lot of overhead.

If you only need a few single values, you can try to grab those with a regular expression.

E.g., this is how I grab the state of my Hue lights from my home automation system (Home-Assistant). It should work similar when talking to the Hue bridge directly:

var request = [];
function getState(entityId) {
    request[entityId] = new com.philips.HttpLibrary.HttpRequest();
    request[entityId].open("GET", "http://" + ha_host + ":" + ha_port + "/api/states/" + entityId, true);
    request[entityId].setRequestHeader("Authorization:", "Bearer " + ha_token);
    request[entityId].setRequestHeader("Content-Type:", "application/json");

    var state;
    request[entityId].onreadystatechange = function() {
        if (request[entityId].readyState == 4) {

            regex = /"state": "(\w+)"/;
            state = (request[entityId].responseText).match(regex)[1];
            if (state == "on") {
                CF.widget(entityId).setImage(IMG_ON);
                CF.widget(entityId).label = "on";
            } else {
                CF.widget(entityId).setImage(IMG_OFF);
                CF.widget(entityId).label = "off";
            }
        }
    }
    request[entityId].send();
};


Sebastian
Post 7 made on Sunday March 10, 2024 at 11:58
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,999
M,

If you decide you want to use the RegExp, then that is much better than parsing using splits, etc...

i've sent the json libraries to your alternate email address as well as a modified version of the Philips Http Library that support transfer encoding of 'chunked'.
Lyndel McGee
Philips Pronto Addict/Beta Tester


Jump to


Protected Feature Before you can reply to a message...
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now.

Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.

Hosting Services by ipHouse