Search in sources :

Example 26 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class SetPointEventHandlerVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    DataPointDao dataPointDao = DataPointDao.instance;
    String xid = jsonObject.getString("targetPointId");
    if (xid != null) {
        Integer id = dataPointDao.getIdByXid(xid);
        if (id == null)
            throw new TranslatableJsonException("emport.error.missingPoint", xid);
        targetPointId = id;
    }
    // Active
    String text = jsonObject.getString("activeAction");
    if (text != null) {
        activeAction = SET_ACTION_CODES.getId(text);
        if (!SET_ACTION_CODES.isValidId(activeAction))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "activeAction", text, SET_ACTION_CODES.getCodeList());
    }
    if (activeAction == SET_ACTION_POINT_VALUE) {
        xid = jsonObject.getString("activePointId");
        if (xid != null) {
            Integer id = dataPointDao.getIdByXid(xid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            activePointId = id;
        }
    } else if (activeAction == SET_ACTION_STATIC_VALUE) {
        text = jsonObject.getString("activeValueToSet");
        if (text != null)
            activeValueToSet = text;
    } else if (activeAction == SET_ACTION_SCRIPT_VALUE) {
        text = jsonObject.getString("activeScript");
        if (text == null)
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveScript");
        activeValueToSet = text;
    }
    // Inactive
    text = jsonObject.getString("inactiveAction");
    if (text != null) {
        inactiveAction = SET_ACTION_CODES.getId(text);
        if (!SET_ACTION_CODES.isValidId(inactiveAction))
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveAction", text, SET_ACTION_CODES.getCodeList());
    }
    if (inactiveAction == SET_ACTION_POINT_VALUE) {
        xid = jsonObject.getString("inactivePointId");
        if (xid != null) {
            Integer id = dataPointDao.getIdByXid(xid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            inactivePointId = id;
        }
    } else if (inactiveAction == SET_ACTION_STATIC_VALUE) {
        text = jsonObject.getString("inactiveValueToSet");
        if (text != null)
            inactiveValueToSet = text;
    } else if (inactiveAction == SET_ACTION_SCRIPT_VALUE) {
        text = jsonObject.getString("inactiveScript");
        if (text == null)
            throw new TranslatableJsonException("emport.error.eventHandler.invalid", "inactiveScript");
        inactiveValueToSet = text;
    }
    JsonArray context = jsonObject.getJsonArray("additionalContext");
    if (context != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (JsonValue jv : context) {
            JsonObject jo = jv.toJsonObject();
            String dataPointXid = jo.getString("dataPointXid");
            if (dataPointXid == null)
                throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
            Integer id = DataPointDao.instance.getIdByXid(dataPointXid);
            if (id == null)
                throw new TranslatableJsonException("emport.error.missingPoint", dataPointXid);
            String contextKey = jo.getString("contextKey");
            if (contextKey == null)
                throw new TranslatableJsonException("emport.error.context.missing", "contextKey");
            additionalContext.add(new IntStringPair(id, contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    JsonObject permissions = jsonObject.getJsonObject("scriptPermissions");
    ScriptPermissions scriptPermissions = new ScriptPermissions();
    if (permissions != null) {
        String perm = permissions.getString(ScriptPermissions.DATA_SOURCE);
        if (perm != null)
            scriptPermissions.setDataSourcePermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_READ);
        if (perm != null)
            scriptPermissions.setDataPointReadPermissions(perm);
        perm = permissions.getString(ScriptPermissions.DATA_POINT_SET);
        if (perm != null)
            scriptPermissions.setDataPointSetPermissions(perm);
    }
    this.scriptPermissions = scriptPermissions;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) ScriptPermissions(com.serotonin.m2m2.rt.script.ScriptPermissions)

Example 27 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class SetPointEventHandlerVO method jsonWrite.

@Override
public void jsonWrite(ObjectWriter writer) throws IOException, JsonException {
    super.jsonWrite(writer);
    String dpXid = DataPointDao.instance.getXidById(targetPointId);
    writer.writeEntry("targetPointId", dpXid);
    // Active
    writer.writeEntry("activeAction", SET_ACTION_CODES.getCode(activeAction));
    if (activeAction == SET_ACTION_POINT_VALUE) {
        dpXid = DataPointDao.instance.getXidById(activePointId);
        writer.writeEntry("activePointId", dpXid);
    } else if (activeAction == SET_ACTION_STATIC_VALUE)
        writer.writeEntry("activeValueToSet", activeValueToSet);
    else if (activeAction == SET_ACTION_SCRIPT_VALUE)
        writer.writeEntry("activeScript", activeScript);
    // Inactive
    writer.writeEntry("inactiveAction", SET_ACTION_CODES.getCode(inactiveAction));
    if (inactiveAction == SET_ACTION_POINT_VALUE) {
        dpXid = DataPointDao.instance.getXidById(inactivePointId);
        writer.writeEntry("inactivePointId", dpXid);
    } else if (inactiveAction == SET_ACTION_STATIC_VALUE)
        writer.writeEntry("inactiveValueToSet", inactiveValueToSet);
    else if (inactiveAction == SET_ACTION_SCRIPT_VALUE)
        writer.writeEntry("inactiveScript", inactiveScript);
    JsonArray context = new JsonArray();
    for (IntStringPair pnt : additionalContext) {
        DataPointVO dpvo = DataPointDao.instance.getDataPoint(pnt.getKey(), false);
        if (dpvo != null) {
            JsonObject point = new JsonObject();
            point.put("dataPointXid", dpvo.getXid());
            point.put("contextKey", pnt.getValue());
            context.add(point);
        }
    }
    writer.writeEntry("additionalContext", context);
    if (scriptPermissions != null) {
        JsonObject permissions = new JsonObject();
        permissions.put(ScriptPermissions.DATA_SOURCE, scriptPermissions.getDataSourcePermissions());
        permissions.put(ScriptPermissions.DATA_POINT_READ, scriptPermissions.getDataPointReadPermissions());
        permissions.put(ScriptPermissions.DATA_POINT_SET, scriptPermissions.getDataPointSetPermissions());
        writer.writeEntry("scriptPermissions", permissions);
    } else {
        writer.writeEntry("scriptPermissions", null);
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonObject(com.serotonin.json.type.JsonObject)

Example 28 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class VarNames method jsonWriteVarContext.

public static void jsonWriteVarContext(ObjectWriter writer, List<IntStringPair> context) throws IOException, JsonException {
    DataPointDao dataPointDao = DataPointDao.instance;
    JsonArray pointList = new JsonArray();
    for (IntStringPair p : context) {
        JsonObject point = new JsonObject();
        pointList.add(point);
        point.put("varName", new JsonString(p.getValue()));
        point.put("dataPointXid", new JsonString(dataPointDao.getXidById(p.getKey())));
    }
    writer.writeEntry("context", pointList);
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString)

Example 29 with IntStringPair

use of com.serotonin.db.pair.IntStringPair in project ma-core-public by infiniteautomation.

the class VarNames method jsonReadVarContext.

public static void jsonReadVarContext(JsonObject json, List<IntStringPair> context) throws JsonException {
    JsonArray jsonContext = json.getJsonArray("context");
    if (jsonContext != null) {
        context.clear();
        DataPointDao dataPointDao = DataPointDao.instance;
        for (JsonValue jv : jsonContext) {
            JsonObject jo = jv.toJsonObject();
            String xid = jo.getString("dataPointXid");
            if (xid == null)
                throw new TranslatableJsonException("emport.error.meta.missing", "dataPointXid");
            Integer dpid = dataPointDao.getIdByXid(xid);
            if (dpid == null)
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            String var = jo.getString("varName");
            if (var == null)
                throw new TranslatableJsonException("emport.error.meta.missing", "varName");
            context.add(new IntStringPair(dpid, var));
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) IntStringPair(com.serotonin.db.pair.IntStringPair) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonString(com.serotonin.json.type.JsonString)

Example 30 with IntStringPair

use of com.serotonin.db.pair.IntStringPair 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;
    }
    int targetDataType = targetPoint.getVO().getPointLocator().getDataTypeId();
    DataValue value;
    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 (DataTypes.getDataType(valueTime.getValue()) != 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) {
        if (activeScript == null) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScript"), evt.getEventType());
            return;
        }
        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(SetPointEventHandlerVO.EVENT_CONTEXT_KEY, new EventInstanceWrapper(evt));
        try {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            PointValueTime pvt = CompiledScriptExecutor.execute(activeScript, context, additionalContext, evt.getActiveTimestamp(), targetPoint.getDataTypeId(), evt.getActiveTimestamp(), vo.getScriptPermissions(), NULL_WRITER, new ScriptLog(NULL_WRITER, LogLevel.FATAL), setCallback, importExclusions, false);
            value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getCause().getMessage()), 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 (CompiledScriptExecutor.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) IntStringPair(com.serotonin.db.pair.IntStringPair) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Aggregations

IntStringPair (com.serotonin.db.pair.IntStringPair)31 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)10 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)9 JsonArray (com.serotonin.json.type.JsonArray)6 JsonObject (com.serotonin.json.type.JsonObject)6 LinkedHashMap (java.util.LinkedHashMap)6 ScriptException (javax.script.ScriptException)5 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)4 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)4 User (com.serotonin.m2m2.vo.User)4 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 JsonValue (com.serotonin.json.type.JsonValue)3 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 DataPointRT (com.serotonin.m2m2.rt.dataImage.DataPointRT)3 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)3 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)3