Created
February 27, 2018 09:55
-
-
Save davidwallis3101/1e7f15b5092e89e7955b611ccf42b2b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Stateless; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Xml; | |
using System.Xml.Serialization; | |
namespace StatelessTest | |
{ | |
[Serializable] | |
public class Location | |
{ | |
[NonSerialized] | |
private readonly System.Timers.Timer _occupancyTimer; | |
private Location() | |
{ | |
// parameterless constructor for serialization | |
} | |
private TimeSpan _occupancyTimeout; | |
[XmlIgnore] | |
public TimeSpan OccupancyTimeout | |
{ | |
get => this._occupancyTimeout; | |
set => this._occupancyTimeout = value; | |
} | |
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] | |
[XmlElement(DataType = "duration", ElementName = "OccupancyTimeout")] | |
public string OccupancyTimeoutString | |
{ | |
get => XmlConvert.ToString(this.OccupancyTimeout); | |
set => this.OccupancyTimeout = string.IsNullOrEmpty(value) ? TimeSpan.Zero : XmlConvert.ToTimeSpan(value); | |
} | |
public Location Parent { get; } | |
public string Name { get; set; } | |
public List<Location> Children { get; set; } | |
public bool IsTimerRunning { get; set; } | |
public State OccupancyState | |
{ | |
get => _state; | |
set => _state = value; | |
} | |
public Location(Location parent) | |
{ | |
Parent = parent; | |
Children = new List<Location>(); | |
OccupancyTimeout = new TimeSpan(0, 0, 0, 5); | |
parent?.Children.Add(this); | |
_occupancyTimer = new System.Timers.Timer(); | |
} | |
public IEnumerable<Location> AllChildren => Children.Union(Children.SelectMany(child => child.AllChildren)); | |
public bool HasOccupiedChildren => AllChildren.Any(child => child.OccupancyState == State.Occupied); | |
} | |
} | |
// threw this in here to: | |
[Serializable] | |
public enum State | |
{ | |
UnOccupied, | |
Occupied | |
} | |
// and to create: | |
var locations = new List<Location>(); | |
locations.Add(new Location(null) { Name = "Home" }); | |
locations.Add(new Location(locations.Find(x => x.Name == "Home")) { Name = "House" }); | |
locations.Add(new Location(locations.Find(x => x.Name == "Home")) { Name = "Garden" }); | |
locations.Add(new Location(locations.Find(x => x.Name == "Home")) { Name = "House" }); | |
locations.Add(new Location(locations.Find(x => x.Name == "House")) { Name = "Upstairs" }); | |
locations.Add(new Location(locations.Find(x => x.Name == "House")) { Name = "Downstairs" }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment