User Tools

Site Tools


zalozeni:logika:experimentlogicoverview

ExperimentLogic overview

Pro uplnost cela trida DokuwikiExpLogic

public class DokuwikiExpLogic extends ExperimentLogic {
 
  private DokuwikiInstrument instrument;
  private DokuwikiExpInstanceSettignsService instanceSS;
  private DokuwikiDataService dataService;
 
  @Override
  public void setExpInstance(ExperimentInstance expInstance) {
    super.setExpInstance(expInstance);
 
    instrument = (DokuwikiInstrument) instrument;
    instanceSS = (DokuwikiExpInstanceSettignsService) expInstance.getSettignsService();
    dataService = (DokuwikiDataService) expInstance.getDataService();
 
    ServiceLocator.getInstance().getLogicModuleManager().registerListener(expInstance, new ExperimentLogicListener() {
      @Override
      public void processEvent(LogicModuleEvent event) {
        if (event instanceof DokuwikiNewRunningSubstatusEvent) {
          DokuwikiNewRunningSubstatusEvent pevent = (DokuwikiNewRunningSubstatusEvent) event;
          ChangingStatusReturnCode status;
          switch (pevent.getSubstatus()) {
          case PREPARATION:
            status = initSamplePreparation();
            break;
          case RUNNING:
            status = initRunning();
            break;
          case FINISHED:
            status = initFinished();
            break;
          default:
            status = ChangingStatusReturnCode.ERROR;
          }
          if (status == ChangingStatusReturnCode.OK) {
            changeRunningSubstatus(pevent.getSubstatus());
          }
        }
      }
    });
  }
 
  @Override
  public ChangingStatusReturnCode postAbortedRun() {
    return terminate();
  }
 
  @Override
  public ChangingStatusReturnCode postChashed() {
    return init();
  }
 
  @Override
  public ChangingStatusReturnCode postPause() {
    return init();
  }
 
  @Override
  public ChangingStatusReturnCode postRun() {
    return terminate();
  }
 
  @Override
  public ChangingStatusReturnCode postSuspend() {
    return ChangingStatusReturnCode.OK;
  }
 
  @Override
  public ChangingStatusReturnCode prePause() {
    return ChangingStatusReturnCode.OK;
  }
 
  @Override
  public ChangingStatusReturnCode preRun() {
    return init();
  }
 
  @Override
  public ChangingStatusReturnCode preSuspend() {
    return terminate();
  }
 
  @Override
  public ChangingStatusReturnCode restore() {
    return ChangingStatusReturnCode.OK;
  }
 
  @Override
  public ChangingStatusReturnCode runLogic() {
    while (true) {
 
      if (!running)
        break;
 
      try {
        Thread.sleep(1);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
 
      try {
        switch (runningSubstatus) {
        case PREPARATION:
          if (System.currentTimeMillis() - lastReadOutTick > intervalReadOut) {
            tempObserver.readValue();
            lastReadOutTick = System.currentTimeMillis();
 
            // nejaka podminka je splnena, takze se prepnu do
            // RUNNING
            if (true) {
              raiseExpInstanceEvent(new DokuwikiNewRunningSubstatusEvent(DokuwikiRunningSubstatus.RUNNING));
            }
          }
          break;
        case RUNNING:
          if (System.currentTimeMillis() - lastReadOutTick > intervalReadOut) {
            tempObserver.readValue();
            counterObserver.readValue();
            lastReadOutTick = System.currentTimeMillis();
 
            // persistovani
            if (System.currentTimeMillis() - lastPersistTick > intervalPersist) {
              DokuwikiDataRow dataRow = new DokuwikiDataRow(getActualExperimentTime());
              dataRow.setValue1(tempObserver.getTemp());
              dataRow.setValue2(counterObserver.getCounter());
              dataService.saveDataRow(dataRow);
              raiseExpInstanceEvent(new DokuwikiNewDataEvent(dataRow));
              lastPersistTick = System.currentTimeMillis();
            }
 
            // normalni zakonceni
            if (Math.abs(tempObserver.getTemp() - 600) < 5) {
              raiseExpInstanceEvent(new DokuwikiNewRunningSubstatusEvent(DokuwikiRunningSubstatus.FINISHED));
            }
          }
 
          break;
        case FINISHED:
          DokuwikiInstanceCalculatorFacotry.calculate(expInstance, CalculatorEnum.BY_NORM_CALCULATOR);
          return ChangingStatusReturnCode.SUCCESSFUL_END;
        }
 
        consecutiveErrorCount = 0;
 
      } catch (IOException e) {
        consecutiveErrorCount++;
        e.printStackTrace();
      } catch (GetValueException e) {
        consecutiveErrorCount++;
        e.printStackTrace();
      } catch (Exception e) {
        consecutiveErrorCount++;
        e.printStackTrace();
      }
 
    }
 
    if (consecutiveErrorCount > MAX_CONSECUTIVE_ERRORS) {
      return ChangingStatusReturnCode.ERROR;
    }
 
    return ChangingStatusReturnCode.OK;
  }
 
  private void changeRunningSubstatus(DokuwikiRunningSubstatus substatus) {
    runningSubstatus = substatus;
    if (instanceSS != null) {
      instanceSS.setRunningSubstatus(substatus);
    }
  }
 
  private DokuwikiTempObserver tempObserver;
  private DokuwikiCounterObserver counterObserver;
 
  private long lastReadOutTick;
  private long lastPersistTick;
  private long intervalReadOut;
  private long intervalPersist;
 
  private int consecutiveErrorCount = 0;
  private final static int MAX_CONSECUTIVE_ERRORS = 5;
 
  private double startTemp;
  private DokuwikiRunningSubstatus runningSubstatus;
 
  private ChangingStatusReturnCode init() {
    tempObserver = new DokuwikiTempObserver(expInstance);
    counterObserver = new DokuwikiCounterObserver(expInstance);
 
    lastReadOutTick = System.currentTimeMillis();
    lastPersistTick = System.currentTimeMillis();
 
    intervalReadOut = instanceSS.getIntervalReadOut();
    intervalPersist = instanceSS.getIntervalPersist();
 
    consecutiveErrorCount = 0;
 
    startTemp = instanceSS.getStartT();
    runningSubstatus = instanceSS.getRunningSubstatus();
 
    try {
      instrument.getEurotherm().connect();
    } catch (ConnectException e) {
      LogService.getInstance().error("ConnectException while connecting to Eurothoerm");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    } catch (Exception e) {
      LogService.getInstance().error("Unknown Exception while connecting to Eurothoerm");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    }
 
    try {
      instrument.getLabjack().connect();
    } catch (ConnectException e) {
      LogService.getInstance().error("ConnectException while connecting to LabJack");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    } catch (Exception e) {
      LogService.getInstance().error("Unknown Exception while connecting to LabJack");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    }
 
    switch (runningSubstatus) {
    case PREPARATION:
      return initSamplePreparation();
    case RUNNING:
      return initRunning();
    case FINISHED:
      return initFinished();
    default:
      break;
    }
 
    return ChangingStatusReturnCode.OK;
  }
 
  private ChangingStatusReturnCode initFinished() {
    return ChangingStatusReturnCode.OK;
  }
 
  private ChangingStatusReturnCode initRunning() {
    try {
      instrument.setSetPoint(startTemp);
    } catch (IOException e) {
      LogService.getInstance().error("IOException while writing setPoint");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    } catch (SetValueException e) {
      LogService.getInstance().error("SetValueException while writing setPoint");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    } catch (Exception e) {
      LogService.getInstance().error("Unkown Exception while writing setPoint");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    }
    return ChangingStatusReturnCode.OK;
  }
 
  private ChangingStatusReturnCode initSamplePreparation() {
    return ChangingStatusReturnCode.OK;
  }
 
  private ChangingStatusReturnCode terminate() {
    try {
      instrument.setSetPoint(startTemp);
    } catch (IOException e) {
      LogService.getInstance().error("IOException while writing setPoint");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    } catch (SetValueException e) {
      LogService.getInstance().error("SetValueException while writing setPoint");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    } catch (Exception e) {
      LogService.getInstance().error("Unkown Exception while writing setPoint");
      e.printStackTrace();
      return ChangingStatusReturnCode.ERROR;
    }
    return ChangingStatusReturnCode.OK;
  }
 
}
zalozeni/logika/experimentlogicoverview.txt · Last modified: 2016/08/01 09:53 by jlochman