Skip to content

Commit e224365

Browse files
authored
Merge pull request #15 from target/scale_log_addition
Adding lock and log for scale stable weight
2 parents 3cbf4f7 + 85313ae commit e224365

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/main/java/com/target/devicemanager/components/scale/ScaleController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ public SseEmitter getLiveWeight() throws IOException {
8686
content = @Content(schema = @Schema(implementation = DeviceError.class)))
8787
})
8888
public FormattedWeight getStableWeight() throws ScaleException {
89+
long randomWithTS = System.currentTimeMillis();
8990
String url = "/v1/scale/stableweight";
90-
LOGGER.info("request: " + url);
91+
LOGGER.info("request: " + randomWithTS + " " + url);
9192
CompletableFuture<FormattedWeight> completableFuture = new CompletableFuture<>();
9293
try {
9394
FormattedWeight weight = scaleManager.getStableWeight(completableFuture);
94-
LOGGER.info("response: " + url + " - 200 OK");
95+
LOGGER.info("response: " + randomWithTS + " " + url + " - 200 OK ");
9596
return weight;
9697
} catch (ScaleException scaleException) {
97-
LOGGER.info("response: " + url + " - " + scaleException.getDeviceError().getStatusCode().toString() + ", " + scaleException.getDeviceError());
98+
LOGGER.info("response: " + randomWithTS + " " + url + " - " + scaleException.getDeviceError().getStatusCode().toString() + ", " + scaleException.getDeviceError());
9899
throw scaleException;
99100
}
100101
}

src/main/java/com/target/devicemanager/components/scale/ScaleDevice.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,19 @@ void startStableWeightRead(int timeout) {
193193
long currentTimeMsec = System.currentTimeMillis();
194194
long endTimeMsec = currentTimeMsec + timeout;
195195
while(currentTimeMsec <= endTimeMsec) {
196+
LOGGER.error("Read Weight Time Remaining " + (endTimeMsec - currentTimeMsec));
196197
try {
197198
scale.readWeight(weight, STABLE_WEIGHT_READ_TIMEOUT);
199+
LOGGER.error("After ReadWeight " + weight[0]);
198200
fireScaleStableWeightDataEvent(new FormattedWeight(weight[0]));
199201
stableWeightInProgress = false;
200202
weight = new int[1];
201203
return;
202204
} catch (JposException jposException) {
203205
if(isConnected()) {
204206
LOGGER.error(MARKER, "Scale Failed to Read Stable Weight: " + jposException.getErrorCode() + ", " + jposException.getErrorCodeExtended());
207+
} else {
208+
LOGGER.error(MARKER, "Scale not connected in Read Stable Weight: " + jposException.getErrorCode() + ", " + jposException.getErrorCodeExtended());
205209
}
206210
if(jposException.getErrorCode() != JposConst.JPOS_E_TIMEOUT) {
207211
fireScaleWeightErrorEvent(jposException);

src/main/java/com/target/devicemanager/components/scale/ScaleManager.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,27 @@ void subscribeToLiveWeight(SseEmitter liveWeightEmitter) throws IOException {
113113
}
114114

115115
public FormattedWeight getStableWeight(CompletableFuture<FormattedWeight> stableWeightClient) throws ScaleException {
116-
//Create new future and add it to the list
117-
stableWeightClients.add(stableWeightClient);
118-
scaleDevice.startStableWeightRead(STABLE_WEIGHT_TIMEOUT_MSEC);
119-
try {
120-
//Timeout as a double check against timing errors that would cause us to hang forever
121-
return stableWeightClient.get(HANG_TIMEOUT_MSEC, TimeUnit.MILLISECONDS);
122-
} catch (ExecutionException executionException) {
123-
Throwable jposException = executionException.getCause();
124-
ScaleException scaleException = new ScaleException((JposException)jposException);
125-
throw scaleException;
126-
} catch (InterruptedException interruptedException) {
127-
ScaleException scaleException = new ScaleException(new JposException(JposConst.JPOS_E_FAILURE));
128-
throw scaleException;
129-
} catch (TimeoutException timeoutException) {
130-
ScaleException scaleException = new ScaleException(new JposException(JposConst.JPOS_E_TIMEOUT));
131-
throw scaleException;
116+
if (scaleDevice.tryLock()) {
117+
//Create new future and add it to the list
118+
stableWeightClients.add(stableWeightClient);
119+
scaleDevice.startStableWeightRead(STABLE_WEIGHT_TIMEOUT_MSEC);
120+
try {
121+
//Timeout as a double check against timing errors that would cause us to hang forever
122+
return stableWeightClient.get(HANG_TIMEOUT_MSEC, TimeUnit.MILLISECONDS);
123+
} catch (ExecutionException executionException) {
124+
Throwable jposException = executionException.getCause();
125+
throw (new ScaleException((JposException)jposException));
126+
} catch (InterruptedException interruptedException) {
127+
throw (new ScaleException(new JposException(JposConst.JPOS_E_FAILURE)));
128+
} catch (TimeoutException timeoutException) {
129+
throw (new ScaleException(new JposException(JposConst.JPOS_E_TIMEOUT)));
130+
}
131+
finally {
132+
scaleDevice.unlock();
133+
}
134+
} else {
135+
LOGGER.error("Scale Device Busy. Please Wait To Get Stable Weight.");
136+
throw (new ScaleException(new JposException(JposConst.JPOS_E_BUSY)));
132137
}
133138
}
134139

0 commit comments

Comments
 (0)