2020-04-13 12:17:12 +08:00
|
|
|
package cn.dxl.com;
|
|
|
|
|
|
|
|
|
|
import java.io.Closeable;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Abstract device check base class, can judge whether the device and communication are normal!
|
|
|
|
|
*
|
|
|
|
|
* @author DXL
|
|
|
|
|
* @version 1.0
|
|
|
|
|
*/
|
|
|
|
|
public abstract class DeviceChecker implements Serializable, Closeable {
|
|
|
|
|
protected Communication communication;
|
|
|
|
|
|
|
|
|
|
public DeviceChecker(Communication communication) {
|
|
|
|
|
this.communication = communication;
|
2020-04-14 18:42:35 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-13 12:17:12 +08:00
|
|
|
/**
|
|
|
|
|
* Test a device is can work!
|
|
|
|
|
*
|
|
|
|
|
* @return if can work return true, if no, return false
|
|
|
|
|
*/
|
2020-04-15 19:02:35 +08:00
|
|
|
public final boolean check() throws IOException {
|
|
|
|
|
// Auto init communication to mapper.
|
|
|
|
|
LocalComBridgeAdapter.getInstance()
|
|
|
|
|
.setInputStream(communication.getInput())
|
|
|
|
|
.setOutputStream(communication.getOutput())
|
|
|
|
|
.startServer();
|
|
|
|
|
if (!checkDevice()) {
|
|
|
|
|
// if check failed, we must to close device!
|
|
|
|
|
LocalComBridgeAdapter.getInstance().stopClient();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2020-04-14 18:42:35 +08:00
|
|
|
}
|
2020-04-13 12:17:12 +08:00
|
|
|
|
2020-04-15 19:02:35 +08:00
|
|
|
protected abstract boolean checkDevice() throws IOException;
|
|
|
|
|
|
2020-04-13 12:17:12 +08:00
|
|
|
/**
|
|
|
|
|
* Close the device(Should no close communication. only close device!)
|
|
|
|
|
*/
|
|
|
|
|
public abstract void close() throws IOException;
|
|
|
|
|
}
|