pub struct Registry { /* private fields */ }Expand description
Holds plugins and device instances, routes input to devices, records everything in each
device’s event log and publishes BusEvents.
The registry is sans-IO: it reads time only from the injected Clock and never blocks.
Methods that feed a device return the device’s DeviceEvents so the caller can deliver
ToHost bytes, wedge scans and connection changes to the app.
use hardless_core::{FakeClock, Registry};
let registry = Registry::new(FakeClock::new());
assert!(registry.list().is_empty());Implementations§
Source§impl Registry
impl Registry
Sourcepub fn with_config(clock: impl Clock + 'static, config: RegistryConfig) -> Self
pub fn with_config(clock: impl Clock + 'static, config: RegistryConfig) -> Self
A registry with custom limits.
Sourcepub fn config(&self) -> RegistryConfig
pub fn config(&self) -> RegistryConfig
The registry’s limits.
Sourcepub fn now(&mut self) -> Millis
pub fn now(&mut self) -> Millis
The current time from the injected clock, clamped so it never goes backwards.
Sourcepub fn register(
&mut self,
plugin: impl DevicePlugin,
) -> Result<(), RegistryError>
pub fn register( &mut self, plugin: impl DevicePlugin, ) -> Result<(), RegistryError>
Registers a device plugin. Each kind can be registered once.
Sourcepub fn register_boxed(
&mut self,
plugin: Box<dyn DevicePlugin>,
) -> Result<(), RegistryError>
pub fn register_boxed( &mut self, plugin: Box<dyn DevicePlugin>, ) -> Result<(), RegistryError>
Registers a boxed device plugin.
Sourcepub fn plugin(&self, kind: &str) -> Option<&dyn DevicePlugin>
pub fn plugin(&self, kind: &str) -> Option<&dyn DevicePlugin>
The plugin for a kind.
Sourcepub fn plugin_kinds(&self) -> impl Iterator<Item = &'static str> + '_
pub fn plugin_kinds(&self) -> impl Iterator<Item = &'static str> + '_
Registered kinds, sorted.
Sourcepub fn create(
&mut self,
kind: &str,
id: Option<DeviceId>,
config: Value,
) -> Result<DeviceId, RegistryError>
pub fn create( &mut self, kind: &str, id: Option<DeviceId>, config: Value, ) -> Result<DeviceId, RegistryError>
Creates a device. Without an id, one is generated as <kind>-<n>.
Emits device:added.
Sourcepub fn remove(&mut self, id: &DeviceId) -> Result<(), RegistryError>
pub fn remove(&mut self, id: &DeviceId) -> Result<(), RegistryError>
Removes a device. Emits device:removed.
Sourcepub fn summary(&self, id: &DeviceId) -> Option<DeviceSummary>
pub fn summary(&self, id: &DeviceId) -> Option<DeviceSummary>
A device’s summary: kind, state and active faults.
Sourcepub fn list(&self) -> Vec<DeviceSummary>
pub fn list(&self) -> Vec<DeviceSummary>
Every device, sorted by ID.
Sourcepub fn host_bytes(
&mut self,
id: &DeviceId,
data: &[u8],
) -> Result<Vec<DeviceEvent>, RegistryError>
pub fn host_bytes( &mut self, id: &DeviceId, data: &[u8], ) -> Result<Vec<DeviceEvent>, RegistryError>
Delivers host -> device bytes and returns the device’s events.
Sourcepub fn host_signals(
&mut self,
id: &DeviceId,
signals: SerialSignals,
) -> Result<(), RegistryError>
pub fn host_signals( &mut self, id: &DeviceId, signals: SerialSignals, ) -> Result<(), RegistryError>
Records serial output signals set by the app (SerialPort.setSignals()) in the
device’s event log. Devices do not react to them yet.
Sourcepub fn control(
&mut self,
id: &DeviceId,
command: ControlCommand,
) -> Result<Vec<DeviceEvent>, RegistryError>
pub fn control( &mut self, id: &DeviceId, command: ControlCommand, ) -> Result<Vec<DeviceEvent>, RegistryError>
Sends a control command and returns the device’s events.
Sourcepub fn log(
&mut self,
id: &DeviceId,
entry: LogEntry,
) -> Result<(), RegistryError>
pub fn log( &mut self, id: &DeviceId, entry: LogEntry, ) -> Result<(), RegistryError>
Records a diagnostic in a device’s event log on behalf of a transport, e.g. a part of an HTTP print request the device cannot reproduce.
Sourcepub fn set_fault(
&mut self,
id: &DeviceId,
fault: FaultId,
active: bool,
options: FaultOptions,
) -> Result<Vec<DeviceEvent>, RegistryError>
pub fn set_fault( &mut self, id: &DeviceId, fault: FaultId, active: bool, options: FaultOptions, ) -> Result<Vec<DeviceEvent>, RegistryError>
Turns a fault on or off, returns the device’s events (e.g. Disconnected for
offline) and emits device:state, since the active faults are part of the summary.
A fault the device does not declare in Device::faults is rejected with
RegistryError::UnsupportedFault, whether it is being turned on or off, and nothing
is changed or recorded.
Sourcepub fn reset(
&mut self,
id: &DeviceId,
) -> Result<Vec<DeviceEvent>, RegistryError>
pub fn reset( &mut self, id: &DeviceId, ) -> Result<Vec<DeviceEvent>, RegistryError>
Resets a device: clears its state, jobs and faults (its event log is kept), returns
the device’s events and emits device:state.
Sourcepub fn tick(&mut self) -> Vec<(DeviceId, DeviceEvent)>
pub fn tick(&mut self) -> Vec<(DeviceId, DeviceEvent)>
Runs on_tick for every device whose deadline has passed and returns their events.
Sourcepub fn next_deadline(&self) -> Option<Millis>
pub fn next_deadline(&self) -> Option<Millis>
The earliest deadline of any device: when the caller should next call Registry::tick.
Sourcepub fn events(
&self,
id: &DeviceId,
since: Option<u64>,
) -> Result<Vec<&EventRecord>, RegistryError>
pub fn events( &self, id: &DeviceId, since: Option<u64>, ) -> Result<Vec<&EventRecord>, RegistryError>
A device’s event log, oldest first, optionally only records with seq > since.
Sourcepub fn jobs(
&self,
id: &DeviceId,
) -> Result<impl Iterator<Item = &Job> + '_, RegistryError>
pub fn jobs( &self, id: &DeviceId, ) -> Result<impl Iterator<Item = &Job> + '_, RegistryError>
A device’s completed jobs, newest first.