|
@@ -1,360 +0,0 @@
|
|
|
-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 org.openscada.opc.dcom.da.OPCSERVERSTATE;
|
|
|
-import org.openscada.opc.lib.da.Item;
|
|
|
-import org.openscada.opc.lib.da.*;
|
|
|
-
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-public class OpcDaExceedTask3 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> map;
|
|
|
-
|
|
|
- private final List<com.example.opc_common.entity.Item> itemList;
|
|
|
-
|
|
|
- private final String timeFormat;
|
|
|
-
|
|
|
- private final Long endTime;
|
|
|
-
|
|
|
- public OpcDaExceedTask3(RedisUtil redisUtil,
|
|
|
- OpcAsyncTask opcAsyncTask,
|
|
|
- String cronId,
|
|
|
- CronTaskRegister cronTaskRegister,
|
|
|
- ItemGroupDao itemGroupDao,
|
|
|
- MessageNoticeDao messageNoticeDao,
|
|
|
- Timer timer,
|
|
|
- ItemGroup itemGroup,
|
|
|
- DataSource dataSource,
|
|
|
- Map<String, DataModel> map,
|
|
|
- List<com.example.opc_common.entity.Item> itemList,
|
|
|
- String timeFormat,
|
|
|
- Long endTime) {
|
|
|
- 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.map = map;
|
|
|
- this.itemList = itemList;
|
|
|
- this.timeFormat = timeFormat;
|
|
|
- this.endTime = endTime;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- Server server = OpcDaUtil.createServer(dataSource);
|
|
|
- Integer id = itemGroup.getId();
|
|
|
- Integer eventMode = itemGroup.getEventMode();
|
|
|
- Double modeValue = itemGroup.getModeValue();
|
|
|
- Integer dataSourceId = dataSource.getId();
|
|
|
- SyncAccess access = null;
|
|
|
- try {
|
|
|
- server.connect();
|
|
|
- server.addStateListener(connected -> {
|
|
|
- if (!connected) {
|
|
|
- Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
|
|
|
- if (Blank.isEmpty(flage)) {
|
|
|
- flage = false;
|
|
|
- }
|
|
|
- if (flage) {
|
|
|
- messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
- itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务断开",
|
|
|
- "服务断开,马上进行重新连接",
|
|
|
- ConstantStr.NO_READ));
|
|
|
- itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
|
|
|
- Timer listenerTimer = new Timer();
|
|
|
- listenerTimer.schedule(new TimerTask() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- try {
|
|
|
- server.connect();
|
|
|
- if (Blank.isNotEmpty(server.getServerState()) && server.getServerState().getServerState() == OPCSERVERSTATE.OPC_STATUS_RUNNING) {
|
|
|
- messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
- itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接成功",
|
|
|
- "服务重新连接成功",
|
|
|
- ConstantStr.NO_READ));
|
|
|
- itemGroupDao.stopItemGroupById(id, ConstantStr.START_UP);
|
|
|
- listenerTimer.cancel();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
-// redisUtil.set(ConstantStr.ITEM_GROUP + id, false);
|
|
|
-// String message = OpcDaUtil.genException(e.getMessage());
|
|
|
-// messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
-// itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "服务重新连接失败",
|
|
|
-// message,
|
|
|
-// ConstantStr.NO_READ));
|
|
|
-// listenerTimer.cancel();
|
|
|
- }
|
|
|
- }
|
|
|
- }, 60000);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- } catch (Exception e) {
|
|
|
- String message = OpcDaUtil.genException(e.getMessage());
|
|
|
- messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
- itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
|
|
|
- message,
|
|
|
- ConstantStr.NO_READ));
|
|
|
- if (Blank.isNotEmpty(server)) {
|
|
|
- server.dispose();
|
|
|
- }
|
|
|
- itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.STOP_IT);
|
|
|
- cronTaskRegister.removeCronTask(cronId);
|
|
|
- timer.cancel();
|
|
|
- }
|
|
|
- try {
|
|
|
- access = new SyncAccess(server, 500);
|
|
|
- for (com.example.opc_common.entity.Item item : itemList) {
|
|
|
- String itemId = item.getItemReadName();
|
|
|
- Integer isDriverItem = item.getIsDriverItem();
|
|
|
- access.addItem(itemId, new DataCallback() {
|
|
|
- private Long index = 0L;
|
|
|
- private BigDecimal oldValue;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void changed(Item item1, ItemState itemState) {
|
|
|
- index++;
|
|
|
- try {
|
|
|
- Map<String, Object> val = OpcDaUtil.getVal(itemState.getValue());
|
|
|
- DataModel dm = map.get(itemId);
|
|
|
-
|
|
|
- //读取的值
|
|
|
- Object value = val.get("value");
|
|
|
- String javaType = String.valueOf(val.get("javaType"));
|
|
|
- //值对应取值的时间
|
|
|
- Date time = itemState.getTimestamp().getTime();
|
|
|
- //归属时间
|
|
|
- String currentYmdh = DateUtil.dateChangeStr(time, timeFormat);
|
|
|
- String currentYmdhmss = DateUtil.dateChangeStrYmdhmss(time);
|
|
|
- if (isDriverItem.equals(ConstantStr.IS_DRIVER_ITEM)) {
|
|
|
- if (index % 20 == 0) {
|
|
|
- opcAsyncTask.packageEventTable(itemList, dataSourceId, index - 1);
|
|
|
- redisUtil.set(ConstantStr.INDEX + id, index, ConstantStr.TWO_HOUR);
|
|
|
- }
|
|
|
- }
|
|
|
- if (javaType.toLowerCase().equals("boolean")) {
|
|
|
- Boolean dmData = JSON.parseObject(value.toString(), Boolean.class);
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(dmData), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- } else {
|
|
|
- try {
|
|
|
- BigDecimal bigDecimal = JSON.parseObject(value.toString(), BigDecimal.class);
|
|
|
- BigDecimal dmData = Blank.isNotEmpty(dm) ?
|
|
|
- MathUtil.quadricOperation(dm.getMathParameter(), dm.getOperationRule(), bigDecimal) :
|
|
|
- bigDecimal;
|
|
|
- if (isDriverItem.equals(ConstantStr.IS_DRIVER_ITEM)) {
|
|
|
- BigDecimal bigDecimal1 = new BigDecimal(modeValue);
|
|
|
- if (eventMode == ConstantStr.EVENT_MODEL_EXCEED) {
|
|
|
- if (dmData.compareTo(bigDecimal1) == 1) {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- } else if (eventMode == ConstantStr.EVENT_MODEL_LOWER) {
|
|
|
- if (dmData.compareTo(bigDecimal1) == -1) {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- } else if (eventMode == ConstantStr.EVENT_MODEL_EQUAL) {
|
|
|
- if (dmData.compareTo(bigDecimal1) == 0) {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- } else if (eventMode == ConstantStr.EVENT_TRENDS_EXCEED) {
|
|
|
- if (oldValue.compareTo(bigDecimal1) != 1) {
|
|
|
- if (dmData.compareTo(bigDecimal1) == 1) {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- oldValue = dmData;
|
|
|
- } else if (eventMode == ConstantStr.EVENT_TRENDS_LOWER) {
|
|
|
- if (oldValue.compareTo(bigDecimal1) != -1) {
|
|
|
- if (dmData.compareTo(bigDecimal1) == -1) {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.IS_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- oldValue = dmData;
|
|
|
- }
|
|
|
- } else {
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, JSON.toJSONString(bigDecimal), JSON.toJSONString(dmData),
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- String valueStr = value.toString();
|
|
|
- CursorRawData cursorRawData = new CursorRawData(id, dataSourceId, itemId, javaType, valueStr, "".equals(valueStr) ? ConstantStr.STRING_EMPTY : valueStr,
|
|
|
- currentYmdhmss, index, currentYmdh, ConstantStr.NOT_RECORD, new Date());
|
|
|
- opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- opcAsyncTask.packageEventTable(itemList, dataSourceId, index);
|
|
|
- redisUtil.del(ConstantStr.INDEX + id);
|
|
|
-
|
|
|
- messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
- itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
|
|
|
- e.getMessage(),
|
|
|
- ConstantStr.NO_READ));
|
|
|
- e.printStackTrace();
|
|
|
- if (Blank.isNotEmpty(server)) {
|
|
|
- server.dispose();
|
|
|
- }
|
|
|
- itemGroupDao.stopItemGroupById(id, ConstantStr.STOP_IT);
|
|
|
- cronTaskRegister.removeCronTask(cronId);
|
|
|
- redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
|
- timer.cancel();
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- access.bind();
|
|
|
- while (true) {
|
|
|
- Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
|
|
|
- if (Blank.isEmpty(flage)) {
|
|
|
- flage = false;
|
|
|
- }
|
|
|
-// if (!access.isActive()) {
|
|
|
-// Long index = Long.valueOf(String.valueOf(redisUtil.get(ConstantStr.INDEX + id)));
|
|
|
-// opcAsyncTask.packageEventTable(itemList, dataSourceId, index);
|
|
|
-// redisUtil.del(ConstantStr.INDEX + id);
|
|
|
-//
|
|
|
-// messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
-// itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
|
|
|
-// "服务异常停止了",
|
|
|
-// ConstantStr.NO_READ));
|
|
|
-// if (Blank.isNotEmpty(access)) {
|
|
|
-// access.clear();
|
|
|
-// }
|
|
|
-// if (Blank.isNotEmpty(server)) {
|
|
|
-// server.dispose();
|
|
|
-// }
|
|
|
-// itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
|
|
|
-// cronTaskRegister.removeCronTask(cronId);
|
|
|
-// redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
|
-// timer.cancel();
|
|
|
-// break;
|
|
|
-// }
|
|
|
- if (!flage) {
|
|
|
- Long index = Long.valueOf(String.valueOf(redisUtil.get(ConstantStr.INDEX + id)));
|
|
|
- opcAsyncTask.packageEventTable(itemList, dataSourceId, index);
|
|
|
- redisUtil.del(ConstantStr.INDEX + id);
|
|
|
-
|
|
|
- if (Blank.isNotEmpty(access)) {
|
|
|
- access.clear();
|
|
|
- }
|
|
|
- if (Blank.isNotEmpty(server)) {
|
|
|
- server.dispose();
|
|
|
- }
|
|
|
- cronTaskRegister.removeCronTask(cronId);
|
|
|
- redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
|
- timer.cancel();
|
|
|
- break;
|
|
|
- }
|
|
|
- if (System.currentTimeMillis() >= endTime) {
|
|
|
- Long index = Long.valueOf(String.valueOf(redisUtil.get(ConstantStr.INDEX + id)));
|
|
|
- opcAsyncTask.packageEventTable(itemList, dataSourceId, index);
|
|
|
- redisUtil.del(ConstantStr.INDEX + id);
|
|
|
-
|
|
|
- if (Blank.isNotEmpty(access)) {
|
|
|
- access.clear();
|
|
|
- }
|
|
|
- if (Blank.isNotEmpty(server)) {
|
|
|
- server.dispose();
|
|
|
- }
|
|
|
- timer.cancel();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
|
|
|
- if (Blank.isEmpty(flage)) {
|
|
|
- flage = false;
|
|
|
- }
|
|
|
- if (!flage) {
|
|
|
- //执行组装数据库的数据,以及生成驱动报表
|
|
|
- String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
|
|
|
- opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
|
|
|
- redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
|
|
|
- if (Blank.isNotEmpty(access)) {
|
|
|
- access.clear();
|
|
|
- }
|
|
|
- if (Blank.isNotEmpty(server)) {
|
|
|
- server.dispose();
|
|
|
- }
|
|
|
- itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.STOP_IT);
|
|
|
- cronTaskRegister.removeCronTask(cronId);
|
|
|
- timer.cancel();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|