Search in sources :

Example 11 with JsonObject

use of com.serotonin.json.type.JsonObject 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 12 with JsonObject

use of com.serotonin.json.type.JsonObject 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 13 with JsonObject

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

the class MangoTestBase method loadConfiguration.

protected void loadConfiguration(File jsonFile) throws JsonException, IOException, URISyntaxException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), Common.UTF8_CS));
    JsonReader jr = new JsonReader(reader);
    JsonObject jo = jr.read(JsonObject.class);
    ImportTask task = new ImportTask(jo, Common.getTranslations(), null, false);
    task.run(Common.timer.currentTimeMillis());
    if (task.getResponse().getHasMessages()) {
        for (ProcessMessage message : task.getResponse().getMessages()) {
            switch(message.getLevel()) {
                case error:
                case warning:
                    fail(message.toString(Common.getTranslations()));
                case info:
                    LOG.info(message.toString(Common.getTranslations()));
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ImportTask(com.serotonin.m2m2.web.dwr.emport.ImportTask) BufferedReader(java.io.BufferedReader) JsonReader(com.serotonin.json.JsonReader) JsonObject(com.serotonin.json.type.JsonObject) ProcessMessage(com.serotonin.m2m2.i18n.ProcessMessage) FileInputStream(java.io.FileInputStream)

Example 14 with JsonObject

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

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getEventHandlerDao().generateUniqueXid();
    AbstractEventHandlerVO<?> handler = ctx.getEventHandlerDao().getEventHandler(xid);
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    EventType eventType = null;
    List<EventType> eventTypes = null;
    try {
        // Find the event type.
        if (et != null)
            eventType = ctx.getReader().read(EventType.class, et);
        else if (ets != null) {
            eventTypes = new ArrayList<EventType>(ets.size());
            Iterator<JsonValue> iter = ets.iterator();
            while (iter.hasNext()) eventTypes.add(ctx.getReader().read(EventType.class, iter.next()));
        }
        ctx.getReader().readInto(handler, json);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        handler.validate(voResponse);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.eventHandler.prefix", xid);
        else {
            // Sweet.
            boolean isnew = handler.getId() == Common.NEW_ID;
            // Save it.
            if (et != null)
                ctx.getEventHandlerDao().saveEventHandler(eventType, handler);
            else
                ctx.getEventHandlerDao().saveEventHandler(handler);
            if (eventTypes != null)
                for (EventType type : eventTypes) ctx.getEventHandlerDao().addEventHandlerMappingIfMissing(handler.getId(), type);
            addSuccessMessage(isnew, "emport.eventHandler.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) Iterator(java.util.Iterator)

Example 15 with JsonObject

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

the class MapConverter method jsonWrite.

@Override
public JsonValue jsonWrite(JsonTypeWriter writer, Object value) throws JsonException {
    Map<?, ?> map = (Map<?, ?>) value;
    JsonObject jsonObject = new JsonObject();
    for (Map.Entry<?, ?> entry : map.entrySet()) jsonObject.put(entry.getKey().toString(), writer.writeObject(entry.getValue()));
    return jsonObject;
}
Also used : 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