feat: Add IHasCameraMuteMessenger and IHasCameraPresetsMessenger for camera control messaging

This commit is contained in:
Neil Dorin 2026-06-08 14:33:47 -06:00
parent 48e64a291f
commit a27f604660
2 changed files with 187 additions and 0 deletions

View file

@ -0,0 +1,89 @@
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Devices.Common.Cameras;
namespace PepperDash.Essentials.AppServer.Messengers
{
/// <summary>
/// Provides a messaging bridge for devices implementing <see cref="IHasCameraMute"/>
/// </summary>
public class IHasCameraMuteMessenger : MessengerBase
{
private readonly IHasCameraMute _cameraMute;
/// <summary>
/// Initializes a new instance of the <see cref="IHasCameraMuteMessenger"/> class.
/// </summary>
/// <param name="key">The key for the messenger.</param>
/// <param name="messagePath">The message path for the messenger.</param>
/// <param name="device">The device implementing <see cref="IHasCameraMute"/>.</param>
public IHasCameraMuteMessenger(string key, string messagePath, EssentialsDevice device)
: base(key, messagePath, device)
{
_cameraMute = device as IHasCameraMute ?? throw new ArgumentNullException(nameof(device));
_cameraMute.CameraIsMutedFeedback.OutputChange += CameraIsMutedFeedback_OutputChange;
}
/// <inheritdoc />
protected override void RegisterActions()
{
base.RegisterActions();
AddAction("/fullStatus", (id, content) => SendFullStatus(id));
AddAction("/cameraMuteStatus", (id, content) => SendFullStatus(id));
AddAction("/cameraMuteOn", (id, content) => _cameraMute.CameraMuteOn());
AddAction("/cameraMuteOff", (id, content) => _cameraMute.CameraMuteOff());
AddAction("/cameraMuteToggle", (id, content) => _cameraMute.CameraMuteToggle());
}
private void CameraIsMutedFeedback_OutputChange(object sender, FeedbackEventArgs e)
{
try
{
PostStatusMessage(new IHasCameraMuteStateMessage
{
CameraIsMuted = e.BoolValue
});
}
catch (Exception ex)
{
this.LogError(ex, "Error posting camera mute state");
}
}
private void SendFullStatus(string id = null)
{
try
{
var state = new IHasCameraMuteStateMessage
{
CameraIsMuted = _cameraMute.CameraIsMutedFeedback.BoolValue
};
Task.Run(() => PostStatusMessage(state, id));
}
catch (Exception ex)
{
this.LogError(ex, "Error sending camera mute full status");
}
}
}
/// <summary>
/// State message for <see cref="IHasCameraMute"/>
/// </summary>
public class IHasCameraMuteStateMessage : DeviceStateMessageBase
{
/// <summary>
/// Gets or sets a value indicating whether the camera is muted. Null if unknown or not applicable.
/// </summary>
[JsonProperty("cameraIsMuted", NullValueHandling = NullValueHandling.Ignore)]
public bool? CameraIsMuted { get; set; }
}
}

View file

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Core.Logging;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Devices.Common.Cameras;
namespace PepperDash.Essentials.AppServer.Messengers
{
/// <summary>
/// Provides a messaging bridge for devices implementing <see cref="IHasCameraPresets"/>
/// </summary>
public class IHasCameraPresetsMessenger : MessengerBase
{
private readonly IHasCameraPresets _cameraPresets;
/// <summary>
/// Initializes a new instance of the <see cref="IHasCameraPresetsMessenger"/> class.
/// </summary>
/// <param name="key">The key for the messenger.</param>
/// <param name="messagePath">The message path for the messenger.</param>
/// <param name="device">The device implementing <see cref="IHasCameraPresets"/>.</param>
public IHasCameraPresetsMessenger(string key, string messagePath, EssentialsDevice device)
: base(key, messagePath, device)
{
_cameraPresets = device as IHasCameraPresets ?? throw new ArgumentNullException(nameof(device));
_cameraPresets.PresetsListHasChanged += PresetsListHasChanged;
}
/// <inheritdoc />
protected override void RegisterActions()
{
base.RegisterActions();
AddAction("/fullStatus", (id, content) => SendFullStatus(id));
AddAction("/presetsStatus", (id, content) => SendFullStatus(id));
AddAction("/recallPreset", (id, content) =>
{
var msg = content.ToObject<MobileControlSimpleContent<int>>();
_cameraPresets.PresetSelect(msg.Value);
});
AddAction("/savePreset", (id, content) =>
{
var msg = content.ToObject<MobileControlSimpleContent<int>>();
_cameraPresets.PresetStore(msg.Value, string.Empty);
});
}
private void PresetsListHasChanged(object sender, EventArgs e)
{
try
{
PostStatusMessage(new IHasCameraPresetsStateMessage
{
Presets = _cameraPresets.Presets
});
}
catch (Exception ex)
{
this.LogError(ex, "Error posting camera presets state");
}
}
private void SendFullStatus(string id = null)
{
try
{
var state = new IHasCameraPresetsStateMessage
{
Presets = _cameraPresets.Presets
};
Task.Run(() => PostStatusMessage(state, id));
}
catch (Exception ex)
{
this.LogError(ex, "Error sending camera presets full status");
}
}
}
/// <summary>
/// State message for <see cref="IHasCameraPresets"/>
/// </summary>
public class IHasCameraPresetsStateMessage : DeviceStateMessageBase
{
/// <summary>
/// Gets or sets the list of camera presets.
/// </summary>
[JsonProperty("presets", NullValueHandling = NullValueHandling.Ignore)]
public List<CameraPreset> Presets { get; set; }
}
}