using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; namespace PepperDash.Essentials.Core { /// /// Represents an abstract controller device for a partition dividing rooms that are combinable /// /// In Auto mode, it can use a partition sensor to automatically determine whether the partition is present. /// /// In Manual mode it accepts user input to tell it whether the partition is present. /// public class EssentialsPartitionController : IPartitionController { private IPartitionStateProvider _partitionSensor; private bool isInAutoMode; private bool partitionPresent; public EssentialsPartitionController(string key, string name, IPartitionStateProvider sensor, bool defaultToManualMode, List adjacentRoomKeys) { Key = key; Name = name; AdjacentRoomKeys = adjacentRoomKeys; if (sensor != null) { _partitionSensor = sensor; if (!defaultToManualMode) { SetAutoMode(); } else { SetManualMode(); } } else { SetManualMode(); } PartitionPresentFeedback.FireUpdate(); } void PartitionPresentFeedback_OutputChange(object sender, FeedbackEventArgs e) { if (isInAutoMode) { PartitionPresentFeedback.FireUpdate(); } } #region IPartitionController Members public List AdjacentRoomKeys { get; private set; } public void SetAutoMode() { isInAutoMode = true; if (PartitionPresentFeedback != null) { PartitionPresentFeedback.SetValueFunc(() => _partitionSensor.PartitionPresentFeedback.BoolValue); } else { PartitionPresentFeedback = new BoolFeedback(() => _partitionSensor.PartitionPresentFeedback.BoolValue); } if (_partitionSensor != null) { _partitionSensor.PartitionPresentFeedback.OutputChange += PartitionPresentFeedback_OutputChange; } } public void SetManualMode() { isInAutoMode = false; if (PartitionPresentFeedback != null) { PartitionPresentFeedback.SetValueFunc(() => partitionPresent); } else { PartitionPresentFeedback = new BoolFeedback(() => partitionPresent); } if (_partitionSensor != null) { _partitionSensor.PartitionPresentFeedback.OutputChange -= PartitionPresentFeedback_OutputChange; } } public void SetPartitionStatePresent() { if (!isInAutoMode) { partitionPresent = true; PartitionPresentFeedback.FireUpdate(); } } public void SetPartitionStateNotPresent() { if (!isInAutoMode) { partitionPresent = false; PartitionPresentFeedback.FireUpdate(); } } public void ToggglePartitionState() { if (!isInAutoMode) { partitionPresent = !partitionPresent; PartitionPresentFeedback.FireUpdate(); } } #endregion #region IPartitionStateProvider Members public BoolFeedback PartitionPresentFeedback { get; private set; } #endregion #region IKeyName Members public string Name { get; private set; } #endregion #region IKeyed Members public string Key { get; private set; } #endregion } }