Search in sources :

Example 11 with Device

use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.

the class APIUtil method getDeviceManagementService.

/**
 * @return Device management service of current context
 */
public static DeviceManagementProviderService getDeviceManagementService() {
    PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    DeviceManagementProviderService deviceManagementProviderService = (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
    if (deviceManagementProviderService == null) {
        String msg = "Device Management service has not initialized.";
        log.error(msg);
        throw new IllegalStateException(msg);
    }
    return deviceManagementProviderService;
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) DeviceManagementProviderService(org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService)

Example 12 with Device

use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.

the class DeviceTypeDAOImpl method addDevice.

public boolean addDevice(Device device) throws DeviceMgtPluginException {
    boolean status = false;
    Connection conn;
    PreparedStatement stmt = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String createDBQuery = "INSERT INTO sampledevice_DEVICE(sampledevice_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
        stmt = conn.prepareStatement(createDBQuery);
        stmt.setString(1, device.getDeviceIdentifier());
        stmt.setString(2, device.getName());
        int rows = stmt.executeUpdate();
        if (rows > 0) {
            status = true;
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + device.getDeviceIdentifier() + " data has been" + " added to the sampledevice database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while adding the sampledevice device '" + device.getDeviceIdentifier() + "' to the sampledevice db.";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, null);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 13 with Device

use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.

the class DeviceTypeDAOImpl method deleteDevice.

public boolean deleteDevice(String deviceId) throws DeviceMgtPluginException {
    boolean status = false;
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String deleteDBQuery = "DELETE FROM sampledevice_DEVICE WHERE sampledevice_DEVICE_ID = ?";
        stmt = conn.prepareStatement(deleteDBQuery);
        stmt.setString(1, deviceId);
        int rows = stmt.executeUpdate();
        if (rows > 0) {
            status = true;
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + deviceId + " data has deleted" + " from the sampledevice database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while deleting sampledevice device " + deviceId;
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, null);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 14 with Device

use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.

the class DeviceTypeDAOImpl method getAllDevices.

public List<Device> getAllDevices() throws DeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet resultSet = null;
    Device device;
    List<Device> iotDevices = new ArrayList<>();
    try {
        conn = DeviceTypeDAO.getConnection();
        String selectDBQuery = "SELECT sampledevice_DEVICE_ID, DEVICE_NAME " + "FROM sampledevice_DEVICE";
        stmt = conn.prepareStatement(selectDBQuery);
        resultSet = stmt.executeQuery();
        while (resultSet.next()) {
            device = new Device();
            device.setDeviceIdentifier(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_ID));
            device.setName(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_NAME));
            List<Device.Property> propertyList = new ArrayList<>();
            device.setProperties(propertyList);
        }
        if (log.isDebugEnabled()) {
            log.debug("All sampledevice device details have fetched from sampledevice database.");
        }
        return iotDevices;
    } catch (SQLException e) {
        String msg = "Error occurred while fetching all sampledevice device data'";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, resultSet);
        DeviceTypeDAO.closeConnection();
    }
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Device(org.wso2.carbon.device.mgt.common.Device) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 15 with Device

use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.

the class DeviceTypeDAOImpl method updateDevice.

public boolean updateDevice(Device device) throws DeviceMgtPluginException {
    boolean status = false;
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String updateDBQuery = "UPDATE sampledevice_DEVICE SET  DEVICE_NAME = ? WHERE sampledevice_DEVICE_ID = ?";
        stmt = conn.prepareStatement(updateDBQuery);
        if (device.getProperties() == null) {
            device.setProperties(new ArrayList<Device.Property>());
        }
        stmt.setString(1, device.getName());
        stmt.setString(2, device.getDeviceIdentifier());
        int rows = stmt.executeUpdate();
        if (rows > 0) {
            status = true;
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + device.getDeviceIdentifier() + " data has been" + " modified.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while modifying the sampledevice device '" + device.getDeviceIdentifier() + "' data.";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, null);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Test (org.testng.annotations.Test)20 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)17 DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)13 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)11 JsonParser (com.google.gson.JsonParser)10 SQLException (java.sql.SQLException)8 JsonObject (com.google.gson.JsonObject)7 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 HashMap (java.util.HashMap)6 ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)6 Device (org.wso2.carbon.device.mgt.common.Device)6 JsonArray (com.google.gson.JsonArray)5 JsonElement (com.google.gson.JsonElement)4 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 Path (javax.ws.rs.Path)4 JSONObject (org.json.simple.JSONObject)4 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)4 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)4