more G-Labs products

Author Topic: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)  (Read 6890 times)

March 27, 2014, 11:50:01 PM
Read 6890 times

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Dear HomeGenie-Enthusiasts,

I?ve been curiously following this forum for a while. So I think it?s about time to take make my first move and share with you my experiments in writing an utility program in Javascript.

I started basically out of four reasons:

  • I looked for examples written in Javascript, but count?t find any.
    So I decided to try to put together one on my own, which hopefully can serve to others as a starting point.
  • I found that the EarthTools script would return wrong values as sunrise/sunset for my current location - for whatever reasons there might be.
  • As in previous posts, I also was in need of more granular time settings to perform my automation tasks
  • And last but not least: I admire and appreciate all the hard work that has gone into this project and therefore like to contribute at least some little bits and pieces. Thanks Gene!


So, this scipt does the following:

  • it locally calculates sunrise/sunset with civil, nautical and astronomical timings.
    no internet connection is needed, thanks to a BSD-licensed script from Vladimir Agafonkin I found on GitHub.
  • it sets and automatically updates timers for the scheduler service
  • it has a nice dashboard visualization which is fully localized with an german example .json file
  • it shows details on the timers meanings as well as the scheduler names as tooltips when you hover over the times in the dashboard



If you like, give it a try and let me know what you think about it.

I attached the source as well as two screenshots of how it will look like on your dashboard. I also included a little ?how to? for installation.

Thanks again for all the great work and sorry if I posted this in the wrong section - just couldn?t find any ?Share your Javascript plugins here.? yet ;-)

Best,
jan


March 28, 2014, 01:31:52 AM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
This looks really nice!  Since weather isn't always necessary, this makes it nice to have a non-internet HA server running in the background.

March 28, 2014, 12:02:57 PM
Reply #2

Gene

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

this is awesome =)
I would suggest some improvements to the suncalc.json:

1) get utility functions out of the RenderView and reference them as this.parseTime, this.updateStatusIcon. As an example see the homegenie/generic/mediareceiver.json and its PollStatus function.
2) call the HG.WebApp.Locales.Localize just once. To achieve this, add an "Initialized" field next to the Description one so that you can have code like that :

Code: [Select]
if (!this.Initialized)
{
     // put widget initialization stuff here
     var userLang = window.navigator.userLanguage || window.navigator.language;
     HG.WebApp.Locales.Localize('pages/control/widgets/jkUtils/suncalc/locales/' + userLang.toLowerCase().substring(0, 2) + '.json'); 
     this.Initialized = true;
}

as an example see the homegenie/generic/program.json.

Thanks for sharing this cool widget.
Cheers,
g.

March 29, 2014, 07:27:29 PM
Reply #3

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Hey bkenobi,
Hey Gene,

thanks for the kind response and also for the advices on how to improve.

I implemented them, also cleaned a lot of clutter and quick'n'dirty codings and changed the naming scheme of the variables so that I can mostly reconstruct all necessary variables just by parsing the html template.

Gene: Maybe you could give me one more hint, since I couldn't figure out what the equivalent to the

Code: [Select]
Program.Setup(()=>
{ ... });


in the trigger part would be in Javascript. So, I understand that this seems to be a delegate in C#. But I can't seem to replicate this in Javascript (tried with passing an object, function or redefining function with no success). Could you hint me in the right direction here?

Thanks,
jan
« Last Edit: March 30, 2014, 02:27:09 PM by Jan »

March 29, 2014, 08:23:30 PM
Reply #4

Gene

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

For now all helper class methods that require a delegate as an argument won't work from Js, or at least I didn't yet  find a way to pass a delegate from javascript engine to .Net.
I just wrote a message in the Jint forum to get help about it.
Your contribution made me think that would be nice to let users import widgets from the UI as well.
Pheraps importing from a zip archive with predetermined file structure that might contains programs and widgets files.
Btw, if you agree, I will include your program as a system program in HG and make it replace Earth Tools widget in the demo configuration.

Cheers,
g.


March 29, 2014, 09:28:15 PM
Reply #5

Jan

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

Thanks for the quick response and the info! Looking forward to it then.

For the idea of importing widgets: This would certainly be great to have. But for now - with the greatly appreciated regular release updates -  I guess it will do if you just include new widgets there and have more of your valuable time to address the matters of the other fellow users :-)

Also in respect to this: yes, absolutely feel free to include this in the next release. I feel honored and it's BSD licensed anyways  ;)

