Search in sources :

Example 6 with DataType

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());
}
Also used : VariableDS(ucar.nc2.dataset.VariableDS) DataType(ucar.ma2.DataType)

Example 7 with DataType

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;
}
Also used : DataType(ucar.ma2.DataType)

Example 8 with DataType

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<>());
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) ArrayList(java.util.ArrayList) DataType(com.serotonin.m2m2.DataType) MangoJavaScriptService(com.infiniteautomation.mango.spring.service.MangoJavaScriptService)

Example 9 with DataType

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;
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) DataType(com.serotonin.m2m2.DataType)

Example 10 with 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));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) JsonImportExclusion(com.serotonin.m2m2.rt.script.JsonImportExclusion) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) IntStringPair(com.serotonin.db.pair.IntStringPair) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Aggregations

Array (ucar.ma2.Array)9 DataType (ucar.ma2.DataType)8 DataType (com.serotonin.m2m2.DataType)7 ArrayList (java.util.ArrayList)7 Index (ucar.ma2.Index)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Variable (ucar.nc2.Variable)6 ArrayFloat (ucar.ma2.ArrayFloat)5 Attribute (ucar.nc2.Attribute)5 Dimension (ucar.nc2.Dimension)5 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)4 File (java.io.File)4 NetcdfFile (ucar.nc2.NetcdfFile)4 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)4 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 CorruptDataException (com.ibm.j9ddr.CorruptDataException)2 GeneratedFieldAccessor (com.ibm.j9ddr.GeneratedFieldAccessor)2 GeneratedPointerClass (com.ibm.j9ddr.GeneratedPointerClass)2 DataType (com.ibm.j9ddr.vm29.j9.DataType)2