-
Notifications
You must be signed in to change notification settings - Fork 18
Local Overrides Sample: Sunrise Sunset Calculation
bp2008 edited this page May 9, 2022
·
2 revisions
If this script is pasted into your ui3-local-overrides.js
file, it will add sunrise and sunset times to the left bar of the Live View tab. The times will be recalculated daily without even requiring a page refresh.
To learn more about ui3-local-overrides.js, see: Local Overrides Scripts and Styles
This script utilizes https://github.com/mourner/suncalc for Sunrise and Sunset calculations.
/* mourner/suncalc is licensed under the BSD 2-Clause "Simplified" License */
(function(){function x(a){return new Date(864E5*(a+.5-2440588))}function E(a,b){return l(d(a)*e(y)-A(b)*d(y),e(a))}function B(a,b){return F(d(b)*e(y)+e(b)*d(y)*d(a))}function G(a,b,c){return l(d(a),e(a)*d(b)-A(c)*e(b))}function H(a,b,c){return F(d(b)*d(c)+e(b)*e(c)*e(a))}function I(a){var b=f*(1.9148*d(a)+.02*d(2*a)+3E-4*d(3*a));return a+b+102.9372*f+w}function J(a){a=I(f*(357.5291+.98560028*a));return{dec:B(a,0),ra:E(a,0)}}function K(a){var b=f*(134.963+13.064993*a),c=f*(93.272+13.22935*a);a=f*(218.316+
13.176396*a)+6.289*f*d(b);c=5.128*f*d(c);b=385001-20905*e(b);return{ra:E(a,c),dec:B(a,c),dist:b}}function z(a,b){return new Date(a.valueOf()+864E5*b/24)}var w=Math.PI,d=Math.sin,e=Math.cos,A=Math.tan,F=Math.asin,l=Math.atan2,L=Math.acos,f=w/180,y=23.4397*f,h={getPosition:function(a,b,c){c=f*-c;b*=f;var d=a.valueOf()/864E5-.5+2440588-2451545;a=J(d);c=f*(280.16+360.9856235*d)-c-a.ra;return{azimuth:G(c,b,a.dec),altitude:H(c,b,a.dec)}}},C=h.times=[[-.833,"sunrise","sunset"],[-.3,"sunriseEnd","sunsetStart"],
[-6,"dawn","dusk"],[-12,"nauticalDawn","nauticalDusk"],[-18,"nightEnd","night"],[6,"goldenHourEnd","goldenHour"]];h.addTime=function(a,b,c){C.push([a,b,c])};h.getTimes=function(a,b,c){c=f*-c;b*=f;a=Math.round(a.valueOf()/864E5-.5+2440588-2451545-9E-4-c/(2*w));var g=9E-4+(0+c)/(2*w)+a,h=f*(357.5291+.98560028*g),k=I(h),M=B(k,0);g=2451545+g+.0053*d(h)-.0069*d(2*k);var u,p={solarNoon:x(g),nadir:x(g-.5)};var v=0;for(u=C.length;v<u;v+=1){var n=C[v];var q=c;var D=a;var r=h,t=k;var m=b;var l=M;m=L((d(n[0]*
f)-d(m)*d(l))/(e(m)*e(l)));q=2451545+(9E-4+(m+q)/(2*w)+D)+.0053*d(r)-.0069*d(2*t);D=g-(q-g);p[n[1]]=x(D);p[n[2]]=x(q)}return p};h.getMoonPosition=function(a,b,c){c=f*-c;b*=f;var g=a.valueOf()/864E5-.5+2440588-2451545;a=K(g);c=f*(280.16+360.9856235*g)-c-a.ra;g=H(c,b,a.dec);var h=l(d(c),A(b)*e(a.dec)-d(a.dec)*e(c)),k=g;0>k&&(k=0);g+=2.967E-4/Math.tan(k+.00312536/(k+.08901179));return{azimuth:G(c,b,a.dec),altitude:g,distance:a.dist,parallacticAngle:h}};h.getMoonIllumination=function(a){var b=(a||new Date).valueOf()/
864E5-.5+2440588-2451545;a=J(b);b=K(b);var c=L(d(a.dec)*d(b.dec)+e(a.dec)*e(b.dec)*e(a.ra-b.ra));c=l(149598E3*d(c),b.dist-149598E3*e(c));a=l(e(a.dec)*d(a.ra-b.ra),d(a.dec)*e(b.dec)-e(a.dec)*d(b.dec)*e(a.ra-b.ra));return{fraction:(1+e(c))/2,phase:.5+.5*c*(0>a?-1:1)/Math.PI,angle:a}};h.getMoonTimes=function(a,b,c,d){a=new Date(a);d?a.setUTCHours(0,0,0,0):a.setHours(0,0,0,0);d=.133*f;for(var e=h.getMoonPosition(a,b,c).altitude-d,k,g,u,p,v,n,q,l,r,t,m=1;24>=m;m+=2){k=h.getMoonPosition(z(a,m),b,c).altitude-
d;g=h.getMoonPosition(z(a,m+1),b,c).altitude-d;v=(e+g)/2-k;n=(g-e)/2;q=-n/(2*v);l=(v*q+n)*q+k;n=n*n-4*v*k;k=0;0<=n&&(t=Math.sqrt(n)/(2*Math.abs(v)),r=q-t,t=q+t,1>=Math.abs(r)&&k++,1>=Math.abs(t)&&k++,-1>r&&(r=t));1===k?0>e?u=m+r:p=m+r:2===k&&(u=m+(0>l?t:r),p=m+(0>l?r:t));if(u&&p)break;e=g}b={};u&&(b.rise=z(a,u));p&&(b.set=z(a,p));u||p||(b[0<l?"alwaysUp":"alwaysDown"]=!0);return b};"object"===typeof exports&&"undefined"!==typeof module?module.exports=h:"function"===typeof define&&define.amd?define(h):
window.SunCalc=h})();
/* UI3 sunrise/sunset implementation */
(function ($)
{
var $sunrisesunset = $('<div class="leftBarBox"></div>');
$("#layoutleftLiveScrollable").append($sunrisesunset);
$sunrisesunset.attr('style', $sunrisesunset.prev().attr('style'));
$sunrisesunset.prev().removeAttr('style');
var lat, lon, lastUpdateDay = -1;
function SetSunriseSunsetText()
{
var date = new Date();
if (lastUpdateDay != date.getDate())
{
lastUpdateDay = date.getDate();
if (lat && lon)
{
var times = SunCalc.getTimes(date, lat, lon);
$sunrisesunset.html('<div class="leftBarHeading">Rise: ' + GetTimeHoursMinutes(times.sunrise) + '</div><div class="leftBarHeading">Set: ' + GetTimeHoursMinutes(times.sunset) + '</div>');
}
}
}
// Update the text periodically
setInterval(SetSunriseSunsetText, 10000);
function GetTimeHoursMinutes(date)
{
var ampm = "AM";
var hour = date.getHours();
if (hour == 0)
hour = 12;
else if (hour == 12)
ampm = "PM";
else if (hour > 12)
{
hour -= 12;
ampm = "PM";
}
var str = hour + ":" + date.getMinutes().toString().padLeft(2, '0') + " " + ampm;
return str;
}
BI_CustomEvent.AddListener("ExecJSON_Success", function (e)
{
if (e.args.cmd === "login" && e.data && e.data.data && e.data.result === "success")
{
lat = e.data.data.latitude;
lon = e.data.data.longitude;
if (!lat && !lon)
{
toaster.Info("Sunrise/sunset calculations require your latitude and longitude to be specified in Blue Iris Options > Schedule tab.", 30000);
}
else
SetSunriseSunsetText();
}
});
})(jQuery);