|
|
|
@ -1,11 +1,13 @@
|
|
|
|
// DeviceTask.java - 原有设备任务
|
|
|
|
// DeviceTask.java - 原有设备任务
|
|
|
|
package cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.coretask;
|
|
|
|
package cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.coretask;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.DeviceConnectionStatusEnum;
|
|
|
|
import cn.iocoder.yudao.framework.common.util.opc.OpcUtils;
|
|
|
|
import cn.iocoder.yudao.framework.common.util.opc.OpcUtils;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceBasicStatusEnum;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceBasicStatusEnum;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceStatusEnum;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceStatusEnum;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.TaskTypeEnum;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.enums.TaskTypeEnum;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.core.Task;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.core.Task;
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.scheduler.TaskSchedulerManager;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSaveReqVO;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.DeviceSaveReqVO;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.devicemodelrules.vo.PointRulesRespVO;
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.devicemodelrules.vo.PointRulesRespVO;
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
|
|
|
@ -28,6 +30,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
@ -51,6 +54,7 @@ public class DeviceTask implements Task {
|
|
|
|
private TDengineService tDengineService;
|
|
|
|
private TDengineService tDengineService;
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
@Lazy
|
|
|
|
private DeviceService deviceService;
|
|
|
|
private DeviceService deviceService;
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
@ -62,6 +66,10 @@ public class DeviceTask implements Task {
|
|
|
|
@Resource
|
|
|
|
@Resource
|
|
|
|
private DeviceWarinningRecordMapper deviceWarinningRecordMapper;
|
|
|
|
private DeviceWarinningRecordMapper deviceWarinningRecordMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
|
|
|
@Lazy
|
|
|
|
|
|
|
|
private TaskSchedulerManager taskSchedulerManager;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String getTaskType() {
|
|
|
|
public String getTaskType() {
|
|
|
|
return TaskTypeEnum.DEVICE.getCode();
|
|
|
|
return TaskTypeEnum.DEVICE.getCode();
|
|
|
|
@ -171,29 +179,72 @@ public class DeviceTask implements Task {
|
|
|
|
String username = StringUtils.defaultString(device.getUsername());
|
|
|
|
String username = StringUtils.defaultString(device.getUsername());
|
|
|
|
String password = StringUtils.defaultString(device.getPassword());
|
|
|
|
String password = StringUtils.defaultString(device.getPassword());
|
|
|
|
|
|
|
|
|
|
|
|
boolean connected = OpcUtils.connect(device.getUrl(), username, password, 10);
|
|
|
|
boolean connected = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
connected = OpcUtils.connect(device.getUrl(), username, password, 10);
|
|
|
|
if (!connected) {
|
|
|
|
if (!connected) {
|
|
|
|
//停止任务
|
|
|
|
|
|
|
|
deviceService.connectDevice(new DeviceSaveReqVO().setId(device.getId()).setIsConnect(2));
|
|
|
|
log.error("设备 {} 连接OPC服务器失败,URL: {}", device.getId(), device.getUrl());
|
|
|
|
throw new RuntimeException("连接OPC服务器失败,URL: " + device.getUrl());
|
|
|
|
device.setStatus(String.valueOf(DeviceConnectionStatusEnum.DISCONNECTED.getStatus()));
|
|
|
|
|
|
|
|
deviceMapper.updateById(device);
|
|
|
|
|
|
|
|
taskSchedulerManager.stopDeviceTask(device.getId());
|
|
|
|
|
|
|
|
DeviceOperationRecordDO deviceOperationRecordDO = new DeviceOperationRecordDO();
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setDeviceId(device.getId());
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setRule(DeviceStatusEnum.OFFLINE.getCode());
|
|
|
|
|
|
|
|
//TODO 默认内置管理员
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setCreator("1");
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setUpdater("1");
|
|
|
|
|
|
|
|
deviceOperationRecordMapper.insert(deviceOperationRecordDO);
|
|
|
|
|
|
|
|
//抛出异常终止任务
|
|
|
|
|
|
|
|
throw new RuntimeException("连接opcuv服务器异常");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("设备 {} 成功连接OPC服务器,URL: {}", device.getId(), device.getUrl());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
log.error("设备 {} 连接OPC服务器异常,URL: {}", device.getId(), device.getUrl(), e);
|
|
|
|
|
|
|
|
device.setStatus(String.valueOf(DeviceConnectionStatusEnum.DISCONNECTED.getStatus()));
|
|
|
|
|
|
|
|
deviceMapper.updateById(device);
|
|
|
|
|
|
|
|
taskSchedulerManager.stopDeviceTask(device.getId());
|
|
|
|
|
|
|
|
DeviceOperationRecordDO deviceOperationRecordDO = new DeviceOperationRecordDO();
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setDeviceId(device.getId());
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setRule(DeviceStatusEnum.OFFLINE.getCode());
|
|
|
|
|
|
|
|
//TODO 默认内置管理员
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setCreator("1");
|
|
|
|
|
|
|
|
deviceOperationRecordDO.setUpdater("1");
|
|
|
|
|
|
|
|
deviceOperationRecordMapper.insert(deviceOperationRecordDO);
|
|
|
|
|
|
|
|
//抛出异常终止任务
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 处理设备数据
|
|
|
|
* 处理设备数据
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private void processDeviceData(Long deviceId, DeviceDO device) {
|
|
|
|
private void processDeviceData(Long deviceId, DeviceDO device) {
|
|
|
|
|
|
|
|
DeviceDO deviceDO = deviceMapper.selectById(deviceId);
|
|
|
|
// 1. 查询点位配置
|
|
|
|
// 1. 查询点位配置
|
|
|
|
List<DeviceContactModelDO> points = getDevicePoints(deviceId);
|
|
|
|
List<DeviceContactModelDO> points = getDevicePoints(deviceId);
|
|
|
|
if (CollectionUtils.isEmpty(points)) {
|
|
|
|
if (CollectionUtils.isEmpty(points)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.warn("设备 {} 未配置点位", deviceId);
|
|
|
|
logger.warn("设备 {} 未配置点位", deviceId);
|
|
|
|
//更新状态为待机中
|
|
|
|
//更新状态为待机中
|
|
|
|
DeviceOperationRecordDO record = new DeviceOperationRecordDO();
|
|
|
|
DeviceOperationRecordDO record = new DeviceOperationRecordDO();
|
|
|
|
record.setDeviceId(device.getId());
|
|
|
|
record.setDeviceId(device.getId());
|
|
|
|
record.setRule(DeviceStatusEnum.STANDBY.getCode());
|
|
|
|
record.setRule(DeviceStatusEnum.STANDBY.getCode());
|
|
|
|
|
|
|
|
record.setTotalStandbyTime(deviceDO.getSampleCycle());
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
record.setCreator("1");
|
|
|
|
record.setCreator("1");
|
|
|
|
record.setUpdater("1");
|
|
|
|
record.setUpdater("1");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deviceOperationRecordMapper.insert(record);
|
|
|
|
deviceOperationRecordMapper.insert(record);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -209,6 +260,7 @@ public class DeviceTask implements Task {
|
|
|
|
DeviceOperationRecordDO record = new DeviceOperationRecordDO();
|
|
|
|
DeviceOperationRecordDO record = new DeviceOperationRecordDO();
|
|
|
|
record.setDeviceId(device.getId());
|
|
|
|
record.setDeviceId(device.getId());
|
|
|
|
record.setRule(DeviceStatusEnum.STANDBY.getCode());
|
|
|
|
record.setRule(DeviceStatusEnum.STANDBY.getCode());
|
|
|
|
|
|
|
|
record.setTotalStandbyTime(deviceDO.getSampleCycle());
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
record.setCreator("1");
|
|
|
|
record.setCreator("1");
|
|
|
|
record.setUpdater("1");
|
|
|
|
record.setUpdater("1");
|
|
|
|
|