Search in sources :

Example 31 with JsonArray

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

the class EventDetectorImporter method importImpl.

@Override
protected void importImpl() {
    String dataPointXid = json.getString("dataPointXid");
    DataPointVO dpvo;
    // Everyone is in the same thread so no synchronization on dataPointMap required.
    if (dataPointMap.containsKey(dataPointXid))
        dpvo = dataPointMap.get(dataPointXid);
    else if (StringUtils.isEmpty(dataPointXid) || (dpvo = DataPointDao.instance.getByXid(dataPointXid)) == null) {
        addFailureMessage("emport.error.missingPoint", dataPointXid);
        return;
    } else {
        dataPointMap.put(dataPointXid, dpvo);
        // We're only going to use this to house event detectors imported in the eventDetectors object.
        dpvo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>());
    }
    String typeStr = json.getString("type");
    if (typeStr == null)
        addFailureMessage("emport.error.ped.missingAttr", "type");
    EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
    if (def == null) {
        addFailureMessage("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
        return;
    }
    JsonArray handlerXids = json.getJsonArray("handlers");
    if (handlerXids != null)
        for (int k = 0; k < handlerXids.size(); k += 1) {
            AbstractEventHandlerVO<?> eh = EventHandlerDao.instance.getByXid(handlerXids.getString(k));
            if (eh == null) {
                addFailureMessage("emport.eventHandler.missing", handlerXids.getString(k));
                return;
            }
        }
    AbstractEventDetectorVO<?> importing = def.baseCreateEventDetectorVO();
    importing.setDefinition(def);
    String xid = json.getString("xid");
    // Create a new one
    importing.setId(Common.NEW_ID);
    importing.setXid(xid);
    AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) importing;
    dped.njbSetDataPoint(dpvo);
    dpvo.getEventDetectors().add(dped);
    try {
        ctx.getReader().readInto(importing, json);
    // try {
    // if(Common.runtimeManager.getState() == RuntimeManager.RUNNING){
    // Common.runtimeManager.saveDataPoint(dpvo);
    // addSuccessMessage(isNew, "emport.eventDetector.prefix", xid);
    // }else{
    // addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
    // }
    // } catch(LicenseViolatedException e) {
    // addFailureMessage(new ProcessMessage(e.getErrorMessage()));
    // }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventDetector.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) JsonArray(com.serotonin.json.type.JsonArray) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) ArrayList(java.util.ArrayList) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 32 with JsonArray

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

the class JsonArrayConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonArray jsonArray = (JsonArray) obj;
    jsonArray.clear();
    jsonArray.addAll(jsonValue.toJsonArray());
}
Also used : JsonArray(com.serotonin.json.type.JsonArray)

Example 33 with JsonArray

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

the class ArrayConverter method jsonWrite.

@Override
public JsonValue jsonWrite(JsonTypeWriter writer, Object value) throws JsonException {
    int length = Array.getLength(value);
    JsonArray jsonArray = new JsonArray();
    for (int i = 0; i < length; i++) jsonArray.add(writer.writeObject(Array.get(value, i)));
    return jsonArray;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray)

Example 34 with JsonArray

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

the class ArrayConverter method newInstance.

@Override
protected Object newInstance(JsonContext context, JsonValue jsonValue, Type type) {
    JsonArray jsonArray = (JsonArray) jsonValue;
    Class<?> clazz = TypeUtils.getRawClass(type).getComponentType();
    return Array.newInstance(clazz, jsonArray.size());
}
Also used : JsonArray(com.serotonin.json.type.JsonArray)

Example 35 with JsonArray

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

the class ArrayConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object array, Type type) throws JsonException {
    JsonArray jsonArray = (JsonArray) jsonValue;
    Class<?> clazz = array.getClass().getComponentType();
    for (int i = 0; i < jsonArray.size(); i++) {
        try {
            Array.set(array, i, reader.read(clazz, jsonArray.get(i)));
        } catch (Exception e) {
            throw new JsonException("JsonException reading array element " + i, e);
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) JsonException(com.serotonin.json.JsonException) JsonException(com.serotonin.json.JsonException) IOException(java.io.IOException)

Aggregations

JsonArray (com.serotonin.json.type.JsonArray)39 JsonValue (com.serotonin.json.type.JsonValue)26 JsonObject (com.serotonin.json.type.JsonObject)17 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)14 JsonWriter (com.serotonin.json.JsonWriter)10 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)8 IntStringPair (com.serotonin.db.pair.IntStringPair)6 JsonString (com.serotonin.json.type.JsonString)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 ArrayList (java.util.ArrayList)5 JsonException (com.serotonin.json.JsonException)4 User (com.serotonin.m2m2.vo.User)4 JsonBoolean (com.serotonin.json.type.JsonBoolean)3 IOException (java.io.IOException)3 TypeDefinition (com.serotonin.json.util.TypeDefinition)2 ScriptPermissions (com.serotonin.m2m2.rt.script.ScriptPermissions)2