245 lines
4.3 KiB
Java
245 lines
4.3 KiB
Java
package org.hwo.pulscounter;
|
|
|
|
import org.hwo.i18n.Messages;
|
|
import org.hwo.pulscounter.device.IDeviceConnector;
|
|
import static org.hwo.logging.Logging.*;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import static org.hwo.logging.LogLevel.*;
|
|
|
|
public class DeviceTester extends Thread{
|
|
|
|
private IDeviceConnector device;
|
|
|
|
public DeviceTester(IDeviceConnector device){
|
|
this.device = device;
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
prepare();
|
|
|
|
try {
|
|
if (fullTest()){
|
|
PulsCounterApplication.getApplication().message(String.format(
|
|
Messages.getString("Der Selbsttest für Zähler #%d war erfolgreich."),
|
|
device.getDeviceSerial()
|
|
));
|
|
} else {
|
|
PulsCounterApplication.getApplication().message(String.format(
|
|
Messages.getString("Der Selbsttest für Zähler #%d ist fehlgeschlagen."),
|
|
device.getDeviceSerial()
|
|
));
|
|
}
|
|
} catch (Exception e){
|
|
log(ERROR,"Exception while testing device");
|
|
log(e);
|
|
}
|
|
|
|
unprepare();
|
|
}
|
|
|
|
private void sleep(int ms){
|
|
try {
|
|
Thread.sleep(ms);
|
|
} catch (InterruptedException e){
|
|
log(e);
|
|
}
|
|
}
|
|
|
|
public boolean fullTest(){
|
|
|
|
if (!setIncrements(1))
|
|
return false;
|
|
|
|
if (!setPullups(false))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setOutputs(false))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setInverts(false))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setCounters(999999))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setCounters(0))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
|
|
for (int i=0;i<10;i++){
|
|
if (!setInverts(true))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setInverts(false))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
}
|
|
|
|
if (!checkCounters(10))
|
|
return false;
|
|
|
|
for (int i=0;i<10;i++){
|
|
if (!setPullups(true))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setPullups(false))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
}
|
|
|
|
sleep( 500 );
|
|
|
|
if (!checkCounters(20))
|
|
return false;
|
|
|
|
if (!setPullups(true))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
for (int i=0;i<10;i++){
|
|
if (!setOutputs(true))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
if (!setOutputs(false))
|
|
return false;
|
|
|
|
sleep( 50 );
|
|
|
|
}
|
|
|
|
if (!checkCounters(30))
|
|
return false;
|
|
|
|
if (!setPullups(false))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
public void prepare(){
|
|
device.escape(IDeviceConnector.ESC_PREPARE_DEVICETEST, 0);
|
|
try {
|
|
Thread.sleep(250);
|
|
} catch (InterruptedException e){
|
|
log(e);
|
|
}
|
|
|
|
}
|
|
public void unprepare(){
|
|
device.escape(IDeviceConnector.ESC_UNPREPARE_DEVICETEST, 0);
|
|
}
|
|
|
|
public boolean setPullups(boolean on){
|
|
int set,act;
|
|
|
|
set = on ? 0xFFFFFFFF : 0;
|
|
device.setPullups(set);
|
|
act = device.getPullups();
|
|
|
|
log(INFO,"setPullups(%s): Result: %s",on,(act==set));
|
|
return (set == act);
|
|
}
|
|
|
|
public boolean setOutputs(boolean on){
|
|
int set,act;
|
|
|
|
set = on ? 0xFFFFFFFF : 0;
|
|
device.setOutputs(set);
|
|
act = device.getOutputs();
|
|
|
|
log(INFO,"setOutputs(%s): Result: %s",on,(act==set));
|
|
return (set == act);
|
|
}
|
|
|
|
public boolean setInverts(boolean on){
|
|
int set,act;
|
|
|
|
set = on ? 0xFFFFFFFF : 0;
|
|
device.setInverts(set);
|
|
act = device.getInverts();
|
|
|
|
log(INFO,"setInverts(%s): Result: %s",on,(act==set));
|
|
return (set == act);
|
|
}
|
|
|
|
public boolean setCounters(int set){
|
|
boolean success = true;
|
|
int[] _set = new int[32];
|
|
int[] _act = new int[32];
|
|
|
|
Arrays.fill(_set, set);
|
|
device.setCounters(_set);
|
|
_act = device.getCounters();
|
|
|
|
for (int i=0;i<32;i++){
|
|
if (_set[i] != _act[i]){
|
|
success = false;
|
|
log(INFO,"Setting Channel Counter %d failed [%d!=%d]",i,_set[i],_act[i]);
|
|
}
|
|
}
|
|
return success;
|
|
}
|
|
|
|
public boolean setIncrements(int set){
|
|
boolean success = true;
|
|
int[] _set = new int[32];
|
|
int[] _act = new int[32];
|
|
|
|
Arrays.fill(_set, set);
|
|
device.setIncrements(_set);
|
|
_act = device.getIncrements();
|
|
|
|
for (int i=0;i<32;i++){
|
|
if (_set[i] != _act[i]){
|
|
success = false;
|
|
log(INFO,"Setting Channel Counter Increment %d failed",i);
|
|
}
|
|
}
|
|
return success;
|
|
}
|
|
|
|
|
|
public boolean checkCounters(int check){
|
|
boolean success = true;
|
|
int[] _check = new int[32];
|
|
int[] _act = new int[32];
|
|
|
|
Arrays.fill(_check, check);
|
|
_act = device.getCounters();
|
|
|
|
for (int i=0;i<32;i++){
|
|
if (_check[i] != _act[i]){
|
|
success = false;
|
|
log(INFO,"Check Channel Counter %d failed (0x%08x != 0x%08x)",i,check,_act[i]);
|
|
}
|
|
}
|
|
return success;
|
|
}
|
|
|
|
|
|
}
|