Adds IDebuggable interface and implments on Device

This commit is contained in:
Neil Dorin
2020-10-07 20:48:10 -06:00
parent 7ea4cb8847
commit 4e4e0cb20c
5 changed files with 126 additions and 14 deletions

View File

@@ -9,8 +9,10 @@ namespace PepperDash.Core
/// The core event and status-bearing class that most if not all device /// The core event and status-bearing class that most if not all device
/// and connectors can derive from. /// and connectors can derive from.
/// </summary> /// </summary>
public class Device : IKeyName public class Device : IDebuggable
{ {
public DeviceDebug Debug { get; private set; }
/// <summary> /// <summary>
/// Unique Key /// Unique Key
/// </summary> /// </summary>
@@ -46,12 +48,18 @@ namespace PepperDash.Core
/// <param name="key"></param> /// <param name="key"></param>
public Device(string key) public Device(string key)
{ {
Debug = new DeviceDebug(this);
Key = key; Key = key;
if (key.Contains('.')) Debug.Console(0, this, "WARNING: Device name's should not include '.'"); if (key.Contains('.')) Debug.Console(0, this, "WARNING: Device name's should not include '.'");
Name = ""; Name = "";
} }
/// <summary>
/// Optional constructor for all Devices with Name
/// </summary>
/// <param name="key"></param>
/// <param name="name"></param>
public Device(string key, string name) : this(key) public Device(string key, string name) : this(key)
{ {
Name = name; Name = name;
@@ -64,6 +72,10 @@ namespace PepperDash.Core
// Config = config; // Config = config;
//} //}
/// <summary>
/// Adds an action to be executed in the pre-activation phase
/// </summary>
/// <param name="act">Action to execute</param>
public void AddPreActivationAction(Action act) public void AddPreActivationAction(Action act)
{ {
if (_PreActivationActions == null) if (_PreActivationActions == null)
@@ -71,6 +83,11 @@ namespace PepperDash.Core
_PreActivationActions.Add(act); _PreActivationActions.Add(act);
} }
/// <summary>
/// Adds an action to be executed in the post-activation phase
/// </summary>
/// <param name="act">Action to execute</param>
public void AddPostActivationAction(Action act) public void AddPostActivationAction(Action act)
{ {
if (_PostActivationActions == null) if (_PostActivationActions == null)
@@ -78,6 +95,9 @@ namespace PepperDash.Core
_PostActivationActions.Add(act); _PostActivationActions.Add(act);
} }
/// <summary>
/// Exectues the pre-activation actions
/// </summary>
public void PreActivate() public void PreActivate()
{ {
if (_PreActivationActions != null) if (_PreActivationActions != null)
@@ -99,6 +119,9 @@ namespace PepperDash.Core
return result; return result;
} }
/// <summary>
/// Executes the post-activation actions
/// </summary>
public void PostActivate() public void PostActivate()
{ {
if (_PostActivationActions != null) if (_PostActivationActions != null)

View File

@@ -13,7 +13,7 @@ using PepperDash.Core.DebugThings;
namespace PepperDash.Core namespace PepperDash.Core
{ {
public static class Debug internal static class Debug
{ {
/// <summary> /// <summary>
/// Describes the folder location where a given program stores it's debug level memory. By default, the /// Describes the folder location where a given program stores it's debug level memory. By default, the
@@ -70,9 +70,12 @@ namespace PepperDash.Core
static Debug() static Debug()
{ {
// Get the assembly version and print it to console and the log // Get the assembly version and print it to console and the log
var version = Assembly.GetExecutingAssembly().GetName().Version;
PepperDashCoreVersion = string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision); var fullVersion = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false);
AssemblyInformationalVersionAttribute fullVersionAtt = fullVersion[0] as AssemblyInformationalVersionAttribute;
PepperDashCoreVersion = fullVersionAtt.InformationalVersion;
var msg = string.Format("[App {0}] Using PepperDash_Core v{1}", InitialParametersClass.ApplicationNumber, PepperDashCoreVersion); var msg = string.Format("[App {0}] Using PepperDash_Core v{1}", InitialParametersClass.ApplicationNumber, PepperDashCoreVersion);
@@ -553,9 +556,11 @@ namespace PepperDash.Core
return string.Format(@"\NVRAM\debugSettings\program{0}", InitialParametersClass.ApplicationNumber); return string.Format(@"\NVRAM\debugSettings\program{0}", InitialParametersClass.ApplicationNumber);
} }
}
public enum ErrorLogLevel public enum ErrorLogLevel
{ {
Error, Warning, Notice, None Error, Warning, Notice, None
} }
}
} }

View File

@@ -149,7 +149,7 @@ namespace PepperDash.Core
Console(level, "[{0}] {1}", dev.Key, string.Format(format, items)); Console(level, "[{0}] {1}", dev.Key, string.Format(format, items));
} }
public void Console(uint level, IKeyed dev, Debug.ErrorLogLevel errorLogLevel, public void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel,
string format, params object[] items) string format, params object[] items)
{ {
if (SaveData.Level >= level) if (SaveData.Level >= level)
@@ -160,7 +160,7 @@ namespace PepperDash.Core
} }
} }
public void Console(uint level, Debug.ErrorLogLevel errorLogLevel, public void Console(uint level, ErrorLogLevel errorLogLevel,
string format, params object[] items) string format, params object[] items)
{ {
if (SaveData.Level >= level) if (SaveData.Level >= level)
@@ -171,18 +171,18 @@ namespace PepperDash.Core
} }
} }
public void LogError(Debug.ErrorLogLevel errorLogLevel, string str) public void LogError(ErrorLogLevel errorLogLevel, string str)
{ {
string msg = string.Format("App {0}:{1}", InitialParametersClass.ApplicationNumber, str); string msg = string.Format("App {0}:{1}", InitialParametersClass.ApplicationNumber, str);
switch (errorLogLevel) switch (errorLogLevel)
{ {
case Debug.ErrorLogLevel.Error: case ErrorLogLevel.Error:
ErrorLog.Error(msg); ErrorLog.Error(msg);
break; break;
case Debug.ErrorLogLevel.Warning: case ErrorLogLevel.Warning:
ErrorLog.Warn(msg); ErrorLog.Warn(msg);
break; break;
case Debug.ErrorLogLevel.Notice: case ErrorLogLevel.Notice:
ErrorLog.Notice(msg); ErrorLog.Notice(msg);
break; break;
} }

View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronLogger;
namespace PepperDash.Core
{
/// <summary>
/// Indicates a class capabel of console debugging and logging
/// </summary>
public interface IDebuggable : IKeyName
{
/// <summary>
/// The class that handles console debugging and logging
/// </summary>
DeviceDebug Debug { get; }
}
/// <summary>
/// A class to handle implementation of IDebuggable
/// </summary>
public class DeviceDebug
{
Device _parentDevice;
/// <summary>
/// The current Debug Level for the device instance
/// </summary>
public int DebugLevel;
public DeviceDebug(Device parentDevice)
{
_parentDevice = parentDevice;
}
public void Console(uint level, string format, params object[] items)
{
if (DebugLevel >= level)
{
Debug.Console(level, format, items);
}
}
public void Console(uint level, IKeyed dev, string format, params object[] items)
{
if (DebugLevel >= level)
{
Debug.Console(level, _parentDevice, format, items);
}
}
public void Console(uint level, ErrorLogLevel errorLogLevel, string format, params object[] items)
{
if (DebugLevel >= level)
{
Debug.Console(level, errorLogLevel, format, items);
}
}
public void ConsoleWithLog(uint level, string format, params object[] items)
{
if (DebugLevel >= level)
{
Debug.Console(level, format, items);
}
}
public void ConsoleWithLog(uint level, IKeyed dev, string format, params object[] items)
{
if (DebugLevel >= level)
{
Debug.Console(level, _parentDevice, format, items);
}
}
public void LogError(ErrorLogLevel errorLogLevel, string str)
{
Debug.LogError(errorLogLevel, str);
}
}
}

View File

@@ -110,6 +110,7 @@
<Compile Include="JsonToSimpl\REMOVE JsonToSimplFixedPathObject.cs" /> <Compile Include="JsonToSimpl\REMOVE JsonToSimplFixedPathObject.cs" />
<Compile Include="JsonToSimpl\JsonToSimplGenericMaster.cs" /> <Compile Include="JsonToSimpl\JsonToSimplGenericMaster.cs" />
<Compile Include="JsonToSimpl\JsonToSimplMaster.cs" /> <Compile Include="JsonToSimpl\JsonToSimplMaster.cs" />
<Compile Include="Logging\IDebuggable.cs" />
<Compile Include="Network\DiscoveryThings.cs" /> <Compile Include="Network\DiscoveryThings.cs" />
<Compile Include="PasswordManagement\Config.cs" /> <Compile Include="PasswordManagement\Config.cs" />
<Compile Include="PasswordManagement\Constants.cs" /> <Compile Include="PasswordManagement\Constants.cs" />