|
@@ -12,6 +12,7 @@ import com.sckj.opc.service.OPCServerServiceImpl;
|
|
|
import com.sckj.opc.utils.CustomUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.math.NumberUtils;
|
|
|
import org.jinterop.dcom.common.JIException;
|
|
|
import org.openscada.opc.lib.common.ConnectionInformation;
|
|
|
import org.openscada.opc.lib.da.*;
|
|
@@ -22,9 +23,9 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.text.DecimalFormat;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.concurrent.Executors;
|
|
@@ -95,7 +96,7 @@ public class OPCDAServiceImpl {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- newPointName = CustomUtil.createNewPointName(newPointName,activeProfiles,"1");
|
|
|
+ newPointName = CustomUtil.createNewPointName(newPointName, activeProfiles, "1");
|
|
|
|
|
|
log.info("{} start subscribe", newPointName);
|
|
|
|
|
@@ -108,46 +109,41 @@ public class OPCDAServiceImpl {
|
|
|
Object object = null;
|
|
|
try {
|
|
|
object = itemstate.getValue().getObject();
|
|
|
+ if (NumberUtils.isCreatable(object.toString())) {
|
|
|
+ // 如果 digits 为 null,默认设为 0
|
|
|
+ int decimalDigits = (opcPoint.getDigits() != null) ? opcPoint.getDigits() : 0;
|
|
|
+ // 构建格式化模式
|
|
|
+ String pattern = "#";
|
|
|
+ if (decimalDigits > 0) {
|
|
|
+ pattern += ".";
|
|
|
+ for (int i = 0; i < decimalDigits; i++) {
|
|
|
+ pattern += "#";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DecimalFormat decimalFormat = new DecimalFormat(pattern);
|
|
|
+ String formatString = decimalFormat.format(object);
|
|
|
+ if (formatString.contains(".")) {
|
|
|
+ object = Double.parseDouble(formatString);
|
|
|
+ } else {
|
|
|
+ object = Integer.parseInt(formatString);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ OPCData opcData = OPCData.builder()
|
|
|
+ .data(object)
|
|
|
+ .serverTime(calendar.getTime())
|
|
|
+ .sourceTime(calendar.getTime())
|
|
|
+ .statusCode((long) errorCode)
|
|
|
+ .pointName(pointName)
|
|
|
+ .dataType(opcPoint.getDataType())
|
|
|
+ .build();
|
|
|
+ //post给其他模块使用
|
|
|
+ asyncEventBus.post(opcData);
|
|
|
+ log.debug("DA,{},{}", item.getId(), itemstate);
|
|
|
} catch (JIException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- synchronized (mOPCDaPointsMap) {
|
|
|
- //DA中订阅是按照定时计算的,存在重复的数据项,进行过滤
|
|
|
- ItemState previousData = mOPCDaPointsMap.get(opcPoint.getPointName());
|
|
|
- Object currentData = object;
|
|
|
-
|
|
|
- //直接比较原始对象,避免字符串转换误差
|
|
|
- boolean isNewData = true;
|
|
|
- try {
|
|
|
- isNewData = previousData == null || previousData.getValue() == null ||
|
|
|
- !Objects.equals(previousData.getValue().getObject(), currentData);
|
|
|
- } catch (JIException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
|
|
|
- // 添加数值精度处理(处理小数点后2位)
|
|
|
-// if (!isNewData && currentData instanceof Number) {
|
|
|
-// DecimalFormat df = new DecimalFormat("#.##");
|
|
|
-// String prev = df.format(previousData.getData());
|
|
|
-// String curr = df.format(currentData);
|
|
|
-// isNewData = !prev.equals(curr);
|
|
|
-// }
|
|
|
-
|
|
|
- if (isNewData) {
|
|
|
- OPCData opcData = OPCData.builder()
|
|
|
- .data(object)
|
|
|
- .serverTime(calendar.getTime())
|
|
|
- .sourceTime(calendar.getTime())
|
|
|
- .statusCode((long) errorCode)
|
|
|
- .pointName(pointName)
|
|
|
- .build();
|
|
|
- // 使用put原子操作更新数据
|
|
|
- mOPCDaPointsMap.put(opcPoint.getPointName(), itemstate);
|
|
|
- //post给其他模块使用
|
|
|
- asyncEventBus.post(opcData);
|
|
|
- log.debug("DA,{},{}", item.getId(), itemstate);
|
|
|
- }
|
|
|
- }
|
|
|
});
|
|
|
|
|
|
access.bind();
|