1
1
package com .licheedev .modbus4android ;
2
2
3
+ import android .os .SystemClock ;
3
4
import android .support .annotation .NonNull ;
4
5
import android .util .Log ;
5
6
import com .serotonin .modbus4j .ModbusMaster ;
@@ -50,6 +51,8 @@ public class ModbusWorker implements IModbusWorker {
50
51
private final ExecutorService mRequestExecutor ;
51
52
52
53
protected ModbusMaster mModbusMaster ;
54
+ private long mSendTime ;
55
+ private long mSendIntervalTime ;
53
56
54
57
public ModbusWorker () {
55
58
@@ -115,13 +118,33 @@ public void checkWorkingState() throws ModbusInitException, IllegalStateExceptio
115
118
* @throws ModbusRespException
116
119
* @throws ExecutionException
117
120
*/
118
- private <T > T doSync (Callable <T > callable )
121
+ private <T > T doSync (final Callable <T > callable )
119
122
throws InterruptedException , ModbusInitException , ModbusTransportException ,
120
123
ModbusRespException , ExecutionException {
121
124
122
125
Future <T > submit = null ;
123
126
try {
124
- submit = mRequestExecutor .submit (callable );
127
+
128
+ Callable <T > finalCallable = callable ;
129
+
130
+ if (getSendIntervalTime () > 0 ) {
131
+ finalCallable = new Callable <T >() {
132
+ @ Override
133
+ public T call () throws Exception {
134
+ if (mSendTime > 0 ) {
135
+ long offset =
136
+ (getSendIntervalTime () - SystemClock .uptimeMillis () - mSendTime );
137
+ if (offset > 0 ) {
138
+ SystemClock .sleep (offset );
139
+ }
140
+ }
141
+ T result = callable .call ();
142
+ mSendTime = SystemClock .uptimeMillis ();
143
+ return result ;
144
+ }
145
+ };
146
+ }
147
+ submit = mRequestExecutor .submit (finalCallable );
125
148
return submit .get ();
126
149
} catch (InterruptedException e ) {
127
150
if (submit != null ) {
@@ -144,6 +167,31 @@ private <T> T doSync(Callable<T> callable)
144
167
}
145
168
}
146
169
170
+ /**
171
+ * 发送命令间隔时间
172
+ *
173
+ * @return
174
+ */
175
+ protected long getSendIntervalTime () {
176
+ return mSendIntervalTime ;
177
+ }
178
+
179
+ /**
180
+ * 设置两次发送命令之间必须要等待的时间
181
+ *
182
+ * @param ms
183
+ * @return
184
+ */
185
+ public void setSendIntervalTime (long ms ) {
186
+
187
+ if (ms < 0 ) {
188
+ throw new IllegalArgumentException (
189
+ "Send interval time should not be negative, but now ms=" + ms );
190
+ }
191
+
192
+ mSendIntervalTime = ms ;
193
+ }
194
+
147
195
/**
148
196
* Rx发送数据源
149
197
*
@@ -234,6 +282,9 @@ private Callable<ModbusMaster> callableInit(final ModbusParam param) {
234
282
@ Override
235
283
public ModbusMaster call () throws Exception {
236
284
285
+ // 重置发送时间
286
+ mSendTime = 0 ;
287
+
237
288
if (mModbusMaster != null ) {
238
289
mModbusMaster .destroy ();
239
290
mModbusMaster = null ;
@@ -286,7 +337,7 @@ public synchronized ModbusMaster syncInit(final ModbusParam param)
286
337
*/
287
338
@ Override
288
339
public Observable <ModbusMaster > rxInit (final ModbusParam param ) {
289
- return getRxObservable (callableInit (param ));
340
+ return getRxObservable (callableInit (param )). subscribeOn ( Schedulers . io ()) ;
290
341
}
291
342
292
343
/**
0 commit comments