Skip to content

Instantly share code, notes, and snippets.

@benatwork
Created November 18, 2019 15:38
Show Gist options
  • Save benatwork/52d0e06293976f18332a6f3c3f9bf6dd to your computer and use it in GitHub Desktop.
Save benatwork/52d0e06293976f18332a6f3c3f9bf6dd to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// import { Machine, send } from 'xstate';
//----------------------------------------
const findingDevice = {
initial: 'findingDevice',
states: {
/* BASE STATES */
foundDevice: {
type: 'final',
entry: send('DEVICE_FOUND')
},
/* ACTION STATES */
findingDevice: {
on: {
BT_EXISTING_CONNECTION: 'foundDevice',
BT_PREVIOUSLY_STORED_DEVICE_ID: 'foundDevice',
BT_NO_EXISTING_CONNECTION: 'scanning'
}
},
scanning: {
on: {
BT_SCANNING_WARNING: 'scanningWarning',
BT_DEVICE_FOUND: 'foundDevice'
}
},
scanningWarning: {
on: {
BT_SCANNING_TIMEOUT: 'deviceNotFound',
BT_DEVICE_FOUND: 'foundDevice'
}
},
deviceNotFound: {
type: 'final',
on: {
BT_RESCANNING: 'findingDevice'
}
}
}
};
const connectingToDevice = {
initial: 'connectingToDevice',
states: {
/* BASE STATES */
connected: {
type: 'final'
},
connectingError: { type: 'final' },
connectingTimeout: { type: 'final' },
bondingError: { type: 'final' },
bondingTimeout: { type: 'final' },
/* ACTION STATES */
connectingToDevice: {
on: {
BT_CONNECTED: 'connected',
BT_ANDROID_CONNECTED: 'bonding',
BT_CONNECTED_ERROR: 'connectingError',
BT_CONNECTED_TIMEOUT: 'connectingTimeout'
}
},
bonding: {
on: {
BT_BONDING_ERROR: 'bondingError',
BT_BONDING_TIMEOUT: 'bondingTimeout',
BT_CONNECTED: 'connected'
}
}
}
};
const inittingDevice = {
initial: 'inittingDevice',
states: {
/* ACTION STATES */
inittingDevice: {
on: {
BT_READING_SERVICES: 'readingServices',
}
},
readingServices: {
on: {
BT_READING_BATTERY: 'readingBattery',
}
},
readingBattery: {
on: {
BT_READING_FIRMWARE_VERSION: 'readingFirmwareVersion',
}
},
readingFirmwareVersion: {
on: {
BT_READING_FIRMWARE_DATE: 'readingFirmwareDate',
}
},
readingFirmwareDate: {
on: {
BT_CREATING_NOTIFICATIONS: 'creatingNotifications',
}
},
creatingNotifications: {
on: {
BT_READING_DEVICE_STATE: 'readingDeviceState',
}
},
readingDeviceState: {
on: {
BT_READING_DEVICE_LOGS: 'readingDeviceLogs',
}
},
readingDeviceLogs: {
type: 'final'
},
}
};
const sessions = {
initial: 'sessionReady',
states: {
/* BASE STATES */
sessionError: {},
sessionReady: {
on: {
SYS_STATE_SESSION_START: 'startingSession',
}
},
/* ACTION STATES */
startingSession: {
on: {
SYS_STATE_SESSION_INHALING: 'sessionInhaling',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
},
sessionInhaling: {
on: {
SYS_STATE_SESSION_EXHALING: 'sessionExhaling',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
},
sessionExhaling: {
on: {
SYS_STATE_SESSION_DOSE_COMPLETE: 'sessionDoseComplete',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
},
sessionDoseComplete: {
on: {
SYS_STATE_SESSION_IN_PROGRESS: 'sessionInProgress',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
},
sessionInProgress: {
on: {
SYS_STATE_SESSION_START: 'startingSession',
SYS_STATE_SESSION_END: 'sessionReady',
SYS_STATE_SESSION_ERROR: 'sessionError'
}
}
}
};
const cartridgeHandler = {
initial: 'validatingLocalCartridge',
states: {
/* BASE STATES */
localCartridgeError: {},
remoteCartridgeError: {},
cartridgeValid: {
type: 'final'
},
dosingProfileInvalid: {
type: 'final',
},
/* ACTION STATES */
validatingLocalCartridge: {
on: {
EVNT_INVLD_LOCAL_AUTH_FAILURE: 'localCartridgeError',
EVNT_INVLD_CARTRIDGE_EMPTY: 'localCartridgeError',
EVNT_INVLD_REMOTE_TIMEOUT: 'localCartridgeError',
EVNT_INLVD_CARTRIDGE_REMOVED: 'localCartridgeError',
EVNT_INVLD_READ_ERROR: 'localCartridgeError',
SYS_STATE_REMOTE_CARTRIDGE_AUTH: 'validatingRemoteCartridge'
}
},
validatingRemoteCartridge: {
on: {
RMT_INVLD_GENERAL_ERROR: 'remoteCartridgeError',
RMT_INVLD_NO_DOSING_PROFILE:
'remoteCartridgeError',
RMT_VALIDATING_DOSING_PROFILE: 'validatingDosingProfile'
}
},
validatingDosingProfile: {
on: {
SYS_STATE_READY: 'cartridgeValid',
INVALID_DOSING_PROFILE: 'dosingProfileInvalid',
}
}
}
};
const ecosystemMachine = Machine({
id: 'deviceEcoSystem',
initial: 'disconnected',
states: {
/* BASE STATES */
disconnected: {
on: {
FIND_AND_CONNECT: 'connecting.findingDevice'
}
},
charging: {
on: {
SYS_STATE_CHARGE_COMPLETE: 'connected.waitingForCartridge',
SYS_STATE_WAITING_FOR_CARTRIDGE: 'connected.waitingForCartridge'
}
},
lowBattery: {
on: {
CHARGING: 'charging'
}
},
connecting: {
on: {
CONNECTING_ERROR: 'disconnected',
DEVICE_CONNECTED: 'connected.waitingForCartridge',
DISCONNECT: 'disconnected',
LOW_BATT: 'lowBattery'
},
states: {
findingDevice: {
on: {
DEVICE_FOUND: 'connectingToDevice',
DEVICE_NOT_FOUND: 'deviceNotFound'
},
...findingDevice
},
connectingToDevice: {
on: {
CONNECTED: 'inittingDevice'
},
...connectingToDevice
},
inittingDevice: {
on: {
DEVICE_INITTED: 'deviceInitted',
ERROR_INITTING: 'deviceReadTimeout',
DEVICE_READ_TIMEOUT: 'deviceReadTimeout'
},
...inittingDevice
},
deviceInitted: {
type: 'final',
entry: send('DEVICE_CONNECTED')
},
deviceReadTimeout: {
type: 'final'
},
deviceNotFound: {
type: 'final'
},
connectingError: {
type: 'final'
},
}
},
connected: {
on: {
SYS_STATE_WAITING_FOR_CARTRIDGE: 'connected.waitingForCartridge',
SYS_STATE_OFF: 'disconnected',
DISCONNECT: 'disconnected',
CHARGING: 'charging',
LOW_BATT: 'lowBattery'
},
states: {
waitingForCartridge: {
on: {
CARTRIDGE_CONNECTED: 'validatingCartridge',
},
},
validatingCartridge: {
on: {
CARTRIDGE_VALID: 'deviceReady',
CARTRIDGE_INVALID: 'invalidCartridge',
BAD_FILL_LEVEL_READ: 'badFillLevelRead'
},
...cartridgeHandler
},
badFillLevelRead: {},
invalidCartridge: {
on: {
SYS_STATE_WAITING_FOR_CARTRIDGE: 'waitingForCartridge'
}
},
deviceReady: {
on: {
START_SESSION: 'dosing',
CARTRIDGE_DISCONNECT: 'waitingForCartridge'
}
},
dosing: {
on: {
SESSION_END: 'deviceReady',
SESSION_ERROR: 'sessionError'
},
...sessions
},
sessionError: {
on: {
CONTINUE: 'deviceReady'
}
}
}
},
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment