Last active
July 23, 2021 14:02
-
-
Save anpin/80811ef34dffe8623d3c50a56ebaadee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Crestron.SimplSharp; | |
using Crestron.SimplSharp.Scheduler; | |
namespace APES.SSharp.Light | |
{ | |
public static partial class LightService | |
{ | |
static ScheduledEventGroup _schedule = new ScheduledEventGroup("LightService"); | |
static void ScheduleDuskOn(DateTime date) | |
{ | |
string eventName = string.Format("dusk-{0}-{1}-{2}", date.Date, date.Month, date.Year); | |
//disable previous event | |
if (_schedule.ScheduledEvents.ContainsKey(eventName)) | |
{ | |
//this might throw if event was created, but not enabled | |
_schedule.ScheduledEvents[eventName].Disable(); | |
_schedule.ScheduledEvents[eventName].Dispose(); | |
} | |
//create new event and callback | |
var duskEvent = new ScheduledEvent(eventName, _schedule); | |
duskEvent.UserCallBack += (e, reason) => | |
{ | |
if (reason == ScheduledEventCommon.eCallbackReason.NormalExpiration) | |
{ | |
_logger.InfoFormat("Dusk event fired at {0}", DateTime.Now.ToLongTimeString()); | |
ScheduleDawnOff(date.AddDays(1)); | |
//do you work here | |
} | |
}; | |
//set location, you can get it from CrestronEnviroment class | |
duskEvent.Location.Latitude = _latitude; | |
duskEvent.Location.Longitude = _longitude; | |
duskEvent.DateAndTime.SetAstronomicalEventTime(ScheduledEventCommon.eAstronomicalEvent.Sunset, date.Year, date.Month, date.Day); | |
duskEvent.Enable(); | |
ushort dawnHour, dawnMinute; | |
//this will throw ArgumentOutofRange if event is not valid | |
ScheduledEventCommon.CalculateAstronomicalTime(duskEvent, out dawnHour, out dawnMinute); | |
_logger.InfoFormat("My task is scheduled to execute at {0}:{1} {2}/{3}/{4}", dawnHour, dawnMinute, date.Date, date.Month, date.Year); | |
//this will always return false for some reason, even if event is created and will fire, so catch exception above if needed | |
if (ScheduledEventCommon.isAstronomicalEventValid(duskEvent)) | |
{ | |
_logger.ErrorFormat("Dusk event is not valid! Time is {0}:{1} {2}/{3}/{4}", dawnHour, dawnMinute, date.Date, date.Month, date.Year); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey @rahmanoff, checkout updated version with comments