forked from community/device-mgt-core
Resolves harshanl/product-iots#518
parent
7e83b53bb4
commit
35d1c9897e
@ -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 deviceId;
|
||||||
|
int tenantId;
|
||||||
|
long createdTime;
|
||||||
|
String owner;
|
||||||
|
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 int getDeviceId() {
|
||||||
|
return deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceId(int deviceId) {
|
||||||
|
this.deviceId = deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getCreatedTime() {
|
||||||
|
return createdTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedTime(long createdTime) {
|
||||||
|
this.createdTime = createdTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwner(String owner) {
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnrolmentInfo.Status getDeviceStatus() {
|
||||||
|
return deviceStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceStatus(String deviceStatus) {
|
||||||
|
this.deviceStatus = EnrolmentInfo.Status.valueOf(deviceStatus);
|
||||||
|
}
|
||||||
|
}
|
@ -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,60 @@
|
|||||||
|
/*
|
||||||
|
* 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.config.status.task.DeviceStatusTaskConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 deviceStatusTaskConfig - DeviceStatusTaskConfig
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
void startTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will stop the task.
|
||||||
|
* @param deviceStatusTaskConfig - DeviceStatusTaskConfig
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
void stopTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will update the task frequency which it runs.
|
||||||
|
* @param deviceStatusTaskConfig - DeviceStatusTaskConfig
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
void updateTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will check weather the task is scheduled.
|
||||||
|
* @param deviceType - Device Type
|
||||||
|
* @throws DeviceStatusTaskException
|
||||||
|
*/
|
||||||
|
boolean isTaskScheduled(String deviceType) throws DeviceStatusTaskException;
|
||||||
|
}
|
@ -0,0 +1,148 @@
|
|||||||
|
/*
|
||||||
|
* 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.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.core.encryption.SymmetricEncryption;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.status.task.DeviceStatusTaskConfig;
|
||||||
|
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.Date;
|
||||||
|
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 Log log = LogFactory.getLog(DeviceStatusMonitoringTask.class);
|
||||||
|
private String deviceType;
|
||||||
|
private DeviceStatusTaskPluginConfig deviceStatusTaskPluginConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setProperties(Map<String, String> properties) {
|
||||||
|
deviceType = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE);
|
||||||
|
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<>();
|
||||||
|
EnrolmentInfo enrolmentInfo;
|
||||||
|
try {
|
||||||
|
operationEnrolmentMappings = this.getOperationEnrolmentMappings();
|
||||||
|
} catch (DeviceStatusTaskException e) {
|
||||||
|
log.error("Error occurred while fetching OperationEnrolment mappings of deviceType '" + deviceType + "'", e);
|
||||||
|
}
|
||||||
|
for (OperationEnrolmentMapping mapping:operationEnrolmentMappings) {
|
||||||
|
EnrolmentInfo.Status newStatus = this.determineDeviceStatus(mapping);
|
||||||
|
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 lastContactedBefore = (System.currentTimeMillis()/1000) - opMapping.getCreatedTime();
|
||||||
|
EnrolmentInfo.Status status = opMapping.getDeviceStatus();
|
||||||
|
if (lastContactedBefore >= this.deviceStatusTaskPluginConfig.getIdleTimeToMarkInactive()) {
|
||||||
|
status = EnrolmentInfo.Status.INACTIVE;
|
||||||
|
} else if (lastContactedBefore >= this.deviceStatusTaskPluginConfig.getIdleTimeToMarkUnreachable()) {
|
||||||
|
status = EnrolmentInfo.Status.UNREACHABLE;
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getTimeWindow() {
|
||||||
|
return (System.currentTimeMillis()/1000) - this.deviceStatusTaskPluginConfig.getIdleTimeToMarkInactive();
|
||||||
|
}
|
||||||
|
|
||||||
|
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.getTimeWindow());
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,165 @@
|
|||||||
|
/*
|
||||||
|
* 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.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
||||||
|
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 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_STATUS_TASK_CONFIG = "DEVICE_STATUS_TASK_CONFIG";
|
||||||
|
private static String TASK_CLASS = DeviceStatusMonitoringTask.class.getName();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException {
|
||||||
|
log.info("Device Status monitoring Task adding for " + deviceType);
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
||||||
|
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType;
|
||||||
|
|
||||||
|
if (!taskManager.isTaskScheduled(deviceType)) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while creating the Device Status monitoring task " +
|
||||||
|
"for device-type : " + deviceType, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stopTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException {
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
if (taskService.isServerInit()) {
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
taskManager.deleteTask(deviceType);
|
||||||
|
}
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while deleting the Device Status monitoring task " +
|
||||||
|
"for tenant " + getTenantId(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTask(String deviceType, DeviceStatusTaskPluginConfig deviceStatusTaskConfig)
|
||||||
|
throws DeviceStatusTaskException {
|
||||||
|
int tenantId = getTenantId();
|
||||||
|
try {
|
||||||
|
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
|
||||||
|
TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE);
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType + "_" + String.valueOf(tenantId);
|
||||||
|
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);
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String deviceStatusTaskConfigs = gson.toJson(deviceStatusTaskConfig);
|
||||||
|
properties.put(DEVICE_STATUS_TASK_CONFIG, deviceStatusTaskConfigs);
|
||||||
|
|
||||||
|
TaskInfo taskInfo = new TaskInfo(deviceType, 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 tenant " +
|
||||||
|
tenantId + ". Please start the task first.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (TaskException e) {
|
||||||
|
throw new DeviceStatusTaskException("Error occurred while updating the Device Status monitoring task for tenant " + tenantId,
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTaskScheduled(String deviceType) throws DeviceStatusTaskException {
|
||||||
|
int tenantId = getTenantId();
|
||||||
|
String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType + "_" + String.valueOf(tenantId);
|
||||||
|
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 tenant " +
|
||||||
|
tenantId, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getTenantId() {
|
||||||
|
return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue