Search in sources :

Example 1 with JsonString

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

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

use of com.serotonin.json.type.JsonString in project ma-modules-public by infiniteautomation.

the class ModulesRestController method getUpgrades.

@ApiOperation(value = "Get Available Upgrades", notes = "Check the store for Upgrades")
@RequestMapping(method = RequestMethod.GET, value = "/upgrades-available", produces = { "application/json" })
public ResponseEntity<ModuleUpgradesModel> getUpgrades(HttpServletRequest request) {
    RestProcessResult<ModuleUpgradesModel> result = new RestProcessResult<ModuleUpgradesModel>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            // Do the check
            try {
                JsonValue jsonResponse = ModulesDwr.getAvailableUpgrades();
                if (jsonResponse instanceof JsonString) {
                    result.addRestMessage(getInternalServerErrorMessage(jsonResponse.toString()));
                } else {
                    List<ModuleUpgradeModel> upgrades = new ArrayList<>();
                    List<ModuleUpgradeModel> newInstalls = new ArrayList<>();
                    ModuleUpgradesModel model = new ModuleUpgradesModel(upgrades, newInstalls);
                    JsonObject root = jsonResponse.toJsonObject();
                    JsonValue jsonUpgrades = root.get("upgrades");
                    JsonArray jsonUpgradesArray = jsonUpgrades.toJsonArray();
                    Iterator<JsonValue> it = jsonUpgradesArray.iterator();
                    while (it.hasNext()) {
                        JsonValue v = it.next();
                        if (v.getJsonValue("name") == null) {
                            it.remove();
                            continue;
                        }
                        String name = v.getJsonValue("name").toString();
                        Module module = "core".equals(name) ? ModuleRegistry.getCoreModule() : ModuleRegistry.getModule(name);
                        if (module == null) {
                            it.remove();
                            continue;
                        }
                        upgrades.add(new ModuleUpgradeModel(module, v));
                    }
                    JsonValue jsonInstalls = root.get("newInstalls");
                    JsonArray jsonInstallsArray = jsonInstalls.toJsonArray();
                    for (JsonValue v : jsonInstallsArray) {
                        newInstalls.add(new ModuleUpgradeModel(v));
                    }
                    return result.createResponseEntity(model);
                }
            } catch (SocketTimeoutException e) {
                result.addRestMessage(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.error.requestTimeout", Common.envProps.getString("store.url")));
            } catch (UnknownHostException e) {
                result.addRestMessage(HttpStatus.INTERNAL_SERVER_ERROR, new TranslatableMessage("rest.error.unknownHost", Common.envProps.getString("store.url")));
            } catch (Exception e) {
                result.addRestMessage(getInternalServerErrorMessage(e.getMessage()));
            }
        } else {
            result.addRestMessage(this.getUnauthorizedMessage());
        }
    }
    return result.createResponseEntity();
}
Also used : User(com.serotonin.m2m2.vo.User) UnknownHostException(java.net.UnknownHostException) JsonValue(com.serotonin.json.type.JsonValue) ArrayList(java.util.ArrayList) JsonObject(com.serotonin.json.type.JsonObject) JsonString(com.serotonin.json.type.JsonString) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) FileNotFoundException(java.io.FileNotFoundException) ModuleRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.ModuleRestV2Exception) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JsonArray(com.serotonin.json.type.JsonArray) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ModuleUpgradeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.modules.ModuleUpgradeModel) SocketTimeoutException(java.net.SocketTimeoutException) ModuleUpgradesModel(com.serotonin.m2m2.web.mvc.rest.v1.model.modules.ModuleUpgradesModel) JsonString(com.serotonin.json.type.JsonString) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Module(com.serotonin.m2m2.module.Module) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with JsonString

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

the class VarNames method jsonWriteVarContext.

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

Example 5 with JsonString

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

Aggregations

JsonObject (com.serotonin.json.type.JsonObject)5 JsonString (com.serotonin.json.type.JsonString)5 JsonArray (com.serotonin.json.type.JsonArray)4 JsonValue (com.serotonin.json.type.JsonValue)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 JsonException (com.serotonin.json.JsonException)2 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)2 HttpException (org.apache.http.HttpException)2 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)1 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)1 ModuleRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.ModuleRestV2Exception)1 IntStringPair (com.serotonin.db.pair.IntStringPair)1 JsonBoolean (com.serotonin.json.type.JsonBoolean)1 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 Module (com.serotonin.m2m2.module.Module)1 ModuleNotificationListener (com.serotonin.m2m2.module.ModuleNotificationListener)1 User (com.serotonin.m2m2.vo.User)1