Search in sources :

Example 41 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.

the class ScriptContextVariable method jsonWriteVarContext.

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

Example 42 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.

the class ScriptContextVariable method jsonReadVarContext.

/**
 * Read in context,
 * @param json
 * @param context
 * @return if my XID is in the context, return the name it has to map into the VO otherwise return null
 * @throws JsonException
 */
public static String jsonReadVarContext(JsonObject json, List<ScriptContextVariable> context, boolean isContextUpdate) 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.context.missing", "dataPointXid");
            Integer dpid = dataPointDao.getIdByXid(xid);
            if (dpid == null) {
                // This can also happen if the point is in its own context (Bug from legacy systems).
                throw new TranslatableJsonException("emport.error.missingPoint", xid);
            }
            // For compatibility with varName and variableName json types
            String var = jo.getString("varName");
            if (var == null) {
                var = jo.getString("variableName");
                if (var == null)
                    throw new TranslatableJsonException("emport.error.context.missing", "varName");
            }
            // Default for legacy systems
            if (jo.containsKey("updateContext"))
                isContextUpdate = jo.getBoolean("updateContext");
            context.add(new ScriptContextVariable(dpid, var, isContextUpdate));
        }
    }
    return json.getString("variableName");
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) DataPointDao(com.serotonin.m2m2.db.dao.DataPointDao) 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 43 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.

the class EventDetectorRowMapper method mapRow.

/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
	 */
@Override
public AbstractEventDetectorVO<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
    EventDetectorDefinition<?> definition = ModuleRegistry.getEventDetectorDefinition(rs.getString(this.firstColumn + 3));
    if (definition == null)
        throw new ShouldNeverHappenException("Event Detector defintion of type: " + rs.getString(this.firstColumn + 3) + " not found.");
    AbstractEventDetectorVO<?> vo = definition.baseCreateEventDetectorVO();
    vo.setId(rs.getInt(this.firstColumn));
    vo.setXid(rs.getString(this.firstColumn + 1));
    vo.setDefinition(definition);
    // Extract the source id
    int sourceIdColumnIndex;
    if (this.sourceIdColumnOffset < 0)
        sourceIdColumnIndex = this.firstColumn + 5 + EventDetectorDao.instance.getSourceIdIndex(definition.getSourceTypeName());
    else
        sourceIdColumnIndex = this.firstColumn + this.sourceIdColumnOffset;
    vo.setSourceId(rs.getInt(sourceIdColumnIndex));
    // Read Into Detector
    JsonTypeReader typeReader = new JsonTypeReader(rs.getString(this.firstColumn + 4));
    try {
        JsonValue value = typeReader.read();
        JsonObject root = value.toJsonObject();
        JsonReader reader = new JsonReader(Common.JSON_CONTEXT, root);
        root.remove("handlers");
        reader.readInto(vo);
    } catch (ClassCastException | IOException | JsonException e) {
        LOG.error(e.getMessage(), e);
    }
    return vo;
}
Also used : JsonException(com.serotonin.json.JsonException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonReader(com.serotonin.json.JsonReader) IOException(java.io.IOException) JsonTypeReader(com.serotonin.json.type.JsonTypeReader)

Example 44 with JsonObject

use of com.serotonin.json.type.JsonObject in project ma-core-public by infiniteautomation.

the class MapConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonObject jsonObject = (JsonObject) jsonValue;
    @SuppressWarnings("unchecked") Map<Object, Object> map = (Map<Object, Object>) obj;
    Type valueType = TypeUtils.getActualTypeArgument(type, 1);
    for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) map.put(entry.getKey(), reader.read(valueType, entry.getValue()));
}
Also used : Type(java.lang.reflect.Type) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonObject(com.serotonin.json.type.JsonObject) Map(java.util.Map)

Aggregations

JsonObject (com.serotonin.json.type.JsonObject)44 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)24 JsonValue (com.serotonin.json.type.JsonValue)20 JsonArray (com.serotonin.json.type.JsonArray)17 JsonException (com.serotonin.json.JsonException)15 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)11 JsonString (com.serotonin.json.type.JsonString)7 IOException (java.io.IOException)7 IntStringPair (com.serotonin.db.pair.IntStringPair)6 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)6 JsonReader (com.serotonin.json.JsonReader)5 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)5 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)4 ArrayList (java.util.ArrayList)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)3 User (com.serotonin.m2m2.vo.User)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)3 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)2