13 changed files with 139 additions and 351 deletions
-
BINapp.db
-
29pom.xml
-
18src/main/java/a8k/MyApplicationRunner.java
-
17src/main/java/a8k/SpringBootBeanUtil.java
-
31src/main/java/a8k/extapi_controler/ExtApiControler.java
-
37src/main/java/a8k/extapi_controler/utils/EnginnerParamReader.java
-
3src/main/java/a8k/service/app/devicedriver/ctrl/ReactionPlatesTransmitCtrl.java
-
160src/main/java/a8k/service/app/devicedriver/testscript/TestScript.java
-
104src/main/java/a8k/service/db/HardwareServiceSetting.java
-
10src/main/java/a8k/utils/AppService.java
-
9src/main/java/a8k/utils/AppServiceAction.java
-
69src/main/java/a8k/utils/AppServiceManager.java
-
3src/main/java/com/iflytop/a800/BoditechA800Application.java
@ -1,18 +0,0 @@ |
|||
package a8k; |
|||
import a8k.utils.AppServiceManager; |
|||
import jakarta.annotation.Resource; |
|||
import org.springframework.boot.ApplicationArguments; |
|||
import org.springframework.boot.ApplicationRunner; |
|||
import org.springframework.core.annotation.Order; |
|||
import org.springframework.stereotype.Component; |
|||
@Order(1) |
|||
@Component |
|||
public class MyApplicationRunner implements ApplicationRunner { |
|||
@Resource |
|||
private AppServiceManager appServiceManager; |
|||
|
|||
@Override |
|||
public void run(ApplicationArguments args) throws Exception { |
|||
this.appServiceManager.loadActions(); |
|||
} |
|||
} |
@ -1,37 +0,0 @@ |
|||
package a8k.extapi_controler.utils; |
|||
|
|||
import a8k.service.db.HardwareServiceSetting; |
|||
import a8k.type.exception.AppException; |
|||
|
|||
|
|||
public class EnginnerParamReader { |
|||
String serviceName; |
|||
|
|||
public EnginnerParamReader(Class<?> cla) { |
|||
this.serviceName = cla.getSimpleName(); |
|||
} |
|||
|
|||
public Integer getInteger(String key, Integer defaultValue) { |
|||
return HardwareServiceSetting.getInteger(serviceName, key, defaultValue); |
|||
} |
|||
|
|||
public <T> T getObject(String key, Class<T> clazs, T defaultVal) { |
|||
return HardwareServiceSetting.getObject(serviceName, key, clazs, defaultVal); |
|||
} |
|||
|
|||
public <T> T getObject(String key, Class<T> clazs) throws AppException { |
|||
return HardwareServiceSetting.getObject(serviceName, key, clazs); |
|||
} |
|||
|
|||
public void setInteger(String key, Integer value) { |
|||
HardwareServiceSetting.setInteger(serviceName, key, value); |
|||
} |
|||
|
|||
public void setObject(String key, Object value) { |
|||
try { |
|||
HardwareServiceSetting.setOption(serviceName, key, value); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException(e); |
|||
} |
|||
} |
|||
} |
@ -1,104 +0,0 @@ |
|||
package a8k.service.db; |
|||
|
|||
import a8k.hardware.type.a8kcanprotocol.A8kEcode; |
|||
import a8k.type.exception.AppException; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import com.iflytop.uf.UfActiveRecord; |
|||
import com.iflytop.uf.UfActiveRecordField; |
|||
import com.iflytop.uf.util.UfJsonHelper; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public class HardwareServiceSetting extends UfActiveRecord { |
|||
@UfActiveRecordField |
|||
public String key; |
|||
@UfActiveRecordField |
|||
public String serviceName; //属于哪个服务的, hardware目录下的类名 |
|||
@UfActiveRecordField |
|||
public String valType; |
|||
@UfActiveRecordField |
|||
public String val; |
|||
|
|||
public static String getTableName() { |
|||
return "zapp_hardware_services_setting"; |
|||
} |
|||
|
|||
public static void deleteAllByServiceName(String serviceName) { |
|||
var items = UfActiveRecord.find(HardwareServiceSetting.class, Map.of("ServiceName", serviceName)); |
|||
for (var item : items) { |
|||
item.delete(); |
|||
} |
|||
} |
|||
|
|||
public void setValue(Object value) throws Exception { |
|||
if (value instanceof String) { |
|||
this.val = (String) value; |
|||
} else if (value instanceof Integer) { |
|||
this.val = String.valueOf((Integer) value); |
|||
} else if (value instanceof Map) { |
|||
this.val = UfJsonHelper.objectToJson(value); |
|||
} else { |
|||
this.val = UfJsonHelper.objectToJson(value); |
|||
} |
|||
} |
|||
|
|||
public static HardwareServiceSetting getOption(String service, String key) { |
|||
var option = UfActiveRecord.findOne(HardwareServiceSetting.class, Map.of("serviceName", service, "key", key)); |
|||
if (null == option) { |
|||
option = new HardwareServiceSetting(); |
|||
option.serviceName = service; |
|||
option.key = key; |
|||
option.val = null; |
|||
} |
|||
return option; |
|||
} |
|||
|
|||
|
|||
public static void setOption(String service, String key, Object value) throws Exception { |
|||
var options = getOption(service, key); |
|||
options.setValue(value); |
|||
options.save(); |
|||
} |
|||
|
|||
public static <T> T getObject(String service, String key, Class<T> clazs) throws AppException { |
|||
var option = UfActiveRecord.findOne(HardwareServiceSetting.class, Map.of("serviceName", service, "key", key)); |
|||
if (option == null) { |
|||
throw new AppException(A8kEcode.CODEERROR_GET_PARAM_FAIL); |
|||
} |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
try { |
|||
return mapper.readValue(option.val, clazs); |
|||
} catch (JsonProcessingException e) { |
|||
throw new AppException(A8kEcode.CODEERROR_PARSE_PARAM_FAIL); |
|||
} |
|||
} |
|||
|
|||
|
|||
public static <T> T getObject(String service, String key, Class<T> clazs, T defaultVal) { |
|||
var option = UfActiveRecord.findOne(HardwareServiceSetting.class, Map.of("serviceName", service, "key", key)); |
|||
if (option == null) { |
|||
return defaultVal; |
|||
} |
|||
ObjectMapper mapper = new ObjectMapper(); |
|||
try { |
|||
return mapper.readValue(option.val, clazs); |
|||
} catch (JsonProcessingException e) { |
|||
return defaultVal; |
|||
} |
|||
} |
|||
|
|||
public static Integer getInteger(String service, String key, Integer defaultValue) { |
|||
var option = HardwareServiceSetting.getOption(service, key); |
|||
if (null == option.val) { |
|||
return defaultValue; |
|||
} |
|||
return Integer.parseInt(option.val); |
|||
} |
|||
|
|||
public static void setInteger(String service, String key, Integer value) { |
|||
var option = HardwareServiceSetting.getOption(service, key); |
|||
option.val = String.valueOf(value); |
|||
option.save(); |
|||
} |
|||
} |
@ -1,10 +0,0 @@ |
|||
package a8k.utils; |
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Target({ElementType.TYPE}) |
|||
public @interface AppService { |
|||
String name(); |
|||
} |
@ -1,9 +0,0 @@ |
|||
package a8k.utils; |
|||
import java.lang.annotation.ElementType; |
|||
import java.lang.annotation.Retention; |
|||
import java.lang.annotation.RetentionPolicy; |
|||
import java.lang.annotation.Target; |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Target({ElementType.METHOD}) |
|||
public @interface AppServiceAction { |
|||
} |
@ -1,69 +0,0 @@ |
|||
package a8k.utils; |
|||
import com.iflytop.uf.UfApplication; |
|||
import com.iflytop.uf.util.UfClassHelper; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.stereotype.Component; |
|||
import java.lang.reflect.Method; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
@Component |
|||
public class AppServiceManager { |
|||
public static final Logger LOG = LoggerFactory.getLogger(AppServiceManager.class); |
|||
|
|||
// actions |
|||
public Map<String, Method> actions; |
|||
// services |
|||
public Map<Method, Class<?>> actionServices; |
|||
|
|||
// load actions |
|||
public void loadActions() throws Exception { |
|||
this.actions = new HashMap<String, Method>(); |
|||
this.actionServices = new HashMap<Method, Class<?>>(); |
|||
var classList = UfClassHelper.getAllClassesInPackage("a8k"); |
|||
for (var clazz : classList) { |
|||
var serviceAnnotation = clazz.getAnnotation(AppService.class); |
|||
if (serviceAnnotation == null) { |
|||
continue ; |
|||
} |
|||
var methods = clazz.getMethods(); |
|||
for (var method : methods) { |
|||
if (!method.isAnnotationPresent(AppServiceAction.class)) { |
|||
continue ; |
|||
} |
|||
String actionKey = String.format("%s.%s", serviceAnnotation.name(), method.getName()); |
|||
if (this.actions.containsKey(actionKey)) { |
|||
throw new Exception(String.format("重复action '%s'", actionKey)); |
|||
} |
|||
this.actions.put(actionKey, method); |
|||
this.actionServices.put(method, clazz); |
|||
LOG.info("load app action : {}", actionKey); |
|||
} |
|||
} |
|||
} |
|||
|
|||
// execute action |
|||
public Object executeAction(String action, Map<String, Object> params) throws Exception { |
|||
var method = actions.get(action); |
|||
if (method == null) { |
|||
throw new Exception("不存在"); |
|||
} |
|||
|
|||
var actionParamDefs = method.getParameters(); |
|||
Object[] args = new Object[actionParamDefs.length]; |
|||
for ( var i=0; i<actionParamDefs.length; i++ ) { |
|||
var name = actionParamDefs[i].getName(); |
|||
var value = params.get(name); |
|||
args[i] = value; |
|||
} |
|||
|
|||
var serviceClass = this.actionServices.get(method); |
|||
var service = UfApplication.getContext().getBean(serviceClass); |
|||
if ( method.getReturnType().equals(Void.TYPE)) { |
|||
method.invoke(service, args); |
|||
return null; |
|||
} else { |
|||
return method.invoke(service, args); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue