Minor updates to tcp client

This commit is contained in:
Neil Dorin
2017-10-31 14:19:51 -06:00
parent 44c9bcb788
commit adf7fd05ae
2 changed files with 118 additions and 109 deletions

View File

@@ -307,7 +307,8 @@ namespace PepperDash.Core
{ {
//if (Debug.Level == 2) //if (Debug.Level == 2)
// Debug.Console(2, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes)); // Debug.Console(2, this, "Sending {0} bytes: '{1}'", bytes.Length, ComTextHelper.GetEscapedText(bytes));
Client.SendData(bytes, bytes.Length); if(Client != null)
Client.SendData(bytes, bytes.Length);
} }

View File

@@ -1,109 +1,117 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace PepperDash.Core namespace PepperDash.Core
{ {
//********************************************************************************************************* //*********************************************************************************************************
/// <summary> /// <summary>
/// The core event and status-bearing class that most if not all device and connectors can derive from. /// The core event and status-bearing class that most if not all device and connectors can derive from.
/// </summary> /// </summary>
public class Device : IKeyName public class Device : IKeyName
{ {
public string Key { get; protected set; } public string Key { get; protected set; }
public string Name { get; protected set; } public string Name { get; protected set; }
public bool Enabled { get; protected set; } public bool Enabled { get; protected set; }
///// <summary> ///// <summary>
///// A place to store reference to the original config object, if any. These values should ///// A place to store reference to the original config object, if any. These values should
///// NOT be used as properties on the device as they are all publicly-settable values. ///// NOT be used as properties on the device as they are all publicly-settable values.
///// </summary> ///// </summary>
//public DeviceConfig Config { get; private set; } //public DeviceConfig Config { get; private set; }
///// <summary> ///// <summary>
///// Helper method to check if Config exists ///// Helper method to check if Config exists
///// </summary> ///// </summary>
//public bool HasConfig { get { return Config != null; } } //public bool HasConfig { get { return Config != null; } }
List<Action> _PreActivationActions; List<Action> _PreActivationActions;
List<Action> _PostActivationActions; List<Action> _PostActivationActions;
public static Device DefaultDevice { get { return _DefaultDevice; } } public static Device DefaultDevice { get { return _DefaultDevice; } }
static Device _DefaultDevice = new Device("Default", "Default"); static Device _DefaultDevice = new Device("Default", "Default");
/// <summary> /// <summary>
/// Base constructor for all Devices. /// Base constructor for all Devices.
/// </summary> /// </summary>
/// <param name="key"></param> /// <param name="key"></param>
public Device(string key) public Device(string key)
{ {
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 = "";
}
}
public Device(string key, string name) : this(key)
{ public Device(string key, string name) : this(key)
Name = name; {
} Name = name;
//public Device(DeviceConfig config) }
// : this(config.Key, config.Name)
//{ //public Device(DeviceConfig config)
// Config = config; // : this(config.Key, config.Name)
//} //{
// Config = config;
public void AddPreActivationAction(Action act) //}
{
if (_PreActivationActions == null) public void EnableUsageTracker()
_PreActivationActions = new List<Action>(); {
_PreActivationActions.Add(act); UsageTracker = new UsageTracker();
} }
public void AddPostActivationAction(Action act) public void AddPreActivationAction(Action act)
{ {
if (_PostActivationActions == null) if (_PreActivationActions == null)
_PostActivationActions = new List<Action>(); _PreActivationActions = new List<Action>();
_PostActivationActions.Add(act); _PreActivationActions.Add(act);
} }
/// <summary> public void AddPostActivationAction(Action act)
/// Gets this device ready to be used in the system. Runs any added pre-activation items, and {
/// all post-activation at end. Classes needing additional logic to if (_PostActivationActions == null)
/// run should override CustomActivate() _PostActivationActions = new List<Action>();
/// </summary> _PostActivationActions.Add(act);
public bool Activate() }
{
if (_PreActivationActions != null) /// <summary>
_PreActivationActions.ForEach(a => a.Invoke()); /// Gets this device ready to be used in the system. Runs any added pre-activation items, and
var result = CustomActivate(); /// all post-activation at end. Classes needing additional logic to
if(result && _PostActivationActions != null) /// run should override CustomActivate()
_PostActivationActions.ForEach(a => a.Invoke()); /// </summary>
return result; public bool Activate()
} {
if (_PreActivationActions != null)
/// <summary> _PreActivationActions.ForEach(a => a.Invoke());
/// Called in between Pre and PostActivationActions when Activate() is called. var result = CustomActivate();
/// Override to provide addtitional setup when calling activation. Overriding classes if(result && _PostActivationActions != null)
/// do not need to call base.CustomActivate() _PostActivationActions.ForEach(a => a.Invoke());
/// </summary> return result;
/// <returns>true if device activated successfully.</returns> }
public virtual bool CustomActivate() { return true; }
/// <summary>
/// <summary> /// Called in between Pre and PostActivationActions when Activate() is called.
/// Call to deactivate device - unlink events, etc. Overriding classes do not /// Override to provide addtitional setup when calling activation. Overriding classes
/// need to call base.Deactivate() /// do not need to call base.CustomActivate()
/// </summary> /// </summary>
/// <returns></returns> /// <returns>true if device activated successfully.</returns>
public virtual bool Deactivate() { return true; } public virtual bool CustomActivate() { return true; }
/// <summary> /// <summary>
/// Helper method to check object for bool value false and fire an Action method /// Call to deactivate device - unlink events, etc. Overriding classes do not
/// </summary> /// need to call base.Deactivate()
/// <param name="o">Should be of type bool, others will be ignored</param> /// </summary>
/// <param name="a">Action to be run when o is false</param> /// <returns></returns>
public void OnFalse(object o, Action a) public virtual bool Deactivate() { return true; }
{
if (o is bool && !(bool)o) a(); /// <summary>
} /// Helper method to check object for bool value false and fire an Action method
} /// </summary>
/// <param name="o">Should be of type bool, others will be ignored</param>
/// <param name="a">Action to be run when o is false</param>
public void OnFalse(object o, Action a)
{
if (o is bool && !(bool)o) a();
}
}
} }