using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; namespace PepperDash.Essentials.Core { /// /// Provides in use tracking. Objects can register with this. InUseFeedback can provide /// events when usage changes. /// public class InUseTracking { /// /// Returns a copied list of all users of this tracker. /// public IEnumerable Users { get { return new List(_Users); } } List _Users = new List(); /// /// Feedback that changes when this goes in/out of use /// public BoolFeedback InUseFeedback { get; private set; } /// /// Feedback that changes with the count of users /// public IntFeedback InUseCountFeedback { get; private set; } public InUseTracking() { InUseFeedback = new BoolFeedback(() => _Users.Count > 0); InUseCountFeedback = new IntFeedback(() => _Users.Count); } /// /// Add a "user" object to this tracker. A user can be added to this tracker /// multiple times, provided that the label is different /// /// A label to identify the instance of the user. Treated like a "role", etc. public void AddUser(object objectToAdd, string label) { // check if an exact object/label pair exists and ignore if so. No double-registers. var check = _Users.FirstOrDefault(u => u.Label == label && u.User == objectToAdd); if (check != null) return; var prevCount = _Users.Count; _Users.Add(new InUseTrackingObject(objectToAdd, label)); // if this is the first add, fire an update if (prevCount == 0 && _Users.Count > 0) InUseFeedback.FireUpdate(); InUseCountFeedback.FireUpdate(); } /// /// Remove a user object from this tracking /// public void RemoveUser(object objectToRemove, string label) { // Find the user object if exists and remove it var toRemove = _Users.FirstOrDefault(u => u.Label == label && u.User == objectToRemove); if (toRemove != null) { _Users.Remove(toRemove); if (_Users.Count == 0) InUseFeedback.FireUpdate(); InUseCountFeedback.FireUpdate(); } } } /// /// Wrapper for label/object pair representing in-use status. Allows the same object to /// register for in-use with different roles. /// public class InUseTrackingObject { public string Label { get; private set; } public object User { get; private set; } public InUseTrackingObject(object user, string label) { User = user; Label = label; } } //public class InUseEventArgs //{ // public int EventType { get; private set; } // public InUseTracking Tracker { get; private set; } // public InUseEventArgs(InUseTracking tracker, int eventType) // { // Tracker = tracker; // EventType = eventType; // } //} }