Last active
October 27, 2020 08:07
-
-
Save thomasnordquist/ab1a8409296b4b6adfec00244938dab4 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
// Action to increment the context amount | |
const turnOn = assign({ | |
power: 'on', | |
brightness: 1, | |
ignoreMaxIlluminance: false | |
}) | |
const turnOnIgnoringMaxIlluminance = assign({ | |
power: 'on', | |
brightness: 1, | |
ignoreMaxIlluminance: true | |
}) | |
const turnOnABit = assign({ | |
power: 'on', | |
brightness: 0.05, | |
ignoreMaxIlluminance: false | |
}) | |
function isLate() { | |
let date = new Date() | |
let hours = date.getHours() | |
return hours >= 22 || hours < 9 | |
} | |
const turnOff = assign({ | |
power: 'off', | |
brightness: 1 | |
}) | |
const machine = Machine({ | |
context: { | |
power: 'off', | |
brightness: 1.0, | |
ignoreMaxIlluminance: false | |
}, | |
id: 'kitchen', | |
initial: 'initial', | |
on: { | |
doubleTap: { | |
target: 'forceOn', | |
actions: 'turnOn' | |
}, | |
tripleTap: { | |
target: 'forceOn', | |
actions: 'turnOnIgnoringMaxIlluminance' | |
} | |
}, | |
states: { | |
day: { | |
initial: 'lightOff', | |
on: { | |
night: 'night', | |
}, | |
states: { | |
lightOff: { | |
on: { | |
toggle: { | |
target: 'lightOn', | |
actions: 'turnOn', | |
} | |
}, | |
}, | |
lightOn: { | |
on: { | |
toggle: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
}, | |
no_motion: 'vacancy', | |
night: '#kitchen.night.lightOn', | |
off: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
} | |
} | |
}, | |
vacancy: { | |
on: { | |
motion: 'lightOn', | |
off: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
} | |
}, | |
after: { | |
60000: 'lightOff' | |
} | |
} | |
} | |
}, | |
night: { | |
initial: 'lightOff', | |
id: 'night', | |
on: { | |
day: 'day', | |
}, | |
states: { | |
lightOff: { | |
id: 'lightOff', | |
on: { | |
toggle: { | |
target: 'lightOn', | |
actions: 'turnOn', | |
}, | |
motion: [ | |
{ | |
target: 'lightOn', | |
actions: 'turnOnABit', | |
cond: isLate | |
}, | |
{ | |
target: 'lightOn', | |
actions: 'turnOn' | |
} | |
], | |
} | |
}, | |
lightOn: { | |
on: { | |
toggle: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
}, | |
no_motion: 'vacancy', | |
off: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
} | |
}, | |
}, | |
vacancy: { | |
on: { | |
motion: 'lightOn', | |
toggle: { | |
target: 'lightOff', | |
actions: 'turnOff', | |
}, | |
off: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
} | |
}, | |
after: { | |
60000: { | |
target: 'lightOff', | |
actions: 'turnOff' | |
} | |
} | |
} | |
} | |
}, | |
initial: { | |
on: { | |
day: 'day', | |
night: 'night', | |
toggle: { | |
target: 'day.lightOn', | |
actions: 'turnOn' | |
} | |
} | |
}, | |
forceOn: { | |
on: { | |
toggle: { | |
target: 'initial', | |
actions: 'turnOff' | |
}, | |
off: { | |
target: 'initial', | |
actions: 'turnOff' | |
} | |
} | |
}, | |
} | |
}, | |
{ | |
actions: { turnOn, turnOff, turnOnABit, turnOnIgnoringMaxIlluminance }, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment