Search in sources :

Example 31 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method updatePointValueImpl.

long updatePointValueImpl(final int pointId, final PointValueTime pvt, final SetPointSource source, boolean async) {
    DataValue value = pvt.getValue();
    final int dataType = DataTypes.getDataType(value);
    double dvalue = 0;
    String svalue = null;
    if (dataType == DataTypes.IMAGE) {
        ImageValue imageValue = (ImageValue) value;
        dvalue = imageValue.getType();
        if (imageValue.isSaved())
            svalue = Long.toString(imageValue.getId());
    } else if (value.hasDoubleRepresentation())
        dvalue = value.getDoubleValue();
    else
        svalue = value.getStringValue();
    // Check if we need to create an annotation.
    long id;
    try {
        if (svalue != null || source != null || dataType == DataTypes.IMAGE)
            async = false;
        id = updatePointValue(pointId, dataType, dvalue, pvt.getTime(), svalue, source, async);
    } catch (ConcurrencyFailureException e) {
        // Still failed to insert after all of the retries. Store the data
        synchronized (UNSAVED_POINT_UPDATES) {
            UNSAVED_POINT_UPDATES.add(new UnsavedPointUpdate(pointId, pvt, source));
        }
        return -1;
    }
    // Check if we need to save an image
    if (dataType == DataTypes.IMAGE) {
        ImageValue imageValue = (ImageValue) value;
        if (!imageValue.isSaved()) {
            imageValue.setId(id);
            File file = new File(Common.getFiledataPath(), imageValue.getFilename());
            // Write the file.
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);
                StreamUtils.transfer(new ByteArrayInputStream(imageValue.getData()), out);
            } catch (IOException e) {
                // Rethrow as an RTE
                throw new ImageSaveException(e);
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (IOException e) {
                // no op
                }
            }
            // Allow the data to be GC'ed
            imageValue.setData(null);
        }
    }
    clearUnsavedPointUpdates();
    return id;
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) FileOutputStream(java.io.FileOutputStream) ImageSaveException(com.serotonin.m2m2.ImageSaveException) ImageValue(com.serotonin.m2m2.rt.dataImage.types.ImageValue) File(java.io.File)

Example 32 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class PointValueDaoSQL method updatePointValueAnnotation.

private void updatePointValueAnnotation(long id, int dataType, String svalue, SetPointSource source) {
    if (svalue == null && dataType == DataTypes.IMAGE)
        svalue = Long.toString(id);
    // Check if we need to create an annotation.
    TranslatableMessage sourceMessage = null;
    if (source != null)
        sourceMessage = source.getSetPointSourceMessage();
    if (svalue != null || sourceMessage != null) {
        String shortString = null;
        String longString = null;
        if (svalue != null) {
            if (svalue.length() > 128)
                longString = svalue;
            else
                shortString = svalue;
        }
        // 
        ejt.update(// 
        POINT_VALUE_ANNOTATION_UPDATE + "WHERE pointValueId = ?", // 
        new Object[] { shortString, longString, writeTranslatableMessage(sourceMessage), id }, new int[] { Types.VARCHAR, Types.CLOB, Types.CLOB, Types.INTEGER });
    }
}
Also used : TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage)

Example 33 with DataType

use of com.serotonin.m2m2.DataType in project ma-core-public by infiniteautomation.

the class DeltamationCommon method validatePoint.

public static DataPointVO validatePoint(int pointId, String name, ProcessResult response, Integer dataType, boolean requireSettable) {
    DataPointDao points = DataPointDao.instance;
    DataPointVO point = points.getDataPoint(pointId);
    if (point == null) {
        response.addContextualMessage(name, "validate.noPoint");
        return null;
    }
    if (requireSettable && !point.getPointLocator().isSettable()) {
        response.addContextualMessage(name, "validate.pointNotSettable", point.getName());
    }
    if (dataType != null && point.getPointLocator().getDataTypeId() != dataType) {
        response.addContextualMessage(name, "validate.pointWrongType", point.getName());
    }
    return point;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao)

