Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.