From 7ad8218af0314fab558d08e6d09e70009875e4db Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Mon, 29 Dec 2025 13:03:47 -0600 Subject: [PATCH] fix: fusion controller now sets only associated room custom values --- .../Fusion/FusionCustomPropertiesBridge.cs | 72 ++++++++++--------- .../Fusion/IEssentialsRoomFusionController.cs | 14 ++-- ...alsRoomFusionControllerPropertiesConfig.cs | 9 ++- 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/src/PepperDash.Essentials.Core/Fusion/FusionCustomPropertiesBridge.cs b/src/PepperDash.Essentials.Core/Fusion/FusionCustomPropertiesBridge.cs index a0cd8e6e..b170d28e 100644 --- a/src/PepperDash.Essentials.Core/Fusion/FusionCustomPropertiesBridge.cs +++ b/src/PepperDash.Essentials.Core/Fusion/FusionCustomPropertiesBridge.cs @@ -1,16 +1,10 @@  using System; -using System.Collections.Generic; using System.Linq; -using System.Text; -using Crestron.SimplSharp; using Newtonsoft.Json; using Newtonsoft.Json.Linq; - using PepperDash.Core; -using PepperDash.Essentials.Core; -using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Devices; using Serilog.Events; @@ -25,17 +19,25 @@ namespace PepperDash.Essentials.Core.Fusion /// /// Evaluates the room info and custom properties from Fusion and updates the system properties aa needed /// - /// - public void EvaluateRoomInfo(string roomKey, RoomInformation roomInfo) + /// The room associated with this Fusion instance + /// The room information from Fusion + /// + public void EvaluateRoomInfo(IEssentialsRoom room, RoomInformation roomInfo, bool useFusionRoomName) { try { - var reconfigurableDevices = DeviceManager.AllDevices.Where(d => d is ReconfigurableDevice); + var reconfigurableDevices = DeviceManager.AllDevices.OfType(); foreach (var device in reconfigurableDevices) { // Get the current device config so new values can be overwritten over existing - var deviceConfig = (device as ReconfigurableDevice).Config; + var deviceConfig = device.Config; + + if (device is IEssentialsRoom) + { + // Skipping room name as this will affect ALL room instances in the configuration and cause unintended consequences when multiple rooms are present and multiple Fusion instances are used + continue; + } if (device is RoomOnToDefaultSourceWhenOccupied) { @@ -85,36 +87,42 @@ namespace PepperDash.Essentials.Core.Fusion deviceConfig.Properties = JToken.FromObject(devProps); } - else if (device is IEssentialsRoom) - { - // Set the room name - if (!string.IsNullOrEmpty(roomInfo.Name)) - { - Debug.LogMessage(LogEventLevel.Debug, "Current Room Name: {0}. New Room Name: {1}", deviceConfig.Name, roomInfo.Name); - // Set the name in config - deviceConfig.Name = roomInfo.Name; - - Debug.LogMessage(LogEventLevel.Debug, "Room Name Successfully Changed."); - } - - // Set the help message - var helpMessage = roomInfo.FusionCustomProperties.FirstOrDefault(p => p.ID.Equals("RoomHelpMessage")); - if (helpMessage != null) - { - //Debug.LogMessage(LogEventLevel.Debug, "Current Help Message: {0}. New Help Message: {1}", deviceConfig.Properties["help"]["message"].Value(ToString()), helpMessage.CustomFieldValue); - deviceConfig.Properties["helpMessage"] = (string)helpMessage.CustomFieldValue; - } - } // Set the config on the device - (device as ReconfigurableDevice).SetConfig(deviceConfig); + device.SetConfig(deviceConfig); } + if (!(room is ReconfigurableDevice reconfigurable)) + { + Debug.LogWarning("FusionCustomPropertiesBridge: Room is not a ReconfigurableDevice. Cannot map custom properties."); + return; + } + var roomConfig = reconfigurable.Config; + + // Set the room name + if (!string.IsNullOrEmpty(roomInfo.Name) && useFusionRoomName) + { + Debug.LogDebug("Current Room Name: {currentName}. New Room Name: {fusionName}", roomConfig.Name, roomInfo.Name); + // Set the name in config + roomConfig.Name = roomInfo.Name; + + Debug.LogDebug("Room Name Successfully Changed."); + } + + // Set the help message + var helpMessage = roomInfo.FusionCustomProperties.FirstOrDefault(p => p.ID.Equals("RoomHelpMessage")); + if (helpMessage != null) + { + roomConfig.Properties["helpMessage"] = helpMessage.CustomFieldValue; + } + + reconfigurable.SetConfig(roomConfig); } catch (Exception e) { - Debug.LogMessage(LogEventLevel.Debug, "FusionCustomPropetiesBridge: Error mapping properties: {0}", e); + Debug.LogError("FusionCustomPropetiesBridge: Exception mapping properties for {roomKey}: {message}", room.Key, e.Message); + Debug.LogDebug(e, "Stack Trace: "); } } } diff --git a/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionController.cs b/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionController.cs index bcf6509e..308e5c12 100644 --- a/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionController.cs +++ b/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionController.cs @@ -1,4 +1,9 @@ -using Crestron.SimplSharp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Timers; +using Crestron.SimplSharp; using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.CrestronXml; using Crestron.SimplSharp.CrestronXml.Serialization; @@ -10,11 +15,6 @@ using PepperDash.Core.Logging; using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.DeviceTypeInterfaces; using Serilog.Events; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Timers; namespace PepperDash.Essentials.Core.Fusion { @@ -1091,7 +1091,7 @@ namespace PepperDash.Essentials.Core.Fusion } RoomInfoChange?.Invoke(this, new EventArgs()); - CustomPropertiesBridge.EvaluateRoomInfo(Room.Key, roomInformation); + CustomPropertiesBridge.EvaluateRoomInfo(Room, roomInformation, _config.UseFusionRoomName); } } catch (Exception e) diff --git a/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionControllerPropertiesConfig.cs b/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionControllerPropertiesConfig.cs index c9dea4c3..98234e61 100644 --- a/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionControllerPropertiesConfig.cs +++ b/src/PepperDash.Essentials.Core/Fusion/IEssentialsRoomFusionControllerPropertiesConfig.cs @@ -27,7 +27,7 @@ public class IEssentialsRoomFusionControllerPropertiesConfig } else { - Debug.LogWarning( "Failed to parse IpId '{0}' as UInt16", IpId); + Debug.LogWarning("Failed to parse IpId '{0}' as UInt16", IpId); return 0; } } @@ -45,6 +45,13 @@ public class IEssentialsRoomFusionControllerPropertiesConfig [JsonProperty("roomKey")] public string RoomKey { get; set; } + /// + /// Gets or sets whether to use the Fusion room name for this room + /// + /// Defaults to true to preserve current behavior. Set to false to skip updating the room name from Fusion + [JsonProperty("useFusionRoomName")] + public bool UseFusionRoomName { get; set; } = true; + /// /// Gets or sets whether to use HTML format for help requests ///