Skip to content

Instantly share code, notes, and snippets.

@davidwallis3101
Created February 27, 2018 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwallis3101/1e7f15b5092e89e7955b611ccf42b2b4 to your computer and use it in GitHub Desktop.
Save davidwallis3101/1e7f15b5092e89e7955b611ccf42b2b4 to your computer and use it in GitHub Desktop.
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