From adf7fd05aeb7b575730d5387abb1764cebefa5a5 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Tue, 31 Oct 2017 14:19:51 -0600 Subject: [PATCH] Minor updates to tcp client --- .../Comm/GenericTcpIpClient.cs | 3 +- Pepperdash Core/Pepperdash Core/Device.cs | 224 +++++++++--------- 2 files changed, 118 insertions(+), 109 deletions(-) diff --git a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs index 0d025fa..aa6304d 100644 --- a/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/Pepperdash Core/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -307,7 +307,8 @@ namespace PepperDash.Core { //if (Debug.Level == 2) // 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); } diff --git a/Pepperdash Core/Pepperdash Core/Device.cs b/Pepperdash Core/Pepperdash Core/Device.cs index 0e526e9..99968e0 100644 --- a/Pepperdash Core/Pepperdash Core/Device.cs +++ b/Pepperdash Core/Pepperdash Core/Device.cs @@ -1,109 +1,117 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace PepperDash.Core -{ - //********************************************************************************************************* - /// - /// The core event and status-bearing class that most if not all device and connectors can derive from. - /// - public class Device : IKeyName - { - public string Key { get; protected set; } - public string Name { get; protected set; } - public bool Enabled { get; protected set; } - - ///// - ///// 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. - ///// - //public DeviceConfig Config { get; private set; } - ///// - ///// Helper method to check if Config exists - ///// - //public bool HasConfig { get { return Config != null; } } - - List _PreActivationActions; - List _PostActivationActions; - - public static Device DefaultDevice { get { return _DefaultDevice; } } - static Device _DefaultDevice = new Device("Default", "Default"); - - /// - /// Base constructor for all Devices. - /// - /// - public Device(string key) - { - Key = key; - if (key.Contains('.')) Debug.Console(0, this, "WARNING: Device name's should not include '.'"); - Name = ""; - } - - public Device(string key, string name) : this(key) - { - Name = name; - } - - //public Device(DeviceConfig config) - // : this(config.Key, config.Name) - //{ - // Config = config; - //} - - public void AddPreActivationAction(Action act) - { - if (_PreActivationActions == null) - _PreActivationActions = new List(); - _PreActivationActions.Add(act); - } - - public void AddPostActivationAction(Action act) - { - if (_PostActivationActions == null) - _PostActivationActions = new List(); - _PostActivationActions.Add(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 - /// run should override CustomActivate() - /// - public bool Activate() - { - if (_PreActivationActions != null) - _PreActivationActions.ForEach(a => a.Invoke()); - var result = CustomActivate(); - if(result && _PostActivationActions != null) - _PostActivationActions.ForEach(a => a.Invoke()); - return result; - } - - /// - /// Called in between Pre and PostActivationActions when Activate() is called. - /// Override to provide addtitional setup when calling activation. Overriding classes - /// do not need to call base.CustomActivate() - /// - /// true if device activated successfully. - public virtual bool CustomActivate() { return true; } - - /// - /// Call to deactivate device - unlink events, etc. Overriding classes do not - /// need to call base.Deactivate() - /// - /// - public virtual bool Deactivate() { return true; } - - /// - /// Helper method to check object for bool value false and fire an Action method - /// - /// Should be of type bool, others will be ignored - /// Action to be run when o is false - public void OnFalse(object o, Action a) - { - if (o is bool && !(bool)o) a(); - } - } +using System; +using System.Collections.Generic; +using System.Linq; + +namespace PepperDash.Core +{ + //********************************************************************************************************* + /// + /// The core event and status-bearing class that most if not all device and connectors can derive from. + /// + public class Device : IKeyName + { + public string Key { get; protected set; } + public string Name { get; protected set; } + public bool Enabled { get; protected set; } + + ///// + ///// 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. + ///// + //public DeviceConfig Config { get; private set; } + ///// + ///// Helper method to check if Config exists + ///// + //public bool HasConfig { get { return Config != null; } } + + List _PreActivationActions; + List _PostActivationActions; + + public static Device DefaultDevice { get { return _DefaultDevice; } } + static Device _DefaultDevice = new Device("Default", "Default"); + + /// + /// Base constructor for all Devices. + /// + /// + public Device(string key) + { + Key = key; + if (key.Contains('.')) Debug.Console(0, this, "WARNING: Device name's should not include '.'"); + Name = ""; + + } + + public Device(string key, string name) : this(key) + { + Name = name; + + } + + //public Device(DeviceConfig config) + // : this(config.Key, config.Name) + //{ + // Config = config; + //} + + public void EnableUsageTracker() + { + UsageTracker = new UsageTracker(); + } + + public void AddPreActivationAction(Action act) + { + if (_PreActivationActions == null) + _PreActivationActions = new List(); + _PreActivationActions.Add(act); + } + + public void AddPostActivationAction(Action act) + { + if (_PostActivationActions == null) + _PostActivationActions = new List(); + _PostActivationActions.Add(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 + /// run should override CustomActivate() + /// + public bool Activate() + { + if (_PreActivationActions != null) + _PreActivationActions.ForEach(a => a.Invoke()); + var result = CustomActivate(); + if(result && _PostActivationActions != null) + _PostActivationActions.ForEach(a => a.Invoke()); + return result; + } + + /// + /// Called in between Pre and PostActivationActions when Activate() is called. + /// Override to provide addtitional setup when calling activation. Overriding classes + /// do not need to call base.CustomActivate() + /// + /// true if device activated successfully. + public virtual bool CustomActivate() { return true; } + + /// + /// Call to deactivate device - unlink events, etc. Overriding classes do not + /// need to call base.Deactivate() + /// + /// + public virtual bool Deactivate() { return true; } + + /// + /// Helper method to check object for bool value false and fire an Action method + /// + /// Should be of type bool, others will be ignored + /// Action to be run when o is false + public void OnFalse(object o, Action a) + { + if (o is bool && !(bool)o) a(); + } + + } } \ No newline at end of file