Search in sources :

Example 6 with JsonObject

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

the class EmailEventHandlerVO method jsonWrite.

@Override
public void jsonWrite(ObjectWriter writer) throws IOException, JsonException {
    super.jsonWrite(writer);
    writer.writeEntry("activeRecipients", activeRecipients);
    writer.writeEntry("sendEscalation", sendEscalation);
    if (sendEscalation) {
        writer.writeEntry("escalationDelayType", Common.TIME_PERIOD_CODES.getCode(escalationDelayType));
        writer.writeEntry("escalationDelay", escalationDelay);
        writer.writeEntry("escalationRecipients", escalationRecipients);
        writer.writeEntry("keepSendingEscalations", repeatEscalations);
    }
    writer.writeEntry("sendInactive", sendInactive);
    if (sendInactive) {
        writer.writeEntry("inactiveOverride", inactiveOverride);
        if (inactiveOverride)
            writer.writeEntry("inactiveRecipients", inactiveRecipients);
    }
    writer.writeEntry("includeSystemInformation", includeSystemInfo);
    writer.writeEntry("includePointValueCount", includePointValueCount);
    writer.writeEntry("includeLogfile", includeLogfile);
    writer.writeEntry("customTemplate", customTemplate);
    JsonArray context = new JsonArray();
    for (IntStringPair pnt : additionalContext) {
        DataPointVO dpvo = DataPointDao.instance.get(pnt.getKey());
        if (dpvo != null) {
            JsonObject point = new JsonObject();
            point.put("dataPointXid", dpvo.getXid());
            point.put("contextKey", pnt.getValue());
            context.add(point);
        }
    }
    writer.writeEntry("additionalContext", context);
    writer.writeEntry("script", script);
    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 7 with JsonObject

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

the class EmailEventHandlerVO method jsonRead.

@SuppressWarnings("unchecked")
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    String text = null;
    TypeDefinition recipType = new TypeDefinition(List.class, RecipientListEntryBean.class);
    JsonArray jsonActiveRecipients = jsonObject.getJsonArray("activeRecipients");
    if (jsonActiveRecipients != null)
        activeRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonActiveRecipients);
    JsonBoolean b = jsonObject.getJsonBoolean("sendEscalation");
    if (b != null)
        sendEscalation = b.booleanValue();
    if (sendEscalation) {
        text = jsonObject.getString("escalationDelayType");
        if (text != null) {
            escalationDelayType = Common.TIME_PERIOD_CODES.getId(text);
            if (escalationDelayType == -1)
                throw new TranslatableJsonException("emport.error.invalid", "escalationDelayType", text, Common.TIME_PERIOD_CODES.getCodeList());
        }
        Integer i = jsonObject.getInt("escalationDelay", 1);
        if (i != null)
            escalationDelay = i;
        JsonArray jsonEscalationRecipients = jsonObject.getJsonArray("escalationRecipients");
        if (jsonEscalationRecipients != null)
            escalationRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonEscalationRecipients);
        b = jsonObject.getJsonBoolean("keepSendingEscalations");
        if (b != null)
            repeatEscalations = b.booleanValue();
    }
    b = jsonObject.getJsonBoolean("sendInactive");
    if (b != null)
        sendInactive = b.booleanValue();
    if (sendInactive) {
        b = jsonObject.getJsonBoolean("inactiveOverride");
        if (b != null)
            inactiveOverride = b.booleanValue();
        if (inactiveOverride) {
            JsonArray jsonInactiveRecipients = jsonObject.getJsonArray("inactiveRecipients");
            if (jsonInactiveRecipients != null)
                inactiveRecipients = (List<RecipientListEntryBean>) reader.read(recipType, jsonInactiveRecipients);
        }
    }
    b = jsonObject.getJsonBoolean("includeSystemInformation");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    includePointValueCount = jsonObject.getInt("includePointValueCount", 0);
    b = jsonObject.getJsonBoolean("includeLogfile");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    customTemplate = jsonObject.getString("customTemplate");
    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");
            DataPointVO dpvo = DataPointDao.instance.getByXid(dataPointXid);
            if (dpvo == 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(dpvo.getId(), contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    script = jsonObject.getString("script");
    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 : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) 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) TypeDefinition(com.serotonin.json.util.TypeDefinition) JsonArray(com.serotonin.json.type.JsonArray) ArrayList(java.util.ArrayList) List(java.util.List) JsonBoolean(com.serotonin.json.type.JsonBoolean)

Example 8 with JsonObject

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

