using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Essentials.Core;
namespace PepperDash.Essentials.AppServer.Messengers
{
///
/// Provides messaging capabilities for power control operations with feedback.
/// Handles power on/off commands and power state feedback reporting.
///
public class IHasPowerControlWithFeedbackMessenger : MessengerBase
{
private readonly IHasPowerControlWithFeedback _powerControl;
///
/// Initializes a new instance of the class.
///
/// The unique identifier for this messenger instance.
/// The message path for power control messages.
/// The device that provides power control functionality.
public IHasPowerControlWithFeedbackMessenger(string key, string messagePath, IHasPowerControlWithFeedback powerControl)
: base(key, messagePath, powerControl as IKeyName)
{
_powerControl = powerControl;
}
///
/// Sends the full power control status to connected clients.
///
public void SendFullStatus(string id = null)
{
var messageObj = new PowerControlWithFeedbackStateMessage
{
PowerState = _powerControl.PowerIsOnFeedback.BoolValue
};
PostStatusMessage(messageObj, id);
}
///
/// Registers actions for handling power control operations.
/// Includes power on, power off, power toggle, and full status reporting.
///
protected override void RegisterActions()
{
base.RegisterActions();
AddAction("/fullStatus", (id, content) => SendFullStatus(id));
_powerControl.PowerIsOnFeedback.OutputChange += PowerIsOnFeedback_OutputChange; ;
}
private void PowerIsOnFeedback_OutputChange(object sender, FeedbackEventArgs args)
{
PostStatusMessage(JToken.FromObject(new
{
powerState = args.BoolValue
})
);
}
}
///
/// Represents a power control state message containing power state information.
///
public class PowerControlWithFeedbackStateMessage : DeviceStateMessageBase
{
///
/// Gets or sets the power state of the device.
///
[JsonProperty("powerState", NullValueHandling = NullValueHandling.Ignore)]
public bool? PowerState { get; set; }
}
}