简体   繁体   中英

Hierarchical state with stateless

am wondering if it will do what I am after.

I am thinking about a statemachine for home automation with some logic similar to this:

var stateMachine = new StateMachine<State, Trigger>(State.UnOccupied);
        stateMachine.Configure(State.UnOccupied)
            .Permit(Trigger.SensorActivity, State.Occupied)
            .Ignore(Trigger.AlarmFullSet);

        stateMachine.Configure(State.Occupied)
            .Permit(Trigger.AlarmFullSet, State.UnOccupied)
            .Permit(Trigger.AlarmPartSet, State.Asleep)
            .PermitReentry(Trigger.SensorActivity);

        stateMachine.Configure(State.Asleep)
            .SubstateOf(State.Occupied)
            .Permit(Trigger.AlarmUnset, State.Occupied);

However I want to represent the state of rooms within a house and also the overall state of the house..

IE

Home

    House Object
                        Upstairs
                                        Bedroom 1
                                        Bedroom 2
                        Downstairs
                                        Kitchen
                                        Living Room

    Garden
                        Front
                        Back
                        Side                       

So if Living room is occupied then so is the downstairs and the house and the home..

Apologies in advance my C# isn't the best and I am throwing myself in at the deep end!

Also is it possible to do timed leaving of states.. so a sensor could trigger a room to be occupied, then more activity triggers 'reentrant?' (Restarts a counter / timer / adjusts a schedule) on an occupied state entry – then after 15mins of no more entry/activity events, occupancy clears on that area and the state transitions to unoccupied for that area.

Yes, I would think that is possible to archive with stateless. I would model each room as an Room object, which has a set of trigger and states. The Room object can have one or more timers to take care of scheduled triggers, which would Fire the trigger when the timer expires. Some rooms might be special, so those can be modelled as specialized classes that inherit from the Room class. Maybe even create Floor and House objects, if that makes sense. Then you could do house.SetVacationMode().

Stateless has a an StateChanged event, which you can use to let each room know about the state of the other rooms. I suspect that you'll need a manager object that interfaces with the UI, and a UI / Control object as well. These could also have a state machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM