|
|
@ -21,8 +21,8 @@ package org.wso2.carbon.device.mgt.core.status.task.impl;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskManagerService;
|
|
|
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskManagerService;
|
|
|
@ -43,20 +43,21 @@ public class DeviceStatusTaskManagerServiceImpl implements DeviceStatusTaskManag
|
|
|
|
|
|
|
|
|
|
|
|
public static final String DEVICE_STATUS_MONITORING_TASK_TYPE = "DEVICE_STATUS_MONITORING";
|
|
|
|
public static final String DEVICE_STATUS_MONITORING_TASK_TYPE = "DEVICE_STATUS_MONITORING";
|
|
|
|
static final String DEVICE_TYPE = "DEVICE_TYPE";
|
|
|
|
static final String DEVICE_TYPE = "DEVICE_TYPE";
|
|
|
|
|
|
|
|
static final String DEVICE_TYPE_ID = "DEVICE_TYPE_ID";
|
|
|
|
static final String DEVICE_STATUS_TASK_CONFIG = "DEVICE_STATUS_TASK_CONFIG";
|
|
|
|
static final String DEVICE_STATUS_TASK_CONFIG = "DEVICE_STATUS_TASK_CONFIG";
|
|
|
|
private static String TASK_CLASS = DeviceStatusMonitoringTask.class.getName();
|
|
|
|
private static String TASK_CLASS = DeviceStatusMonitoringTask.class.getName();
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void startTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
|
|
|
public void startTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
|
|
|
throws DeviceStatusTaskException {
|
|
|
|
throws DeviceStatusTaskException {
|
|
|
|
log.info("Device Status monitoring Task adding for " + deviceType);
|
|
|
|
log.info("Device Status monitoring Task adding for " + deviceType.getName());
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
taskService.registerTaskType(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
taskService.registerTaskType(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Device Status monitoring task is started for the device type " + deviceType);
|
|
|
|
log.debug("Device Status monitoring task is started for the device type " + deviceType.getName());
|
|
|
|
log.debug(
|
|
|
|
log.debug(
|
|
|
|
"Device Status monitoring task is at frequency of : " + deviceStatusTaskConfig.getFrequency());
|
|
|
|
"Device Status monitoring task is at frequency of : " + deviceStatusTaskConfig.getFrequency());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -73,48 +74,49 @@ public class DeviceStatusTaskManagerServiceImpl implements DeviceStatusTaskManag
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> properties = new HashMap<>();
|
|
|
|
Map<String, String> properties = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
properties.put(DEVICE_TYPE, deviceType);
|
|
|
|
properties.put(DEVICE_TYPE, deviceType.getName());
|
|
|
|
|
|
|
|
properties.put(DEVICE_TYPE_ID, deviceType.getId() + "");
|
|
|
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
|
|
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
|
|
|
|
|
|
|
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType;
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId();
|
|
|
|
|
|
|
|
|
|
|
|
if (!taskManager.isTaskScheduled(deviceType)) {
|
|
|
|
if (!taskManager.isTaskScheduled(taskName)) {
|
|
|
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
|
|
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
taskManager.rescheduleTask(taskInfo.getName());
|
|
|
|
taskManager.rescheduleTask(taskInfo.getName());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new DeviceStatusTaskException(
|
|
|
|
throw new DeviceStatusTaskException(
|
|
|
|
"Device Status monitoring task is already started for this device-type : " + deviceType);
|
|
|
|
"Device Status monitoring task is already started for this device-type : " + deviceType.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (TaskException e) {
|
|
|
|
} catch (TaskException e) {
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while creating the Device Status monitoring task " +
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while creating the Device Status monitoring task " +
|
|
|
|
"for device-type : " + deviceType, e);
|
|
|
|
"for device-type : " + deviceType.getName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void stopTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
|
|
|
public void stopTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
|
|
|
throws DeviceStatusTaskException {
|
|
|
|
throws DeviceStatusTaskException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId();
|
|
|
|
if (taskService.isServerInit()) {
|
|
|
|
if (taskService.isServerInit()) {
|
|
|
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
taskManager.deleteTask(deviceType);
|
|
|
|
taskManager.deleteTask(taskName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (TaskException e) {
|
|
|
|
} catch (TaskException e) {
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while deleting the Device Status monitoring task " +
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while deleting the Device Status monitoring task " +
|
|
|
|
"for tenant " + getTenantId(), e);
|
|
|
|
"for device-type : " + deviceType.getName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void updateTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
|
|
|
public void updateTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
|
|
|
throws DeviceStatusTaskException {
|
|
|
|
throws DeviceStatusTaskException {
|
|
|
|
int tenantId = getTenantId();
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType + "_" + String.valueOf(tenantId);
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType + "_" + deviceType.getId();
|
|
|
|
if (taskManager.isTaskScheduled(taskName)) {
|
|
|
|
if (taskManager.isTaskScheduled(taskName)) {
|
|
|
|
taskManager.deleteTask(taskName);
|
|
|
|
taskManager.deleteTask(taskName);
|
|
|
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
|
|
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
|
|
@ -122,44 +124,40 @@ public class DeviceStatusTaskManagerServiceImpl implements DeviceStatusTaskManag
|
|
|
|
triggerInfo.setRepeatCount(-1);
|
|
|
|
triggerInfo.setRepeatCount(-1);
|
|
|
|
|
|
|
|
|
|
|
|
Map<String, String> properties = new HashMap<>();
|
|
|
|
Map<String, String> properties = new HashMap<>();
|
|
|
|
properties.put(DEVICE_TYPE, deviceType);
|
|
|
|
properties.put(DEVICE_TYPE, deviceType.getName());
|
|
|
|
|
|
|
|
properties.put(DEVICE_TYPE_ID, deviceType.getId() + "");
|
|
|
|
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
Gson gson = new Gson();
|
|
|
|
String deviceStatusTaskConfigs = gson.toJson(deviceStatusTaskConfig);
|
|
|
|
String deviceStatusTaskConfigs = gson.toJson(deviceStatusTaskConfig);
|
|
|
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
|
|
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
|
|
|
|
|
|
|
|
|
|
|
TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo);
|
|
|
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
|
|
|
|
|
|
|
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
taskManager.registerTask(taskInfo);
|
|
|
|
taskManager.rescheduleTask(taskInfo.getName());
|
|
|
|
taskManager.rescheduleTask(taskInfo.getName());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
throw new DeviceStatusTaskException(
|
|
|
|
throw new DeviceStatusTaskException(
|
|
|
|
"Device details retrieving Device Status monitoring task has not been started for this tenant " +
|
|
|
|
"Device details retrieving Device Status monitoring task has not been started for this device-type " +
|
|
|
|
tenantId + ". Please start the task first.");
|
|
|
|
deviceType.getName() + ". Please start the task first.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (TaskException e) {
|
|
|
|
} catch (TaskException e) {
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while updating the Device Status monitoring task for tenant " + tenantId,
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while updating the Device Status monitoring " +
|
|
|
|
e);
|
|
|
|
"task for device-type : " + deviceType.getName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean isTaskScheduled(String deviceType) throws DeviceStatusTaskException {
|
|
|
|
public boolean isTaskScheduled(DeviceType deviceType) throws DeviceStatusTaskException {
|
|
|
|
int tenantId = getTenantId();
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId();
|
|
|
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType + "_" + String.valueOf(tenantId);
|
|
|
|
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
|
|
|
TaskManager taskManager;
|
|
|
|
TaskManager taskManager;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
|
|
|
return taskManager.isTaskScheduled(taskName);
|
|
|
|
return taskManager.isTaskScheduled(taskName);
|
|
|
|
} catch (TaskException e) {
|
|
|
|
} catch (TaskException e) {
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while checking Device Status monitoring task for tenant " +
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while checking Device Status monitoring task for device-type : " +
|
|
|
|
tenantId, e);
|
|
|
|
deviceType.getName(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int getTenantId() {
|
|
|
|
|
|
|
|
return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|