more G-Labs products

Author Topic: Solar Altitude (replaces: SunCalc)  (Read 6562 times)

May 09, 2014, 12:12:04 AM
Read 6562 times

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hi all,

after some learning with reflow/scalability from coding the OpenWeatherMap-Widget, I went back to rework the SunCalc Widget accordingly.

So here you have it: the new Solar Altitude Widget.

I gave it a new name since:

  • it was rewritten in large parts
  • I wanted to give you a chance to use the old version or try the new one in parallel without both versions influencing each other
  • this name is more self-explanatory

Most important new features include:

  • reflow / adapting display for smaller width (s. attached image)
  • custom time format AM/PM or 24h
  • improved current status display
  • consistent look with OpenWeatherMap-Widget

Let me know, what you think.

Best,
jan

« Last Edit: May 09, 2014, 01:24:43 AM by Jan »

May 09, 2014, 08:21:32 AM
Reply #1

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Very nice, I've just installed it and it looks great, now off to install the new Weather widget.  :)

May 17, 2014, 04:48:13 PM
Reply #2

dutchie

  • **
  • Information
  • Jr. Member
  • Posts: 47
Where can I find a list with the colorcodes (Hue value)?

*edit
Never mind
Didn't notice there is a slider in it  :-[
« Last Edit: May 17, 2014, 04:55:12 PM by dutchie »

May 31, 2014, 12:03:10 AM
Reply #3

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hi Gene,

I recently noticed the NaN problem appearing again (see attached screenshot). So far I could trace it back to the HG-script computing the timings and not the javascript included in the json. Since I haven't really changed anything to this part over the last releases, my question is, whether this might be linked to the javascript engine of homegenie?

Were there any changes to this part over the last updates of HG?
Or do I have to try looking some place else ? ;)

Cheers,
jan

May 31, 2014, 11:29:38 AM
Reply #4

badfinger

  • **
  • Information
  • Jr. Member
  • Posts: 32
Hi Jan,
I've got the same issue. The NaN error dissapears when i erase the longitude and latidude values. After that all works fine, but with "Timbuktu" or any else timezone ;-))

Grue

Alex


May 31, 2014, 03:20:08 PM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Jan,

this seem to be a number formatting/parsing error. You should check how the number is passed to the function that parses it.
Most commonly the decimal separator can cause such problems when using different language settings on the server (internationalization settings should be kept to en-US on raspberry to prevent such issues, still you can change the timezone if needed).

Also some other user reported me another problem with the widget. This other problem may be related to a wrong file encoding.
If someone else is experiencing same problem (see attacched picture) and know how to use the browser's developer tools, please verify the following:

1) open the widget's json and html file directly in your browser and using the Network tab in the develpers tools see what econding is sent from server (see attacched pic)

http://your_raspi_address/hg/html/pages/control/widgets/jkUtils/SolarAltitude/SolarAltitude.html
http://your_raspi_address/hg/html/pages/control/widgets/jkUtils/SolarAltitude/SolarAltitude.json

2) look both files source if text is containing wrong encoded characters.

Just to let you know, I am not experiencing none of these issues. SolarAltitude is working well for me.

g.

June 01, 2014, 08:30:07 PM
Reply #6

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Jan,

finally I updated the Android app so that now Solar Altitude and OpenWeather works.
Currently I only wrote the adapters for loading data into the standard weather and earth tools widgets.
Didn't write a specific widget for now.

I have a couple of issues though:

- the OpenWeather icons are not transparent and have white background
- there's no easy way to get the url of the Solar Altitude widget (I put a fixed icon for now)

So I would suggest to put the code for the SolarAltitude icon in the app rather than in the widget.
I will wait some more before publishing new hg and new android app, just in case you can help with this in the meantime.

Cheers,
g.

June 02, 2014, 02:56:14 PM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Jan,

