Files
Essentials/src/PepperDash.Essentials.MobileControl.Messengers/Messengers/ShadeBaseMessenger.cs
Andrew Welker 8b3eda1d18 refactor: make messenger constructors more consistent
Some constructors for messengers were taking Device rather than the specific type they needed.
2025-03-26 08:55:07 -05:00

100 lines
2.9 KiB
C#

using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Essentials.Core.Shades;
using System;
namespace PepperDash.Essentials.AppServer.Messengers
{
public class IShadesOpenCloseStopMessenger : MessengerBase
{
private readonly IShadesOpenCloseStop device;
public IShadesOpenCloseStopMessenger(string key, IShadesOpenCloseStop shades, string messagePath)
: base(key, messagePath, shades as IKeyName)
{
device = shades;
}
protected override void RegisterActions()
{
base.RegisterActions();
AddAction("/fullStatus", (id, content) => SendFullStatus());
AddAction("/shadeUp", (id, content) =>
{
device.Open();
});
AddAction("/shadeDown", (id, content) =>
{
device.Close();
});
var stopDevice = device;
if (stopDevice != null)
{
AddAction("/stopOrPreset", (id, content) =>
{
stopDevice.Stop();
});
}
if (device is IShadesOpenClosedFeedback feedbackDevice)
{
feedbackDevice.ShadeIsOpenFeedback.OutputChange += new EventHandler<Core.FeedbackEventArgs>(ShadeIsOpenFeedback_OutputChange);
feedbackDevice.ShadeIsClosedFeedback.OutputChange += new EventHandler<Core.FeedbackEventArgs>(ShadeIsClosedFeedback_OutputChange);
}
}
private void ShadeIsOpenFeedback_OutputChange(object sender, Core.FeedbackEventArgs e)
{
var state = new ShadeBaseStateMessage
{
IsOpen = e.BoolValue
};
PostStatusMessage(state);
}
private void ShadeIsClosedFeedback_OutputChange(object sender, Core.FeedbackEventArgs e)
{
var state = new ShadeBaseStateMessage
{
IsClosed = e.BoolValue
};
PostStatusMessage(state);
}
private void SendFullStatus()
{
var state = new ShadeBaseStateMessage();
if (device is IShadesOpenClosedFeedback feedbackDevice)
{
state.IsOpen = feedbackDevice.ShadeIsOpenFeedback.BoolValue;
state.IsClosed = feedbackDevice.ShadeIsClosedFeedback.BoolValue;
}
PostStatusMessage(state);
}
}
public class ShadeBaseStateMessage : DeviceStateMessageBase
{
[JsonProperty("middleButtonLabel", NullValueHandling = NullValueHandling.Ignore)]
public string MiddleButtonLabel { get; set; }
[JsonProperty("isOpen", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsOpen { get; set; }
[JsonProperty("isClosed", NullValueHandling = NullValueHandling.Ignore)]
public bool? IsClosed { get; set; }
}
}