Best,
jan

March 30, 2014, 01:04:07 AM
Reply #6

Gene

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

I couldn't import the latest hgx, I had to manually edit it and modify the Address tag that was empty and arising a deserialization error:

Code: [Select]
<Address>0</Address>

g.

March 30, 2014, 01:38:02 AM
Reply #7

Jan

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

Ah, ok. I assumed the system would automatically assign the next available ID above 1000. The import of the hgx worked for me - but most likely that was because I had it installed and deleted previously and the system kept the values?!?

But now that you bring this up:
What is the official scheme for assigning IDs?
Or, in other words, what number should I give the widget?

Thanks,
jan

March 30, 2014, 03:02:29 AM
Reply #8

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Yes this is a right assumption.
Though the Address field of the ProgramBlock object must be a int number anyway, otherwise when deserializing from xml text to c# class would throw an exception.
When the importing fails, it just fails silently on the UI, so you didn't notice. But it will show the error in the log file.
Currently there is no scheme for assigning IDs.
Since your program will be included among the system ones it will have an ID < 1000.
Pheraps something > 500 could be the space for user-contributed programs. It is a very manual procedure right now.
Pick a number :)

Cheers,
g.



March 30, 2014, 02:27:31 PM
Reply #9

Jan

  • **
  • Information
  • Jr. Member
  • Posts: 34
Ok, I changed it to 501 then  :)
« Last Edit: March 30, 2014, 02:38:13 PM by Jan »

March 31, 2014, 06:24:03 PM
Reply #10

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I enabled the version Gene included in the r356 test package and it looks awesome!  One question about formatting (nit picky probably).  I'm not as used to looking at the 24:00 format and generally see 12:00 AM/PM.  Is there an easy way to have the widget check which the user prefers for the web display?  Probably needs to be included in something for the Android version as well (though I haven't even looked at it there yet, so don't know if it's even available yet).

March 31, 2014, 08:13:36 PM
Reply #11

Gene

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

I made a little modification to the SunCalc widget. I made it preserve the "dotted" notation for scheduler items name.
Also made hg ui group automatically items containing at least one dot separator.
See attached picture.
Is it ok to preserve dots?

Cheers,
g.

March 31, 2014, 08:51:38 PM
Reply #12

Jan

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

sure thing - I totally fine with the modifications. Can't even really recall why I used underscores for separations in the first place.

Quote
Also made hg ui group automatically items containing at least one dot separator.

Not quite sure what you mean by that. But again, feel free to edit it as you like. I'm sure it will be for the best.

@bkenobi:
I'll look into your request once Gene has uploaded or included his changes in the next release.

I probably have to make some small changes to the display functions anyways since the localization calls are not all triggered as expected ever since the change from polling to presistent http connection.

Thanks for the feedback !
Best,
jan

March 31, 2014, 10:26:51 PM
Reply #13

Gene

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

I will take care of updating the SunCalc widget for the next hg release in order to have a correct ui refresh.
To make the widget ui refresh you have either to call Program.Notify or Program.RaiseEvent.
When you use Parameter(...).Value = ..., this will silently update the parameter without any notification to the event system.
I also had to modify the Wundergroud and Earth Tools widget by adding a:
Code: [Select]
Program.Notify("Earth Tools", "Data updated.");
in order to trigger the widget ui refresh.
This will not work in testing/r356 due to a bug of Program.Notify that is now fixed anyway.
Program.RaiseEvent will indeed work in r356 too.

Quote
Also made hg ui group automatically items containing at least one dot separator.

If you look at the picture, you'll see there's a separator in the list that is labeled "SunCalc".
This is done automatically by detecting the first part of schedule names (eg. SunCalc.Day.Start).
The schedule item name itself will be displayed without the prefix.
Since the scheduler has not got groups facility, this seemed like a convenient way of sorting items.

g.

April 07, 2014, 01:19:44 AM
Reply #14

Jan

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

Thanks for elaborating and also adapting the script for the release!

I used this method for the OpenWeatherMap widget already and will rework the SunCalc soon.

If you look at the picture, you'll see there's a separator in the list that is labeled "SunCalc".
This is done automatically by detecting the first part of schedule names (eg. SunCalc.Day.Start).
The schedule item name itself will be displayed without the prefix.

Yep, seems like I saw that but didn't really notice   ;)

Best,
jan


Sorry, this topic is locked. Only admins and moderators can reply.