Event
device.getEvents() Subscribe to operational status events.
Each event is logged again when it is triggered addLoggedListener(with the severity level matching the source event), regardless of whether a corresponding event has been subscribed to. Subscribe addLoggedListener to receive messages in Chinese.
Subscribe first, then read the runtime ST; only then will it compare changes and trigger a callback. On the first call, it only records the current ST and does not fire an event.
Feature Overview
| Category | Event | Note | Automatic Logging |
|---|---|---|---|
| Connect | device.getEvents().addConnectedChangedListener | Alternate between on and off in sync with the IO controller's cycle | ✓ |
| Operating State | device.getEvents().addStateChangedListener | Changes in Operating State | ✓ |
| Runtime Error | device.getEvents().addErrorOccurredListener | Runtime Error | ✓ |
| IO Quality | device.getEvents().addIoStatusChangedListener | Changes in the "non-GOOD" byte count | ✓ |
| Link | device.getEvents().addLinkChangedListener | Changes in the Network Card's Physical Link | ✓ |
| Log | device.getEvents().addLoggedListener | Each event listed above will trigger this one again. | — |
Unsubscribe
public boolean removeConnectedChangedListener(Consumer<ConnectedChanged> listener)
public boolean removeStateChangedListener(Consumer<StateChanged> listener)
public boolean removeErrorOccurredListener(Consumer<ErrorOccurred> listener)
public boolean removeIoStatusChangedListener(Consumer<IoStatusChanged> listener)
public boolean removeLinkChangedListener(Consumer<LinkChanged> listener)
public boolean removeLoggedListener(BiConsumer<PnSEventLevel, String> listener)
- Returns
true= A record has indeed been removed; if the listener does not exist or is passed,nullreturnsfalse(idempotent, no exception thrown). - If the same listener is registered N times, the
removemethod must be called N times (removing the first matching entry each time); sincelambdacompares by object identity, be sure to retain the same instance used during registration.
Connect
addConnectedChangedListener
public void addConnectedChangedListener(Consumer<ConnectedChanged> listener)
Triggered when the periodic data exchange connection is lost (isConnected() == false where isEverConnected() == true = disconnection; isEverConnected() == false = never connected).
Callback parameters:
public static final class ConnectedChanged {
public boolean isConnected() { } // Is the data currently being updated for a new cycle?
public boolean isEverConnected() { } // Have you ever connected to it?
public PnSEventLevel getLevel() { } // Connected = ; INFO Disconnected / Never connected = WARN
}
Example:
device.getEvents().addConnectedChangedListener(e -> {
if (!e.isConnected() && e.isEverConnected())
System.out.println("掉线");
});
Operating State
addStateChangedListener
public void addStateChangedListener(Consumer<StateChanged> listener)
Triggered when the operating state changes (SDK observation only; does not actively switch states).
Callback parameters:
public static final class StateChanged {
public PnSState getOldState() { } // Before the change, see [PnSState](./api#pnsstate)
public PnSState getNewState() { } // After the change
public PnSEventLevel getLevel() { } // INFO
}
Example:
device.getEvents().addStateChangedListener(e ->
System.out.println(e.getOldState() + " → " + e.getNewState()));
addErrorOccurredListener
public void addErrorOccurredListener(Consumer<ErrorOccurred> listener)
Triggered when a runtime error occurs; e.getErrorCode() it is an int primitive value; for enumeration types, use device.getErrorCodeEnum().
Callback parameters:
public static final class ErrorOccurred {
public int getErrorCode() { } // Original value of a runtime error (not an enumeration)
public String getMessage() { } // Chinese message. Missing value is an empty string.
public PnSEventLevel getLevel() { } // WARN
}
Example:
device.getEvents().addErrorOccurredListener(e ->
System.out.println(e.getErrorCode() + ": " + e.getMessage()));
addIoStatusChangedListener
public void addIoStatusChangedListener(Consumer<IoStatusChanged> listener)
The first time I read it, I IOPS only recorded the baseline—no callback. inputIopsHex If outputIopsHex both are missing, no fiction, no callback. A callback is triggered only when the GOOD byte count changes (0 i.e., all GOOD). The same rule applies across all six languages—there is no callback on every read.
Callback parameters:
public static final class IoStatusChanged {
public int getIopsBad() { } // BAD Byte Count
public PnSEventLevel getLevel() { } // >0 = WARN; 0 = INFO
}
addLinkChangedListener
public void addLinkChangedListener(Consumer<LinkChanged> listener)
Triggered when the network card's physical link changes (dispatched only if the ST actually contains a linkUp field).
Callback parameters:
public static final class LinkChanged {
public boolean isLinkUp() { } // Is the link up?
public PnSEventLevel getLevel() { } // Up = INFO; Down = WARN
}
Log
addLoggedListener
public void addLoggedListener(BiConsumer<PnSEventLevel, String> listener)
This message is sent again each time an event is triggered, and its severity level follows that of the source event.
Callback parameters:
level(PnSEventLevel) — Log Levelmessage(String) — Log body
PnSEventLevel
Events and log levels (addLoggedListener the level parameter).
public enum PnSEventLevel {
INFO, // Information
WARN, // Warning
ERROR // Error
}
link = ; INFO Disconnected / Run fault = WARN.
Example:
device.getEvents().addLoggedListener((level, message) ->
System.out.println("[" + level + "] " + message));
Thread Safety
Event callbacks are not triggered on a thread UI . Callbacks are dispatched asynchronously pns-events on a shared daemon thread pool, and the execution order of multiple listeners for the same event is not guaranteed. Each listener is completely isolated: exceptions thrown by one listener are confined to that listener getEventsDropped(), and subsequent listeners continue to execute as normal. Thread synchronization is required when updating the user interface:
// Swing
device.getEvents().addConnectedChangedListener(e ->
SwingUtilities.invokeLater(() ->
labelStatus.setText(e.isConnected() ? "已连接" : "未连接")));
// JavaFX
device.getEvents().addConnectedChangedListener(e ->
Platform.runLater(() ->
statusText.set(e.isConnected() ? "已连接" : "未连接")));