use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method execute.
/**
* Reset the result and execute for PointValueTime result
*/
public void execute(CompiledMangoJavaScript script, long runtime, long timestamp, DataType resultDataType) throws ScriptError, ResultTypeException, ScriptPermissionsException {
try {
runAs.runAsCallable(script.getPermissionHolder(), () -> {
execute(script, runtime, timestamp);
Object ts = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE).get(MangoJavaScriptService.TIMESTAMP_CONTEXT_KEY);
long scriptRuntime;
if (ts != null) {
// Check the type of the object.
if (ts instanceof Number) {
// Convert to long
scriptRuntime = ((Number) ts).longValue();
} else {
scriptRuntime = timestamp;
}
} else {
scriptRuntime = timestamp;
}
Object resultObject = script.getResult().getResult();
DataValue value = coerce(resultObject, resultDataType);
script.getResult().setResult(new PointValueTime(value, scriptRuntime));
return null;
});
} catch (ScriptException e) {
throw ScriptError.create(e, script.isWrapInFunction());
} catch (RuntimeException e) {
// Nashorn seems to like to wrap exceptions in RuntimeException
if (e.getCause() instanceof ScriptPermissionsException)
throw (ScriptPermissionsException) e.getCause();
else
throw new ShouldNeverHappenException(e);
} catch (Exception e) {
if (e instanceof ResultTypeException) {
throw (ResultTypeException) e;
}
throw new ShouldNeverHappenException(e);
}
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method coerce.
/**
* Coerce an object into a DataValue
*/
public DataValue coerce(Object input, DataType toDataType) throws ResultTypeException {
DataValue value;
if (input instanceof DataValue)
return (DataValue) input;
else if (input instanceof PointValueTime)
return ((PointValueTime) input).getValue();
if (input == null) {
if (toDataType == DataType.BINARY)
value = new BinaryValue(false);
else if (toDataType == DataType.MULTISTATE)
value = new MultistateValue(0);
else if (toDataType == DataType.NUMERIC)
value = new NumericValue(0);
else if (toDataType == DataType.ALPHANUMERIC)
value = new AlphanumericValue("");
else
value = null;
} else if (input instanceof AbstractPointWrapper) {
value = ((AbstractPointWrapper) input).getValueImpl();
if ((value != null) && (value.getDataType() != toDataType))
throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
} else // See if the type matches.
if (toDataType == DataType.BINARY && input instanceof Boolean)
value = new BinaryValue((Boolean) input);
else if (toDataType == DataType.MULTISTATE) {
if (input instanceof Number)
value = new MultistateValue(((Number) input).intValue());
else if (input instanceof String) {
try {
value = new MultistateValue(Integer.parseInt((String) input));
} catch (NumberFormatException e) {
throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
}
} else
throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
} else if (toDataType == DataType.NUMERIC) {
if (input instanceof Number)
value = new NumericValue(((Number) input).doubleValue());
else if (input instanceof String) {
try {
value = new NumericValue(Double.parseDouble((String) input));
} catch (NumberFormatException e) {
throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
}
} else
throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
} else if (toDataType == DataType.ALPHANUMERIC)
value = new AlphanumericValue(input.toString());
else
// If not, ditch it.
throw new ResultTypeException(new TranslatableMessage("event.script.convertError", input, toDataType.getDescription()));
return value;
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class SetPointEventHandlerDefinition method commonValidation.
private void commonValidation(ProcessResult response, SetPointEventHandlerVO vo) {
DataPointVO dp = DataPointDao.getInstance().get(vo.getTargetPointId());
DataType dataType = null;
if (dp == null)
response.addContextualMessage("targetPointId", "eventHandlers.noTargetPoint");
else {
dataType = dp.getPointLocator().getDataType();
if (!dp.getPointLocator().isSettable())
response.addContextualMessage("targetPointId", "event.setPoint.targetNotSettable");
}
if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE && vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE) {
response.addContextualMessage("activeAction", "eventHandlers.noSetPointAction");
response.addContextualMessage("inactiveAction", "eventHandlers.noSetPointAction");
}
MangoJavaScriptService javaScriptService = Common.getBean(MangoJavaScriptService.class);
// Active
if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.MULTISTATE) {
try {
Integer.parseInt(vo.getActiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("activeValueToSet", "eventHandlers.invalidActiveValue");
}
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.NUMERIC) {
try {
Double.parseDouble(vo.getActiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("activeValueToSet", "eventHandlers.invalidActiveValue");
}
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
DataPointVO dpActive = DataPointDao.getInstance().get(vo.getActivePointId());
if (dpActive == null)
response.addContextualMessage("activePointId", "eventHandlers.invalidActiveSource");
else if (dataType != dpActive.getPointLocator().getDataType())
response.addContextualMessage("activeDataPointId", "eventHandlers.invalidActiveSourceType");
} else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
if (StringUtils.isEmpty(vo.getActiveScript())) {
response.addContextualMessage("activeScript", "eventHandlers.invalidActiveScript");
} else {
try {
javaScriptService.compile(vo.getActiveScript(), true);
} catch (ScriptError e) {
response.addContextualMessage("activeScript", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
}
}
}
// Inactive
if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.MULTISTATE) {
try {
Integer.parseInt(vo.getInactiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("inactiveValueToSet", "eventHandlers.invalidInactiveValue");
}
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE && dataType == DataType.NUMERIC) {
try {
Double.parseDouble(vo.getInactiveValueToSet());
} catch (NumberFormatException e) {
response.addContextualMessage("inactiveValueToSet", "eventHandlers.invalidInactiveValue");
}
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
DataPointVO dpInactive = DataPointDao.getInstance().get(vo.getInactivePointId());
if (dpInactive == null)
response.addContextualMessage("inactivePointId", "eventHandlers.invalidInactiveSource");
else if (dataType != dpInactive.getPointLocator().getDataType())
response.addContextualMessage("inactivePointId", "eventHandlers.invalidInactiveSourceType");
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
if (StringUtils.isEmpty(vo.getInactiveScript())) {
response.addContextualMessage("inactiveScript", "eventHandlers.invalidInactiveScript");
} else {
try {
javaScriptService.compile(vo.getInactiveScript(), true);
} catch (ScriptError e) {
response.addContextualMessage("inactiveScript", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
}
}
}
if (vo.getAdditionalContext() != null)
validateScriptContext(vo.getAdditionalContext(), response);
else
vo.setAdditionalContext(new ArrayList<>());
}
use of com.serotonin.m2m2.DataType in project openj9 by eclipse-openj9.
the class StructurePointer method getStructureFields.
public StructureField[] getStructureFields() {
List<StructureField> fields = new LinkedList<StructureField>();
Class<?> working = this.getClass();
while (working != null) {
GeneratedPointerClass classAnnotation = working.getAnnotation(GeneratedPointerClass.class);
if (null == classAnnotation) {
break;
}
for (Method thisMethod : working.getMethods()) {
if (thisMethod.isAnnotationPresent(GeneratedFieldAccessor.class)) {
GeneratedFieldAccessor fieldAnnotation = thisMethod.getAnnotation(GeneratedFieldAccessor.class);
Field offsetField = null;
try {
offsetField = classAnnotation.structureClass().getField(fieldAnnotation.offsetFieldName());
} catch (SecurityException e) {
throw new Error("Unexpected security exception", e);
} catch (NoSuchFieldException e) {
// This will happen if we reach for a field that doesn't exist on this level
continue;
}
int offset = -1;
try {
offset = offsetField.getInt(null);
} catch (Exception e) {
throw new Error(e);
}
DataType result = null;
CorruptDataException cde = null;
try {
result = (DataType) thisMethod.invoke(this);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof CorruptDataException) {
cde = (CorruptDataException) cause;
} else {
throw new RuntimeException(e);
}
}
fields.add(new StructureField(thisMethod.getName(), fieldAnnotation.declaredType(), offset, result, cde));
}
}
working = working.getSuperclass();
}
Collections.sort(fields);
StructureField[] result = new StructureField[fields.size()];
fields.toArray(result);
return result;
}
use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.
the class SetPointEventHandlerVO method validate.
public void validate(ProcessResult response) {
super.validate(response);
DataPointVO dp = DataPointDao.instance.getDataPoint(targetPointId, false);
if (dp == null)
response.addGenericMessage("eventHandlers.noTargetPoint");
else {
int dataType = dp.getPointLocator().getDataTypeId();
if (activeAction == SET_ACTION_NONE && inactiveAction == SET_ACTION_NONE)
response.addGenericMessage("eventHandlers.noSetPointAction");
// Active
if (activeAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.MULTISTATE) {
try {
Integer.parseInt(activeValueToSet);
} catch (NumberFormatException e) {
response.addGenericMessage("eventHandlers.invalidActiveValue");
}
} else if (activeAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.NUMERIC) {
try {
Double.parseDouble(activeValueToSet);
} catch (NumberFormatException e) {
response.addGenericMessage("eventHandlers.invalidActiveValue");
}
} else if (activeAction == SET_ACTION_POINT_VALUE) {
DataPointVO dpActive = DataPointDao.instance.getDataPoint(activePointId, false);
if (dpActive == null)
response.addGenericMessage("eventHandlers.invalidActiveSource");
else if (dataType != dpActive.getPointLocator().getDataTypeId())
response.addGenericMessage("eventHandlers.invalidActiveSourceType");
} else if (activeAction == SET_ACTION_SCRIPT_VALUE) {
if (StringUtils.isEmpty(activeScript))
response.addGenericMessage("eventHandlers.invalidActiveScript");
try {
CompiledScriptExecutor.compile(activeScript);
} catch (ScriptException e) {
response.addGenericMessage("eventHandlers.invalidActiveScriptError", e.getMessage() == null ? e.getCause().getMessage() : e.getMessage());
}
}
// Inactive
if (inactiveAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.MULTISTATE) {
try {
Integer.parseInt(inactiveValueToSet);
} catch (NumberFormatException e) {
response.addGenericMessage("eventHandlers.invalidInactiveValue");
}
} else if (inactiveAction == SET_ACTION_STATIC_VALUE && dataType == DataTypes.NUMERIC) {
try {
Double.parseDouble(inactiveValueToSet);
} catch (NumberFormatException e) {
response.addGenericMessage("eventHandlers.invalidInactiveValue");
}
} else if (inactiveAction == SET_ACTION_POINT_VALUE) {
DataPointVO dpInactive = DataPointDao.instance.getDataPoint(inactivePointId, false);
if (dpInactive == null)
response.addGenericMessage("eventHandlers.invalidInactiveSource");
else if (dataType != dpInactive.getPointLocator().getDataTypeId())
response.addGenericMessage("eventHandlers.invalidInactiveSourceType");
} else if (inactiveAction == SET_ACTION_SCRIPT_VALUE) {
if (StringUtils.isEmpty(inactiveScript))
response.addGenericMessage("eventHandlers.invalidInactiveScript");
try {
CompiledScriptExecutor.compile(inactiveScript);
} catch (ScriptException e) {
response.addGenericMessage("eventHandlers.invalidInactiveScriptError", e.getMessage() == null ? e.getCause().getMessage() : e.getMessage());
}
}
List<String> varNameSpace = new ArrayList<String>();
varNameSpace.add(TARGET_CONTEXT_KEY);
for (IntStringPair cxt : additionalContext) {
if (DataPointDao.instance.getDataPoint(cxt.getKey(), false) == null)
response.addGenericMessage("event.script.contextPointMissing", cxt.getKey(), cxt.getValue());
String varName = cxt.getValue();
if (StringUtils.isBlank(varName)) {
response.addGenericMessage("validate.allVarNames");
break;
}
if (!VarNames.validateVarName(varName)) {
response.addGenericMessage("validate.invalidVarName", varName);
break;
}
if (varNameSpace.contains(varName)) {
response.addGenericMessage("validate.duplicateVarName", varName);
break;
}
varNameSpace.add(varName);
}
}
}
Aggregations