|
|
|
|
@ -1,11 +1,14 @@
|
|
|
|
|
// DeviceTask.java - 原有设备任务
|
|
|
|
|
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.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.TaskTypeEnum;
|
|
|
|
|
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.devicemodelrules.vo.PointRulesRespVO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.DeviceDO;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.dataobject.devicecontactmodel.DeviceContactModelDO;
|
|
|
|
|
@ -17,6 +20,7 @@ import cn.iocoder.yudao.module.iot.dal.mysql.devicecontactmodel.DeviceContactMod
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.mysql.deviceoperationrecord.DeviceOperationRecordMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.mysql.devicepointrules.DevicePointRulesMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.dal.mysql.devicewarinningrecord.DeviceWarinningRecordMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.device.DeviceService;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.service.device.TDengineService;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
@ -26,6 +30,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
@ -48,6 +53,10 @@ public class DeviceTask implements Task {
|
|
|
|
|
@Resource
|
|
|
|
|
private TDengineService tDengineService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
@Lazy
|
|
|
|
|
private DeviceService deviceService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private DevicePointRulesMapper devicePointRulesMapper;
|
|
|
|
|
|
|
|
|
|
@ -57,6 +66,10 @@ public class DeviceTask implements Task {
|
|
|
|
|
@Resource
|
|
|
|
|
private DeviceWarinningRecordMapper deviceWarinningRecordMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
@Lazy
|
|
|
|
|
private TaskSchedulerManager taskSchedulerManager;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getTaskType() {
|
|
|
|
|
return TaskTypeEnum.DEVICE.getCode();
|
|
|
|
|
@ -103,13 +116,6 @@ public class DeviceTask implements Task {
|
|
|
|
|
logger.info("执行设备任务,任务ID: {}, 参数: {}, 时间: {}",
|
|
|
|
|
taskId, taskParam, currentTime);
|
|
|
|
|
|
|
|
|
|
// 解析参数,假设格式为 deviceId:deviceCode
|
|
|
|
|
// String[] params = taskParam.split(":");
|
|
|
|
|
// if (params.length >= 2) {
|
|
|
|
|
// Long deviceId = Long.parseLong(params[0]);
|
|
|
|
|
// String deviceCode = params[1];
|
|
|
|
|
// executeDeviceLogic(deviceId, deviceCode);
|
|
|
|
|
// }
|
|
|
|
|
executeDeviceLogic(taskId,taskParam);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -148,32 +154,6 @@ public class DeviceTask implements Task {
|
|
|
|
|
OpcUtils.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 具体的设备执行逻辑
|
|
|
|
|
*/
|
|
|
|
|
// private void executeDeviceLogic(Long sourceDeviceId, String param) {
|
|
|
|
|
// logger.info("执行设备逻辑,源设备ID: {},参数: {}", sourceDeviceId, param);
|
|
|
|
|
//
|
|
|
|
|
// // 1. 计算实际设备ID
|
|
|
|
|
// Long deviceId = sourceDeviceId - 1000000L;
|
|
|
|
|
// logger.info("处理后设备ID: {}", deviceId);
|
|
|
|
|
//
|
|
|
|
|
// if (deviceId == null) {
|
|
|
|
|
// throw new RuntimeException("设备ID不能为空");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // 2. 获取设备信息
|
|
|
|
|
// DeviceDO device = getDeviceInfo(deviceId);
|
|
|
|
|
//
|
|
|
|
|
// // 3. 连接OPC服务器
|
|
|
|
|
// connectOpcServer(device);
|
|
|
|
|
//
|
|
|
|
|
// // 4. 处理数据读取和入库
|
|
|
|
|
// processDeviceData(deviceId, device);
|
|
|
|
|
//
|
|
|
|
|
// // 5. 断开连接
|
|
|
|
|
// OpcUtils.disconnect();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取设备信息
|
|
|
|
|
@ -199,25 +179,95 @@ public class DeviceTask implements Task {
|
|
|
|
|
String username = StringUtils.defaultString(device.getUsername());
|
|
|
|
|
String password = StringUtils.defaultString(device.getPassword());
|
|
|
|
|
|
|
|
|
|
boolean connected = OpcUtils.connect(device.getUrl(), username, password, 10);
|
|
|
|
|
if (!connected) {
|
|
|
|
|
throw new RuntimeException("连接OPC服务器失败,URL: " + device.getUrl());
|
|
|
|
|
boolean connected = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
connected = OpcUtils.connect(device.getUrl(), username, password, 10);
|
|
|
|
|
if (!connected) {
|
|
|
|
|
|
|
|
|
|
log.error("设备 {} 连接OPC服务器失败,URL: {}", device.getId(), 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) {
|
|
|
|
|
DeviceDO deviceDO = deviceMapper.selectById(deviceId);
|
|
|
|
|
// 1. 查询点位配置
|
|
|
|
|
List<DeviceContactModelDO> points = getDevicePoints(deviceId);
|
|
|
|
|
if (CollectionUtils.isEmpty(points)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.warn("设备 {} 未配置点位", deviceId);
|
|
|
|
|
//更新状态为待机中
|
|
|
|
|
DeviceOperationRecordDO record = new DeviceOperationRecordDO();
|
|
|
|
|
record.setDeviceId(device.getId());
|
|
|
|
|
record.setRule(DeviceStatusEnum.STANDBY.getCode());
|
|
|
|
|
record.setTotalStandbyTime(deviceDO.getSampleCycle());
|
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
|
record.setCreator("1");
|
|
|
|
|
record.setUpdater("1");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deviceOperationRecordMapper.insert(record);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("设备 {} 需要读取 {} 个点位", deviceId, points.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DevicePointRulesDO devicePointRulesDO = devicePointRulesMapper.selectOne(Wrappers.<DevicePointRulesDO>lambdaQuery()
|
|
|
|
|
.eq(DevicePointRulesDO::getDeviceId, deviceId)
|
|
|
|
|
.eq(DevicePointRulesDO::getIdentifier, DeviceBasicStatusEnum.RUNNING));
|
|
|
|
|
if(devicePointRulesDO !=null && devicePointRulesDO.getFieldRule() == null ){
|
|
|
|
|
//更新状态为待机中
|
|
|
|
|
DeviceOperationRecordDO record = new DeviceOperationRecordDO();
|
|
|
|
|
record.setDeviceId(device.getId());
|
|
|
|
|
record.setRule(DeviceStatusEnum.STANDBY.getCode());
|
|
|
|
|
record.setTotalStandbyTime(deviceDO.getSampleCycle());
|
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
|
record.setCreator("1");
|
|
|
|
|
record.setUpdater("1");
|
|
|
|
|
deviceOperationRecordMapper.insert(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 读取并处理数据
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
List<DeviceContactModelDO> validDataList = new ArrayList<>();
|
|
|
|
|
@ -333,9 +383,9 @@ public class DeviceTask implements Task {
|
|
|
|
|
|
|
|
|
|
// 2. 遍历规则
|
|
|
|
|
for (DevicePointRulesDO devicePointRulesDO : devicePointRulesDOList) {
|
|
|
|
|
if (StringUtils.isBlank(devicePointRulesDO.getFieldRule())) {
|
|
|
|
|
if (StringUtils.isBlank(devicePointRulesDO.getFieldRule())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. 解析规则列表
|
|
|
|
|
List<PointRulesRespVO> pointRulesVOList = JSON.parseArray(
|
|
|
|
|
|