|
@@ -12,6 +12,7 @@ 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.jinterop.dcom.common.JIException;
|
|
|
import org.openscada.opc.dcom.da.OPCSERVERSTATE;
|
|
|
import org.openscada.opc.lib.da.Item;
|
|
|
import org.openscada.opc.lib.da.*;
|
|
@@ -81,7 +82,7 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
Server server = OpcDaUtil.createServer(dataSource);
|
|
|
Integer id = itemGroup.getId();
|
|
|
Integer dataSourceId = dataSource.getId();
|
|
|
- SyncAccess access = null;
|
|
|
+ Timer chTimer = new Timer();
|
|
|
try {
|
|
|
server.connect();
|
|
|
server.addStateListener(connected -> {
|
|
@@ -127,7 +128,7 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
}, 10000);
|
|
|
}
|
|
|
});
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
String message = OpcDaUtil.genException(e.getMessage());
|
|
|
messageNoticeDao.addMessageNotice(new MessageNotice(itemGroup.getUserId(),
|
|
|
itemGroup.getGroupName() + DateUtil.dateChangeStrYmdhms(new Date()) + "运行失败",
|
|
@@ -138,21 +139,37 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
}
|
|
|
itemGroupDao.stopItemGroupById(itemGroup.getId(), ConstantStr.EXCEPT_STOP_UP);
|
|
|
cronTaskRegister.removeCronTask(cronId);
|
|
|
+ chTimer.cancel();
|
|
|
timer.cancel();
|
|
|
}
|
|
|
try {
|
|
|
- access = new SyncAccess(server, (int) (Math.round(itemGroup.getModeValue() * 1000)));
|
|
|
-// AccessBase access = new Async20Access(server, (int) (itemGroup.getModeValue() * 1000), true);
|
|
|
+ Group group = server.addGroup();
|
|
|
+ List<String> itemStrList = new ArrayList<>();
|
|
|
for (com.example.opc_common.entity.Item item : itemList) {
|
|
|
- String itemId = item.getItemReadName();
|
|
|
- access.addItem(itemId, new DataCallback() {
|
|
|
+ itemStrList.add(item.getItemReadName());
|
|
|
+ }
|
|
|
+ String[] items = itemStrList.toArray(new String[]{});
|
|
|
+ Map<String, 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);
|
|
|
|
|
|
- private String sqlCurrentYmdh = "";
|
|
|
- private Long index = 0L;
|
|
|
+ chTimer.schedule(new TimerTask() {
|
|
|
+ private String sqlCurrentYmdh = "";
|
|
|
+ private Long index = 0L;
|
|
|
|
|
|
- @Override
|
|
|
- public void changed(Item item1, ItemState itemState) {
|
|
|
- index++;
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ index++;
|
|
|
+ Map<Item, ItemState> resultMap = null;
|
|
|
+ try {
|
|
|
+ resultMap = group.read(true, itemArr);
|
|
|
+ } catch (JIException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ for (Item item : resultMap.keySet()) {
|
|
|
+ String itemId = item.getId();
|
|
|
+ ItemState itemState = resultMap.get(item);
|
|
|
try {
|
|
|
Map<String, Object> val = OpcDaUtil.getVal(itemState.getValue());
|
|
|
DataModel dm = map.get(itemId);
|
|
@@ -196,7 +213,7 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
opcAsyncTask.addCursorRawData(cursorRawData);
|
|
|
}
|
|
|
}
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (JIException e) {
|
|
|
//执行组装数据库的数据,以及生成驱动报表
|
|
|
opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
|
|
|
redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
|
|
@@ -211,51 +228,29 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
itemGroupDao.stopItemGroupById(id, ConstantStr.EXCEPT_STOP_UP);
|
|
|
cronTaskRegister.removeCronTask(cronId);
|
|
|
redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
|
+ chTimer.cancel();
|
|
|
timer.cancel();
|
|
|
}
|
|
|
}
|
|
|
- });
|
|
|
- }
|
|
|
- access.bind();
|
|
|
+ }
|
|
|
+ }, new Date(), (int) (Math.round(itemGroup.getModeValue() * 1000)));
|
|
|
+
|
|
|
while (true) {
|
|
|
Boolean flage = (Boolean) redisUtil.get(ConstantStr.ITEM_GROUP + id);
|
|
|
if (Blank.isEmpty(flage)) {
|
|
|
flage = false;
|
|
|
}
|
|
|
- if (!access.isActive()) {
|
|
|
- //执行组装数据库的数据,以及生成驱动报表
|
|
|
- String sqlCurrentYmdh = String.valueOf(redisUtil.get(ConstantStr.VALUE_BELONG_TIME + id));
|
|
|
- opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
|
|
|
- redisUtil.del(ConstantStr.VALUE_BELONG_TIME + 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) {
|
|
|
//执行组装数据库的数据,以及生成驱动报表
|
|
|
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();
|
|
|
}
|
|
|
cronTaskRegister.removeCronTask(cronId);
|
|
|
redisUtil.del(ConstantStr.ITEM_GROUP + id);
|
|
|
+ chTimer.cancel();
|
|
|
timer.cancel();
|
|
|
break;
|
|
|
}
|
|
@@ -265,12 +260,10 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
opcAsyncTask.packageRawDataList(itemList, dataSourceId, sqlCurrentYmdh);
|
|
|
redisUtil.del(ConstantStr.VALUE_BELONG_TIME + id);
|
|
|
|
|
|
- if (Blank.isNotEmpty(access)) {
|
|
|
- access.clear();
|
|
|
- }
|
|
|
if (Blank.isNotEmpty(server)) {
|
|
|
server.dispose();
|
|
|
}
|
|
|
+ chTimer.cancel();
|
|
|
timer.cancel();
|
|
|
break;
|
|
|
}
|
|
@@ -285,14 +278,12 @@ public class OpcDaFrequencyTask extends TimerTask {
|
|
|
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.EXCEPT_STOP_UP);
|
|
|
cronTaskRegister.removeCronTask(cronId);
|
|
|
+ chTimer.cancel();
|
|
|
timer.cancel();
|
|
|
}
|
|
|
}
|