pub trait Device: Send {
// Required methods
fn kind(&self) -> &'static str;
fn state(&self) -> Value;
fn on_host_bytes(&mut self, data: &[u8], now: Millis) -> Vec<DeviceEvent>;
fn on_control(
&mut self,
cmd: ControlCommand,
now: Millis,
) -> Vec<DeviceEvent>;
fn on_tick(&mut self, now: Millis) -> Vec<DeviceEvent>;
fn next_deadline(&self) -> Option<Millis>;
fn faults(&self) -> Vec<FaultId>;
fn set_fault(
&mut self,
fault: FaultId,
active: bool,
opts: FaultOptions,
now: Millis,
) -> Vec<DeviceEvent>;
fn reset(&mut self, now: Millis) -> Vec<DeviceEvent>;
// Provided methods
fn attached_to(&self) -> Option<DeviceId> { ... }
fn on_peripheral(
&mut self,
from: &DeviceId,
signal: PeripheralSignal,
now: Millis,
) -> Vec<DeviceEvent> { ... }
}Expand description
A device instance.
Sans-IO: a device is a pure state machine. Transports own all I/O and time, and pass the
current time in as now. A device asks to be woken by returning a deadline from
Device::next_deadline; the caller then calls Device::on_tick at or after it.
Required Methods§
Sourcefn kind(&self) -> &'static str
fn kind(&self) -> &'static str
The device kind, matching its plugin’s DevicePlugin::kind.
Sourcefn state(&self) -> Value
fn state(&self) -> Value
A JSON snapshot of the device’s state, shown in the dashboard and APIs.
Sourcefn on_host_bytes(&mut self, data: &[u8], now: Millis) -> Vec<DeviceEvent>
fn on_host_bytes(&mut self, data: &[u8], now: Millis) -> Vec<DeviceEvent>
Host -> device bytes. A command may be split across calls.
Sourcefn on_control(&mut self, cmd: ControlCommand, now: Millis) -> Vec<DeviceEvent>
fn on_control(&mut self, cmd: ControlCommand, now: Millis) -> Vec<DeviceEvent>
A control action from the dashboard, API, CLI or a scenario: scan, tap, close drawer.
Sourcefn on_tick(&mut self, now: Millis) -> Vec<DeviceEvent>
fn on_tick(&mut self, now: Millis) -> Vec<DeviceEvent>
Time has reached (or passed) Device::next_deadline: pulses, idle job close.
Sourcefn next_deadline(&self) -> Option<Millis>
fn next_deadline(&self) -> Option<Millis>
When the device next needs Device::on_tick, if ever. A deadline at or before the
current time means “as soon as possible”.
Sourcefn faults(&self) -> Vec<FaultId>
fn faults(&self) -> Vec<FaultId>
The faults that apply to this instance, which may depend on its config (a serial
printer has baud-mismatch, a USB one has stall). The registry rejects any other
fault with RegistryError::UnsupportedFault.
Sourcefn set_fault(
&mut self,
fault: FaultId,
active: bool,
opts: FaultOptions,
now: Millis,
) -> Vec<DeviceEvent>
fn set_fault( &mut self, fault: FaultId, active: bool, opts: FaultOptions, now: Millis, ) -> Vec<DeviceEvent>
Turns a fault on or off at now, returning what that caused, e.g.
DeviceEvent::Disconnected for offline. Turning on a fault that is already on
replaces its options.
Sourcefn reset(&mut self, now: Millis) -> Vec<DeviceEvent>
fn reset(&mut self, now: Millis) -> Vec<DeviceEvent>
Back to the freshly created state at now: clears buffers, jobs and faults. Returns
what that caused, e.g. DeviceEvent::Connected if the device was offline.
Provided Methods§
Sourcefn attached_to(&self) -> Option<DeviceId>
fn attached_to(&self) -> Option<DeviceId>
The device this one is attached to as a peripheral, e.g. a cash drawer’s printer.
DeviceEvent::Peripheral signals travel between attached devices.
Sourcefn on_peripheral(
&mut self,
from: &DeviceId,
signal: PeripheralSignal,
now: Millis,
) -> Vec<DeviceEvent>
fn on_peripheral( &mut self, from: &DeviceId, signal: PeripheralSignal, now: Millis, ) -> Vec<DeviceEvent>
A PeripheralSignal from an attached device: from a peripheral to the device it is
attached to (e.g. the drawer’s open sensor), or from that device to its peripherals
(e.g. a drawer-kick pulse).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".