|
|
|
|
@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.iot.controller.admin.device.enums.DeviceBasicStat
|
|
|
|
|
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.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 +18,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;
|
|
|
|
|
@ -48,6 +50,9 @@ public class DeviceTask implements Task {
|
|
|
|
|
@Resource
|
|
|
|
|
private TDengineService tDengineService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private DeviceService deviceService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private DevicePointRulesMapper devicePointRulesMapper;
|
|
|
|
|
|
|
|
|
|
@ -103,13 +108,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 +146,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();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取设备信息
|
|
|
|
|
@ -201,6 +173,8 @@ public class DeviceTask implements Task {
|
|
|
|
|
|
|
|
|
|
boolean connected = OpcUtils.connect(device.getUrl(), username, password, 10);
|
|
|
|
|
if (!connected) {
|
|
|
|
|
//停止任务
|
|
|
|
|
deviceService.connectDevice(new DeviceSaveReqVO().setId(device.getId()).setIsConnect(2));
|
|
|
|
|
throw new RuntimeException("连接OPC服务器失败,URL: " + device.getUrl());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -213,11 +187,35 @@ public class DeviceTask implements Task {
|
|
|
|
|
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());
|
|
|
|
|
//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());
|
|
|
|
|
//TODO 创建人和更新人为内置默认管理员
|
|
|
|
|
record.setCreator("1");
|
|
|
|
|
record.setUpdater("1");
|
|
|
|
|
deviceOperationRecordMapper.insert(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 读取并处理数据
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
List<DeviceContactModelDO> validDataList = new ArrayList<>();
|
|
|
|
|
|