refactor: make messenger constructors more consistent

Some constructors for messengers were taking Device rather than the specific type they needed.
This commit is contained in:
Andrew Welker
2025-03-26 08:55:07 -05:00
parent 6c710dd209
commit 8b3eda1d18
26 changed files with 59 additions and 112 deletions

View File

@@ -8,9 +8,9 @@ namespace PepperDash.Essentials.Room.MobileControl
{ {
private readonly IChannel channelDevice; private readonly IChannel channelDevice;
public IChannelMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public IChannelMessenger(string key, string messagePath, IChannel device) : base(key, messagePath, device as IKeyName)
{ {
channelDevice = device as IChannel; channelDevice = device;
} }
protected override void RegisterActions() protected override void RegisterActions()

View File

@@ -7,7 +7,7 @@ namespace PepperDash.Essentials.Room.MobileControl
public class IColorMessenger : MessengerBase public class IColorMessenger : MessengerBase
{ {
private readonly IColor colorDevice; private readonly IColor colorDevice;
public IColorMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public IColorMessenger(string key, string messagePath, IColor device) : base(key, messagePath, device as IKeyName)
{ {
colorDevice = device as IColor; colorDevice = device as IColor;
} }

View File

@@ -7,9 +7,9 @@ namespace PepperDash.Essentials.Room.MobileControl
public class IDPadMessenger : MessengerBase public class IDPadMessenger : MessengerBase
{ {
private readonly IDPad dpadDevice; private readonly IDPad dpadDevice;
public IDPadMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public IDPadMessenger(string key, string messagePath, IDPad device) : base(key, messagePath, device as IKeyName)
{ {
dpadDevice = device as IDPad; dpadDevice = device;
} }

View File

@@ -7,9 +7,9 @@ namespace PepperDash.Essentials.Room.MobileControl
public class IDvrMessenger : MessengerBase public class IDvrMessenger : MessengerBase
{ {
private readonly IDvr dvrDevice; private readonly IDvr dvrDevice;
public IDvrMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public IDvrMessenger(string key, string messagePath, IDvr device) : base(key, messagePath, device as IKeyName)
{ {
dvrDevice = device as IDvr; dvrDevice = device;
} }
protected override void RegisterActions() protected override void RegisterActions()

View File

@@ -7,9 +7,9 @@ namespace PepperDash.Essentials.Room.MobileControl
public class IHasPowerMessenger : MessengerBase public class IHasPowerMessenger : MessengerBase
{ {
private readonly IHasPowerControl powerDevice; private readonly IHasPowerControl powerDevice;
public IHasPowerMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public IHasPowerMessenger(string key, string messagePath, IHasPowerControl device) : base(key, messagePath, device as IKeyName)
{ {
powerDevice = device as IHasPowerControl; powerDevice = device;
} }
protected override void RegisterActions() protected override void RegisterActions()

View File

@@ -7,9 +7,9 @@ namespace PepperDash.Essentials.Room.MobileControl
public class INumericKeypadMessenger : MessengerBase public class INumericKeypadMessenger : MessengerBase
{ {
private readonly INumericKeypad keypadDevice; private readonly INumericKeypad keypadDevice;
public INumericKeypadMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public INumericKeypadMessenger(string key, string messagePath, INumericKeypad device) : base(key, messagePath, device as IKeyName)
{ {
keypadDevice = device as INumericKeypad; keypadDevice = device;
} }
protected override void RegisterActions() protected override void RegisterActions()

View File

@@ -7,9 +7,9 @@ namespace PepperDash.Essentials.Room.MobileControl
public class ISetTopBoxControlsMessenger : MessengerBase public class ISetTopBoxControlsMessenger : MessengerBase
{ {
private readonly ISetTopBoxControls stbDevice; private readonly ISetTopBoxControls stbDevice;
public ISetTopBoxControlsMessenger(string key, string messagePath, IKeyName device) : base(key, messagePath, device) public ISetTopBoxControlsMessenger(string key, string messagePath, ISetTopBoxControls device) : base(key, messagePath, device as IKeyName)
{ {
stbDevice = device as ISetTopBoxControls; stbDevice = device;
} }
protected override void RegisterActions() protected override void RegisterActions()

View File

@@ -7,9 +7,9 @@ namespace PepperDash.Essentials.Room.MobileControl
public class ITransportMessenger : MessengerBase public class ITransportMessenger : MessengerBase
{ {
private readonly ITransport transportDevice; private readonly ITransport transportDevice;
public ITransportMessenger(string key, string messagePath, Device device) : base(key, messagePath, device) public ITransportMessenger(string key, string messagePath, ITransport device) : base(key, messagePath, device as IKeyName)
{ {
transportDevice = device as ITransport; transportDevice = device;
} }
protected override void RegisterActions() protected override void RegisterActions()

View File

@@ -13,7 +13,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly IEssentialsRoomCombiner _roomCombiner; private readonly IEssentialsRoomCombiner _roomCombiner;
public IEssentialsRoomCombinerMessenger(string key, string messagePath, IEssentialsRoomCombiner roomCombiner) public IEssentialsRoomCombinerMessenger(string key, string messagePath, IEssentialsRoomCombiner roomCombiner)
: base(key, messagePath, roomCombiner as Device) : base(key, messagePath, roomCombiner as IKeyName)
{ {
_roomCombiner = roomCombiner; _roomCombiner = roomCombiner;
} }

View File

@@ -10,7 +10,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly IHasPowerControlWithFeedback _powerControl; private readonly IHasPowerControlWithFeedback _powerControl;
public IHasPowerControlWithFeedbackMessenger(string key, string messagePath, IHasPowerControlWithFeedback powerControl) public IHasPowerControlWithFeedbackMessenger(string key, string messagePath, IHasPowerControlWithFeedback powerControl)
: base(key, messagePath, powerControl as Device) : base(key, messagePath, powerControl as IKeyName)
{ {
_powerControl = powerControl; _powerControl = powerControl;
} }

View File

@@ -12,7 +12,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
public IHasScheduleAwareness ScheduleSource { get; private set; } public IHasScheduleAwareness ScheduleSource { get; private set; }
public IHasScheduleAwarenessMessenger(string key, IHasScheduleAwareness scheduleSource, string messagePath) public IHasScheduleAwarenessMessenger(string key, IHasScheduleAwareness scheduleSource, string messagePath)
: base(key, messagePath, scheduleSource as Device) : base(key, messagePath, scheduleSource as IKeyName)
{ {
ScheduleSource = scheduleSource ?? throw new ArgumentNullException("scheduleSource"); ScheduleSource = scheduleSource ?? throw new ArgumentNullException("scheduleSource");
ScheduleSource.CodecSchedule.MeetingsListHasChanged += new EventHandler<EventArgs>(CodecSchedule_MeetingsListHasChanged); ScheduleSource.CodecSchedule.MeetingsListHasChanged += new EventHandler<EventArgs>(CodecSchedule_MeetingsListHasChanged);

View File

@@ -10,7 +10,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly IHumiditySensor device; private readonly IHumiditySensor device;
public IHumiditySensorMessenger(string key, IHumiditySensor device, string messagePath) public IHumiditySensorMessenger(string key, IHumiditySensor device, string messagePath)
: base(key, messagePath, device as Device) : base(key, messagePath, device as IKeyName)
{ {
this.device = device; this.device = device;
} }

View File

@@ -10,7 +10,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
public class ILevelControlsMessenger : MessengerBase public class ILevelControlsMessenger : MessengerBase
{ {
private ILevelControls levelControlsDevice; private ILevelControls levelControlsDevice;
public ILevelControlsMessenger(string key, string messagePath, ILevelControls device) : base(key, messagePath, device as Device) public ILevelControlsMessenger(string key, string messagePath, ILevelControls device) : base(key, messagePath, device as IKeyName)
{ {
levelControlsDevice = device; levelControlsDevice = device;
} }

View File

@@ -16,7 +16,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
public class IMatrixRoutingMessenger : MessengerBase public class IMatrixRoutingMessenger : MessengerBase
{ {
private readonly IMatrixRouting matrixDevice; private readonly IMatrixRouting matrixDevice;
public IMatrixRoutingMessenger(string key, string messagePath, IMatrixRouting device) : base(key, messagePath, device as Device) public IMatrixRoutingMessenger(string key, string messagePath, IMatrixRouting device) : base(key, messagePath, device as IKeyName)
{ {
matrixDevice = device; matrixDevice = device;
} }

View File

@@ -12,7 +12,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly IProjectorScreenLiftControl device; private readonly IProjectorScreenLiftControl device;
public IProjectorScreenLiftControlMessenger(string key, string messagePath, IProjectorScreenLiftControl screenLiftDevice) public IProjectorScreenLiftControlMessenger(string key, string messagePath, IProjectorScreenLiftControl screenLiftDevice)
: base(key, messagePath, screenLiftDevice as Device) : base(key, messagePath, screenLiftDevice as IKeyName)
{ {
device = screenLiftDevice; device = screenLiftDevice;
} }

View File

@@ -15,7 +15,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
public IRunRouteAction RoutingDevice { get; private set; } public IRunRouteAction RoutingDevice { get; private set; }
public RunRouteActionMessenger(string key, IRunRouteAction routingDevice, string messagePath) public RunRouteActionMessenger(string key, IRunRouteAction routingDevice, string messagePath)
: base(key, messagePath, routingDevice as Device) : base(key, messagePath, routingDevice as IKeyName)
{ {
RoutingDevice = routingDevice ?? throw new ArgumentNullException("routingDevice"); RoutingDevice = routingDevice ?? throw new ArgumentNullException("routingDevice");

View File

@@ -9,10 +9,10 @@ namespace PepperDash.Essentials.AppServer.Messengers
public class ISelectableItemsMessenger<TKey> : MessengerBase public class ISelectableItemsMessenger<TKey> : MessengerBase
{ {
private static readonly JsonSerializer serializer = new JsonSerializer { Converters = { new StringEnumConverter() } }; private static readonly JsonSerializer serializer = new JsonSerializer { Converters = { new StringEnumConverter() } };
private ISelectableItems<TKey> itemDevice; private readonly ISelectableItems<TKey> itemDevice;
private readonly string _propName; private readonly string _propName;
public ISelectableItemsMessenger(string key, string messagePath, ISelectableItems<TKey> device, string propName) : base(key, messagePath, device as Device) public ISelectableItemsMessenger(string key, string messagePath, ISelectableItems<TKey> device, string propName) : base(key, messagePath, device as IKeyName)
{ {
itemDevice = device; itemDevice = device;
_propName = propName; _propName = propName;

View File

@@ -10,7 +10,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly IShutdownPromptTimer _room; private readonly IShutdownPromptTimer _room;
public IShutdownPromptTimerMessenger(string key, string messagePath, IShutdownPromptTimer room) public IShutdownPromptTimerMessenger(string key, string messagePath, IShutdownPromptTimer room)
: base(key, messagePath, room as Device) : base(key, messagePath, room as IKeyName)
{ {
_room = room; _room = room;
} }

View File

@@ -11,7 +11,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly ISwitchedOutput device; private readonly ISwitchedOutput device;
public ISwitchedOutputMessenger(string key, ISwitchedOutput device, string messagePath) public ISwitchedOutputMessenger(string key, ISwitchedOutput device, string messagePath)
: base(key, messagePath, device as Device) : base(key, messagePath, device as IKeyName)
{ {
this.device = device; this.device = device;
} }

View File

@@ -9,7 +9,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly ITechPassword _room; private readonly ITechPassword _room;
public ITechPasswordMessenger(string key, string messagePath, ITechPassword room) public ITechPasswordMessenger(string key, string messagePath, ITechPassword room)
: base(key, messagePath, room as Device) : base(key, messagePath, room as IKeyName)
{ {
_room = room; _room = room;
} }

View File

@@ -10,7 +10,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly ITemperatureSensor device; private readonly ITemperatureSensor device;
public ITemperatureSensorMessenger(string key, ITemperatureSensor device, string messagePath) public ITemperatureSensorMessenger(string key, ITemperatureSensor device, string messagePath)
: base(key, messagePath, device as Device) : base(key, messagePath, device as IKeyName)
{ {
this.device = device; this.device = device;
} }

View File

@@ -11,7 +11,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
protected ILightingScenes Device { get; private set; } protected ILightingScenes Device { get; private set; }
public ILightingScenesMessenger(string key, ILightingScenes device, string messagePath) public ILightingScenesMessenger(string key, ILightingScenes device, string messagePath)
: base(key, messagePath, device as Device) : base(key, messagePath, device as IKeyName)
{ {
Device = device ?? throw new ArgumentNullException("device"); Device = device ?? throw new ArgumentNullException("device");
Device.LightingSceneChange += new EventHandler<LightingSceneChangeEventArgs>(LightingDevice_LightingSceneChange); Device.LightingSceneChange += new EventHandler<LightingSceneChangeEventArgs>(LightingDevice_LightingSceneChange);

View File

@@ -14,7 +14,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
public RoomEventScheduleMessenger(string key, string messagePath, IRoomEventSchedule room) public RoomEventScheduleMessenger(string key, string messagePath, IRoomEventSchedule room)
: base(key, messagePath, room as Device) : base(key, messagePath, room as IKeyName)
{ {
_room = room; _room = room;
} }

View File

@@ -10,7 +10,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
private readonly IShadesOpenCloseStop device; private readonly IShadesOpenCloseStop device;
public IShadesOpenCloseStopMessenger(string key, IShadesOpenCloseStop shades, string messagePath) public IShadesOpenCloseStopMessenger(string key, IShadesOpenCloseStop shades, string messagePath)
: base(key, messagePath, shades as Device) : base(key, messagePath, shades as IKeyName)
{ {
device = shades; device = shades;
} }

View File

@@ -9,12 +9,8 @@ namespace PepperDash.Essentials.AppServer.Messengers
{ {
private readonly TwoWayDisplayBase _display; private readonly TwoWayDisplayBase _display;
public TwoWayDisplayBaseMessenger(string key, string messagePath) : base(key, messagePath)
{
}
public TwoWayDisplayBaseMessenger(string key, string messagePath, TwoWayDisplayBase display) public TwoWayDisplayBaseMessenger(string key, string messagePath, TwoWayDisplayBase display)
: this(key, messagePath) : base(key, messagePath, display)
{ {
_display = display; _display = display;
} }

View File

@@ -566,18 +566,16 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is ISetTopBoxControls) if (device is ISetTopBoxControls stbDevice)
{ {
this.LogVerbose( this.LogVerbose(
"Adding ISetTopBoxControlMessenger for {deviceKey}" "Adding ISetTopBoxControlMessenger for {deviceKey}"
); );
var dev = device as Device;
var messenger = new ISetTopBoxControlsMessenger( var messenger = new ISetTopBoxControlsMessenger(
$"{device.Key}-stb-{Key}", $"{device.Key}-stb-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev stbDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -585,18 +583,16 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is IChannel) if (device is IChannel channelDevice)
{ {
this.LogVerbose( this.LogVerbose(
"Adding IChannelMessenger for {deviceKey}", device.Key "Adding IChannelMessenger for {deviceKey}", device.Key
); );
var dev = device as PepperDash.Core.Device;
var messenger = new IChannelMessenger( var messenger = new IChannelMessenger(
$"{device.Key}-channel-{Key}", $"{device.Key}-channel-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev channelDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -604,16 +600,14 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is IColor) if (device is IColor colorDevice)
{ {
this.LogVerbose("Adding IColorMessenger for {deviceKey}", device.Key); this.LogVerbose("Adding IColorMessenger for {deviceKey}", device.Key);
var dev = device as PepperDash.Core.Device;
var messenger = new IColorMessenger( var messenger = new IColorMessenger(
$"{device.Key}-color-{Key}", $"{device.Key}-color-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev colorDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -621,16 +615,14 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is IDPad) if (device is IDPad dPadDevice)
{ {
this.LogVerbose("Adding IDPadMessenger for {deviceKey}", device.Key); this.LogVerbose("Adding IDPadMessenger for {deviceKey}", device.Key);
var dev = device as PepperDash.Core.Device;
var messenger = new IDPadMessenger( var messenger = new IDPadMessenger(
$"{device.Key}-dPad-{Key}", $"{device.Key}-dPad-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev dPadDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -638,16 +630,14 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is INumericKeypad) if (device is INumericKeypad nkDevice)
{ {
this.LogVerbose("Adding INumericKeyapdMessenger for {deviceKey}", device.Key); this.LogVerbose("Adding INumericKeyapdMessenger for {deviceKey}", device.Key);
var dev = device as PepperDash.Core.Device;
var messenger = new INumericKeypadMessenger( var messenger = new INumericKeypadMessenger(
$"{device.Key}-numericKeypad-{Key}", $"{device.Key}-numericKeypad-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev nkDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -655,16 +645,14 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is IHasPowerControl) if (device is IHasPowerControl pcDevice)
{ {
this.LogVerbose("Adding IHasPowerControlMessenger for {deviceKey}", device.Key); this.LogVerbose("Adding IHasPowerControlMessenger for {deviceKey}", device.Key);
var dev = device as PepperDash.Core.Device;
var messenger = new IHasPowerMessenger( var messenger = new IHasPowerMessenger(
$"{device.Key}-powerControl-{Key}", $"{device.Key}-powerControl-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev pcDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -689,18 +677,16 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is ITransport) if (device is ITransport transportDevice)
{ {
this.LogVerbose( this.LogVerbose(
"Adding ITransportMessenger for {deviceKey}", device.Key "Adding ITransportMessenger for {deviceKey}", device.Key
); );
var dev = device as PepperDash.Core.Device; var messenger = new ITransportMessenger(
var messenger = new IChannelMessenger(
$"{device.Key}-transport-{Key}", $"{device.Key}-transport-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
dev transportDevice
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -708,14 +694,14 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is IHasCurrentSourceInfoChange) if (device is IHasCurrentSourceInfoChange csiChange)
{ {
this.LogVerbose("Adding IHasCurrentSourceInfoMessenger for {deviceKey}", device.Key); this.LogVerbose("Adding IHasCurrentSourceInfoMessenger for {deviceKey}", device.Key);
var messenger = new IHasCurrentSourceInfoMessenger( var messenger = new IHasCurrentSourceInfoMessenger(
$"{device.Key}-currentSource-{Key}", $"{device.Key}-currentSource-{Key}",
$"/device/{device.Key}", $"/device/{device.Key}",
device as IHasCurrentSourceInfoChange csiChange
); );
AddDefaultDeviceMessenger(messenger); AddDefaultDeviceMessenger(messenger);
@@ -723,7 +709,7 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is ISwitchedOutput) if (device is ISwitchedOutput switchedDevice)
{ {
this.LogVerbose( this.LogVerbose(
"Adding ISwitchedOutputMessenger for {deviceKey}", device.Key "Adding ISwitchedOutputMessenger for {deviceKey}", device.Key
@@ -731,7 +717,7 @@ namespace PepperDash.Essentials
var messenger = new ISwitchedOutputMessenger( var messenger = new ISwitchedOutputMessenger(
$"{device.Key}-switchedOutput-{Key}", $"{device.Key}-switchedOutput-{Key}",
device as ISwitchedOutput, switchedDevice,
$"/device/{device.Key}" $"/device/{device.Key}"
); );
@@ -773,40 +759,6 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
// This will work if TKey and TSelector are both string types.
// Otherwise plugin device needs to instantiate ISelectableItemsMessenger and add it to the controller.
//if (device is IHasInputs<string, string> inputs)
//{
// this.LogVerbose("Adding InputsMessenger<string,string> for {deviceKey}", device.Key);
// var messenger = new ISelectableItemsMessenger<string>(
// $"{device.Key}-inputs-{Key}",
// $"/device/{device.Key}",
// inputs.Inputs,
// "inputs"
// );
// AddDefaultDeviceMessenger(messenger);
// messengerAdded = true;
//}
//if (device is IHasInputs<byte, int> byteIntInputs)
//{
// this.LogVerbose("Adding InputsMessenger<byte, int> for {deviceKey}", device.Key);
// var messenger = new ISelectableItemsMessenger<byte>(
// $"{device.Key}-inputs-{Key}",
// $"/device/{device.Key}",
// byteIntInputs.Inputs,
// "inputs"
// );
// AddDefaultDeviceMessenger(messenger);
// messengerAdded = true;
//}
if (device is IHasInputs<string> stringInputs) if (device is IHasInputs<string> stringInputs)
{ {
this.LogVerbose("Adding InputsMessenger<string> for {deviceKey}", device.Key); this.LogVerbose("Adding InputsMessenger<string> for {deviceKey}", device.Key);
@@ -855,7 +807,6 @@ namespace PepperDash.Essentials
messengerAdded = true; messengerAdded = true;
} }
if (device is IMatrixRouting matrix) if (device is IMatrixRouting matrix)
{ {
this.LogVerbose( this.LogVerbose(