Example 34 with DataType

use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.

the class PointValueImportResult method saveValue.

public void saveValue(LegacyXidPointValueTimeModel model) {
    if (valid) {
        // Validate the model against our point
        long timestamp = model.getTimestamp();
        if (timestamp == 0)
            timestamp = Common.timer.currentTimeMillis();
        try {
            DataValue value;
            switch(vo.getPointLocator().getDataType()) {
                case ALPHANUMERIC:
                    value = new AlphanumericValue((String) model.getValue());
                    break;
                case BINARY:
                    value = new BinaryValue((Boolean) model.getValue());
                    break;
                case MULTISTATE:
                    if (model.getValue() instanceof String) {
                        try {
                            value = vo.getTextRenderer().parseText((String) model.getValue(), vo.getPointLocator().getDataType());
                        } catch (Exception e) {
                            // Lots can go wrong here so let the user know
                            result.addContextualMessage("value", "event.valueParse.textParse", e.getMessage());
                            return;
                        }
                    } else {
                        value = new MultistateValue(((Number) model.getValue()).intValue());
                    }
                    break;
                case NUMERIC:
                    value = new NumericValue(((Number) model.getValue()).doubleValue());
                    break;
                default:
                    result.addContextualMessage("dataType", "common.default", vo.getPointLocator().getDataType() + " data type not supported yet");
                    return;
            }
            PointValueTime pvt;
            if (model.getAnnotation() == null) {
                pvt = new PointValueTime(value, timestamp);
            } else {
                pvt = new AnnotatedPointValueTime(value, timestamp, new TranslatableMessage("common.default", model.getAnnotation()));
            }
            if (rt == null) {
                dao.savePointValueAsync(vo, pvt);
            } else {
                rt.savePointValueDirectToCache(pvt, null, true, true, fireEvents);
            }
            total++;
        } catch (Exception e) {
            if (e instanceof ClassCastException) {
                result.addContextualMessage("dataType", "event.ds.dataType");
            } else {
                result.addContextualMessage("value", "common.default", e.getMessage());
            }
        }
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) NotFoundException(com.infiniteautomation.mango.util.exception.NotFoundException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) 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) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 35 with DataType

use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.

the class VirtualPointLocatorModel method toVO.

