001package jmri.jmrit.operations.automation.actions; 002 003import org.slf4j.Logger; 004import org.slf4j.LoggerFactory; 005 006import jmri.InstanceManager; 007import jmri.jmrit.operations.setup.Control; 008import jmri.jmrit.operations.setup.Setup; 009import jmri.jmrit.operations.trains.Train; 010import jmri.jmrit.operations.trains.excel.TrainCustomManifest; 011import jmri.jmrit.operations.trains.excel.TrainCustomSwitchList; 012 013public class RunTrainAction extends Action { 014 015 private static final int _code = ActionCodes.RUN_TRAIN; 016 017 @Override 018 public int getCode() { 019 return _code; 020 } 021 022 @Override 023 public String getName() { 024 return Bundle.getMessage("RunTrain"); 025 } 026 027 @Override 028 public void doAction() { 029 if (getAutomationItem() != null) { 030 if (!Setup.isGenerateCsvManifestEnabled()) { 031 log.warn("Generate CSV Manifest isn't enabled!"); 032 finishAction(false); 033 return; 034 } 035 if (!InstanceManager.getDefault(TrainCustomManifest.class).excelFileExists()) { 036 log.warn("Manifest creator file not found!, directory name: {}, file name: {}", InstanceManager.getDefault(TrainCustomManifest.class) 037 .getDirectoryName(), InstanceManager.getDefault(TrainCustomManifest.class).getFileName()); 038 finishAction(false); 039 return; 040 } 041 Train train = getAutomationItem().getTrain(); 042 if (train == null) { 043 log.warn("No train selected for custom manifest"); 044 finishAction(false); 045 return; 046 } 047 if (!train.isBuilt()) { 048 log.warn("Train ({}) needs to be built before creating a custom manifest", train.getName()); 049 finishAction(false); 050 return; 051 } 052 setRunning(true); 053 // this can wait thread 054 if (!InstanceManager.getDefault(TrainCustomSwitchList.class).checkProcessReady()) { 055 log.warn( 056 "Timeout waiting for excel switch list program to complete previous operation, train ({}), timeout value: {} seconds", 057 train.getName(), Control.excelWaitTime); 058 } 059 // this can wait thread 060 if (!InstanceManager.getDefault(TrainCustomManifest.class).checkProcessReady()) { 061 log.warn( 062 "Timeout waiting for excel manifest program to complete previous operation, train ({}), timeout value: {} seconds", 063 train.getName(), Control.excelWaitTime); 064 } 065 if (InstanceManager.getDefault(TrainCustomManifest.class).doesCommonFileExist()) { 066 log.warn("Manifest CSV common file exists!"); 067 } 068 if (!InstanceManager.getDefault(TrainCustomManifest.class).addCsvFile(train.createCsvManifestFile())) { 069 finishAction(false); 070 return; 071 } 072 log.info("Queued train CSV file ({}) for custom processing", train.getName()); 073 boolean status = InstanceManager.getDefault(TrainCustomManifest.class).process(); 074 if (status) { 075 try { 076 status = InstanceManager.getDefault(TrainCustomManifest.class).waitForProcessToComplete(); // wait for process to complete or timeout 077 } catch (InterruptedException e) { 078 // TODO Auto-generated catch block 079 log.error("Thread unexpectedly interrupted", e); 080 } 081 if (!status) { 082 log.warn("Timeout when creating custom manifest for train ({})", train.getName()); 083 } 084 } 085 finishAction(status); 086 } 087 } 088 089 @Override 090 public void cancelAction() { 091 // no cancel for this action 092 } 093 094 private final static Logger log = LoggerFactory.getLogger(RunTrainAction.class); 095}