the class DataPointVO 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");
    String text = jsonObject.getString("loggingType");
    if (text != null) {
        loggingType = LOGGING_TYPE_CODES.getId(text);
        if (loggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "loggingType", text, LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingPeriodType");
    if (text != null) {
        intervalLoggingPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (intervalLoggingPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    text = jsonObject.getString("intervalLoggingType");
    if (text != null) {
        intervalLoggingType = INTERVAL_LOGGING_TYPE_CODES.getId(text);
        if (intervalLoggingType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "intervalLoggingType", text, INTERVAL_LOGGING_TYPE_CODES.getCodeList());
    }
    text = jsonObject.getString("purgeType");
    if (text != null) {
        purgeType = Common.TIME_PERIOD_CODES.getId(text);
        if (purgeType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "purgeType", text, Common.TIME_PERIOD_CODES.getCodeList(TimePeriods.MILLISECONDS, TimePeriods.SECONDS, TimePeriods.MINUTES, TimePeriods.HOURS));
    }
    JsonObject locatorJson = jsonObject.getJsonObject("pointLocator");
    if (locatorJson != null)
        reader.readInto(pointLocator, locatorJson);
    JsonArray pedArray = jsonObject.getJsonArray("eventDetectors");
    if (pedArray != null) {
        for (JsonValue jv : pedArray) {
            JsonObject pedObject = jv.toJsonObject();
            String pedXid = pedObject.getString("xid");
            if (StringUtils.isBlank(pedXid))
                throw new TranslatableJsonException("emport.error.ped.missingAttr", "xid");
            // Use the ped xid to lookup an existing ped.
            AbstractEventDetectorVO<?> ped = null;
            for (AbstractPointEventDetectorVO<?> existing : eventDetectors) {
                if (StringUtils.equals(pedXid, existing.getXid())) {
                    ped = existing;
                    break;
                }
            }
            JsonArray handlerXids = pedObject.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) {
                        throw new TranslatableJsonException("emport.eventHandler.missing", handlerXids.getString(k));
                    }
                }
            if (ped == null) {
                String typeStr = pedObject.getString("type");
                if (typeStr == null)
                    throw new TranslatableJsonException("emport.error.ped.missingAttr", "type");
                EventDetectorDefinition<?> def = ModuleRegistry.getEventDetectorDefinition(typeStr);
                if (def == null)
                    throw new TranslatableJsonException("emport.error.ped.invalid", "type", typeStr, ModuleRegistry.getEventDetectorDefinitionTypes());
                else {
                    ped = def.baseCreateEventDetectorVO();
                    ped.setDefinition(def);
                }
                // Create a new one
                ped.setId(Common.NEW_ID);
                ped.setXid(pedXid);
                AbstractPointEventDetectorVO<?> dped = (AbstractPointEventDetectorVO<?>) ped;
                dped.njbSetDataPoint(this);
                eventDetectors.add(dped);
            }
            reader.readInto(ped, pedObject);
        }
    }
    text = jsonObject.getString("unit");
    if (text != null) {
        unit = parseUnitString(text, "unit");
        unitString = UnitUtil.formatUcum(unit);
    }
    text = jsonObject.getString("integralUnit");
    if (text != null) {
        useIntegralUnit = true;
        integralUnit = parseUnitString(text, "integralUnit");
        integralUnitString = UnitUtil.formatUcum(integralUnit);
    }
    text = jsonObject.getString("renderedUnit");
    if (text != null) {
        useRenderedUnit = true;
        renderedUnit = parseUnitString(text, "renderedUnit");
        renderedUnitString = UnitUtil.formatUcum(renderedUnit);
    }
    text = jsonObject.getString("plotType");
    if (text != null) {
        plotType = PLOT_TYPE_CODES.getId(text);
        if (plotType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "plotType", text, PLOT_TYPE_CODES.getCodeList());
    }
    // Rollup
    text = jsonObject.getString("rollup");
    if (text != null) {
        rollup = Common.ROLLUP_CODES.getId(text);
        if (rollup == -1)
            throw new TranslatableJsonException("emport.error.chart.invalid", "rollup", text, Common.ROLLUP_CODES.getCodeList());
    }
    // Simplify
    text = jsonObject.getString("simplifyType");
    if (text != null) {
        simplifyType = SIMPLIFY_TYPE_CODES.getId(text);
        if (simplifyType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "simplifyType", text, SIMPLIFY_TYPE_CODES.getCodeList());
    }
    int simplifyTarget = jsonObject.getInt("simplifyTarget", Integer.MIN_VALUE);
    if (simplifyTarget != Integer.MIN_VALUE)
        this.simplifyTarget = simplifyTarget;
    double simplifyTolerance = jsonObject.getDouble("simplifyTolerance", Double.NaN);
    if (simplifyTolerance != Double.NaN)
        this.simplifyTolerance = simplifyTolerance;
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) AbstractPointEventDetectorVO(com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO)

Example 9 with JsonObject

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

the class DataSourceVO 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");
    // Don't change the type.
    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());
                String 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);
            }
        }
    }
    String text = jsonObject.getString("purgeType");
    if (text != null) {
        purgeType = Common.TIME_PERIOD_CODES.getId(text);
        if (purgeType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "purgeType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 10 with JsonObject

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

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