@Override
public VirtualPointLocatorVO toVO() {
    VirtualPointLocatorVO vo = new VirtualPointLocatorVO();
    vo.setDataType(DataType.fromName(dataType));
    vo.setChangeTypeId(ChangeTypeVO.CHANGE_TYPE_CODES.getId(changeType));
    vo.setSettable(settable);
    switch(vo.getChangeTypeId()) {
        case ChangeTypeVO.Types.ALTERNATE_BOOLEAN:
            AlternateBooleanChangeVO abc = new AlternateBooleanChangeVO();
            abc.setStartValue(startValue);
            vo.setAlternateBooleanChange(abc);
            break;
        case ChangeTypeVO.Types.ANALOG_ATTRACTOR:
            AnalogAttractorChangeVO aac = new AnalogAttractorChangeVO();
            aac.setStartValue(startValue);
            aac.setMaxChange(maxChange);
            aac.setVolatility(volatility);
            Integer attractionPointId = DataPointDao.getInstance().getIdByXid(attractionPointXid);
            if (attractionPointId != null) {
                aac.setAttractionPointId(attractionPointId);
            }
            vo.setAnalogAttractorChange(aac);
            break;
        case ChangeTypeVO.Types.BROWNIAN:
            BrownianChangeVO bc = new BrownianChangeVO();
            bc.setStartValue(startValue);
            bc.setMin(min);
            bc.setMax(max);
            bc.setMaxChange(maxChange);
            vo.setBrownianChange(bc);
            break;
        case ChangeTypeVO.Types.INCREMENT_ANALOG:
            IncrementAnalogChangeVO iac = new IncrementAnalogChangeVO();
            iac.setStartValue(startValue);
            iac.setMin(min);
            iac.setMax(max);
            iac.setChange(change);
            iac.setRoll(roll);
            vo.setIncrementAnalogChange(iac);
            break;
        case ChangeTypeVO.Types.INCREMENT_MULTISTATE:
            IncrementMultistateChangeVO imc = new IncrementMultistateChangeVO();
            imc.setStartValue(startValue);
            imc.setValues(values);
            imc.setRoll(roll);
            vo.setIncrementMultistateChange(imc);
            break;
        case ChangeTypeVO.Types.NO_CHANGE:
            NoChangeVO nc = new NoChangeVO();
            nc.setStartValue(startValue);
            vo.setNoChange(nc);
            break;
        case ChangeTypeVO.Types.RANDOM_ANALOG:
            RandomAnalogChangeVO rac = new RandomAnalogChangeVO();
            rac.setStartValue(startValue);
            rac.setMin(min);
            rac.setMax(max);
            vo.setRandomAnalogChange(rac);
            break;
        case ChangeTypeVO.Types.RANDOM_BOOLEAN:
            RandomBooleanChangeVO rbc = new RandomBooleanChangeVO();
            rbc.setStartValue(startValue);
            vo.setRandomBooleanChange(rbc);
            break;
        case ChangeTypeVO.Types.RANDOM_MULTISTATE:
            RandomMultistateChangeVO rmc = new RandomMultistateChangeVO();
            rmc.setStartValue(startValue);
            rmc.setValues(values);
            vo.setRandomMultistateChange(rmc);
            break;
        case ChangeTypeVO.Types.SINUSOIDAL:
            SinusoidalChangeVO sc = new SinusoidalChangeVO();
            sc.setStartValue(startValue);
            sc.setAmplitude(amplitude);
            sc.setOffset(offset);
            sc.setPeriod(period);
            sc.setPhaseShift(phaseShift);
            vo.setSinusoidalChange(sc);
            break;
    }
    return vo;
}
Also used : AlternateBooleanChangeVO(com.serotonin.m2m2.virtual.vo.AlternateBooleanChangeVO) IncrementAnalogChangeVO(com.serotonin.m2m2.virtual.vo.IncrementAnalogChangeVO) RandomAnalogChangeVO(com.serotonin.m2m2.virtual.vo.RandomAnalogChangeVO) BrownianChangeVO(com.serotonin.m2m2.virtual.vo.BrownianChangeVO) AnalogAttractorChangeVO(com.serotonin.m2m2.virtual.vo.AnalogAttractorChangeVO) SinusoidalChangeVO(com.serotonin.m2m2.virtual.vo.SinusoidalChangeVO) RandomBooleanChangeVO(com.serotonin.m2m2.virtual.vo.RandomBooleanChangeVO) RandomMultistateChangeVO(com.serotonin.m2m2.virtual.vo.RandomMultistateChangeVO) VirtualPointLocatorVO(com.serotonin.m2m2.virtual.vo.VirtualPointLocatorVO) IncrementMultistateChangeVO(com.serotonin.m2m2.virtual.vo.IncrementMultistateChangeVO) NoChangeVO(com.serotonin.m2m2.virtual.vo.NoChangeVO)

Aggregations

ArrayList (java.util.ArrayList)26 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)23 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)17 DataType (com.serotonin.m2m2.DataType)14 HashMap (java.util.HashMap)14 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)13 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)11 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)10 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)10 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)10 DataType (org.osate.aadl2.DataType)10 DataType (ucar.ma2.DataType)10 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)9 IOException (java.io.IOException)9 DataImplementation (org.osate.aadl2.DataImplementation)9 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8 File (java.io.File)7 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)6 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)6