Skip to main content

Event

device.events() Subscribe to operational status events.​

Automatic Logs

Every event also logs on_logged when it fires (the level follows the source event), whether or not the matching event has been subscribed to. Bind on_logged to receive the messages in Chinese.

After subscribing, changes are only compared when status_ex() or connected() is called. The first snapshot does not trigger anything (it only records the baseline). Each callback runs under its own catch_unwind: if one callback panics, it will not stop the SDK or block later callbacks.

Each event supports multiple subscriptions:

  • on_*: replaces all existing subscriptions for that event (calling it again with the same name replaces, it does not append);
  • add_*: appends one subscription and returns an EventSubscription credential;
  • off_*: removes one entry by criterion and returns bool (true if found / false if not found).

Type aliases: PnSEvents = ServiceEvents, PnSEventLevel = EventLevel.

The NIC link goes by event: subscribe on_link_changed (it does not trigger when the server state has no linkUp field). Link snapshots are also available from diag_snapshot().link_up.

IO quality goes by on_io_status_changed. The first time IOPS is read, only the baseline is recorded and no callback fires. If both inputIopsHex and outputIopsHex are missing, nothing is invented and no callback fires. A callback fires only when the non-GOOD byte count changes. All six languages follow the same rule; it is not a callback on every read.

Feature Overview​

CategoryEventNoteAutomatic Logging
Connecton_connected_changedAlternate between on and off in sync with the IO controller's cycle✓
Operating Stateon_state_changedChanges in Operating State✓
Runtime Erroron_error_occurredRuntime Error✓
Linkon_link_changedNetwork Card Physical Link Up / Down✓
IOPSon_io_status_changedChanges in the "non-GOOD" byte count✓
Logon_loggedEach event listed above will trigger this one again.—

Subscribe and Unsubscribe​

let sub = device.events().add_connected_changed(|e| {
println!("connected={}", e.connected);
});

assert!(device.events().off_connected_changed(sub)); // Remove this line
assert!(!device.events().off_connected_changed(sub)); // Removing duplicates returns false

Dispatch happens synchronously on the thread where the event occurred. Inside a callback do not call on_* / add_* / off_* on the same subscription surface again (the channel borrow is already held during dispatch; a reborrow panics, and subscription changes do not take effect).

Connect​

on_connected_changed​

pub fn on_connected_changed<F>(&self, cb: F)
where F: FnMut(&ConnectedChangedEvent) + 'static

Triggered when the cyclic data exchange connection opens or closes (connected == false with ever_connected == true = disconnect; ever_connected == false = never connected).

Callback parameters:

pub struct ConnectedChangedEvent {
pub connected: bool, // Is the data currently being updated for a new cycle?
pub ever_connected: bool, // Have you ever connected to it?
pub level: EventLevel, // Connected = Info; Disconnected / Never Connected = Warn
}

Example:

device.events().on_connected_changed(|e| {
if !e.connected && e.ever_connected {
println!("掉线");
}
});

Operating State​

on_state_changed​

pub fn on_state_changed<F>(&self, cb: F)
where F: FnMut(&StateChangedEvent) + 'static

Triggered when the operating state changes (the SDK only observes, it does not switch states itself). The old_state / new_state type is ServiceState.

Callback parameters:

pub struct StateChangedEvent {
pub old_state: ServiceState, // Before the change, see [ServiceState](./api#servicestate)
pub new_state: ServiceState, // After the change
pub level: EventLevel, // Info
}

Example:

device.events().on_state_changed(|e| {
println!("{:?} → {:?}", e.old_state, e.new_state);
});

on_error_occurred​

pub fn on_error_occurred<F>(&self, cb: F)
where F: FnMut(&ErrorOccurredEvent) + 'static

Triggered when a runtime error appears; error_code is the raw int value — for the enum form use device.error_code().

Callback parameters:

pub struct ErrorOccurredEvent {
pub error_code: i32, // Original value of a runtime error (not an enumeration)
pub message: String, // Chinese message; missing value is an empty string
pub level: EventLevel, // Warn
}

Example:

device.events().on_error_occurred(|e| {
println!("{}: {}", e.error_code, e.message);
});
pub fn on_link_changed<F>(&self, cb: F)
where F: FnMut(&LinkChangedEvent) + 'static

Triggered when the network card's physical link goes Up or Down. The comparison is performed inside status_ex() / connected(). The first snapshot only records the baseline and does not trigger the event.

When the server state has no linkUp field, this does not trigger — connected is not used to stand in for the physical link, and nothing is guessed.

Callback parameters:

pub struct LinkChangedEvent {
pub link_up: bool, // Is the physical link up?
pub level: EventLevel, // Up = Info; Down = Warn
}

Example:

device.events().on_link_changed(|e| {
println!("链路 {}", if e.link_up { "Up" } else { "Down" });
});

Process Data Quality​

on_io_status_changed​

pub fn on_io_status_changed<F>(&self, cb: F)
where F: FnMut(&IoStatusChangedEvent) + 'static

The first time IOPS is read, only the baseline is recorded and no callback fires. If both inputIopsHex and outputIopsHex are missing, nothing is invented and no callback fires. A callback fires only when the non-GOOD byte count changes. It is not a callback on every read.

Callback parameters:

pub struct IoStatusChangedEvent {
pub iops_bad: bool, // true = currently IOPS not all GOOD
pub level: EventLevel, // BAD = Warn; otherwise Info
}

Includes only a Boolean value: no module-by-module breakdown IOPS , and no BAD byte count.

Log​

on_logged​

pub fn on_logged<F>(&self, cb: F)
where F: FnMut(EventLevel, &str) + 'static

This message is sent again each time an event is triggered, and its severity level follows that of the source event.

Callback parameters:

  • level (EventLevel) — Log Level
  • message (&str) — Log Body
EventLevel

Events and Log Levels (Aliases PnSEventLevel).

pub enum EventLevel {
Info, // Information
Warn, // Warning
Error, // Error
}

Connection = Info, Disconnected / Runtime Error = Warn. There is no fixed integer value for these, so use the enumeration names instead.

Example:

device.events().on_logged(|level, message| {
println!("[{level:?}] {message}");
});

Complete Example​

device.events().on_connected_changed(|e| {
if !e.connected && e.ever_connected {
println!("掉线");
}
});

device.events().on_state_changed(|e| {
println!("{:?} → {:?}", e.old_state, e.new_state);
});

device.events().on_error_occurred(|e| {
println!("{}: {}", e.error_code, e.message);
});

device.events().on_link_changed(|e| {
println!("链路 {}", if e.link_up { "Up" } else { "Down" });
});

device.events().on_logged(|level, message| {
println!("[{level:?}] {message}");
});

The first time I read it, I IOPS only remembered: "Record the baseline, but don't call the callback; the callback isn't called every time it's read."