forked from community/device-mgt-core
commit
0bbd6e3f52
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.extensions.device.type.deployer.config;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "DeviceStatusTaskConfig")
|
||||||
|
public class DeviceStatusTaskConfiguration {
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
private int frequency;
|
||||||
|
private int idleTimeToMarkInactive;
|
||||||
|
private int idleTimeToMarkUnreachable;
|
||||||
|
|
||||||
|
@XmlElement(name = "RequireStatusMonitoring", required = true)
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Frequency", required = true)
|
||||||
|
public int getFrequency() {
|
||||||
|
return frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrequency(int frequency) {
|
||||||
|
this.frequency = frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "IdleTimeToMarkInactive", required = true)
|
||||||
|
public int getIdleTimeToMarkInactive() {
|
||||||
|
return idleTimeToMarkInactive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleTimeToMarkInactive(int idleTimeToMarkInactive) {
|
||||||
|
this.idleTimeToMarkInactive = idleTimeToMarkInactive;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "IdleTimeToMarkUnreachable", required = true)
|
||||||
|
public int getIdleTimeToMarkUnreachable() {
|
||||||
|
return idleTimeToMarkUnreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleTimeToMarkUnreachable(int idleTimeToMarkUnreachable) {
|
||||||
|
this.idleTimeToMarkUnreachable = idleTimeToMarkUnreachable;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.common;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds plugin specific configurations for Device Status Monitoring Task.
|
||||||
|
*/
|
||||||
|
public class DeviceStatusTaskPluginConfig {
|
||||||
|
|
||||||
|
private boolean requireStatusMonitoring;
|
||||||
|
private int frequency;
|
||||||
|
private int idleTimeToMarkInactive;
|
||||||
|
private int idleTimeToMarkUnreachable;
|
||||||
|
|
||||||
|
public int getFrequency() {
|
||||||
|
return frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFrequency(int frequency) {
|
||||||
|
this.frequency = frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdleTimeToMarkInactive() {
|
||||||
|
return idleTimeToMarkInactive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleTimeToMarkInactive(int idleTimeToMarkInactive) {
|
||||||
|
this.idleTimeToMarkInactive = idleTimeToMarkInactive;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIdleTimeToMarkUnreachable() {
|
||||||
|
return idleTimeToMarkUnreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleTimeToMarkUnreachable(int idleTimeToMarkUnreachable) {
|
||||||
|
this.idleTimeToMarkUnreachable = idleTimeToMarkUnreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isRequireStatusMonitoring() {
|
||||||
|
return requireStatusMonitoring;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequireStatusMonitoring(boolean requireStatusMonitoring) {
|
||||||
|
this.requireStatusMonitoring = requireStatusMonitoring;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.config.status.task;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "DeviceStatusTaskConfig")
|
||||||
|
public class DeviceStatusTaskConfig {
|
||||||
|
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
|
@XmlElement(name = "Enable", required = true)
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.operation.mgt;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This holds results of Pending-operations to enrolment mappings.
|
||||||
|
*/
|
||||||
|
public class OperationEnrolmentMapping {
|
||||||
|
|
||||||
|
int enrolmentId;
|
||||||
|
int tenantId;
|
||||||
|
long createdTime;
|
||||||
|
String deviceType;
|
||||||
|
String deviceId;
|
||||||
|
EnrolmentInfo.Status deviceStatus;
|
||||||
|
|
||||||
|
public int getTenantId() {
|
||||||
|
return tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantId(int tenantId) {
|
||||||
|
this.tenantId = tenantId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getEnrolmentId() {
|
||||||
|
return enrolmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnrolmentId(int enrolmentId) {
|
||||||
|
this.enrolmentId = enrolmentId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceId() {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(String deviceId) {
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreatedTime() {
|
||||||
|
return createdTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedTime(long createdTime) {
|
||||||
|
this.createdTime = createdTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnrolmentInfo.Status getDeviceStatus() {
|
||||||
|
return deviceStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceStatus(String deviceStatus) {
|
||||||
|
this.deviceStatus = EnrolmentInfo.Status.valueOf(deviceStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceType() {
|
||||||
|
return deviceType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceType(String deviceType) {
|
||||||
|
this.deviceType = deviceType;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.status.task;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This exception class defines the custom exceptions thrown by the DeviceStatusMonitoringTask related components.
|
||||||
|
*/
|
||||||
|
public class DeviceStatusTaskException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -31222242646464497L;
|
||||||
|
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceStatusTaskException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceStatusTaskException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceStatusTaskException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceStatusTaskException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceStatusTaskException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.status.task;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface defines the methods that should be implemented by the management service of
|
||||||
|
* DeviceStatusMonitoringTask.
|
||||||
|
*/
|
||||||
|
public interface DeviceStatusTaskManagerService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will start the task.
|
||||||
|
*
|
||||||
|
* @param deviceType - DeviceType
|
||||||
|
* @param deviceStatusTaskConfig - DeviceStatusTaskConfig
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
void startTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will stop the task.
|
||||||
|
*
|
||||||
|
* @param deviceType - DeviceType
|
||||||
|
* @param deviceStatusTaskConfig - DeviceStatusTaskConfig
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
void stopTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will update the task frequency which it runs.
|
||||||
|
*
|
||||||
|
* @param deviceType
|
||||||
|
* @param deviceStatusTaskConfig - DeviceStatusTaskConfig
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
void updateTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will check weather the task is scheduled.
|
||||||
|
* @param deviceType - Device Type
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
boolean isTaskScheduled(DeviceType deviceType) throws DeviceStatusTaskException;
|
||||||
|
}
|
@ -0,0 +1,186 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.status.task.impl;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
||||||
|
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationEnrolmentMapping;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskException;
|
||||||
|
import org.wso2.carbon.ntask.core.Task;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implements the Task service which monitors the device activity periodically & update the device-status if
|
||||||
|
* necessary.
|
||||||
|
*/
|
||||||
|
public class DeviceStatusMonitoringTask implements Task {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DeviceStatusMonitoringTask.class);
|
||||||
|
private String deviceType;
|
||||||
|
private DeviceStatusTaskPluginConfig deviceStatusTaskPluginConfig;
|
||||||
|
private int deviceTypeId = -1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setProperties(Map<String, String> properties) {
|
||||||
|
deviceType = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE);
|
||||||
|
deviceTypeId = Integer.parseInt(properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE_ID));
|
||||||
|
String deviceStatusTaskConfigStr = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_STATUS_TASK_CONFIG);
|
||||||
|
Gson gson = new Gson();
|
||||||
|
deviceStatusTaskPluginConfig = gson.fromJson(deviceStatusTaskConfigStr, DeviceStatusTaskPluginConfig.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
List<OperationEnrolmentMapping> operationEnrolmentMappings = null;
|
||||||
|
List<EnrolmentInfo> enrolmentInfoTobeUpdated = new ArrayList<>();
|
||||||
|
Map<Integer, Long> lastActivities = null;
|
||||||
|
EnrolmentInfo enrolmentInfo;
|
||||||
|
try {
|
||||||
|
operationEnrolmentMappings = this.getOperationEnrolmentMappings();
|
||||||
|
if (operationEnrolmentMappings != null && operationEnrolmentMappings.size() > 0) {
|
||||||
|
lastActivities = this.getLastDeviceActivities();
|
||||||
|
}
|
||||||
|
} catch (DeviceStatusTaskException e) {
|
||||||
|
log.error("Error occurred while fetching OperationEnrolment mappings of deviceType '" + deviceType + "'", e);
|
||||||
|
}
|
||||||
|
for (OperationEnrolmentMapping mapping:operationEnrolmentMappings) {
|
||||||
|
long lastActivity = -1;
|
||||||
|
if (lastActivities != null && lastActivities.containsKey(mapping.getEnrolmentId())) {
|
||||||
|
lastActivity = lastActivities.get(mapping.getEnrolmentId());
|
||||||
|
}
|
||||||
|
EnrolmentInfo.Status newStatus = this.determineDeviceStatus(mapping, lastActivity);
|
||||||
|
if (newStatus != mapping.getDeviceStatus()) {
|
||||||
|
enrolmentInfo = new EnrolmentInfo();
|
||||||
|
enrolmentInfo.setId(mapping.getEnrolmentId());
|
||||||
|
enrolmentInfo.setStatus(newStatus);
|
||||||
|
enrolmentInfoTobeUpdated.add(enrolmentInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (enrolmentInfoTobeUpdated.size() > 0) {
|
||||||
|
try {
|
||||||
|
this.updateDeviceStatus(enrolmentInfoTobeUpdated);
|
||||||
|
} catch (DeviceStatusTaskException e) {
|
||||||
|
log.error("Error occurred while updating non-responsive device-status of devices of type '" + deviceType + "'",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private EnrolmentInfo.Status determineDeviceStatus(OperationEnrolmentMapping opMapping, long lastActivityTime) {
|
||||||
|
long lastPendingOpBefore = (System.currentTimeMillis()/1000) - opMapping.getCreatedTime();
|
||||||
|
EnrolmentInfo.Status newStatus = null;
|
||||||
|
if (lastPendingOpBefore >= this.deviceStatusTaskPluginConfig.getIdleTimeToMarkInactive()) {
|
||||||
|
newStatus = EnrolmentInfo.Status.INACTIVE;
|
||||||
|
} else if (lastPendingOpBefore >= this.deviceStatusTaskPluginConfig.getIdleTimeToMarkUnreachable()) {
|
||||||
|
newStatus = EnrolmentInfo.Status.UNREACHABLE;
|
||||||
|
}
|
||||||
|
if (lastActivityTime != -1) {
|
||||||
|
long lastActivityBefore = (System.currentTimeMillis()/1000) - lastActivityTime;
|
||||||
|
if (lastActivityBefore < lastPendingOpBefore) {
|
||||||
|
return opMapping.getDeviceStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getMinTimeWindow() {
|
||||||
|
return (System.currentTimeMillis()/1000) - this.deviceStatusTaskPluginConfig.getIdleTimeToMarkUnreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getMaxTimeWindow() {
|
||||||
|
//Need to consider the frequency of the task as well
|
||||||
|
return (System.currentTimeMillis()/1000) - this.deviceStatusTaskPluginConfig.getIdleTimeToMarkInactive() -
|
||||||
|
this.deviceStatusTaskPluginConfig.getFrequency();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean updateDeviceStatus(List<EnrolmentInfo> enrolmentInfos) throws
|
||||||
|
DeviceStatusTaskException {
|
||||||
|
boolean updateStatus;
|
||||||
|
try {
|
||||||
|
DeviceManagementDAOFactory.beginTransaction();
|
||||||
|
updateStatus = DeviceManagementDAOFactory.getEnrollmentDAO().updateEnrollmentStatus(enrolmentInfos);
|
||||||
|
DeviceManagementDAOFactory.commitTransaction();
|
||||||
|
} catch (DeviceManagementDAOException e) {
|
||||||
|
DeviceManagementDAOFactory.rollbackTransaction();
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while updating enrollment status of devices of type '"
|
||||||
|
+ deviceType + "'", e);
|
||||||
|
} catch (TransactionManagementException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while initiating a transaction for updating the device " +
|
||||||
|
"status of type '" + deviceType +"'", e);
|
||||||
|
} finally {
|
||||||
|
DeviceManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
return updateStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<OperationEnrolmentMapping> getOperationEnrolmentMappings() throws DeviceStatusTaskException {
|
||||||
|
List<OperationEnrolmentMapping> operationEnrolmentMappings = null;
|
||||||
|
try {
|
||||||
|
OperationManagementDAOFactory.openConnection();
|
||||||
|
operationEnrolmentMappings = OperationManagementDAOFactory.
|
||||||
|
getOperationMappingDAO().getFirstPendingOperationMappingsForActiveEnrolments(this.getMinTimeWindow(),
|
||||||
|
this.getMaxTimeWindow(), this.deviceTypeId);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while getting Enrolment operation mappings for " +
|
||||||
|
"determining device status of deviceType '" + deviceType + "'", e);
|
||||||
|
} catch (OperationManagementDAOException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred obtaining a DB connection for fetching " +
|
||||||
|
"operation-enrolment mappings for status monitoring of deviceType '" + deviceType + "'", e);
|
||||||
|
} finally {
|
||||||
|
OperationManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
return operationEnrolmentMappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Integer, Long> getLastDeviceActivities() throws DeviceStatusTaskException {
|
||||||
|
Map<Integer, Long> lastActivities = null;
|
||||||
|
try {
|
||||||
|
OperationManagementDAOFactory.openConnection();
|
||||||
|
lastActivities = OperationManagementDAOFactory.
|
||||||
|
getOperationMappingDAO().getLastConnectedTimeForActiveEnrolments(this.getMaxTimeWindow(),
|
||||||
|
this.deviceTypeId);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while getting last activities for " +
|
||||||
|
"determining device status of deviceType '" + deviceType + "'", e);
|
||||||
|
} catch (OperationManagementDAOException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred obtaining a DB connection for fetching " +
|
||||||
|
"last activities for status monitoring of deviceType '" + deviceType + "'", e);
|
||||||
|
} finally {
|
||||||
|
OperationManagementDAOFactory.closeConnection();
|
||||||
|
}
|
||||||
|
return lastActivities;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.core.status.task.impl;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
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.status.task.DeviceStatusTaskException;
|
||||||
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskManagerService;
|
||||||
|
import org.wso2.carbon.ntask.common.TaskException;
|
||||||
|
import org.wso2.carbon.ntask.core.TaskInfo;
|
||||||
|
import org.wso2.carbon.ntask.core.TaskManager;
|
||||||
|
import org.wso2.carbon.ntask.core.service.TaskService;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation of DeviceStatusTaskManagerService.
|
||||||
|
*/
|
||||||
|
public class DeviceStatusTaskManagerServiceImpl implements DeviceStatusTaskManagerService {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DeviceStatusTaskManagerServiceImpl.class);
|
||||||
|
|
||||||
|
public static final String DEVICE_STATUS_MONITORING_TASK_TYPE = "DEVICE_STATUS_MONITORING";
|
||||||
|
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";
|
||||||
|
private static final String TASK_CLASS = DeviceStatusMonitoringTask.class.getName();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException {
|
||||||
|
log.info("Device Status monitoring Task adding for " + deviceType.getName());
|
||||||
|
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
taskService.registerTaskType(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device Status monitoring task is started for the device type " + deviceType.getName());
|
||||||
|
log.debug(
|
||||||
|
"Device Status monitoring task is at frequency of : " + deviceStatusTaskConfig.getFrequency());
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
|
||||||
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
||||||
|
//Convert to milli seconds
|
||||||
|
triggerInfo.setIntervalMillis(deviceStatusTaskConfig.getFrequency()*1000);
|
||||||
|
triggerInfo.setRepeatCount(-1);
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String deviceStatusTaskConfigs = gson.toJson(deviceStatusTaskConfig);
|
||||||
|
|
||||||
|
Map<String, String> properties = new HashMap<>();
|
||||||
|
|
||||||
|
properties.put(DEVICE_TYPE, deviceType.getName());
|
||||||
|
properties.put(DEVICE_TYPE_ID, deviceType.getId() + "");
|
||||||
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
||||||
|
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId();
|
||||||
|
|
||||||
|
if (!taskManager.isTaskScheduled(taskName)) {
|
||||||
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
||||||
|
taskManager.registerTask(taskInfo);
|
||||||
|
taskManager.rescheduleTask(taskInfo.getName());
|
||||||
|
} else {
|
||||||
|
throw new DeviceStatusTaskException(
|
||||||
|
"Device Status monitoring task is already started for this device-type : " + deviceType.getName());
|
||||||
|
}
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while creating the Device Status monitoring task " +
|
||||||
|
"for device-type : " + deviceType.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stopTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException {
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId();
|
||||||
|
if (taskService.isServerInit()) {
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
taskManager.deleteTask(taskName);
|
||||||
|
}
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while deleting the Device Status monitoring task " +
|
||||||
|
"for device-type : " + deviceType.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTask(DeviceType deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException {
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType + "_" + deviceType.getId();
|
||||||
|
if (taskManager.isTaskScheduled(taskName)) {
|
||||||
|
taskManager.deleteTask(taskName);
|
||||||
|
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
|
||||||
|
triggerInfo.setIntervalMillis(deviceStatusTaskConfig.getFrequency());
|
||||||
|
triggerInfo.setRepeatCount(-1);
|
||||||
|
|
||||||
|
Map<String, String> properties = new HashMap<>();
|
||||||
|
properties.put(DEVICE_TYPE, deviceType.getName());
|
||||||
|
properties.put(DEVICE_TYPE_ID, deviceType.getId() + "");
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String deviceStatusTaskConfigs = gson.toJson(deviceStatusTaskConfig);
|
||||||
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
||||||
|
|
||||||
|
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
|
||||||
|
|
||||||
|
taskManager.registerTask(taskInfo);
|
||||||
|
taskManager.rescheduleTask(taskInfo.getName());
|
||||||
|
} else {
|
||||||
|
throw new DeviceStatusTaskException(
|
||||||
|
"Device details retrieving Device Status monitoring task has not been started for this device-type " +
|
||||||
|
deviceType.getName() + ". Please start the task first.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while updating the Device Status monitoring " +
|
||||||
|
"task for device-type : " + deviceType.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTaskScheduled(DeviceType deviceType) throws DeviceStatusTaskException {
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId();
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
TaskManager taskManager;
|
||||||
|
try {
|
||||||
|
taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
return taskManager.isTaskScheduled(taskName);
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while checking Device Status monitoring task for device-type : " +
|
||||||
|
deviceType.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue