|
@@ -1,11 +1,14 @@
|
|
|
package com.example.opc_ua.task;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.example.opc_common.entity.*;
|
|
|
import com.example.opc_common.enums.ResultEnum;
|
|
|
import com.example.opc_common.exception.CustomException;
|
|
|
import com.example.opc_common.util.Blank;
|
|
|
import com.example.opc_common.util.ConstantStr;
|
|
|
import com.example.opc_common.util.DateUtil;
|
|
|
+import com.example.opc_common.util.MathUtil;
|
|
|
import com.example.opc_ua.dao.DataModelDao;
|
|
|
import com.example.opc_ua.dao.ItemGroupDao;
|
|
|
import com.example.opc_ua.dao.MessageNoticeDao;
|
|
@@ -14,11 +17,15 @@ import com.example.opc_ua.util.OpcUaUtil;
|
|
|
import com.example.opc_ua.util.RedisUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.builtin.*;
|
|
|
+import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
@@ -62,108 +69,137 @@ public class OpcUaTask {
|
|
|
Date startDate = DateUtil.strYmdhmsChangeDate(startTime);
|
|
|
Date endDate = DateUtil.strYmdhmsChangeDate(endTime);
|
|
|
|
|
|
- Boolean flage = false;
|
|
|
//获取组中所有标签的数据模型,并转换为map<itemName,DataModel>
|
|
|
- List<Item> allItemList = itemGroupDao.getItemListByGroupId(id);
|
|
|
- itemGroup.setItemList(allItemList);
|
|
|
- if (Blank.isNotEmpty(allItemList)) {
|
|
|
- for (Item item : allItemList) {
|
|
|
- if (Blank.isNotEmpty(item.getDataModelId())) {
|
|
|
- flage = true;
|
|
|
- break;
|
|
|
+ List<Item> basicItemList = itemGroupDao.getBasicItemList(id, ConstantStr.BASIC_ATTRIBUTES);
|
|
|
+ List<DataModel> dmBasicItemList = dataModelDao.getDmListByItemList(basicItemList);
|
|
|
+ Map<String, DataModel> basicMap = DataModel.genMap(basicItemList, dmBasicItemList);
|
|
|
+
|
|
|
+ if (Blank.isNotEmpty(basicItemList)) {
|
|
|
+ Timer timer = new Timer();
|
|
|
+ OpcUaClient opcUaClient = null;
|
|
|
+ try {
|
|
|
+ opcUaClient = OpcUaUtil.createClient(dataSource);
|
|
|
+ opcUaClient.connect().get();
|
|
|
+ } catch (Exception e) {
|
|
|
+ messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
+ itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行停止",
|
|
|
+ OpcUaUtil.genException(e.getMessage()),
|
|
|
+ ConstantStr.NO_READ));
|
|
|
+
|
|
|
+ itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
|
|
|
+ if (Blank.isNotEmpty(opcUaClient)) {
|
|
|
+ opcUaClient.disconnect();
|
|
|
}
|
|
|
+ if (Blank.isNotEmpty(timer)) {
|
|
|
+ timer.cancel();
|
|
|
+ }
|
|
|
+ cronTaskRegister.removeCronTask(cronId);
|
|
|
+ }
|
|
|
+ if (readMode == ConstantStr.ON_FREQUENCY) {
|
|
|
+ timer.schedule(new OpcUaFrequencyTask(
|
|
|
+ redisUtil,
|
|
|
+ opcAsyncTask,
|
|
|
+ cronId,
|
|
|
+ cronTaskRegister,
|
|
|
+ itemGroupDao,
|
|
|
+ messageNoticeDao,
|
|
|
+ opcUaClient,
|
|
|
+ basicItemList,
|
|
|
+ basicMap,
|
|
|
+ itemGroup,
|
|
|
+ dataSource,
|
|
|
+ timer,
|
|
|
+ timeFormat,
|
|
|
+ endDate.getTime()),
|
|
|
+ startDate, (int) (Math.round(itemGroup.getModeValue() * 1000)));
|
|
|
+ } else if (readMode == ConstantStr.ON_CHANGE) {
|
|
|
+ timer.schedule(new OpcUaChangeTask(
|
|
|
+ redisUtil,
|
|
|
+ opcAsyncTask,
|
|
|
+ cronId,
|
|
|
+ cronTaskRegister,
|
|
|
+ itemGroupDao,
|
|
|
+ messageNoticeDao,
|
|
|
+ opcUaClient,
|
|
|
+ basicItemList,
|
|
|
+ basicMap,
|
|
|
+ itemGroup,
|
|
|
+ dataSource,
|
|
|
+ timer,
|
|
|
+ timeFormat,
|
|
|
+ endDate.getTime()),
|
|
|
+ startDate, 500);
|
|
|
+ } else if (readMode == ConstantStr.EXCEED_SET_VALUE) {
|
|
|
+ timer.schedule(new OpcUaExceedTask(redisUtil,
|
|
|
+ opcAsyncTask,
|
|
|
+ cronId,
|
|
|
+ cronTaskRegister,
|
|
|
+ itemGroupDao,
|
|
|
+ messageNoticeDao,
|
|
|
+ opcUaClient,
|
|
|
+ basicItemList,
|
|
|
+ basicMap,
|
|
|
+ itemGroup,
|
|
|
+ dataSource,
|
|
|
+ timer,
|
|
|
+ timeFormat,
|
|
|
+ endDate.getTime()),
|
|
|
+ startDate, 500);
|
|
|
+ } else {
|
|
|
+ cronTaskRegister.removeCronTask(cronId);
|
|
|
+ throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种采样模式");
|
|
|
}
|
|
|
}
|
|
|
- Map<String, DataModel> map = new HashMap<>();
|
|
|
- if (flage) {
|
|
|
- List<DataModel> dmListByItemList = dataModelDao.getDmListByItemList(allItemList);
|
|
|
- if (Blank.isNotEmpty(dmListByItemList)) {
|
|
|
- for (int i = 0; i < allItemList.size(); i++) {
|
|
|
- for (DataModel dm : dmListByItemList) {
|
|
|
- if (dm.getId().equals(allItemList.get(i).getDataModelId())) {
|
|
|
- map.put(allItemList.get(i).getItemReadName(), dm);
|
|
|
- break;
|
|
|
+ List<Item> attachItemList = itemGroupDao.getBasicItemList(id, ConstantStr.ATTACH_ATTRIBUTES);
|
|
|
+ List<DataModel> dmAttachItemList = dataModelDao.getDmListByItemList(attachItemList);
|
|
|
+ Map<String, DataModel> attachMap = DataModel.genMap(attachItemList, dmAttachItemList);
|
|
|
+ if (Blank.isNotEmpty(attachItemList)) {
|
|
|
+ OpcUaClient opcUaClient = null;
|
|
|
+ try {
|
|
|
+ opcUaClient = OpcUaUtil.createClient(dataSource);
|
|
|
+ opcUaClient.connect().get();
|
|
|
+ for (Item item : attachItemList) {
|
|
|
+ NodeId n = new NodeId(item.getNodeIndex(), item.getItemReadName());
|
|
|
+ String itemName = item.getItemReadName();
|
|
|
+ DataModel dm = attachMap.get(itemName);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ DataValue dataValue = opcUaClient.readValue(0.0, TimestampsToReturn.Both, n).get();
|
|
|
+ StatusCode statusCode = dataValue.getStatusCode();
|
|
|
+ jsonObject.put("quality", statusCode.isGood());
|
|
|
+ Object value = dataValue.getValue().getValue();
|
|
|
+ Date time = dataValue.getServerTime().getJavaDate();
|
|
|
+ String javaType = OpcUaUtil.getValType(dataValue);
|
|
|
+ jsonObject.put("dataType", javaType);
|
|
|
+ if (javaType.toLowerCase().equals("boolean")) {
|
|
|
+ AttachRawData attachRawData = new AttachRawData(
|
|
|
+ id, dataSource.getId(), itemName, javaType, value.toString(), value.toString(), DateUtil.dateChangeStrYmdhmss(time), new Date()
|
|
|
+ );
|
|
|
+ opcAsyncTask.addAttachRawData(attachRawData);
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
|
|
|
+ AttachRawData attachRawData = new AttachRawData(
|
|
|
+ id, dataSource.getId(), itemName, javaType, bigDecimal.toString(),
|
|
|
+ (Blank.isNotEmpty(dm) ? MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) : bigDecimal).toString(),
|
|
|
+ DateUtil.dateChangeStrYmdhmss(time), new Date()
|
|
|
+ );
|
|
|
+ opcAsyncTask.addAttachRawData(attachRawData);
|
|
|
+ } catch (Exception e) {
|
|
|
+ AttachRawData attachRawData = new AttachRawData(
|
|
|
+ id, dataSource.getId(), itemName, javaType, value.toString(), value.toString(), DateUtil.dateChangeStrYmdhmss(time), new Date()
|
|
|
+ );
|
|
|
+ opcAsyncTask.addAttachRawData(attachRawData);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), OpcUaUtil.genException(e.getMessage()));
|
|
|
+ } finally {
|
|
|
+ if (Blank.isNotEmpty(opcUaClient)) {
|
|
|
+ opcUaClient.disconnect();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- List<Item> itemList = itemGroupDao.getItemListByGroupId(id);
|
|
|
- OpcUaClient opcUaClient = null;
|
|
|
- Timer timer = new Timer();
|
|
|
- try {
|
|
|
- opcUaClient = OpcUaUtil.createClient(dataSource);
|
|
|
- opcUaClient.connect().get();
|
|
|
- } catch (Exception e) {
|
|
|
- messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
- itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行停止",
|
|
|
- OpcUaUtil.genException(e.getMessage()),
|
|
|
- ConstantStr.NO_READ));
|
|
|
-
|
|
|
- itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
|
|
|
- if (Blank.isNotEmpty(opcUaClient)) {
|
|
|
- opcUaClient.disconnect();
|
|
|
- }
|
|
|
- if (Blank.isNotEmpty(timer)) {
|
|
|
- timer.cancel();
|
|
|
- }
|
|
|
- cronTaskRegister.removeCronTask(cronId);
|
|
|
- }
|
|
|
- if (readMode == ConstantStr.ON_FREQUENCY) {
|
|
|
- timer.schedule(new OpcUaFrequencyTask(
|
|
|
- redisUtil,
|
|
|
- opcAsyncTask,
|
|
|
- cronId,
|
|
|
- cronTaskRegister,
|
|
|
- itemGroupDao,
|
|
|
- messageNoticeDao,
|
|
|
- opcUaClient,
|
|
|
- itemList,
|
|
|
- map,
|
|
|
- itemGroup,
|
|
|
- dataSource,
|
|
|
- timer,
|
|
|
- timeFormat,
|
|
|
- endDate.getTime()),
|
|
|
- startDate, (int) (Math.round(itemGroup.getModeValue() * 1000)));
|
|
|
- } else if (readMode == ConstantStr.ON_CHANGE) {
|
|
|
- timer.schedule(new OpcUaChangeTask(
|
|
|
- redisUtil,
|
|
|
- opcAsyncTask,
|
|
|
- cronId,
|
|
|
- cronTaskRegister,
|
|
|
- itemGroupDao,
|
|
|
- messageNoticeDao,
|
|
|
- opcUaClient,
|
|
|
- itemList,
|
|
|
- map,
|
|
|
- itemGroup,
|
|
|
- dataSource,
|
|
|
- timer,
|
|
|
- timeFormat,
|
|
|
- endDate.getTime()),
|
|
|
- startDate, 500);
|
|
|
- } else if (readMode == ConstantStr.EXCEED_SET_VALUE) {
|
|
|
- timer.schedule(new OpcUaExceedTask(redisUtil,
|
|
|
- opcAsyncTask,
|
|
|
- cronId,
|
|
|
- cronTaskRegister,
|
|
|
- itemGroupDao,
|
|
|
- messageNoticeDao,
|
|
|
- opcUaClient,
|
|
|
- itemList,
|
|
|
- map,
|
|
|
- itemGroup,
|
|
|
- dataSource,
|
|
|
- timer,
|
|
|
- timeFormat,
|
|
|
- endDate.getTime()),
|
|
|
- startDate, 500);
|
|
|
- } else {
|
|
|
- cronTaskRegister.removeCronTask(cronId);
|
|
|
- throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种采样模式");
|
|
|
- }
|
|
|
-
|
|
|
}
|
|
|
}
|