Skip to content

Commit

Permalink
Merge pull request #15 from target/scale_log_addition
Browse files Browse the repository at this point in the history
Adding lock and log for scale stable weight
  • Loading branch information
arpal7 authored Oct 26, 2023
2 parents 3cbf4f7 + 85313ae commit e224365
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ public SseEmitter getLiveWeight() throws IOException {
content = @Content(schema = @Schema(implementation = DeviceError.class)))
})
public FormattedWeight getStableWeight() throws ScaleException {
long randomWithTS = System.currentTimeMillis();
String url = "/v1/scale/stableweight";
LOGGER.info("request: " + url);
LOGGER.info("request: " + randomWithTS + " " + url);
CompletableFuture<FormattedWeight> completableFuture = new CompletableFuture<>();
try {
FormattedWeight weight = scaleManager.getStableWeight(completableFuture);
LOGGER.info("response: " + url + " - 200 OK");
LOGGER.info("response: " + randomWithTS + " " + url + " - 200 OK ");
return weight;
} catch (ScaleException scaleException) {
LOGGER.info("response: " + url + " - " + scaleException.getDeviceError().getStatusCode().toString() + ", " + scaleException.getDeviceError());
LOGGER.info("response: " + randomWithTS + " " + url + " - " + scaleException.getDeviceError().getStatusCode().toString() + ", " + scaleException.getDeviceError());
throw scaleException;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,19 @@ void startStableWeightRead(int timeout) {
long currentTimeMsec = System.currentTimeMillis();
long endTimeMsec = currentTimeMsec + timeout;
while(currentTimeMsec <= endTimeMsec) {
LOGGER.error("Read Weight Time Remaining " + (endTimeMsec - currentTimeMsec));
try {
scale.readWeight(weight, STABLE_WEIGHT_READ_TIMEOUT);
LOGGER.error("After ReadWeight " + weight[0]);
fireScaleStableWeightDataEvent(new FormattedWeight(weight[0]));
stableWeightInProgress = false;
weight = new int[1];
return;
} catch (JposException jposException) {
if(isConnected()) {
LOGGER.error(MARKER, "Scale Failed to Read Stable Weight: " + jposException.getErrorCode() + ", " + jposException.getErrorCodeExtended());
} else {
LOGGER.error(MARKER, "Scale not connected in Read Stable Weight: " + jposException.getErrorCode() + ", " + jposException.getErrorCodeExtended());
}
if(jposException.getErrorCode() != JposConst.JPOS_E_TIMEOUT) {
fireScaleWeightErrorEvent(jposException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,27 @@ void subscribeToLiveWeight(SseEmitter liveWeightEmitter) throws IOException {
}

public FormattedWeight getStableWeight(CompletableFuture<FormattedWeight> stableWeightClient) throws ScaleException {
//Create new future and add it to the list
stableWeightClients.add(stableWeightClient);
scaleDevice.startStableWeightRead(STABLE_WEIGHT_TIMEOUT_MSEC);
try {
//Timeout as a double check against timing errors that would cause us to hang forever
return stableWeightClient.get(HANG_TIMEOUT_MSEC, TimeUnit.MILLISECONDS);
} catch (ExecutionException executionException) {
Throwable jposException = executionException.getCause();
ScaleException scaleException = new ScaleException((JposException)jposException);
throw scaleException;
} catch (InterruptedException interruptedException) {
ScaleException scaleException = new ScaleException(new JposException(JposConst.JPOS_E_FAILURE));
throw scaleException;
} catch (TimeoutException timeoutException) {
ScaleException scaleException = new ScaleException(new JposException(JposConst.JPOS_E_TIMEOUT));
throw scaleException;
if (scaleDevice.tryLock()) {
//Create new future and add it to the list
stableWeightClients.add(stableWeightClient);
scaleDevice.startStableWeightRead(STABLE_WEIGHT_TIMEOUT_MSEC);
try {
//Timeout as a double check against timing errors that would cause us to hang forever
return stableWeightClient.get(HANG_TIMEOUT_MSEC, TimeUnit.MILLISECONDS);
} catch (ExecutionException executionException) {
Throwable jposException = executionException.getCause();
throw (new ScaleException((JposException)jposException));
} catch (InterruptedException interruptedException) {
throw (new ScaleException(new JposException(JposConst.JPOS_E_FAILURE)));
} catch (TimeoutException timeoutException) {
throw (new ScaleException(new JposException(JposConst.JPOS_E_TIMEOUT)));
}
finally {
scaleDevice.unlock();
}
} else {
LOGGER.error("Scale Device Busy. Please Wait To Get Stable Weight.");
throw (new ScaleException(new JposException(JposConst.JPOS_E_BUSY)));
}
}

Expand Down

0 comments on commit e224365

Please sign in to comment.