@@ -113,22 +113,27 @@ void subscribeToLiveWeight(SseEmitter liveWeightEmitter) throws IOException {
113
113
}
114
114
115
115
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 )));
132
137
}
133
138
}
134
139
0 commit comments