Last active
August 28, 2020 15:03
-
-
Save dsomel21/548ef41863df7900b3f568fa47a54576 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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
const isUnitInStage = (context) => { | |
const currentStageNum = context?.schedule?.current_stage; | |
return context.target.stage === 'EVERY' || context.target.stage === currentStageNum; | |
} | |
const isUnitInDay = (context) => { | |
return context.target.day_in_stage === 'EVERY' || context.schedule.day_in_stage === context.target.day_in_stage; | |
} | |
const sendNotification = (context) => { | |
console.log(`Notification is scheduled for Stage ${context.schedule.current_stage}, Day ${context.schedule.day_in_stage}`); | |
} | |
// const waterOnDay1Stage1Machine = Machine({ | |
// initial: 'idle', | |
// context: { | |
// schedule: { | |
// id: 15, | |
// plant_id: 21, | |
// current_stage: 2, | |
// day_in_stage: 1, | |
// }, | |
// target: { | |
// stage: 'EVERY', | |
// day_in_stage: 1, | |
// }, | |
// }, | |
// states: { | |
// idle: { | |
// on: { | |
// STAGE_TRANSITION: { | |
// target: 'inCorrectStage', | |
// cond: isUnitInStage, | |
// }, | |
// }, | |
// }, | |
// inCorrectStage: { | |
// on: { | |
// DAY_TRANSITION: { | |
// target: 'inCorrectDay', | |
// cond: isUnitInDay, | |
// }, | |
// }, | |
// }, | |
// inCorrectDay: { | |
// entry: sendNotification, | |
// }, | |
// } | |
// }); | |
// const waterAlertOnDay3 = Machine({ | |
// initial: 'idle', | |
// context: { | |
// schedule: { | |
// id: 15, | |
// plant_id: 21, | |
// current_stage: 3, | |
// day_in_stage: 3, | |
// }, | |
// target: { | |
// stage: 'EVERY', | |
// day_in_stage: 3, | |
// }, | |
// }, | |
// states: { | |
// idle: { | |
// on: { | |
// STAGE_TRANSITION: { | |
// target: 'inCorrectStage', | |
// cond: isUnitInStage, | |
// }, | |
// }, | |
// }, | |
// inCorrectStage: { | |
// on: { | |
// DAY_TRANSITION: [ | |
// { | |
// target: 'inCorrectDay', | |
// cond: isUnitInDay, | |
// }, | |
// { | |
// target: 'inCorrectStage' | |
// } | |
// ] | |
// }, | |
// }, | |
// inCorrectDay: { | |
// on: { | |
// LOW_WATER: { | |
// actions: sendNotification, | |
// target: 'idle' | |
// }, | |
// }, | |
// }, | |
// } | |
// }); | |
// const waterAlertOnDay3Advanced = Machine({ | |
// initial: 'idle', | |
// context: { | |
// low_water_count: 0, | |
// low_water_limit: 3, | |
// notification: { | |
// id: 3, | |
// title: "Your water is low right now! Fill your water." | |
// }, | |
// schedule: { | |
// id: 15, | |
// plant_id: 21, | |
// current_stage: 3, | |
// day_in_stage: 3, | |
// }, | |
// target: { | |
// stage: 'EVERY', | |
// day_in_stage: 3, | |
// }, | |
// }, | |
// states: { | |
// idle: { | |
// on: { | |
// STAGE_TRANSITION: { | |
// target: 'inCorrectStage', | |
// cond: isUnitInStage, | |
// }, | |
// }, | |
// }, | |
// inCorrectStage: { | |
// on: { | |
// DAY_TRANSITION: [ | |
// { | |
// target: 'inCorrectDay', | |
// cond: isUnitInDay, | |
// }, | |
// { | |
// target: 'inCorrectStage' | |
// } | |
// ] | |
// }, | |
// }, | |
// inCorrectDay: { | |
// on: { | |
// LOW_WATER: [ | |
// { | |
// target: 'readyToSendNotification', | |
// cond: (context) => { | |
// return context.low_water_count >= context.low_water_limit | |
// } | |
// }, | |
// { | |
// actions: assign({ | |
// low_water_count: (context) => context.low_water_count + 1 | |
// }), | |
// } | |
// ] | |
// }, | |
// }, | |
// readyToSendNotification: { | |
// on: { | |
// entry: sendNotification, | |
// exit: 'idle' | |
// } | |
// } | |
// } | |
// }); | |
// const inCorrectDayAndExceedThreshold = () => isUnitInDay && hasExceededGap | |
// const waterAlertOnHealthCheck = Machine({ | |
// initial: 'idle', | |
// context: { | |
// hc_count: 0, | |
// hc_limit: 3, | |
// hc_gap: { hours: 4 }, | |
// last_alert: null, | |
// notification: { | |
// id: 3, | |
// title: "Your water is low right now! Fill your water." | |
// }, | |
// schedule: { | |
// id: 15, | |
// plant_id: 21, | |
// current_stage: 3, | |
// day_in_stage: 3, | |
// }, | |
// target: { | |
// stage: 'EVERY', | |
// day_in_stage: 'EVERY', | |
// }, | |
// }, | |
// states: { | |
// idle: { | |
// on: { | |
// STAGE_TRANSITION: { | |
// target: 'inCorrectStage', | |
// cond: isUnitInStage, | |
// }, | |
// }, | |
// }, | |
// inCorrectStage: { | |
// on: { | |
// DAY_TRANSITION: [ | |
// { | |
// target: 'inCorrectDay', | |
// cond: isUnitInDay, | |
// }, | |
// { | |
// target: 'inCorrectStage' | |
// } | |
// ] | |
// }, | |
// }, | |
// inCorrectDay: { | |
// on: { | |
// LOW_WATER_HC: [ | |
// { | |
// target: 'reachedHcLimit', | |
// cond: (context) => { | |
// return context.hc_count >= context.hc_limit | |
// } | |
// }, | |
// { | |
// actions: assign({ | |
// hc_count: (context) => context.hc_count + 1 | |
// }), | |
// } | |
// ] | |
// }, | |
// }, | |
// reachedHcLimit: { | |
// on: { | |
// entry: send('LOW_WATER_HC'), | |
// LOW_WATER_HC: [ | |
// { | |
// target: 'readyToSendNotification', | |
// cond: inCorrectDayAndExceedThreshold | |
// }, | |
// { | |
// actions: assign({ | |
// hc_count: (context) => context.hc_count + 1 | |
// }), | |
// } | |
// ] | |
// } | |
// }, | |
// readyToSendNotification: { | |
// on: { | |
// entry: sendNotification, | |
// exit: 'idle' | |
// } | |
// } | |
// } | |
// }); | |
const inCorrectDayAndExceedThreshold = () => isUnitInDay && hasExceededGap; | |
let d = new Date(); | |
let almost2HrsAgo = d.setMinutes(d.getMinutes() - 119); | |
const hasExceededGap = (context) => { | |
if (!context.last_alert) return true; | |
if (!context.hc_gap) return true; | |
let date = new Date(); | |
date = date.setHours(date.getHours() - context.hc_gap.hours); | |
// Did it exceeds that hc_gap | |
return date > context.last_alert; | |
}; | |
const waterAlertOnHealthCheck = Machine({ | |
initial: 'idle', | |
context: { | |
hc_count: 0, | |
hc_limit: 3, | |
hc_gap: { hours: 2 }, | |
last_alert: almost2HrsAgo, | |
notification: { | |
id: 3, | |
title: 'Your water is low right now! Fill your water.', | |
}, | |
schedule: { | |
id: 15, | |
plant_id: 21, | |
current_stage: 3, | |
day_in_stage: 3, | |
}, | |
target: { | |
stage: 'EVERY', | |
day_in_stage: 'EVERY', | |
}, | |
}, | |
states: { | |
idle: { | |
on: { | |
STAGE_TRANSITION: { | |
target: 'inCorrectStage', | |
cond: isUnitInStage, | |
}, | |
}, | |
}, | |
inCorrectStage: { | |
on: { | |
DAY_TRANSITION: [ | |
{ | |
target: 'inCorrectDay', | |
cond: isUnitInDay, | |
}, | |
{ | |
target: 'inCorrectStage', | |
}, | |
], | |
}, | |
}, | |
inCorrectDay: { | |
on: { | |
LOW_WATER_HC: [ | |
{ | |
target: 'reachedHcLimit', | |
cond: (context) => { | |
return context.hc_count >= context.hc_limit; | |
}, | |
}, | |
{ | |
actions: assign({ | |
hc_count: (context) => context.hc_count + 1, | |
}), | |
}, | |
], | |
}, | |
}, | |
reachedHcLimit: { | |
on: { | |
entry: send('LOW_WATER_HC'), | |
LOW_WATER_HC: [ | |
{ | |
target: 'readyToSendNotification', | |
cond: hasExceededGap, | |
}, | |
{ | |
actions: assign({ | |
hc_count: (context) => context.hc_count + 1, | |
}), | |
}, | |
], | |
}, | |
}, | |
readyToSendNotification: { | |
on: { | |
entry: sendNotification, | |
exit: 'idle', | |
}, | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment