The Blog of Ian Mercer.

Updated Release of the Abodit State Machine

I published a new version of the Abodit State Machine to Nuget this evening. You can find it here.

One breaking change in this version is that the state machine is now specified using three Type parameters instead of two:

public class OccupancyStateMachine :  
    StateMachine<OccupancyStateMachine, Event, BuildingArea>

The third type parameter, TContext, is a context object that can be passed in with every event occurrence or tick. This means that you don't need to store any extraneous data in the state machine itself and can keep it as a pure representation of the state of the system (SRP). So in designing a state machine based on this code, bear that in mind - the state tracks which state it is in and not all the other properties of the object it controls or responds to. TContext might be an ILight for example and all of the physical state and properties like: on, off, brightness, minimum brightness, ramp rate, linger time, ... belong in ILight and the state machine tracks only the intended states like: On, OnManual, OnAutomatic, Off, OffManual, OffAutomatic, Dimming.

In the example above I have an OccupancyStateMachine and the context is a BuildingArea. Each call to EventHappens now takes the event that happened and a BuildingArea object.

When you define your state machine you will need to include four parameters in each lambda expression.

Here, for example, is the current state machine for a BuildingArea in my home automation. It uses a hierarchy of states with two base states: Not Occupied and Occupied. It has timers for activity within a room or for occupancy within rooms that are contained by a floor. Note how it also exposes an IObservable<State> so that other objects can subscribe to state machine changes. I didn't want to take the Rx dependency in the state machine class itself but you can see how easy it is to hook it up.

Of interest also is the way I represent occupancy as three distinct states, the extra one Asleep represents a room that is not-occupied in the sense that there is no motion there now but there was at some point during the evening before.

public class OccupancyStateMachine : StateMachine<OccupancyStateMachine, Event, BuildingArea>
{
    private static readonly ILog log = LogManager.GetLogger("OccState");

    [NonSerialized]
    private readonly Subject<State> watch = new Subject<State>();

    public IObservable<State> Watch => watch.AsObservable();

    public override void OnStateChanging(StateMachine<OccupancyStateMachine, Event, BuildingArea>.State newState, BuildingArea context)
    {
        watch.OnNext(newState);
    }

    [NonSerialized]
    private readonly Subject<Event> watchEvents = new Subject<Event>();

    public IObservable<Event> WatchEvents => watchEvents.AsObservable();

    public override void OnEventHappened(Event @event)
    {
        watchEvents.OnNext(@event);
    }

    //public static readonly State Starting = AddState("Starting");

    public static readonly State NotOccupied = AddState("Not occupied",
    (m, e, s, c) => {
        m.CancelScheduledEvent(eTick);                                                  // Stop the clock
        c.IsTimerRunning.Current = false;
        c.IsHeavilyOccupied.Current = false;
        m.After(new TimeSpan(0,30,0), eHalfHourPostOccupancy );
        m.After(new TimeSpan(0,60,0), eOneHourPostOccupancy );
        },
    (m, e, s, c) => { });

    public static readonly State Asleep = AddState("Asleep",
    (m, e, s, c) =>
    {
        // Set a timer going for morning
        var now = TimeProvider.Current.Now.LocalDateTime;
        var morning = now.Hour < 8 ? now.AddHours(-now.Hour + 8) : now.AddHours(24 - now.Hour + 8);
        m.CancelScheduledEvent(eMorning);
        m.At(morning.ToUniversalTime(), eMorning);
    },
    (m, e, s, c) => { },
    parent:NotOccupied);

    public static readonly State Occupied = AddState("Occupied",
    (m, e, s, c) =>
    {
        m.CancelScheduledEvent(eTimeout);
        m.After(c.OccupancyTimeout, eTimeout);                // start a new timeout
        c.IsTimerRunning.Current = true;

        // Add a timer that runs while we are occupied
        m.CancelScheduledEvent(eTick);      // remove any old eTick events
        m.Every(new TimeSpan(hours:0, minutes:0, seconds:10), eTick);
        // And kill any post occupancy timers
        m.CancelScheduledEvent(eHalfHourPostOccupancy);
        m.CancelScheduledEvent(eOneHourPostOccupancy);
        m.CancelScheduledEvent(eMorning);
    },
    (m, e, s, c) => { });

