HomeGenie Forum

Automation Program Plugins and Wizard Scripting => APP Contributions => Topic started by: Jan on March 27, 2014, 11:50:01 PM

Title: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Jan on March 27, 2014, 11:50:01 PM
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:



So, this scipt does the following:




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

Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: bkenobi on March 28, 2014, 01:31:52 AM
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.
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Gene on March 28, 2014, 12:02:57 PM
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.
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Jan on March 29, 2014, 07:27:29 PM
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
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Gene on March 29, 2014, 08:23:30 PM
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.

Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Jan on March 29, 2014, 09:28:15 PM
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
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Gene on March 30, 2014, 01:04:07 AM
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.
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Jan on March 30, 2014, 01:38:02 AM
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
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Gene on March 30, 2014, 03:02:29 AM
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.


Title: SunCalc - Release V1.1
Post by: Jan on March 30, 2014, 02:27:31 PM
Ok, I changed it to 501 then  :)
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: bkenobi on March 31, 2014, 06:24:03 PM
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).
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Gene on March 31, 2014, 08:13:36 PM
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.
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Jan on March 31, 2014, 08:51:38 PM
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
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Gene on March 31, 2014, 10:26:51 PM
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.
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Jan on April 07, 2014, 01:19:44 AM
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
Title: Re: Javascript App: SunCalc (Extended local sunrise/sunset with scheduling)
Post by: Eric_S on April 26, 2014, 06:38:27 AM
Can this appear in the Android app? I just get an icon.
Title: SunCalc [deprecated]
Post by: Jan on May 09, 2014, 07:28:46 PM

================
notice of deprecation
================

The Solar Altitude Widget will replace the SunCalc Widget in future.

Accordingly, SunCalc will be considered deprecated from here on.
You may still use this widget at your own discretion, but it won't be maintained any longer.

Your can find the new widget in this thread:
http://www.homegenie.it/forum/index.php?topic=194.0 (http://www.homegenie.it/forum/index.php?topic=194.0)