use of ucar.ma2.DataType in project imageio-ext by geosolutions-it.
the class NetCDFUtilities method getRawDataType.
/**
* Returns the data type which most closely represents the "raw" internal
* data of the variable. This is the value returned by the default
* implementation of {@link NetcdfImageReader#getRawDataType}.
*
* @param variable
* The variable.
* @return The data type, or {@link DataBuffer#TYPE_UNDEFINED} if unknown.
*
* @see NetcdfImageReader#getRawDataType
*/
public static int getRawDataType(final VariableIF variable) {
VariableDS ds = (VariableDS) variable;
final DataType type = ds.getOriginalDataType();
return transcodeNetCDFDataType(type, variable.isUnsigned());
}
use of ucar.ma2.DataType in project imageio-ext by geosolutions-it.
the class NetCDFUtilities method getAttributesAsString.
/**
* Return the value of a NetCDF {@code Attribute} instance as a
* {@code String}. The {@code isUnsigned} parameter allow to handle byte
* attributes as unsigned, in order to represent values in the range
* [0,255].
*/
public static String getAttributesAsString(Attribute attr, final boolean isUnsigned) {
String[] values = null;
if (attr != null) {
final int nValues = attr.getLength();
values = new String[nValues];
final DataType datatype = attr.getDataType();
// TODO: Improve the unsigned management
if (datatype == DataType.BYTE) {
if (isUnsigned)
for (int i = 0; i < nValues; i++) {
byte val = attr.getNumericValue(i).byteValue();
int myByte = (0x000000FF & ((int) val));
short anUnsignedByte = (short) myByte;
values[i] = Short.toString(anUnsignedByte);
}
else {
for (int i = 0; i < nValues; i++) {
byte val = attr.getNumericValue(i).byteValue();
values[i] = Byte.toString(val);
}
}
} else if (datatype == DataType.SHORT) {
for (int i = 0; i < nValues; i++) {
short val = attr.getNumericValue(i).shortValue();
values[i] = Short.toString(val);
}
} else if (datatype == DataType.INT) {
for (int i = 0; i < nValues; i++) {
int val = attr.getNumericValue(i).intValue();
values[i] = Integer.toString(val);
}
} else if (datatype == DataType.LONG) {
for (int i = 0; i < nValues; i++) {
long val = attr.getNumericValue(i).longValue();
values[i] = Long.toString(val);
}
} else if (datatype == DataType.DOUBLE) {
for (int i = 0; i < nValues; i++) {
double val = attr.getNumericValue(i).doubleValue();
values[i] = Double.toString(val);
}
} else if (datatype == DataType.FLOAT) {
for (int i = 0; i < nValues; i++) {
float val = attr.getNumericValue(i).floatValue();
values[i] = Float.toString(val);
}
} else if (datatype == DataType.STRING) {
for (int i = 0; i < nValues; i++) {
values[i] = attr.getStringValue(i);
}
} else {
if (LOGGER.isLoggable(Level.WARNING))
LOGGER.warning("Unhandled Attribute datatype " + attr.getDataType().getClassType().toString());
}
}
String value = "";
if (values != null) {
StringBuffer sb = new StringBuffer();
int j = 0;
for (; j < values.length - 1; j++) {
sb.append(values[j]).append(",");
}
sb.append(values[j]);
value = sb.toString();
}
return value;
}
use of ucar.ma2.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 ucar.ma2.DataType in project ma-core-public by infiniteautomation.
the class AbstractPointLocatorVO method readDataType.
protected DataType readDataType(JsonObject json) throws JsonException {
String text = json.getString("dataType");
if (text == null) {
throw new TranslatableJsonException("emport.error.missing", "dataType", DataType.formatNames());
}
DataType dataType;
try {
dataType = DataType.valueOf(text);
} catch (IllegalArgumentException e) {
throw new TranslatableJsonException("emport.error.invalid", "dataType", text, DataType.formatNames());
}
return dataType;
}
use of ucar.ma2.DataType in project ma-core-public by infiniteautomation.
the class SetPointHandlerRT method eventInactive.
@Override
public void eventInactive(EventInstance evt) {
if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
return;
// Validate that the target point is available.
DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
if (targetPoint == null) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
return;
}
if (!targetPoint.getPointLocator().isSettable()) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
return;
}
DataType targetDataType = targetPoint.getVO().getPointLocator().getDataType();
DataValue value = null;
if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
// Get the source data point.
DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getInactivePointId());
if (sourcePoint == null) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointMissing"), evt.getEventType());
return;
}
PointValueTime valueTime = sourcePoint.getPointValue();
if (valueTime == null) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointValue"), evt.getEventType());
return;
}
if (valueTime.getValue().getDataType() != targetDataType) {
raiseFailureEvent(new TranslatableMessage("event.setPoint.inactivePointDataType"), evt.getEventType());
return;
}
value = valueTime.getValue();
} else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE)
value = DataValue.stringToValue(vo.getInactiveValueToSet(), targetDataType);
else if (vo.getInactiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
ArrayList<JsonImportExclusion> importExclusions = new ArrayList<JsonImportExclusion>();
importExclusions.add(new JsonImportExclusion("xid", vo.getXid()) {
@Override
public String getImporterType() {
return ConfigurationExportData.EVENT_HANDLERS;
}
});
Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
context.put("target", targetPoint);
Map<String, Object> additionalContext = new HashMap<String, Object>();
additionalContext.put(EventInstance.CONTEXT_KEY, evt);
additionalContext.put(EventInstanceWrapper.CONTEXT_KEY, new EventInstanceWrapper(evt));
try (ScriptLog scriptLog = new ScriptLog("setPointHandler-" + evt.getId())) {
for (IntStringPair cxt : vo.getAdditionalContext()) {
DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
if (dprt != null)
context.put(cxt.getValue(), dprt);
}
CompiledMangoJavaScript inactiveScript = new CompiledMangoJavaScript(new SetCallback(vo.getScriptRoles()), scriptLog, additionalContext, null, importExclusions, false, service, vo.getScriptRoles());
inactiveScript.compile(vo.getInactiveScript(), true);
inactiveScript.initialize(context);
MangoJavaScriptResult result = inactiveScript.execute(Common.timer.currentTimeMillis(), evt.getRtnTimestamp(), targetPoint.getDataType());
PointValueTime pvt = (PointValueTime) result.getResult();
if (pvt != null)
value = pvt.getValue();
} catch (ScriptPermissionsException e) {
raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
return;
} catch (ScriptError e) {
raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScriptError", e.getTranslatableMessage()), evt.getEventType());
return;
} catch (ResultTypeException e) {
raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidInactiveScriptError", e.getMessage()), evt.getEventType());
return;
}
} else
throw new ShouldNeverHappenException("Unknown active action: " + vo.getInactiveAction());
if (MangoJavaScriptService.UNCHANGED != value)
Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getRtnTimestamp()), this));
}
Aggregations