    /// <summary>
    /// Room is occupied and all doors into it are enclosed
    /// </summary>
    public static readonly State OccupiedAndEnclosed = AddState("Occupied and enclosed",
    (m, e, s, c) => { },
    (m, e, s, c) => { },
    parent: Occupied);

    private static readonly Event eStart = new Event("Starts");
    private static readonly Event eUserActivity = new Event("User activity");
    // maintains occupancy, does not set it
    private static readonly Event eSensorActivity = new Event("Sensor activity");
    // 10s tick while occupied
    private static readonly Event eTick = new Event("Tick");
    private static readonly Event eDoorOpens = new Event("Door opens");
    // All doors closed
    private static readonly Event eDoorCloses = new Event("Door closes");
    
    private static readonly Event eTimeout = new Event("Timeout");
    private static readonly Event eMorning = new Event("Morning");

    private static readonly Event eTimeoutHalfHourPostOccupancy = new Event("Timeout Half Hour");
    private static readonly Event eTimeoutOneHourPostOccupancy = new Event("Timeout One Hour");

    private static readonly Event eAllChildrenNotOccupied = new Event("No child occupied");
    private static readonly Event eAtLeastOneChildOccupied = new Event("At least one child occupied");

    // Public events that we expose
    public static readonly Event eHalfHourPostOccupancy = new Event("Half hour post occupancy");
    public static readonly Event eOneHourPostOccupancy = new Event("One hour post occupancy");

    static OccupancyStateMachine()
    {
        // Note: This is a hierarchical state machine so NotOccupied includes Asleep
        NotOccupied
            .When(eAtLeastOneChildOccupied, Occupied)
            .When(eDoorOpens, Occupied)
            .When(eUserActivity, (m, s, e, c) => 
                {
                    if (c.Enclosed.Current)
                        return OccupiedAndEnclosed;
                    else
                        return Occupied;
                }
                );

        // Asleep is a substate of not occupied so no need for more logic on becoming occupied ...
        Asleep
            .When(eMorning, NotOccupied);

        // Occupied includes recently occupied and heavily occupied ...
        Occupied
            .When(eUserActivity, (m, s, e, c) =>
            {
                m.CancelScheduledEvent(eTimeout);                       // cancel the old timeout
                m.After(c.OccupancyTimeout, eTimeout);                  // start a new timeout
                c.IsTimerRunning.Current = true;
                if (c.Enclosed.Current)
                    return OccupiedAndEnclosed;
                else
                    return s;
            })
            .When(eSensorActivity, (m, s, e, c) =>
            {
                m.CancelScheduledEvent(eTimeout);                       // cancel the old timeout
                m.After(c.OccupancyTimeout, eTimeout);                  // start a new timeout
                c.IsTimerRunning.Current = true;

                return s;
            })
            .When(eDoorCloses, (m, s, e, c) =>
            {
                // Tricky, it closes but unless there is motion who knows what state we are in????
                return s;
            })
            .When(eAllChildrenNotOccupied, (m, s, e, c) =>
                {
                    if (c.IsTimerRunning.Current)
                    {
                        // If the timer is running ... wait until it runs out
                        return s;
                    }
                    else
                    {
                        // recursion??? m.EventHappens(eTimeout, c);
                        // otherwise ...
                        if (c.Time.EveningTo6AM.On)
                            return Asleep;
                        else
                            return NotOccupied;
                    }
                })
            .When(eTick, (m, s, e, c) =>
                {
                    return s;
                })
            .When(eTimeout, (m, s, e, c) =>
                {
                    c.IsTimerRunning.Current = false;
                    // No action if we have occupied children
                    if (c.HasOccupiedChildren.Current)
                        return s;

                    // No timeout if occupied and enclosed ???
                    if (s == OccupiedAndEnclosed)
                    {
                        c.AddToLog("Kept occupied because enclosed");
                        m.After(c.OccupancyTimeout, eTimeout);                  // start a new timeout
                        return s;
                    }

                    if (c.Time.EveningTo6AM.On)
                        return Asleep;
                    else
                        return NotOccupied;
                });

        OccupiedAndEnclosed
            .When(eDoorOpens, (m, s, e, c) => 
            {
                return Occupied;
            })
            .When(eTick, (m, s, e, c) =>
            {
                return s;
            });
    }

    public OccupancyStateMachine()
        : base(NotOccupied)
    {
    }

    public override void Start()
    {
        log.Debug("Start");
        this.EventHappens(eStart, null);
    }

    public void UserActivity(BuildingArea buildingArea)
    {
        string was = this.CurrentState.ToString();
        this.EventHappens(eUserActivity, buildingArea);
        log.Debug("User activity " + was + " -> " + this.CurrentState.ToString());
    }

    public void DoorOpens(BuildingArea buildingArea)
    {
        this.EventHappens(eDoorOpens, buildingArea);
    }

    public void DoorCloses(BuildingArea buildingArea)
    {
        this.EventHappens(eDoorCloses, buildingArea);
    }

    public void SensorMaintainingActivity(BuildingArea buildingArea)
    {
        var was = this.CurrentState;
        this.EventHappens(eUserActivity, buildingArea);
        if (was != this.CurrentState)
            log.Debug($"Sensor maintaining occupancy state {was} -> {this.CurrentState}");
    }

    public void AllChildrenNotOccupied(BuildingArea buildingArea)
    {
        var was = this.CurrentState;
        buildingArea.HasOccupiedChildren.Current = false;
        this.EventHappens(eAllChildrenNotOccupied, buildingArea);
        if (was != this.CurrentState)
        {
            log.Debug($"{buildingArea.Name} all children not occupied " + was + " -> " + this.CurrentState);
        }
    }

    public void AtLeastOneChildOccupied(BuildingArea buildingArea)
    {
        var was = this.CurrentState;
        buildingArea.HasOccupiedChildren.Current = true;
        this.EventHappens(eAtLeastOneChildOccupied, buildingArea);
        if (was != this.CurrentState)
        {
            log.Debug($"{buildingArea.Name} at least one child occupied " + was + " -> " + this.CurrentState);
        }
    }

    public override string ToString()
    {
        return "Occupancy state: " + this.CurrentState.Name;
    }
}

Related Stories

Cover Image for My love/hate relationship with Stackoverflow

My love/hate relationship with Stackoverflow

Stackoverflow is a terrific source of information but can also be infuriating.

Ian Mercer
Ian Mercer
Cover Image for Xamarin Forms Application For Home Automation

Xamarin Forms Application For Home Automation

Building a Xamarin Forms application to control my home automation system

Ian Mercer
Ian Mercer

JSON Patch - a C# implementation

Ian Mercer
Ian Mercer

Websites should stop using passwords for login!

A slightly radical idea to eliminate passwords from many of the websites you use just occasionally

Ian Mercer
Ian Mercer

Dynamically building 'Or' Expressions in LINQ

How to create a LINQ expression that logically ORs together a set of predicates

Ian Mercer
Ian Mercer

VariableWithHistory - making persistence invisible, making history visible

A novel approach to adding history to variables in a programming language

Ian Mercer
Ian Mercer

Neo4j Meetup in Seattle - some observations

Some observations from a meetup in Seattle on graph databases and Neo4j

Ian Mercer
Ian Mercer

My first programme [sic]

At the risk of looking seriously old, here's something found on a paper tape

Ian Mercer
Ian Mercer

Building a better .NET State Machine

A state machine for .NET that I've released on Nuget

Ian Mercer
Ian Mercer
Cover Image for The Internet of Dogs

The Internet of Dogs

Connecting our dog into the home automation

Ian Mercer
Ian Mercer

A simple state machine in C#

State machines are useful in many contexts but especially for home automation

Ian Mercer
Ian Mercer

Convert a property getter to a setter

Ian Mercer
Ian Mercer

MongoDB Map-Reduce - Hints and Tips

Ian Mercer
Ian Mercer
Cover Image for Weather Forecasting for Home Automation

Weather Forecasting for Home Automation

Ian Mercer
Ian Mercer

Lengthening short Urls in C#

Ian Mercer
Ian Mercer

Why don't you trust your build system?

Ian Mercer
Ian Mercer

ASP.NET MVC SEO - Solution Part 1

Ian Mercer
Ian Mercer

Elliott 803 - An Early Computer

Ian Mercer
Ian Mercer

Building sitemap.xml for SEO ASP.NET MVC

Ian Mercer
Ian Mercer

Continuous Integration -> Continuous Deployment

What is "quality" in terms of a released software product or website?

Ian Mercer
Ian Mercer

Making a bootable Windows 7 USB Memory Stick

Here's how I made a bootable USB memory stick for Windows 7

Ian Mercer
Ian Mercer

Tip: getting the index in a foreeach statement

A tip on using LINQ's Select expression with an index

Ian Mercer
Ian Mercer

SQL Server - error: 18456, severity: 14, state: 38 - Incorrect Login

A rant about developers using the same message for different errors

Ian Mercer
Ian Mercer

WCF and the SYSTEM account

Namespace reservations and http.sys, my, oh my!

Ian Mercer
Ian Mercer

404 errors on IIS6 with ASP.NET 4 Beta 2

Ian Mercer
Ian Mercer

Mixed mode assembly errors after upgrade to .NET 4 Beta 2

Fixing this error was fairly simple

Ian Mercer
Ian Mercer

The EntityContainer name could not be determined

How to fix the exception "the entitycontainer" name could not be determined

Ian Mercer
Ian Mercer

Shortened URLs should be treated like a Codec ...

Expanding URLs would help users decide whether or not to click a link

Ian Mercer
Ian Mercer

Tagging File Systems

Isn't it time we stopped knowing which drive our file is on?

Ian Mercer
Ian Mercer

A great site for developing and testing regular expressions

Just a link to a site I found useful

Ian Mercer
Ian Mercer

Introducing Jigsaw menus

A novel UI for menus that combines a breadcrumb and a menu in one visual metaphor

Ian Mercer
Ian Mercer

Entity Framework in .NET 4

Ian Mercer
Ian Mercer

Fix for IE's overflow:hidden problem

Ian Mercer
Ian Mercer

A better Tail program for Windows

A comparison of tail programs for Windows

Ian Mercer
Ian Mercer

Measuring website browser performance

Found this great resource on website performance

Ian Mercer
Ian Mercer

Amazon Instance vs Dedicated Server comparison

Some benchmark performance for Amazon vs a dedicated server

Ian Mercer
Ian Mercer

System.Data.EntitySqlException

Hints for dealing with this exception

Ian Mercer
Ian Mercer

Agile Software Development is Like Sailing

You cannot tack too often when sailing or you get nowhere. Agile is a bit like that.

Ian Mercer
Ian Mercer

Exception Handling using Exception.Data

My latest article on CodeProject covers the lesser known Exception.Data property

Ian Mercer
Ian Mercer

Javascript error reporting

Sending client-side errors back to a server for analysis

Ian Mercer
Ian Mercer

AntiVirus Software is the Worst Software!

When your anti-virus software starts stealing your personal data, it's time to remove it!

Ian Mercer
Ian Mercer

ASP.NET Custom Validation

How to solve a problem encountered with custom validation in ASP.NET

Ian Mercer
Ian Mercer

Optimization Advice

Some advice on software optimization

Ian Mercer
Ian Mercer

Linq's missing link

LinqKit came in handy back in 2009

Ian Mercer
Ian Mercer

Google Chart API

Ian Mercer
Ian Mercer

Cache optimized scanning of pairwise combinations of values

Using space-filling curves to optimize caching

Ian Mercer
Ian Mercer

Threading and User Interfaces

A rant about how few software programs get threading right

Ian Mercer
Ian Mercer

Take out the trash!

Why Windows shutdown takes so long

Ian Mercer
Ian Mercer

Dell upgrades - a pricey way to go

Ian Mercer
Ian Mercer

Programming mostly C#

Ian's advice on programming

Ian Mercer
Ian Mercer