|
@ -1,11 +1,36 @@ |
|
|
package com.iflytop.digester.underframework; |
|
|
package com.iflytop.digester.underframework; |
|
|
import com.iflytop.digester.underframework.dao.model.UfMdbActuator; |
|
|
import com.iflytop.digester.underframework.dao.model.UfMdbActuator; |
|
|
import com.iflytop.digester.underframework.dao.model.UfMdbActuatorCmd; |
|
|
import com.iflytop.digester.underframework.dao.model.UfMdbActuatorCmd; |
|
|
|
|
|
import com.iflytop.digester.underframework.dao.model.UfMdbNotification; |
|
|
import com.iflytop.digester.underframework.dao.record.UfActiveRecord; |
|
|
import com.iflytop.digester.underframework.dao.record.UfActiveRecord; |
|
|
|
|
|
import com.iflytop.digester.underframework.util.UfCommon; |
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.TimerTask; |
|
|
|
|
|
|
|
|
public class UfActuatorCmdExecutor { |
|
|
public class UfActuatorCmdExecutor { |
|
|
|
|
|
// logger |
|
|
|
|
|
public static final Logger LOG = LoggerFactory.getLogger(UfActuatorCmdExecutor.class); |
|
|
|
|
|
|
|
|
|
|
|
// emergency stop restore lock |
|
|
|
|
|
private static final Object emergencyStopRestoreLock = new Object(); |
|
|
|
|
|
|
|
|
|
|
|
// execute command |
|
|
|
|
|
public static String emergencyExecute(UfMdbActuatorCmd cmd) { |
|
|
|
|
|
var con = UfApplication.getApp().connections.get(cmd.connectionKey); |
|
|
|
|
|
return con.execute(cmd); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// execute cmd |
|
|
// execute cmd |
|
|
public static String execute(UfMdbActuatorCmd cmd) { |
|
|
public static String execute(UfMdbActuatorCmd cmd) { |
|
|
|
|
|
if ( UfActuatorCmdExecutor.hasEmergencyStopTriggered() ) { |
|
|
|
|
|
throw new RuntimeException("触发紧急停止"); |
|
|
|
|
|
} |
|
|
var con = UfApplication.getApp().connections.get(cmd.connectionKey); |
|
|
var con = UfApplication.getApp().connections.get(cmd.connectionKey); |
|
|
return con.execute(cmd); |
|
|
return con.execute(cmd); |
|
|
} |
|
|
} |
|
@ -30,4 +55,59 @@ public class UfActuatorCmdExecutor { |
|
|
public static String execute(String actuatorKey, String cmdKey) { |
|
|
public static String execute(String actuatorKey, String cmdKey) { |
|
|
return execute(actuatorKey, cmdKey, null); |
|
|
return execute(actuatorKey, cmdKey, null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// check if emergency stop triggered |
|
|
|
|
|
private static Boolean hasEmergencyStopTriggered() { |
|
|
|
|
|
String filePath = UfApplication.getApp().getEnv().getProperty("app.emergency-stop-file-path"); |
|
|
|
|
|
if ( null == filePath ) { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// read all content from file as string |
|
|
|
|
|
try { |
|
|
|
|
|
String state = Files.readString(Path.of(filePath)); |
|
|
|
|
|
if ( "1".equals(state) ) { |
|
|
|
|
|
return false; // not triggered |
|
|
|
|
|
} |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// triggered |
|
|
|
|
|
LOG.warn("急停触发"); |
|
|
|
|
|
UfMdbNotification.action("EmergencyStop", new HashMap<>()); |
|
|
|
|
|
UfCmdSnippetExecutor.execute("EmergencyStop"); |
|
|
|
|
|
var restoreCheckThread = new Thread(UfActuatorCmdExecutor::emergencyStopRestoreCheck); |
|
|
|
|
|
restoreCheckThread.start(); |
|
|
|
|
|
synchronized ( UfActuatorCmdExecutor.emergencyStopRestoreLock ) { |
|
|
|
|
|
try { |
|
|
|
|
|
UfActuatorCmdExecutor.emergencyStopRestoreLock.wait(); |
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
UfCmdSnippetExecutor.execute("EmergencyStopRestore"); |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 急停恢复 |
|
|
|
|
|
public static void emergencyStopRestoreCheck() { |
|
|
|
|
|
do { |
|
|
|
|
|
UfCommon.delay(1000); |
|
|
|
|
|
String filePath = UfApplication.getApp().getEnv().getProperty("app.emergency-stop-file-path"); |
|
|
|
|
|
assert filePath != null; |
|
|
|
|
|
try { |
|
|
|
|
|
String state = Files.readString(Path.of(filePath)); |
|
|
|
|
|
if ( "1".equals(state) ) { |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} while ( true ); |
|
|
|
|
|
LOG.warn("急停恢复"); |
|
|
|
|
|
synchronized ( UfActuatorCmdExecutor.emergencyStopRestoreLock ) { |
|
|
|
|
|
UfActuatorCmdExecutor.emergencyStopRestoreLock.notify(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |