|
@@ -0,0 +1,201 @@
|
|
|
+package com.example.opc_da.task;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.example.opc_common.entity.*;
|
|
|
+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_da.dao.ItemGroupDao;
|
|
|
+import com.example.opc_da.dao.MessageNoticeDao;
|
|
|
+import com.example.opc_da.dynamicSchedule.CronTaskRegister;
|
|
|
+import com.example.opc_da.util.OpcDaUtil;
|
|
|
+import com.example.opc_da.util.RedisUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.openscada.opc.lib.da.Group;
|
|
|
+import org.openscada.opc.lib.da.ItemState;
|
|
|
+import org.openscada.opc.lib.da.Server;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class OpcDaSpecifyTask extends TimerTask {
|
|
|
+
|
|
|
+ private final RedisUtil redisUtil;
|
|
|
+
|
|
|
+ private final OpcAsyncTask opcAsyncTask;
|
|
|
+
|
|
|
+ private final String cronId;
|
|
|
+
|
|
|
+ private final CronTaskRegister cronTaskRegister;
|
|
|
+
|
|
|
+ private final ItemGroupDao itemGroupDao;
|
|
|
+
|
|
|
+ private final MessageNoticeDao messageNoticeDao;
|
|
|
+
|
|
|
+ private final Timer timer;
|
|
|
+
|
|
|
+ private final ItemGroup itemGroup;
|
|
|
+
|
|
|
+ private final DataSource dataSource;
|
|
|
+
|
|
|
+ private final Map<String, DataModel> dmMap;
|
|
|
+
|
|
|
+ private final List<com.example.opc_common.entity.Item> itemList;
|
|
|
+
|
|
|
+ private final String[] items;
|
|
|
+
|
|
|
+ private final String timeFormat;
|
|
|
+
|
|
|
+ private Boolean eventFlage = false;
|
|
|
+
|
|
|
+ public OpcDaSpecifyTask(RedisUtil redisUtil,
|
|
|
+ OpcAsyncTask opcAsyncTask,
|
|
|
+ String cronId,
|
|
|
+ CronTaskRegister cronTaskRegister,
|
|
|
+ ItemGroupDao itemGroupDao,
|
|
|
+ MessageNoticeDao messageNoticeDao,
|
|
|
+ Timer timer,
|
|
|
+ ItemGroup itemGroup,
|
|
|
+ DataSource dataSource,
|
|
|
+ Map<String, DataModel> dmMap,
|
|
|
+ List<com.example.opc_common.entity.Item> itemList,
|
|
|
+ String[] items,
|
|
|
+ String timeFormat) {
|
|
|
+ this.redisUtil = redisUtil;
|
|
|
+ this.opcAsyncTask = opcAsyncTask;
|
|
|
+ this.cronId = cronId;
|
|
|
+ this.cronTaskRegister = cronTaskRegister;
|
|
|
+ this.itemGroupDao = itemGroupDao;
|
|
|
+ this.messageNoticeDao = messageNoticeDao;
|
|
|
+ this.timer = timer;
|
|
|
+ this.itemGroup = itemGroup;
|
|
|
+ this.dataSource = dataSource;
|
|
|
+ this.dmMap = dmMap;
|
|
|
+ this.itemList = itemList;
|
|
|
+ this.items = items;
|
|
|
+ this.timeFormat = timeFormat;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ Server server = OpcDaUtil.createServer(dataSource);
|
|
|
+ Integer id = itemGroup.getId();
|
|
|
+ Integer dataSourceId = dataSource.getId();
|
|
|
+ String sqlCurrentYmdh = "";
|
|
|
+ try {
|
|
|
+ server.connect();
|
|
|
+ Group group = server.addGroup();
|
|
|
+ Map<String, org.openscada.opc.lib.da.Item> itemResult = group.addItems(items);
|
|
|
+ Set itemSet = new HashSet(itemResult.values());
|
|
|
+ org.openscada.opc.lib.da.Item[] itemArr = new org.openscada.opc.lib.da.Item[itemSet.size()];
|
|
|
+ itemSet.toArray(itemArr);
|
|
|
+ Map<org.openscada.opc.lib.da.Item, ItemState> resultMap = group.read(true, itemArr);
|
|
|
+ List<EventRawData> eventRawDataList = new ArrayList<>();
|
|
|
+ for (com.example.opc_common.entity.Item item : itemList) {
|
|
|
+ String itemReadName = item.getItemReadName();
|
|
|
+ Integer eventMode = item.getEventMode();
|
|
|
+ Double eventValue = item.getEventValue();
|
|
|
+ Integer tableReportId = item.getTableReportId();
|
|
|
+ DataModel dm = dmMap.get(itemReadName);
|
|
|
+ for (org.openscada.opc.lib.da.Item item1 : resultMap.keySet()) {
|
|
|
+ String itemId = item1.getId();
|
|
|
+ if (item.getItemReadName().equals(itemId)) {
|
|
|
+ ItemState itemState = resultMap.get(item1);
|
|
|
+ Map<String, Object> val = OpcDaUtil.getVal(itemState.getValue());
|
|
|
+ String javaType = val.get("javaType").toString();
|
|
|
+ Object value = val.get("value");
|
|
|
+
|
|
|
+ //值对应取值的时间
|
|
|
+ Date time = itemState.getTimestamp().getTime();
|
|
|
+ String currentYmdh = DateUtil.dateChangeStr(time, timeFormat);
|
|
|
+ sqlCurrentYmdh = currentYmdh;
|
|
|
+ //服务器读取的时间
|
|
|
+ String currentYmdhmss = DateUtil.dateChangeStrYmdhmss(time);
|
|
|
+
|
|
|
+ if (javaType.toLowerCase().equals("boolean")) {
|
|
|
+ //存数据
|
|
|
+ Boolean dmData = JSON.parseObject(value.toString(), Boolean.class);
|
|
|
+ RawData rawData = new RawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData),
|
|
|
+ currentYmdhmss, currentYmdh, new Date());
|
|
|
+ opcAsyncTask.addTempRawData(rawData);
|
|
|
+ opcAsyncTask.packageRawData(item, dataSourceId, currentYmdh);
|
|
|
+
|
|
|
+ EventRawData eventRawData = new EventRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData),
|
|
|
+ currentYmdhmss, currentYmdh, new Date(), 0L);
|
|
|
+ eventRawDataList.add(eventRawData);
|
|
|
+
|
|
|
+ if (Blank.isNotEmpty(eventMode) && Blank.isNotEmpty(eventValue) && Blank.isNotEmpty(tableReportId)) {
|
|
|
+ if (eventMode.equals(ConstantStr.EVENT_MODEL_BOOLEAN)) {
|
|
|
+ if ((dmData ? ConstantStr.BOOLEAN_TRUE : ConstantStr.BOOLEAN_FALSE).equals(eventValue.intValue())) {
|
|
|
+ eventFlage = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
|
|
|
+ BigDecimal dmData = Blank.isNotEmpty(dm) ?
|
|
|
+ MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
|
|
|
+ bigDecimal;
|
|
|
+ RawData rawData = new RawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData),
|
|
|
+ currentYmdhmss, currentYmdh, new Date());
|
|
|
+ opcAsyncTask.addTempRawData(rawData);
|
|
|
+
|
|
|
+ EventRawData eventRawData = new EventRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData),
|
|
|
+ currentYmdhmss, currentYmdh, new Date(), 0L);
|
|
|
+ eventRawDataList.add(eventRawData);
|
|
|
+
|
|
|
+ if (Blank.isNotEmpty(eventMode) && Blank.isNotEmpty(eventValue) && Blank.isNotEmpty(tableReportId)) {
|
|
|
+ BigDecimal bigEventValue = new BigDecimal(eventValue);
|
|
|
+ if (eventMode.equals(ConstantStr.EVENT_MODEL_EXCEED)) {
|
|
|
+ if (dmData.compareTo(bigEventValue) == 1) {
|
|
|
+ eventFlage = true;
|
|
|
+ }
|
|
|
+ } else if (eventMode.equals(ConstantStr.EVENT_MODEL_LOWER)) {
|
|
|
+ if (dmData.compareTo(bigEventValue) == -1) {
|
|
|
+ eventFlage = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ RawData rawData = new RawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(value.toString()),
|
|
|
+ currentYmdhmss, currentYmdh, new Date());
|
|
|
+ opcAsyncTask.addTempRawData(rawData);
|
|
|
+
|
|
|
+ EventRawData eventRawData = new EventRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(value.toString()),
|
|
|
+ currentYmdhmss, currentYmdh, new Date(), 0L);
|
|
|
+ eventRawDataList.add(eventRawData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (eventFlage) {
|
|
|
+ if (Blank.isNotEmpty(eventRawDataList)) {
|
|
|
+ for (EventRawData eventRawData : eventRawDataList) {
|
|
|
+ opcAsyncTask.addEventRawData(eventRawData);
|
|
|
+ }
|
|
|
+ if (Blank.isNotEmpty(sqlCurrentYmdh)) {
|
|
|
+ opcAsyncTask.eventTable(dataSourceId, itemList, sqlCurrentYmdh);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
+ itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
|
|
|
+ e.getMessage(),
|
|
|
+ ConstantStr.NO_READ));
|
|
|
+ if (Blank.isNotEmpty(server)) {
|
|
|
+ server.dispose();
|
|
|
+ }
|
|
|
+ redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
|
+ itemGroupDao.stopItemGroupById(id, ConstantStr.STOP_IT);
|
|
|
+ cronTaskRegister.removeCronTask(cronId);
|
|
|
+ timer.cancel();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|