I made the images transparent for the OpenWeatherMap widget (inside htmlpagescontrolwidgetsjkUtilsOpenWeatherMapimagesicons).

So if you have a chance, can you provide any tip about how to set the SolarAltitude icon image url to be used in the program code instead of the widget json?
The widget json code it's not very familiar to me =) so you may go faster in finding a solution for it.

Thanks!
g.

June 02, 2014, 11:29:42 PM
Reply #8

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hi Gene,

Thanks a lot for the effort with bringing the modules to the app as well!

I'm totally packed this week until Friday - but I?ll most definitely get back to you then with some details  on how to select the matching labels and icons.

I can also provide you with another export of the icons with transparent background in whatever size you need them since I have them here as vector graphics. Let me know whether you're fine with your converted versions or if you want me to export them again - It's no biggie.

Sorry for being so brief, but this week is really killing me.
Talk to you on Friday...

Best,
jan


June 02, 2014, 11:52:49 PM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi Jan,

transparent images are ok, no need to export. I just published r374, so no hurry, we'll fix this for the next release.
Have a good week and talk to you soon then =)

Cheers,
g.

June 12, 2014, 09:40:43 PM
Reply #10

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hi Gene,

sorry, long time and no word from my side.
I'll try to make up for this by posting the logic of how I construct the label for the current sunphase.
I extracted the relevant code and generalized it a bit and added comments to explain what is happening.
Let me know if this is self-explaining enough to adapt it for the app or if I need to elaborate a bit more. Here we go:

Code: [Select]

// used variable

var DataTimers = { // this object contains all calculated timestamps for phases start and end in a chronological order
label: // Timestamp label.
// The variable "label" simply contains the variable name from the widget settings that holds the phase time.
// e.g. "jkUtils.SolarAltitude.Evening.Nautical.Start" => "20:52"
//
  // It is used to resolve the localized value of the span with the matching attribute "data-local-id"=label from within
// the hidden settings section of the html template, e.g.
// "jkUtils.SolarAltitude.Morning.Nautical.Start" references the span containing "Nautical Dawn" as text.
time: // unix-timestamp
timeStr: // time as string in 24h format, e.g. "02:01"
timePrefix:     // 'at', 'till' or 'since'
icon: // icon file name, e.g. "Evening.Nautical.Start.png"
                                // the icon-filename itself is always the variable name without the plugin-name-prefix
                                // e.g. jkUtils.SolarAltitude.Evening.Nautical.Start => Evening.Nautical.Start.png
                                // those icons are located in the images/status sub-folder
};

// for the timePrefix part of the object:
// this is hard coded for the relevant/used phases. I currently use the following timers for display:

[ 'Morning.Night.End', 'till' ],
[ 'Morning.Astronomical.Start', 'since' ],
[ 'Morning.Nautical.Start', 'since' ],
[ 'Morning.Civil.Start', 'since' ],
[ 'Morning.Sunrise.Start', 'since' ],
[ 'Morning.Sunrise.End', 'at' ],
[ 'Morning.GoldenHour.Start', 'since' ],
[ 'Morning.GoldenHour.End', 'since' ],
[ 'Day.Noon', 'at' ],
[ 'Evening.GoldenHour.Start', 'till' ],
[ 'Evening.GoldenHour.End', 'till' ],
[ 'Evening.Sunset.Start', 'at' ],
[ 'Evening.Sunset.End', 'till' ],
[ 'Evening.Civil.End', 'till' ],
[ 'Evening.Nautical.End', 'till' ],
[ 'Evening.Astronomical.End', 'till' ],
[ 'Evening.Night.Start', 'since' ],
[ 'Night.Nadir', 'at' ]

//-------------------------------------------------------------------------
// -- START: Code for composing the phase display text

//-- Setup Variables
//
var now = new Date(), // get current timestamp
currentTime = 0,
description, offset, closestOffset;

