@@ -89,9 +89,9 @@ public enum ProcessDataResult {
8989 }
9090
9191 private @ Nullable ScheduledFuture <?> periodicJob = null ;
92- private @ Nullable ScheduledFuture <?> reconnectJob = null ;
9392
9493 private final AtomicBoolean reconnecting = new AtomicBoolean (false );
94+ private long lastExecution = 0 ;
9595
9696 /**
9797 * Constructor
@@ -102,16 +102,16 @@ public SimaticGenericDevice(int pollRate, Charset charset, SimaticUpdateMode upd
102102 this .updateMode = updateMode ;
103103 if (pollRate > 0 ) {
104104 periodicJob = scheduler .scheduleAtFixedRate (() -> {
105- if (!reconnecting .get ()) {
105+ if (System .currentTimeMillis () - lastExecution >= pollRate ) {
106+ lastExecution = System .currentTimeMillis ();
106107 execute ();
107108 }
108109 }, 500 , pollRate , TimeUnit .MILLISECONDS );
109110 } else {
110111 scheduler .execute (() -> {
111112 while (!disposed ) {
113+ execute ();
112114 if (!reconnecting .get ()) {
113- execute ();
114- } else {
115115 try {
116116 Thread .sleep (500 );
117117 } catch (InterruptedException e ) {
@@ -134,10 +134,6 @@ public void dispose() {
134134 periodicJob .cancel (true );
135135 periodicJob = null ;
136136 }
137- if (reconnectJob != null ) {
138- reconnectJob .cancel (true );
139- reconnectJob = null ;
140- }
141137 }
142138
143139 /**
@@ -147,7 +143,9 @@ protected void execute() {
147143 if (shouldReconnect ()) {
148144 reconnectWithDelaying ();
149145 }
150- checkNewData ();
146+ if (!reconnecting .get ()) {
147+ checkNewData ();
148+ }
151149 }
152150
153151 @ Override
@@ -225,44 +223,30 @@ public boolean reconnect() {
225223 * Reconnect device
226224 */
227225 protected void reconnectWithDelaying () {
228- if (!reconnecting .compareAndSet (false , true )) {
229- logger .debug ("{} - reconnectJob(): already running" , toString ());
230- return ;
226+ if (reconnecting .compareAndSet (false , true )) {
227+ logger .debug ("{} - reconnectJob(): started..." , toString ());
231228 }
232229
233- if (reconnectJob != null ) {
234- logger .debug ("{} - reconnectJob(): canceling previous instance" , toString ());
235- reconnectJob .cancel (true );
236- reconnectJob = null ;
237- }
238-
239- logger .debug ("{} - reconnectJob(): create" , toString ());
230+ logger .debug ("{} - reconnectJob(): {}/{}/{}" , toString (), rcTest , rcTestMax , RECONNECT_DELAY_MAX );
240231
241- reconnectJob = scheduler .scheduleAtFixedRate (() -> {
242- logger .debug ("{} - reconnectJob(): {}/{}/{}" , toString (), rcTest , rcTestMax , RECONNECT_DELAY_MAX );
243-
244- if (rcTest < rcTestMax ) {
245- rcTest ++;
246- return ;
247- }
232+ if (rcTest < rcTestMax ) {
233+ rcTest ++;
234+ return ;
235+ }
248236
249- if (reconnect ()) {
250- rcTest = 0 ;
251- rcTestMax = 0 ;
237+ logger .debug ("{} - reconnectJob(): reconnecting..." , toString ());
238+ if (reconnect ()) {
239+ rcTest = 0 ;
240+ rcTestMax = 0 ;
252241
253- logger .debug ("{} - reconnectJob(): reconnected" , toString ());
254- if (reconnectJob .cancel (false )) {
255- reconnectJob = null ;
256- logger .debug ("{} - reconnectJob(): canceled" , toString ());
257- reconnecting .set (false );
258- }
259- } else {
260- if (rcTestMax <= RECONNECT_DELAY_MAX ) {
261- rcTestMax ++;
262- }
263- rcTest = 0 ;
242+ logger .debug ("{} - reconnectJob(): reconnected" , toString ());
243+ reconnecting .set (false );
244+ } else {
245+ if (rcTestMax < RECONNECT_DELAY_MAX ) {
246+ rcTestMax ++;
264247 }
265- }, 1000 , 1000 , TimeUnit .MILLISECONDS );
248+ rcTest = 0 ;
249+ }
266250 }
267251
268252 /**
@@ -428,12 +412,6 @@ protected void checkNewData() {
428412
429413 readed ++;
430414 readedBytes += area .getAddressSpaceLength ();
431-
432- /*
433- * if (logger.isDebugEnabled()) {
434- * logger.debug("{} - Reading finished. Area={}", toString(), area.toString());
435- * }
436- */
437415 }
438416 } catch (Exception ex ) {
439417 logger .error ("{} - Read data error" , toString (), ex );
0 commit comments