Search in sources :

Example 6 with JsonValue

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

the class ModulesDwr method upgradesAvailable.

/**
 * How many upgrades are available
 * @return
 * @throws Exception
 */
public static int upgradesAvailable() throws Exception {
    JsonValue jsonResponse = getAvailableUpgrades();
    if (jsonResponse instanceof JsonString)
        throw new Exception("Mango Store Response Error: " + jsonResponse.toString());
    JsonObject root = jsonResponse.toJsonObject();
    int size = root.getJsonArray("upgrades").size();
    if (size > 0) {
        // Notify the listeners
        JsonValue jsonUpgrades = root.get("upgrades");
        JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
        for (JsonValue v : jsonUpgradesArray) {
            for (ModuleNotificationListener l : listeners) l.moduleUpgradeAvailable(v.getJsonValue("name").toString(), v.getJsonValue("version").toString());
        }
        JsonValue jsonInstalls = root.get("newInstalls");
        JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
        for (JsonValue v : jsonInstallsArray) {
            for (ModuleNotificationListener l : listeners) l.newModuleAvailable(v.getJsonValue("name").toString(), v.getJsonValue("version").toString());
        }
    }
    return size;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) ModuleNotificationListener(com.serotonin.m2m2.module.ModuleNotificationListener) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) JsonException(com.serotonin.json.JsonException) HttpException(org.apache.http.HttpException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 7 with JsonValue

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

the class ModulesDwr method versionCheck.

@DwrPermission(admin = true)
public ProcessResult versionCheck() {
    ProcessResult result = new ProcessResult();
    if (UPGRADE_DOWNLOADER != null) {
        result.addData("error", Common.translate("modules.versionCheck.occupied"));
        return result;
    }
    try {
        JsonValue jsonResponse = getAvailableUpgrades();
        if (jsonResponse instanceof JsonString)
            result.addData("error", jsonResponse.toString());
        else {
            JsonObject root = jsonResponse.toJsonObject();
            result.addData("upgrades", root.get("upgrades").toNative());
            result.addData("newInstalls", root.get("newInstalls").toNative());
            if (root.containsKey("upgradesError"))
                result.addData("upgradesError", root.getString("upgradesError"));
            if (root.containsKey("updates")) {
                result.addData("updates", root.get("updates").toNative());
                result.addData("newInstalls-oldCore", root.get("newInstalls-oldCore").toNative());
            }
            if (root.containsKey("missingModules"))
                result.addData("missingModules", root.getJsonArray("missingModules").toNative());
        }
    } catch (UnknownHostException e) {
        LOG.error("", e);
        result.addData("unknownHost", e.getMessage());
    } catch (Exception e) {
        LOG.error("", e);
        result.addData("error", e.getMessage());
    }
    return result;
}
Also used : UnknownHostException(java.net.UnknownHostException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) JsonException(com.serotonin.json.JsonException) HttpException(org.apache.http.HttpException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 8 with JsonValue

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

the class PublisherVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    // Not reading XID so can't do this: super.jsonRead(reader, jsonObject);
    name = jsonObject.getString("name");
    enabled = jsonObject.getBoolean("enabled");
    // Legacy conversion for publishType
    if (jsonObject.containsKey("publishType")) {
        String publishTypeCode = jsonObject.getString("publishType");
        int publishTypeId = PUBLISH_TYPE_CODES.getId(publishTypeCode);
        if (publishTypeId == -1)
            throw new TranslatableJsonException("emport.error.invalid", "publishType", publishTypeCode, PUBLISH_TYPE_CODES.getCodeList());
        publishType = publishTypeId;
    } else if (jsonObject.containsKey("changesOnly")) {
        boolean changesOnly = jsonObject.getBoolean("changesOnly");
        if (changesOnly) {
            this.publishType = PublishType.CHANGES_ONLY;
        } else {
            this.publishType = PublishType.ALL;
        }
    }
    // Could wrap the readInto with a try-catch in case one dataPointId entry is null,
    // however this would be a silent suppression of the issue, so we have elected not to.
    // infiniteautomation/ma-core-public#948
    JsonArray arr = jsonObject.getJsonArray("points");
    if (arr != null) {
        points.clear();
        for (JsonValue jv : arr) {
            T point = createPublishedPointInstance();
            reader.readInto(point, jv.toJsonObject());
            points.add(point);
        }
    }
    String text = jsonObject.getString("snapshotSendPeriodType");
    if (text != null) {
        snapshotSendPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (snapshotSendPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "snapshotSendPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    JsonObject alarmCodeLevels = jsonObject.getJsonObject("alarmLevels");
    if (alarmCodeLevels != null) {
        ExportCodes eventCodes = getEventCodes();
        if (eventCodes != null && eventCodes.size() > 0) {
            for (String code : alarmCodeLevels.keySet()) {
                int eventId = eventCodes.getId(code);
                if (!eventCodes.isValidId(eventId))
                    throw new TranslatableJsonException("emport.error.eventCode", code, eventCodes.getCodeList());
                text = alarmCodeLevels.getString(code);
                int level = AlarmLevels.CODES.getId(text);
                if (!AlarmLevels.CODES.isValidId(level))
                    throw new TranslatableJsonException("emport.error.alarmLevel", text, code, AlarmLevels.CODES.getCodeList());
                setAlarmLevel(eventId, level);
            }
        }
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) ExportCodes(com.serotonin.m2m2.util.ExportCodes) PublisherRT(com.serotonin.m2m2.rt.publish.PublisherRT) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject)

Example 9 with JsonValue

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

the class PopulateTest method main.

public static void main(String[] args) throws Exception {
    context = new JsonContext();
    context.addFactory(new ObjectFactory() {

        @Override
        public Object create(JsonValue jsonValue) throws JsonException {
            if (jsonValue.toJsonObject().containsKey("sub1Value"))
                return new Subclass1();
            if (jsonValue.toJsonObject().containsKey("sub2Value"))
                return new Subclass2();
            throw new JsonException("Unknown BaseClass: " + jsonValue);
        }
    }, BaseClass.class);
    test1();
    test2();
}
Also used : JsonException(com.serotonin.json.JsonException) JsonContext(com.serotonin.json.JsonContext) ObjectFactory(com.serotonin.json.spi.ObjectFactory) JsonValue(com.serotonin.json.type.JsonValue)

Example 10 with JsonValue

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

the class CollectionConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonArray jsonArray = (JsonArray) jsonValue;
    @SuppressWarnings("unchecked") Collection<Object> collection = (Collection<Object>) obj;
    Type innerType = TypeUtils.getActualTypeArgument(type, 0);
    for (JsonValue element : jsonArray) collection.add(reader.read(innerType, element));
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) Type(java.lang.reflect.Type) JsonValue(com.serotonin.json.type.JsonValue) Collection(java.util.Collection)

Aggregations

JsonValue (com.serotonin.json.type.JsonValue)41 JsonArray (com.serotonin.json.type.JsonArray)26 JsonObject (com.serotonin.json.type.JsonObject)21 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)15 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)15 JsonWriter (com.serotonin.json.JsonWriter)10 StringWriter (java.io.StringWriter)10 HttpPost (org.apache.http.client.methods.HttpPost)10 StringEntity (org.apache.http.entity.StringEntity)10 JsonException (com.serotonin.json.JsonException)8 IOException (java.io.IOException)7 JsonString (com.serotonin.json.type.JsonString)5 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)5 User (com.serotonin.m2m2.vo.User)5 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 IntStringPair (com.serotonin.db.pair.IntStringPair)3 JsonReader (com.serotonin.json.JsonReader)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 Type (java.lang.reflect.Type)3