Search in sources :

Example 11 with DataType

use of ucar.ma2.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;
}
Also used : GeneratedPointerClass(com.ibm.j9ddr.GeneratedPointerClass) Method(java.lang.reflect.Method) CorruptDataException(com.ibm.j9ddr.CorruptDataException) GeneratedFieldAccessor(com.ibm.j9ddr.GeneratedFieldAccessor) LinkedList(java.util.LinkedList) CorruptDataException(com.ibm.j9ddr.CorruptDataException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) DataType(com.ibm.j9ddr.vm29.j9.DataType)

Example 12 with DataType

use of ucar.ma2.DataType in project openj9 by eclipse.

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;
}
Also used : GeneratedPointerClass(com.ibm.j9ddr.GeneratedPointerClass) Method(java.lang.reflect.Method) CorruptDataException(com.ibm.j9ddr.CorruptDataException) GeneratedFieldAccessor(com.ibm.j9ddr.GeneratedFieldAccessor) LinkedList(java.util.LinkedList) CorruptDataException(com.ibm.j9ddr.CorruptDataException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) DataType(com.ibm.j9ddr.vm29.j9.DataType)

Example 13 with DataType

use of ucar.ma2.DataType in project ma-core-public by infiniteautomation.

the class PointValueTimeDeserializer method deserialize.

@Override
public PointValueTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    ObjectCodec codec = jsonParser.getCodec();
    JsonNode node = codec.readTree(jsonParser);
    JsonNode dataTypeNode = node.get("dataType");
    if (dataTypeNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing dataType");
    }
    JsonNode valueNode = node.get("value");
    if (valueNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing value");
    }
    JsonNode timestampNode = node.get("timestamp");
    if (timestampNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing timestamp");
    }
    long timestamp = timestampNode.asLong();
    String dataTypeStr = dataTypeNode.asText();
    DataType dataType = DataType.fromName(dataTypeStr);
    if (dataType == null) {
        throw JsonMappingException.from(jsonParser, "Unknown dataType: " + dataTypeStr);
    }
    DataValue dataValue;
    switch(dataType) {
        case ALPHANUMERIC:
            dataValue = new AlphanumericValue(valueNode.asText());
            break;
        case BINARY:
            dataValue = new BinaryValue(valueNode.asBoolean());
            break;
        case MULTISTATE:
            dataValue = new MultistateValue(valueNode.asInt());
            break;
        case NUMERIC:
            dataValue = new NumericValue(valueNode.asDouble());
            break;
        default:
            throw JsonMappingException.from(jsonParser, "Unsupported dataType " + dataType);
    }
    JsonNode annotationNode = node.get("serializedAnnotation");
    if (annotationNode != null && !annotationNode.isNull()) {
        try {
            return new AnnotatedPointValueTime(dataValue, timestamp, TranslatableMessage.deserialize(annotationNode.asText()));
        } catch (TranslatableMessageParseException e) {
            throw JsonMappingException.from(jsonParser, "Can't deserialize annotation", e);
        }
    }
    return new PointValueTime(dataValue, timestamp);
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) JsonNode(com.fasterxml.jackson.databind.JsonNode) TranslatableMessageParseException(com.serotonin.m2m2.i18n.TranslatableMessageParseException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 14 with DataType

use of ucar.ma2.DataType in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventRaised.

@Override
public void eventRaised(EventInstance evt) {
    if (vo.getActiveAction() == 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.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
            return;
        }
        if (valueTime.getValue().getDataType() != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
        value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
    } else if (vo.getActiveAction() == 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(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, 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 activeScript = new CompiledMangoJavaScript(new SetCallback(vo.getScriptRoles()), scriptLog, additionalContext, null, importExclusions, false, service, vo.getScriptRoles());
            activeScript.compile(vo.getActiveScript(), true);
            activeScript.initialize(context);
            MangoJavaScriptResult result = activeScript.execute(Common.timer.currentTimeMillis(), evt.getActiveTimestamp(), 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.invalidActiveScriptError", e.getTranslatableMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
    // Queue a work item to perform the set point.
    if (MangoJavaScriptService.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), 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)

Example 15 with DataType

use of ucar.ma2.DataType in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method savePointValueImpl.

private long savePointValueImpl(final DataPointVO vo, final PointValueTime pointValue, boolean async) {
    DataValue value = pointValue.getValue();
    final DataType dataType = value.getDataType();
    double dvalue = 0;
    String svalue = null;
    if (value.hasDoubleRepresentation())
        dvalue = value.getDoubleValue();
    else
        svalue = value.getStringValue();
    // Check if we need to create an annotation.
    long id;
    try {
        id = savePointValue(vo, dataType, dvalue, pointValue.getTime(), svalue, getAnnotation(pointValue), async);
    } catch (ConcurrencyFailureException e) {
        // Still failed to insert after all of the retries. Store the data
        unsavedPointValues.add(new UnsavedPointValue(vo, pointValue));
        return -1;
    }
    writeUnsavedPointValues();
    return id;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) DataType(com.serotonin.m2m2.DataType)

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