//-- Find Closest Timestamp To Current Time
//
closestOffset = Infinity; //initially set offset to max value (infinity)
for( var i = 0; i < DataTimers.length; ++i ) { //calculate which given timestamp is closest to current timestamp
var loopTimer = DataTimers[i].time;
offset = Math.abs( +loopTimer - +now );
if ( offset < closestOffset ) {
closestOffset = offset;
currentTime = i;
}
}

//-- Exceptions: pick another timestamp under certain conditions:
//
//
//-- Jump To Next Timestamp When Current Time Is Past 'till' Or 'at'
if( now > DataTimers[currentTime].time &&
( DataTimers[currentTime].timePrefix == 'till' || DataTimers[currentTime].timePrefix == 'at' ) ) currentTime++;

//-- next Exception:
//
//
//-- Jump Back To Previous Timestamp When Time Before 'since'
if( now < this.DataTimers[currentTime].time &&
this.DataTimers[currentTime].timePrefix == 'since' ) currentTime--;


//-- Construct Current Phase Display Text
//
description += '<span>' + DataTimers[currentTime].timePrefix + '</span><span>' + DataTimers[currentTime].timeStr + '</span><br/>';

// -- END: Code for composing the phase display text
« Last Edit: June 12, 2014, 10:46:57 PM by Jan »

June 12, 2014, 10:03:06 PM
Reply #11

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Ups, I hit send to early. But it might make sense to put this in a 2nd post anyways.

So, concerning the earlier mentioned problem with the NaN Display:
I still experience this problem, unfortunately.

Quote
this seem to be a number formatting/parsing error. You should check how the number is passed to the function that parses it.
Most commonly the decimal separator can cause such problems when using different language settings on the server (internationalization settings should be kept to en-US on raspberry to prevent such issues, still you can change the timezone if needed).


Quote
1) open the widget's json and html file directly in your browser and using the Network tab in the develpers tools see what econding is sent from server

Encoding is ok. I also checked and changed the language settings. Still, the json already has "NaN" literally as a value. So it does not seem to be a parsing error in the javascript interpretation of the json.

Interestingly, the function in the HG-JS-Part is always the same. For some values it works, while for others it doesn't. Strange thing is that it is always the same calculation. The only difference is the angle of the sun above the horizon as a basis for the operation. But this always is a full integer / whole number.

One other interesting observation is that that I always get a compiler error for some releases now when I try to save the javascript in HG automation editor. This even happens when I make no changes at all. The script works as fine. But as soon as I hit save (and ever since that moment) I can't re-save the script (no matter what I do/don't change) without a compiler error.

Could you just verify, if you can re-save the script with no modifications and not getting a compile error?

I'm a bit clueless here. But this observation originally led to my earlier assumption/question on whether this might be linked to recent changes in the js-interpreter of .net.

Thanks for your input and all the best,
jan

June 15, 2014, 03:02:53 PM
Reply #12

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hi Gene,

just to let you know: I might be on to something to solve the NaN problem. (might have to do with how acos-calculation are handled by javascript).

So not need to invest any time from your side in this for the moment.

I'll get back to you when I know more

Cheers,
jan

June 21, 2014, 12:02:48 AM
Reply #13

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Ok, I think I found it.

Indeed the acos-function of Javascript was causing trouble.

To fix this, replace the getHourAngle function in line 151 of the widget script with:

Code: [Select]
function getHourAngle(h, phi, d) {
    var first = sin(h) - sin(phi) * sin(d),
        second = cos(phi) * cos(d),
        third = first / second;
    if ( third < -1 || third > 1 ) {
        third = Math.round(third);
    } 
  return acos(third);
}

or delete and reimport the widget using the fixed file attached (please note, that PID will change to value above 1000 since it then will be treated as a custom script)

Please let me know if this fixes your problem as well or if I have to continue investigating  ;)

j.

June 21, 2014, 07:19:24 AM
Reply #14

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Yep, that fixed it for me. Thanks!