Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/main/java/com/target/devicemanager/common/DynamicDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,43 @@ public void disconnect() {
}
}

public boolean isConnected() {
/**
* Whether the underlying device has been opened (its JavaPOS state is no
* longer CLOSED). A claimed device is always opened, but an opened device
* is not necessarily claimed.
*/
public boolean isOpened() {
synchronized (device) {
int deviceState = device.getState();
if (deviceState != JposConst.JPOS_S_IDLE && deviceState != JposConst.JPOS_S_BUSY) {
return false;
}
return deviceState == JposConst.JPOS_S_IDLE || deviceState == JposConst.JPOS_S_BUSY;
}
}

/**
* Whether this process has claimed (taken exclusive ownership of) the
* device. Returns false if the device is not opened or the claim cannot be
* read.
*/
public boolean isClaimed() {
synchronized (device) {
try {
if (!device.getClaimed()) {
return false;
}
int powerState = devicePower.getPowerState(device);
if (powerState != JposConst.JPOS_PS_ONLINE && powerState != JposConst.JPOS_PS_UNKNOWN) {
return false;
}
return device.getClaimed();
} catch (JposException jposException) {
return false;
}
}
}

public boolean isConnected() {
synchronized (device) {
if (!isOpened() || !isClaimed()) {
return false;
}
int powerState = devicePower.getPowerState(device);
if (powerState != JposConst.JPOS_PS_ONLINE && powerState != JposConst.JPOS_PS_UNKNOWN) {
return false;
}
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@ public ConnectionResult connect() {
public boolean isConnected() {
return simulatedDevice.getState() == JposConst.JPOS_S_IDLE;
}

/**
* A simulated device does not go through the real open/claim handshake, so
* opened/claimed track its simulated connection state instead of the
* underlying JavaPOS control (whose getClaimed() would otherwise report not
* claimed).
*/
@Override
public boolean isOpened() {
return isConnected();
}

@Override
public boolean isClaimed() {
return isConnected();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,19 @@ public String getDeviceName() {
*/
public boolean isConnected() { return deviceConnected; }

/**
* The cash drawer tracks a single cached connection flag (set only after a
* successful open + claim + enable), so opened/claimed mirror isConnected()
* to keep the health gate and the health check on one source of truth.
*/
public boolean isOpened() {
return isConnected();
}

public boolean isClaimed() {
return isConnected();
}

/**
* Attaches an event listener and adding it to a new instance.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ public void openCashDrawer() throws DeviceException {

public DeviceHealthResponse getHealth() {
DeviceHealthResponse deviceHealthResponse;
if (cashDrawerDevice.isConnected()) {
// Issue #10: only run the health check when the device is opened AND claimed;
// otherwise immediately report NOT_READY without checking health.
if (!cashDrawerDevice.isOpened() || !cashDrawerDevice.isClaimed()) {
deviceHealthResponse = new DeviceHealthResponse(cashDrawerDevice.getDeviceName(), DeviceHealth.NOTREADY);
} else if (cashDrawerDevice.isConnected()) {
deviceHealthResponse = new DeviceHealthResponse(cashDrawerDevice.getDeviceName(), DeviceHealth.READY);
} else {
deviceHealthResponse = new DeviceHealthResponse(cashDrawerDevice.getDeviceName(), DeviceHealth.NOTREADY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ public boolean isConnected() {
return dynamicMicr.isConnected();
}

/**
* @return Device is opened.
*/
public boolean isOpened() {
return dynamicMicr.isOpened();
}

/**
* @return Device is claimed.
*/
public boolean isClaimed() {
return dynamicMicr.isClaimed();
}

/**
* begins check insertion process.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public void connectionEventOccurred(ConnectionEvent connectionEvent) {

public DeviceHealthResponse getHealth() {
DeviceHealthResponse deviceHealthResponse;
if (micrDevice.isConnected()) {
// Issue #10: only run the health check when the device is opened AND claimed;
// otherwise immediately report NOT_READY without checking health.
if (!micrDevice.isOpened() || !micrDevice.isClaimed()) {
deviceHealthResponse = new DeviceHealthResponse(micrDevice.getDeviceName(), DeviceHealth.NOTREADY);
} else if (micrDevice.isConnected()) {
deviceHealthResponse = new DeviceHealthResponse(micrDevice.getDeviceName(), DeviceHealth.READY);
} else {
deviceHealthResponse = new DeviceHealthResponse(micrDevice.getDeviceName(), DeviceHealth.NOTREADY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ public boolean isConnected() {
return dynamicLineDisplay.isConnected();
}

/**
* @return Device is opened.
*/
public boolean isOpened() {
return dynamicLineDisplay.isOpened();
}

/**
* @return Device is claimed.
*/
public boolean isClaimed() {
return dynamicLineDisplay.isClaimed();
}

/**
* Makes sure it displays the lines on device.
* @param line1Text displays the first line text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ private String formatLineText(String lineText) {

public DeviceHealthResponse getHealth() {
DeviceHealthResponse deviceHealthResponse;
if (lineDisplayDevice.isConnected()) {
// Issue #10: only run the health check when the device is opened AND claimed;
// otherwise immediately report NOT_READY without checking health.
if (!lineDisplayDevice.isOpened() || !lineDisplayDevice.isClaimed()) {
deviceHealthResponse = new DeviceHealthResponse(lineDisplayDevice.getDeviceName(), DeviceHealth.NOTREADY);
} else if (lineDisplayDevice.isConnected()) {
deviceHealthResponse = new DeviceHealthResponse(lineDisplayDevice.getDeviceName(), DeviceHealth.READY);
} else {
deviceHealthResponse = new DeviceHealthResponse(lineDisplayDevice.getDeviceName(), DeviceHealth.NOTREADY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,19 @@ public boolean isConnected() {
return deviceConnected;
}

/**
* The printer tracks a single cached connection flag (set only after a
* successful open + claim + enable), so opened/claimed mirror isConnected()
* to keep the health gate and the health check on one source of truth.
*/
public boolean isOpened() {
return isConnected();
}

public boolean isClaimed() {
return isConnected();
}

public boolean getIsCheckInserted() {
return isCheckInserted;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ public void frankCheck(List<PrinterContent> contents) throws PrinterException {

public DeviceHealthResponse getHealth() {
DeviceHealthResponse deviceHealthResponse;
if (printerDevice.isConnected()) {
// Issue #10: only run the health check when the device is opened AND claimed;
// otherwise immediately report NOT_READY without checking health.
if (!printerDevice.isOpened() || !printerDevice.isClaimed()) {
deviceHealthResponse = new DeviceHealthResponse(printerDevice.getDeviceName(), DeviceHealth.NOTREADY);
} else if (printerDevice.isConnected()) {
deviceHealthResponse = new DeviceHealthResponse(printerDevice.getDeviceName(), DeviceHealth.READY);
} else {
deviceHealthResponse = new DeviceHealthResponse(printerDevice.getDeviceName(), DeviceHealth.NOTREADY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,19 @@ public boolean isConnected() {
return deviceConnected;
}

/**
* The scale tracks a single cached connection flag (set only after a
* successful open + claim + enable), so opened/claimed mirror isConnected()
* to keep the health gate and the health check on one source of truth.
*/
public boolean isOpened() {
return isConnected();
}

public boolean isClaimed() {
return isConnected();
}

/**
* Lock the current resource.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ public boolean isScaleReady() {

public DeviceHealthResponse getHealth() {
DeviceHealthResponse deviceHealthResponse;
if (isScaleReady()) {
// Issue #10: only run the health check when the device is opened AND claimed;
// otherwise immediately report NOT_READY without checking health.
if (!scaleDevice.isOpened() || !scaleDevice.isClaimed()) {
deviceHealthResponse = new DeviceHealthResponse(scaleDevice.getDeviceName(), DeviceHealth.NOTREADY);
} else if (isScaleReady()) {
deviceHealthResponse = new DeviceHealthResponse(scaleDevice.getDeviceName(), DeviceHealth.READY);
} else {
deviceHealthResponse = new DeviceHealthResponse(scaleDevice.getDeviceName(), DeviceHealth.NOTREADY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ public boolean isConnected() {
return dynamicScanner.isConnected();
}

/**
* Whether the scanner has been opened.
* @return Opened status.
*/
public boolean isOpened() {
return dynamicScanner.isOpened();
}

/**
* Whether the scanner has been claimed.
* @return Claimed status.
*/
public boolean isClaimed() {
return dynamicScanner.isClaimed();
}


public void setIsTest(boolean isTest) {
this.isTest = isTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,11 @@ public List<DeviceHealthResponse> getHealth(ScannerType scannerType) {
case "FLATBED":
case "HANDHELD":
if(scanner.getScannerType().equals(scannerType.name())) {
if (scanner.isConnected()) {
response.add(new DeviceHealthResponse(scanner.getDeviceName(), DeviceHealth.READY));
} else {
response.add(new DeviceHealthResponse(scanner.getDeviceName(), DeviceHealth.NOTREADY));
}
response.add(getScannerHealth(scanner));
}
break;
default:
if (scanner.isConnected()) {
response.add(new DeviceHealthResponse(scanner.getDeviceName(), DeviceHealth.READY));
} else {
response.add(new DeviceHealthResponse(scanner.getDeviceName(), DeviceHealth.NOTREADY));
}
response.add(getScannerHealth(scanner));
}
}
try {
Expand All @@ -217,6 +209,18 @@ public List<DeviceHealthResponse> getHealth(ScannerType scannerType) {
return response;
}

/**
* Issue #10: a scanner reports READY only when it is opened AND claimed and
* its health check passes; otherwise the health check is skipped and it
* reports NOT_READY.
*/
private DeviceHealthResponse getScannerHealth(ScannerDevice scanner) {
if (scanner.isOpened() && scanner.isClaimed() && scanner.isConnected()) {
return new DeviceHealthResponse(scanner.getDeviceName(), DeviceHealth.READY);
}
return new DeviceHealthResponse(scanner.getDeviceName(), DeviceHealth.NOTREADY);
}

public List<DeviceHealthResponse> getStatus() {
try {
if (cacheManager != null && Objects.requireNonNull(cacheManager.getCache("scannerHealth")).get("health") != null) {
Expand Down
Loading