Search in sources :

Example 11 with DeviceConfig

use of org.openmuc.framework.config.DeviceConfig in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method doDelete.

@Override
public synchronized void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString != null) {
        setConfigAccess();
        String pathInfo = pathAndQueryString[0];
        String deviceId = null;
        String[] pathInfoArray = ServletLib.getPathInfoArray(pathInfo);
        deviceId = pathInfoArray[0];
        DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
        if (pathInfoArray.length != 1) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
        } else if (deviceConfig == null) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "Device \"" + deviceId + "\" does not exist.");
        } else {
            try {
                deviceConfig.delete();
                configService.setConfig(rootConfig);
                configService.writeConfigToFile();
                if (rootConfig.getDriver(deviceId) == null) {
                    response.setStatus(HttpServletResponse.SC_OK);
                } else {
                    ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to delete driver ", deviceId);
                }
            } catch (ConfigWriteException e) {
                ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to write into config.");
            }
        }
    }
}
Also used : ConfigWriteException(org.openmuc.framework.config.ConfigWriteException) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 12 with DeviceConfig

use of org.openmuc.framework.config.DeviceConfig in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method doGetDevice.

private void doGetDevice(ToJson json, String deviceId, HttpServletResponse response) {
    DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
    json.addDevice(RestDeviceWrapper.getDevice(deviceConfig, configService, dataAccess));
}
Also used : DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 13 with DeviceConfig

use of org.openmuc.framework.config.DeviceConfig in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method doPutConfigs.

private synchronized boolean doPutConfigs(String deviceId, HttpServletResponse response, FromJson json) throws JsonSyntaxException, ConfigWriteException, RestConfigIsNotCorrectException, MissingJsonObjectException, IllegalStateException {
    boolean ok = false;
    DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
    if (deviceConfig != null) {
        try {
            json.setDeviceConfig(deviceConfig, deviceId);
        } catch (IdCollisionException e) {
        }
        configService.setConfig(rootConfig);
        configService.writeConfigToFile();
        response.setStatus(HttpServletResponse.SC_OK);
        ok = true;
    } else {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to access to device ", deviceId);
    }
    return ok;
}
Also used : IdCollisionException(org.openmuc.framework.config.IdCollisionException) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 14 with DeviceConfig

use of org.openmuc.framework.config.DeviceConfig in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method getDeviceIdList.

private List<String> getDeviceIdList() {
    List<String> deviceList = new ArrayList<>();
    Collection<DeviceConfig> deviceConfigs = getConfigsList();
    for (DeviceConfig deviceConfig : deviceConfigs) {
        deviceList.add(deviceConfig.getId());
    }
    return deviceList;
}
Also used : ArrayList(java.util.ArrayList) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 15 with DeviceConfig

use of org.openmuc.framework.config.DeviceConfig in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method doGetConfigField.

private void doGetConfigField(ToJson json, String deviceId, String configField, HttpServletResponse response) throws IOException {
    DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
    if (deviceConfig != null) {
        JsonObject jsoConfigAll = ToJson.getDeviceConfigAsJsonObject(deviceConfig);
        if (jsoConfigAll == null) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "Could not find JSON object \"configs\"");
        } else {
            JsonElement jseConfigField = jsoConfigAll.get(configField);
            if (jseConfigField == null) {
                ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "Requested rest config field is not available.", " configField = ", configField);
            } else {
                JsonObject jso = new JsonObject();
                jso.add(configField, jseConfigField);
                json.addJsonObject(Const.CONFIGS, jso);
            }
        }
    } else {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_ID_IS_NOT_AVAILABLE, REST_ID, deviceId);
    }
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Aggregations

DeviceConfig (org.openmuc.framework.config.DeviceConfig)17 DriverConfig (org.openmuc.framework.config.DriverConfig)6 ArrayList (java.util.ArrayList)5 ChannelConfig (org.openmuc.framework.config.ChannelConfig)4 JsonObject (com.google.gson.JsonObject)3 LinkedList (java.util.LinkedList)3 IdCollisionException (org.openmuc.framework.config.IdCollisionException)3 JsonElement (com.google.gson.JsonElement)2 HashMap (java.util.HashMap)2 DriverInfo (org.openmuc.framework.config.DriverInfo)2 DriverNotAvailableException (org.openmuc.framework.config.DriverNotAvailableException)2 DeviceState (org.openmuc.framework.dataaccess.DeviceState)2 LogicalDevice (org.openmuc.framework.dataaccess.LogicalDevice)2 JsonArray (com.google.gson.JsonArray)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Deque (java.util.Deque)1 LinkedHashMap (java.util.LinkedHashMap)1