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

Login:
Pass:
 
 

Page 1 of 2
Topic:
JSON.parse()
This thread has 22 replies. Displaying posts 1 through 15.
Post 1 made on Wednesday November 13, 2019 at 08:33
randman
Long Time Member
Joined:
Posts:
June 2003
416
Does Prontoscript support a JSON.parse()? If not, I saw another thread that mentioned this: [Link: github.com]

Or does someone have a recommendation for a good JSON.parse() that can be used with Prontoscript?
OP | Post 2 made on Wednesday November 13, 2019 at 19:06
randman
Long Time Member
Joined:
Posts:
June 2003
416
I found two ways to do what I need:

1. Use json2.js from [Link: github.com] . All I had to do was pull json2.js, put it in my Pronto Libraries folder, and add the following lines to the top of the file:

/*!
@author douglascrockford
@title json2
@version 1.1
*/

This file provides JSON.parse() and stringify.

OR

2. Just use eval. For example:

var jsonObj = eval("(" + jsonText + ")");

I suppose JSON.parse is supposed to be more secure than just using eval, although non-native JSON.parse (like in json2.js) also uses eval. I suppose JSON.parse does do other checks. My use of JSON.parse is in response to user actions, so it's not something that's called often, and from my quick testing there is no noticeable negative performance impact of using JSON.parse.
Post 3 made on Thursday November 14, 2019 at 22:48
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
Here's a lightweight JSON parser that does not use eval.

[Link: code.google.com]

Original Source Code
[Link: code.google.com]

Minified version
[Link: code.google.com]

I use this to create objects from JSON markup and then use JSON.stringify() to create JSON markup for transmission outside of Pronto.
Lyndel McGee
Philips Pronto Addict/Beta Tester
OP | Post 4 made on Friday November 15, 2019 at 13:52
randman
Long Time Member
Joined:
Posts:
June 2003
416
On November 14, 2019 at 22:48, Lyndel McGee said...
Here's a lightweight JSON parser that does not use eval.

[Link: code.google.com]

Original Source Code
[Link: code.google.com]

Minified version
[Link: code.google.com]

I use this to create objects from JSON markup and then use JSON.stringify() to create JSON markup for transmission outside of Pronto.

Oh, great. Thanks!
Post 5 made on Tuesday January 21, 2020 at 03:13
Fischi
Long Time Member
Joined:
Posts:
January 2011
114
Hi,
I have added json2.js to my library.
I'm a newbie in Pronto with Json.
Can anybody help me to let the pronto communicate over json ?
My HomeSeer system is able to Json and I want to have access to it with
my TSu-9800.
A .xcf file would be fine for me so I could learn and try.

Regards,
Fischi
OP | Post 6 made on Tuesday January 21, 2020 at 11:12
randman
Long Time Member
Joined:
Posts:
June 2003
416
On January 21, 2020 at 03:13, Fischi said...
Hi,
I have added json2.js to my library.
I'm a newbie in Pronto with Json.
Can anybody help me to let the pronto communicate over json ?
My HomeSeer system is able to Json and I want to have access to it with
my TSu-9800.
A .xcf file would be fine for me so I could learn and try.

Regards,
Fischi

I have HomeSeer as well and I use http from my Pronto TSU9400 to tell HomeSeer to run events. My use case is to define HomeSeer events in HomeSeer, and then have my Pronto call those HomeSeer events. Since the response of running events from HomeSeer back to Pronto is fairly simple, I didn't need to use JSON.parse() to parse the response from HomeSeer. (But, if you have something more complicated than running events to do, then you may need to use JSON.parse() to parse HomeSeer's response).

Below is an excerpt/example of how to tell Pronto to run a HomeSeer event:

// In Main part of JavaScript:

var HomeSeer = {};
HomeSeer.lastMessageSent = "";
HomeSeer.lastResponseReceived = "";
HomeSeer.IP = "x.x.x.x"; // assign HomeSeer IP here
// Below assumes HomeSeer configured to not require user/password when
// request coming from local LAN:
HomeSeer.msgPrefix = "http://" + HomeSeer.IP + "/JSON?request=";

...

function runEvent(groupName, eventName)
{
var msg = "", msgSuffix = "";

// Use encodeURI in case groupName or eventName have spaces:
msgSuffix = "runevent&group=" + encodeURI(groupName) + "&name=" + encodeURI(eventName);
msg = HomeSeer.msgPrefix + msgSuffix;
HomeSeer.lastMessageSent = msgSuffix;
com.philips.HttpLibrary.getHTTP(msg, processResponse);
} // runEvent


function processResponse(rcvd)
{
var arg = "", copyLen;
// For runEvent response, HomeSeer returns "{ "Response":"ok" }" if it
// was successful
if (rcvd.length >= 16) {
HomeSeer.lastResponseReceived = rcvd.substr(14, 2);
}
else if (rcvd.length > 2) {
// Remove "\r\n" at the end of the string
HomeSeer.lastResponseReceived = rcvd.substr(0, rcvd.length - 2);
}
else {
// "Warning: length of string received from HomeSeer <= 2. Length = " + rcvd.length, 0);
HomeSeer.lastResponseReceived = rcvd;
}

if (HomeSeer.lastResponseReceived == "ok") {
// trace/debug string: "Command " + HomeSeer.lastMessageSent + " completed successfully.", 3);
}
else {
// trace/debug string: "ERROR. Command failed: " + HomeSeer.lastMessageSent + ".", 0);
}
} // processResponse

