add schedule config and schedule stuff

This commit is contained in:
Andrew Welker
2020-12-04 16:21:48 -07:00
parent 9888fbf047
commit f11bdcfd53
4 changed files with 143 additions and 0 deletions

View File

@@ -10,11 +10,13 @@ namespace PepperDash.Essentials.Room.Config
public string UserPin;
public string TechPin;
public string PresetsFileName;
public EssentialsRoomScheduledEventsConfig RoomScheduledEvents;
public EssentialsTechRoomConfig()
{
Displays = new List<string>();
Tuners = new List<string>();
RoomScheduledEvents = new EssentialsRoomScheduledEventsConfig();
}
}
}

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Crestron.SimplSharp;
using Crestron.SimplSharp.Scheduler;
using PepperDash.Core;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Config;
@@ -19,6 +21,8 @@ namespace PepperDash.Essentials
private readonly EssentialsTechRoomConfig _config;
private ScheduledEventGroup _roomScheduledEventGroup;
public DevicePresetsModel TunerPresets
{
get
@@ -56,6 +60,10 @@ namespace PepperDash.Essentials
_displays = GetDevices<TwoWayDisplayBase>(_config.Displays);
RoomPowerIsOnFeedback = new BoolFeedback(() => RoomPowerIsOn);
SubscribeToDisplayFeedbacks();
CreateScheduledEvents();
}
private void SubscribeToDisplayFeedbacks()
@@ -66,6 +74,66 @@ namespace PepperDash.Essentials
}
}
private void CreateScheduledEvents()
{
var eventsConfig = _config.RoomScheduledEvents;
_roomScheduledEventGroup = new ScheduledEventGroup(Key);
_roomScheduledEventGroup.RetrieveAllEvents();
Scheduler.AddEventGroup(_roomScheduledEventGroup);
foreach (var eventConfig in eventsConfig.ScheduledEvents)
{
if (!_roomScheduledEventGroup.ScheduledEvents.ContainsKey(eventConfig.Name))
{
SchedulerUtilities.CreateEventFromConfig(eventConfig, _roomScheduledEventGroup);
continue;
}
var roomEvent = _roomScheduledEventGroup.ScheduledEvents[eventConfig.Key];
if (!SchedulerUtilities.CheckEventTimeForMatch(roomEvent, DateTime.Parse(eventConfig.Time)) &&
!SchedulerUtilities.CheckEventRecurrenceForMatch(roomEvent, eventConfig.Days))
{
continue;
}
Debug.Console(1, this, "Existing event does not match new config properties. Deleting exisiting event: '{0}'", roomEvent.Name);
_roomScheduledEventGroup.DeleteEvent(roomEvent);
SchedulerUtilities.CreateEventFromConfig(eventConfig, _roomScheduledEventGroup);
}
_roomScheduledEventGroup.UserGroupCallBack += HandleScheduledEvent;
}
private void HandleScheduledEvent(ScheduledEvent schevent, ScheduledEventCommon.eCallbackReason type)
{
var eventConfig = _config.RoomScheduledEvents.ScheduledEvents.FirstOrDefault(e => e.Key == schevent.Name);
if (eventConfig == null)
{
Debug.Console(1, this, "Event with name {0} not found", schevent.Name);
return;
}
if (eventConfig.Acknowledgeable)
{
schevent.Acknowledge();
}
CrestronInvoke.BeginInvoke((o) =>
{
foreach (var a in eventConfig.Actions)
{
DeviceJsonApi.DoDeviceAction(a.Value);
}
});
}
public void RoomPowerOn()
{
foreach (var display in _displays)

View File

@@ -6,6 +6,8 @@ using Crestron.SimplSharp;
using Crestron.SimplSharp.Scheduler;
using PepperDash.Core;
using PepperDash.Essentials.Core.Fusion;
using PepperDash.Essentials.Room.Config;
namespace PepperDash.Essentials.Core
{
@@ -135,5 +137,39 @@ namespace PepperDash.Essentials.Core
return isMatch;
}
public static bool CheckEventTimeForMatch(ScheduledEvent evnt, DateTime time)
{
return evnt.DateAndTime.Hour == time.Hour && evnt.DateAndTime.Minute == time.Minute;
}
public static bool CheckEventRecurrenceForMatch(ScheduledEvent evnt, ScheduledEventCommon.eWeekDays days)
{
return evnt.Recurrence.RecurrenceDays == days;
}
public static void CreateEventFromConfig(ScheduledEventConfig config, ScheduledEventGroup group)
{
if (group == null)
{
Debug.Console(0, "Unable to create event. Group is null");
return;
}
var scheduledEvent = new ScheduledEvent(config.Key, group)
{
Acknowledgeable = config.Acknowledgeable,
Persistent = config.Persistent
};
scheduledEvent.DateAndTime.SetFirstDayOfWeek(ScheduledEventCommon.eFirstDayOfWeek.Sunday);
scheduledEvent.Recurrence.Weekly(config.Days);
var eventTime = DateTime.Parse(config.Time);
if (DateTime.Now < eventTime) eventTime.AddDays(1);
scheduledEvent.DateAndTime.SetAbsoluteEventTime(eventTime);
}
}
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Crestron.SimplSharp.Scheduler;
using Newtonsoft.Json;
using PepperDash.Essentials.Core;
namespace PepperDash.Essentials.Room.Config
{
public class EssentialsRoomScheduledEventsConfig
{
[JsonProperty("scheduledEvents")]
public List<ScheduledEventConfig> ScheduledEvents;
}
public class ScheduledEventConfig
{
[JsonProperty("key")]
public string Key;
[JsonProperty("name")]
public string Name;
[JsonProperty("days")]
public ScheduledEventCommon.eWeekDays Days;
[JsonProperty("time")]
public string Time;
[JsonProperty("actions")]
public Dictionary<string, DeviceActionWrapper> Actions;
[JsonProperty("persistent")]
public bool Persistent;
[JsonProperty("acknowledgeable")]
public bool Acknowledgeable;
}
}