Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 2.0.0/routing feedback manager nullref #1201

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void Port_SerialDataReceived(ComPort ReceivingComPort, ComPortSerialDataEventArg

void OnDataReceived(string s)
{
var eventSubscribed = false;

var bytesHandler = BytesReceived;
if (bytesHandler != null)
Expand All @@ -101,18 +102,18 @@ void OnDataReceived(string s)
if (StreamDebugging.RxStreamDebuggingIsEnabled)
Debug.LogMessage(LogEventLevel.Information, this, "Received: '{0}'", ComTextHelper.GetEscapedText(bytes));
bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes));
return;
eventSubscribed = true;
}
var textHandler = TextReceived;
if (textHandler != null)
{
if (StreamDebugging.RxStreamDebuggingIsEnabled)
Debug.LogMessage(LogEventLevel.Information, this, "Received: '{0}'", s);
textHandler(this, new GenericCommMethodReceiveTextArgs(s));
return;
eventSubscribed = true;
}

Debug.LogMessage(LogEventLevel.Warning, this, "Received data but no handler is registered");
if(!eventSubscribed) Debug.LogMessage(LogEventLevel.Warning, this, "Received data but no handler is registered");
}

public override bool Deactivate()
Expand Down
8 changes: 4 additions & 4 deletions src/PepperDash.Essentials.Core/Config/BasicConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BasicConfig()
/// </summary>
public Dictionary<string, SourceListItem> GetSourceListForKey(string key)
{
if (string.IsNullOrEmpty(key) || !SourceLists.ContainsKey(key))
if (SourceLists == null || string.IsNullOrEmpty(key) || !SourceLists.ContainsKey(key))
return null;

return SourceLists[key];
Expand All @@ -68,7 +68,7 @@ public Dictionary<string, SourceListItem> GetSourceListForKey(string key)
/// <returns>DestinationList if the key exists, null otherwise</returns>
public Dictionary<string, DestinationListItem> GetDestinationListForKey(string key)
{
if (string.IsNullOrEmpty(key) || !DestinationLists.ContainsKey(key))
if (DestinationLists == null || string.IsNullOrEmpty(key) || !DestinationLists.ContainsKey(key))
{
return null;
}
Expand All @@ -83,7 +83,7 @@ public Dictionary<string, DestinationListItem> GetDestinationListForKey(string k
/// <returns>AudioControlPointList if the key exists, null otherwise</returns>
public AudioControlPointListItem GetAudioControlPointListForKey(string key)
{
if (string.IsNullOrEmpty(key) || !AudioControlPointLists.ContainsKey(key))
if (AudioControlPointLists == null || string.IsNullOrEmpty(key) || !AudioControlPointLists.ContainsKey(key))
return null;

return AudioControlPointLists[key];
Expand All @@ -94,7 +94,7 @@ public AudioControlPointListItem GetAudioControlPointListForKey(string key)
/// </summary>
public Dictionary<string, CameraListItem> GetCameraListForKey(string key)
{
if (string.IsNullOrEmpty(key) || !CameraLists.ContainsKey(key))
if (CameraLists == null || string.IsNullOrEmpty(key) || !CameraLists.ContainsKey(key))
return null;

return CameraLists[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ public string SystemUuid
if (string.IsNullOrEmpty(SystemUrl))
return "missing url";

var result = Regex.Match(SystemUrl, @"https?:\/\/.*\/systems\/(.*)\/#.*");
string uuid = result.Groups[1].Value;
return uuid;
if (SystemUrl.Contains("#"))
{
var result = Regex.Match(SystemUrl, @"https?:\/\/.*\/systems\/(.*)\/#.*");
string uuid = result.Groups[1].Value;
return uuid;
} else
{
var result = Regex.Match(SystemUrl, @"https?:\/\/.*\/systems\/(.*)\/.*");
string uuid = result.Groups[1].Value;
return uuid;
}
}
}

Expand All @@ -44,10 +52,18 @@ public string TemplateUuid
{
if (string.IsNullOrEmpty(TemplateUrl))
return "missing template url";

var result = Regex.Match(TemplateUrl, @"https?:\/\/.*\/templates\/(.*)\/#.*");
string uuid = result.Groups[1].Value;
return uuid;

if (TemplateUrl.Contains("#"))
{
var result = Regex.Match(TemplateUrl, @"https?:\/\/.*\/templates\/(.*)\/#.*");
string uuid = result.Groups[1].Value;
return uuid;
} else
{
var result = Regex.Match(TemplateUrl, @"https?:\/\/.*\/system-templates\/(.*)\/system-template-versions\/(.*)\/.*");
string uuid = result.Groups[2].Value;
return uuid;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PepperDash.Essentials.Core.DeviceTypeInterfaces
{
public interface IHumiditySensor
{
/// <summary>
/// Reports the relative humidity level. Level ranging from 0 to 100 (for 0% to 100%
/// RH). EventIds: HumidityFeedbackFeedbackEventId will trigger to indicate change.
/// </summary>
IntFeedback HumidityFeedback { get; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PepperDash.Essentials.Core.DeviceTypeInterfaces
{
public interface ITemperatureSensor
{
/// <summary>
/// The values will range from -400 to +1760 (for -40° to +176° F) or -400 to +800
/// (for -40° to +80° C)in tenths of a degree.
/// </summary>
IntFeedback TemperatureFeedback { get; }


BoolFeedback TemperatureInCFeedback { get; }

void SetTemperatureFormat(bool setToC);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ namespace PepperDash.Essentials.Core
{
public abstract class AudioControlListItemBase
{
/// <summary>
/// Key of the parent device in the DeviceManager
/// </summary>
[JsonProperty("parentDeviceKey")]
public string ParentDeviceKey { get; set; }

/// <summary>
/// Optional key of the item in the parent device
/// </summary>
[JsonProperty("itemKey")]
public string ItemKey { get; set; }

Expand Down
27 changes: 26 additions & 1 deletion src/PepperDash.Essentials.Core/Devices/DeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class DeviceManager
{
public static event EventHandler<EventArgs> AllDevicesActivated;
public static event EventHandler<EventArgs> AllDevicesRegistered;
public static event EventHandler<EventArgs> AllDevicesInitialized;

private static readonly CCriticalSection DeviceCriticalSection = new CCriticalSection();
private static readonly CEvent AllowAddDevicesCEvent = new CEvent(false, true);
Expand Down Expand Up @@ -67,7 +68,7 @@ public static void ActivateAll()
foreach (var d in Devices.Values)
{
try
{
{
if (d is Device)
(d as Device).PreActivate();
}
Expand Down Expand Up @@ -123,6 +124,18 @@ public static void ActivateAll()
}
}

private static void DeviceManager_Initialized(object sender, EventArgs e)
{
var allInitialized = Devices.Values.OfType<EssentialsDevice>().All(d => d.IsInitialized);

if (allInitialized)
{
Debug.LogMessage(LogEventLevel.Information, "****All Devices Initalized****");

OnAllDevicesInitialized();
}
}

private static void OnAllDevicesActivated()
{
var handler = AllDevicesActivated;
Expand All @@ -141,6 +154,15 @@ private static void OnAllDevicesRegistered()
}
}

private static void OnAllDevicesInitialized()
{
var handler = AllDevicesInitialized;
if (handler != null)
{
handler(null, new EventArgs());
}
}

/// <summary>
/// Calls activate on all Device class items
/// </summary>
Expand Down Expand Up @@ -264,6 +286,9 @@ public static void AddDevice(IKeyed newDev)
Devices.Add(newDev.Key, newDev);
//if (!(_Devices.Contains(newDev)))
// _Devices.Add(newDev);

if (newDev is EssentialsDevice essentialsDev)
essentialsDev.Initialized += DeviceManager_Initialized;
}
finally
{
Expand Down
20 changes: 20 additions & 0 deletions src/PepperDash.Essentials.Core/Devices/EssentialsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ namespace PepperDash.Essentials.Core
[Description("The base Essentials Device Class")]
public abstract class EssentialsDevice : Device
{
public event EventHandler Initialized;

private bool _isInitialized;
public bool IsInitialized {
get { return _isInitialized; }
private set
{
if (_isInitialized == value) return;

_isInitialized = value;

if (_isInitialized)
{
Initialized?.Invoke(this, new EventArgs());
}
}
}

protected EssentialsDevice(string key)
: base(key)
{
Expand All @@ -41,6 +59,8 @@ private void DeviceManagerOnAllDevicesActivated(object sender, EventArgs eventAr
try
{
Initialize();

IsInitialized = true;
}
catch (Exception ex)
{
Expand Down
14 changes: 12 additions & 2 deletions src/PepperDash.Essentials.Core/Devices/LevelControlListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ public string PreferredName
/// The key of the device in the DeviceManager for control
/// </summary>
[JsonProperty("deviceKey")]
public string DeviceKey => DeviceManager.AllDevices.
Where(d => d.Key.Contains(ParentDeviceKey) && d.Key.Contains(ItemKey)).FirstOrDefault()?.Key ?? $"{ParentDeviceKey}--{ItemKey}";
public string DeviceKey
{
get
{
if(string.IsNullOrEmpty(ItemKey)) return ParentDeviceKey;
else
{
return DeviceManager.AllDevices.
Where(d => d.Key.Contains(ParentDeviceKey) && d.Key.Contains(ItemKey)).FirstOrDefault()?.Key ?? $"{ParentDeviceKey}--{ItemKey}";
}
}
}

/// <summary>
/// Indicates if the item is a level, mute , or both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ public EssentialsRoomCombiner(string key, EssentialsRoomCombinerPropertiesConfig
SetupPartitionStateProviders();

SetRooms();
});


// Subscribe to the AllDevicesInitialized event
// We need to wait until all devices are initialized in case
// any actions are dependent on 3rd party devices already being
// connected and initialized
DeviceManager.AllDevicesInitialized += (o, a) =>
{
if (IsInAutoMode)
{
DetermineRoomCombinationScenario();
Expand All @@ -93,7 +101,7 @@ public EssentialsRoomCombiner(string key, EssentialsRoomCombinerPropertiesConfig
{
SetRoomCombinationScenario(_propertiesConfig.defaultScenarioKey);
}
});
};
}

private void CreateScenarios()
Expand Down
32 changes: 21 additions & 11 deletions src/PepperDash.Essentials.Core/Routing/RouteDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,29 @@ public void ExecuteRoutes()
/// </summary>
public void ReleaseRoutes()
{
foreach (var route in Routes.Where(r => r.SwitchingDevice is IRouting))
{
if (route.SwitchingDevice is IRouting switchingDevice)
{
foreach (var route in Routes.Where(r => r.SwitchingDevice is IRouting))
{
if (route.SwitchingDevice is IRouting switchingDevice)
{
if (route.OutputPort == null)
{
continue;
}

switchingDevice.ExecuteSwitch(null, route.OutputPort.Selector, SignalType);

// Pull the route from the port. Whatever is watching the output's in use tracker is
// responsible for responding appropriately.
route.OutputPort.InUseTracker.RemoveUser(Destination, "destination-" + SignalType);
Debug.LogMessage(LogEventLevel.Verbose, "Port {0} releasing. Count={1}",null, route.OutputPort.Key, route.OutputPort.InUseTracker.InUseCountFeedback.UShortValue);
}
}
}
if (route.OutputPort.InUseTracker != null)
{
route.OutputPort.InUseTracker.RemoveUser(Destination, "destination-" + SignalType);
Debug.LogMessage(LogEventLevel.Verbose, "Port {0} releasing. Count={1}", null, route.OutputPort.Key, route.OutputPort.InUseTracker.InUseCountFeedback.UShortValue);
}
else
{
Debug.LogMessage(LogEventLevel.Error, "InUseTracker is null for OutputPort {0}", null, route.OutputPort.Key);
}
}
}
}

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// </summary>
public class RouteSwitchDescriptor
{
public IRoutingInputs SwitchingDevice { get { return InputPort.ParentDevice; } }
public IRoutingInputs SwitchingDevice { get { return InputPort?.ParentDevice; } }
public RoutingOutputPort OutputPort { get; set; }
public RoutingInputPort InputPort { get; set; }

Expand All @@ -23,9 +23,9 @@ public RouteSwitchDescriptor(RoutingOutputPort outputPort, RoutingInputPort inpu
public override string ToString()
{
if (SwitchingDevice is IRouting)
return $"{SwitchingDevice?.Key} switches output {OutputPort.Key} to input {InputPort.Key}";
return $"{(SwitchingDevice != null ? SwitchingDevice.Key : "No Device")} switches output {(OutputPort != null ? OutputPort.Key : "No output port")} to input {(InputPort != null ? InputPort.Key : "No input port")}";
else
return $"{SwitchingDevice.Key} switches to input {InputPort.Key}";
return $"{(SwitchingDevice != null ? SwitchingDevice.Key : "No Device")} switches to input {(InputPort != null ? InputPort.Key : "No input port")}";
}
}

Expand Down
Loading
Loading