….

Then, from another Javscript, or from an Actions tab of Pronto, you can call:

runEvent("Some Group Name","Some Event Name");

EDIT: oops, somehow, the spacing/indentation of above code was not preserved once I submitted my response, but the code should still be fairly straightforward to see.
Post 7 made on Tuesday January 21, 2020 at 11:46
Fischi
Long Time Member
Joined:
Posts:
January 2011
114
Many thanks Randman for your useful help.
Do you also have a solution how to get HS devicevalues and strings from HS
to Pronto with json ?
Because of Scott’s weather module based on wxdata isn‘t working anymore I want to get
temperatures, weather forecasts, humidity ... from HS devices to my TSU‘s.

Thanks again,
Fischi
OP | Post 8 made on Tuesday January 21, 2020 at 16:03
randman
Long Time Member
Joined:
Posts:
June 2003
416
On January 21, 2020 at 11:46, Fischi said...
Many thanks Randman for your useful help.
Do you also have a solution how to get HS devicevalues and strings from HS
to Pronto with json ?
Because of Scott’s weather module based on wxdata isn‘t working anymore I want to get
temperatures, weather forecasts, humidity ... from HS devices to my TSU‘s.

Thanks again,
Fischi

The only thing I’ve done between Pronto and HomeSeer is for Pronto to invoke HomeSeer events (my main intent is to turn on or off lights as part of my Pronto macros).

You can look at the HomeSeer documentation for other types of requests that can be sent to HomeSeer, but the basic request mechanism of using the Pronto https library is similar. Of course, depending on the request, the HomeSeer response may be more complex, necessitating the need to parse the response using JSON.parse in the Pronto callback (and I’m sure there’s examples of doing this in this or in HomeSeer’s forum or look elsewhere online for JSON.parse examples).
OP | Post 9 made on Tuesday January 21, 2020 at 16:34
randman
Long Time Member
Joined:
Posts:
June 2003
416
Below is a (very) simple example:

// This is the callback
function processResponse(rcvd)
{
var jsonObj = JSON.parse(rcvd);
// Response to a successful http runevent request is:
// {"Response":"ok"}
// Note: actual response depends on HomeSeer JSON request (see HomeSeer doc)
var jsonObj = JSON.parse(rcvd);
if (jsonObj.Response == "ok") {
// Success!...
} else {
// Failure...
}
}
Post 10 made on Friday January 24, 2020 at 00:30
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
For weather, I’m currently researching openweatherapi.org
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 11 made on Friday January 24, 2020 at 01:30
Fischi
Long Time Member
Joined:
Posts:
January 2011
114
On January 24, 2020 at 00:30, Lyndel McGee said...
For weather, I’m currently researching openweatherapi.org

Thanks again Lyndel
I also use openweatherapi.org but for Homeseer.
Then I send the values for temperature, humidity and icon value (transvert to
icon) to mY Pronto 9400, 9600and 9800.
It would be more useful to bring the data to the pronto directly.
Do you have a aolution (.xcf) to get the openweather data directly to the pronto ?

Regards, Fischi
Post 12 made on Friday January 24, 2020 at 12:13
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
Not yet as I have to map the older wxdata condition codes to the new codes which then map to up to 10 icons.

As you don’t want to exceed more than 60 requests per minute, I have to use a caching algorithm for multiple locations. I am thinking that I want to put a Centralized NodeJS proxy onto a raspberry pi zero that all Prontos connect to.

I am planning to offer a package once I get everything worked out. I was traveling for work this week but will be back on it over the weekend.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 13 made on Friday January 24, 2020 at 16:33
Fischi
Long Time Member
Joined:
Posts:
January 2011
114
On January 24, 2020 at 12:13, Lyndel McGee said...
Not yet as I have to map the older wxdata condition codes to the new codes which then map to up to 10 icons.

As you don’t want to exceed more than 60 requests per minute, I have to use a caching algorithm for multiple locations. I am thinking that I want to put a Centralized NodeJS proxy onto a raspberry pi zero that all Prontos connect to.

I am planning to offer a package once I get everything worked out. I was traveling for work this week but will be back on it over the weekend.

Lyndel,
are you willing to share this projekt after working out ?
This would be great !
Post 14 made on Friday January 24, 2020 at 22:20
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
I am planning to offer a package for download once I get everything worked out to my liking.

For those of you who want a head start, you will need to go here and get your own personal api key. I will not be sharing my key.

[Link: home.openweathermap.org]

Also see this page that discusses pricing.

[Link: openweathermap.org]

Note that for a free key, you only get 60 requests per minute. So, let's say you have 12 locations you want weather for and to get all the info you need, it takes 5 different API calls per location (60 in total). Unless you cache the data and reuse it, you can only request weather 5 times per minute and include all 12 locations.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 15 made on Friday January 24, 2020 at 22:26
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
And finally, apologies to Randman as I hijacked his thread.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Page 1 of 2


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