Search in sources :

Example 51 with Device

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

the class DeviceTypeDAOImpl method getDevice.

public Device getDevice(String deviceId) throws DeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    Device iotDevice = null;
    ResultSet resultSet = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String selectDBQuery = "SELECT sampledevice_DEVICE_ID, DEVICE_NAME" + " FROM sampledevice_DEVICE WHERE sampledevice_DEVICE_ID = ?";
        stmt = conn.prepareStatement(selectDBQuery);
        stmt.setString(1, deviceId);
        resultSet = stmt.executeQuery();
        if (resultSet.next()) {
            iotDevice = new Device();
            iotDevice.setName(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_NAME));
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + deviceId + " data has been fetched from " + "sampledevice database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while fetching sampledevice device : '" + deviceId + "'";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, resultSet);
        DeviceTypeDAO.closeConnection();
    }
    return iotDevice;
}
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) PreparedStatement(java.sql.PreparedStatement)

Example 52 with Device

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

the class ServiceComponent method activate.

protected void activate(ComponentContext ctx) {
    if (log.isDebugEnabled()) {
        log.debug("Activating b Management Service Component");
    }
    try {
        DeviceTypeManagerService deviceTypeManagerService = new DeviceTypeManagerService();
        BundleContext bundleContext = ctx.getBundleContext();
        serviceRegistration = bundleContext.registerService(DeviceManagementService.class.getName(), deviceTypeManagerService, null);
        String setupOption = System.getProperty("setup");
        if (setupOption != null) {
            if (log.isDebugEnabled()) {
                log.debug("-Dsetup is enabled. Iot Device management repository schema " + "initialization is about to begin");
            }
            try {
                DeviceTypeUtils.setupDeviceManagementSchema();
            } catch (DeviceMgtPluginException e) {
                log.error("Exception occurred while initializing device management database " + "schema", e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("b Management Service Component has been successfully activated");
        }
    } catch (Throwable e) {
        log.error("Error occurred while activating Current Sensor Management Service " + "Component", e);
    }
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceTypeManagerService(org.wso2.iot.sampledevice.plugin.impl.DeviceTypeManagerService) BundleContext(org.osgi.framework.BundleContext)

Example 53 with Device

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

the class DeviceTypeManager method enrollDevice.

@Override
public boolean enrollDevice(Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Enrolling a new sampledevice device : " + device.getDeviceIdentifier());
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().addDevice(device);
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            log.warn("Error occurred while roll back the device enrol transaction :" + device.toString(), iotDAOEx);
        }
        String msg = "Error while enrolling the sampledevice device : " + device.getDeviceIdentifier();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 54 with Device

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

the class DeviceTypeManager method disenrollDevice.

@Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Dis-enrolling sampledevice device : " + deviceId);
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().deleteDevice(deviceId.getId());
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while removing the sampledevice device : " + deviceId.getId();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 55 with Device

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

the class ConnectedCupDAO method getAllDevices.

public List<Device> getAllDevices() throws ConnectedCupDeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet resultSet = null;
    Device connectedCupDevice;
    List<Device> iotDevices = new ArrayList<>();
    try {
        conn = ConnectedCupDAOUtil.getConnection();
        String selectDBQuery = "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME" + "FROM CONNECTED_CUP_DEVICE";
        stmt = conn.prepareStatement(selectDBQuery);
        resultSet = stmt.executeQuery();
        while (resultSet.next()) {
            connectedCupDevice = new Device();
            connectedCupDevice.setDeviceIdentifier(resultSet.getString(ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_ID));
            connectedCupDevice.setName(resultSet.getString(ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME));
        }
        if (log.isDebugEnabled()) {
            log.debug("All Connected Cup device details have fetched from Connected Cup database" + ".");
        }
        return iotDevices;
    } catch (SQLException e) {
        String msg = "Error occurred while fetching all Connected Cup device data'";
        log.error(msg, e);
        throw new ConnectedCupDeviceMgtPluginException(msg, e);
    } finally {
        ConnectedCupUtils.cleanupResources(stmt, resultSet);
        ConnectedCupDAOUtil.closeConnection();
    }
}
Also used : ConnectedCupDeviceMgtPluginException(org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException) 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)

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