Browse Source

1.hdsdk部分优化
2.采集数据统一为整数

wangxiaofei 2 days ago
parent
commit
20cc89ac3c

+ 14 - 8
taphole-iron/src/main/java/com/sckj/iron/socketio/DeviceEventListener.java

@@ -37,6 +37,7 @@ import com.sckj.warn.validate.TExceptionLogCreateValidate;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.expression.Expression;
@@ -49,6 +50,7 @@ import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import java.io.File;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
@@ -407,13 +409,14 @@ public class DeviceEventListener extends EventListener { //
     @Subscribe
     public void onMessageEvent(OPCData opcData) {
         Object obj = opcData.getData();
-        if (obj.toString().contains(".")) {
+        if (NumberUtils.isCreatable(obj.toString())) {
             String s = obj.toString().split("\\.")[0];
 //            double value = ((Number) obj).doubleValue();
             //String formattedValue = String.format("%.2f", value);
-            opcData.setData(Double.parseDouble(s));
+            opcData.setData(Integer.parseInt(s));
         }
 
+
         //异步保存OPC数据
         taskExecutor.submit(() -> {
             opcDataService.save(opcData);
@@ -894,15 +897,17 @@ public class DeviceEventListener extends EventListener { //
                 realtimeData.setDesc("出铁状态");
                 mRealtimeStatus.put(IRON_STATUS, realtimeData);
 
-                ironLoading1.set(Double.parseDouble(opcData.getData().toString()));
-                mContext.setVariable(ExpressionConstants.rtIron01State, ironLoading1.get());
+                double currentVal = Double.parseDouble(opcData.getData().toString());
 
-                if (ironLoading1.get() > 0) {
+                if (ironLoading1.get() <= 0 && currentVal > 0) {
                     taphole1Start();
-                } else {
+                } else if (ironLoading1.get() > 0 && currentVal <= 0) {
                     taphole1End();
                 }
 
+                ironLoading1.set(Double.parseDouble(opcData.getData().toString()));
+                mContext.setVariable(ExpressionConstants.rtIron01State, ironLoading1.get());
+
             } else if (opcData.getPointName().contains(SubscribeTagConstants.TAG_TAPHOLE2_STATUS(opcData.getServerType()))) {
                 ironLoading2.set(Double.parseDouble(opcData.getData().toString()));
                 mContext.setVariable(ExpressionConstants.rtIron02State, ironLoading2.get());
@@ -953,12 +958,12 @@ public class DeviceEventListener extends EventListener { //
             String elementS = fixedLatestElement.getElementS();
 
             RealtimeData realtimeData = new RealtimeData();
-            realtimeData.setValue(new BigDecimal(elementSi).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
+            realtimeData.setValue(new BigDecimal(elementSi).multiply(new BigDecimal(100)).setScale(0, RoundingMode.DOWN).toPlainString());
             realtimeData.setUnit("%");
             realtimeData.setDesc("硅");
 
             RealtimeData realtimeData2 = new RealtimeData();
-            realtimeData2.setValue(new BigDecimal(elementS).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
+            realtimeData2.setValue(new BigDecimal(elementS).multiply(new BigDecimal(100)).setScale(0, RoundingMode.DOWN).toPlainString());
             realtimeData2.setUnit("%");
             realtimeData2.setDesc("硫");
 
@@ -1275,6 +1280,7 @@ public class DeviceEventListener extends EventListener { //
         }
         setStepResult(mSteps);
         PushData.send2Operation(mSteps, ironLoading1.get());
+
     }
 
 

+ 10 - 8
taphole-opc/src/main/java/com/sckj/opc/dataservice/HDServiceImpl.java

@@ -196,19 +196,21 @@ public class HDServiceImpl {
                         asyncEventBus.post(build);
                         //使用put原子操作更新数据
                         mOPCDaPointsMap.put(hdTag.getTagName(), record);
+
+                        if (null != previousData) {
+                            log.info("{}({})当前数据:{},上个数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr(), previousData.getValueStr());
+                        } else {
+                            log.info("{}({})当前数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr());
+                        }
                     }
-                    if (null != previousData) {
-                        log.info("{}({})当前数据:{},上个数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr(), previousData.getValueStr());
-                    } else {
-                        log.info("{}({})当前数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr());
-                    }
+
 
                 }
 
 
             } catch (Exception e) {
                 // e.printStackTrace();
-                log.info("{}({})获取数据异常:{}", hdTag.getTagName(), hdTag.getTagDesc(), e.getMessage());
+                log.error("{}({})获取数据异常:{}", hdTag.getTagName(), hdTag.getTagDesc(), e.getMessage());
             }
         });
     }
@@ -351,7 +353,7 @@ public class HDServiceImpl {
                     }
                     if (ObjectUtils.isEmpty(basicTag)) {
 //                        log.info("{}未查询到信息", opcPoint.getTagName());
-                        sb.append("编号:" + opcPoint.getId() + ",tageId:" + basicTag.getId() + ",tageName:" + opcPoint.getTagName() + ",tageType:" + basicTag.getTagDataType().name() + "\n");
+                        sb.append("编号:" + opcPoint.getId() + ",tageName:" + opcPoint.getTagName() + "\n");
                         continue;
                     }
 //                    log.info("{}查询到信息:TagId:{},TagType:{},Id:{}", opcPoint.getTagName(), basicTag.getId(), basicTag.getTagDataType().name(), opcPoint.getId());
@@ -362,7 +364,7 @@ public class HDServiceImpl {
                     hdTagService.updateById(newTHdTag);
                 }
             }
-            log.info("刷新信息失败:{}", sb.toString());
+            log.info("刷新信息失败:{}", sb.toString());
 //            } catch (HDSdkException e) {
 //                throw new RuntimeException(e);
 //            }

+ 17 - 13
taphole-opc/src/main/java/com/sckj/opc/dataservice/OPCDAServiceImpl.java

@@ -22,7 +22,6 @@ 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;
@@ -55,7 +54,7 @@ public class OPCDAServiceImpl {
     protected String activeProfiles;
 
     private ConcurrentHashMap<Long, Server> mOPCDaClientMap = new ConcurrentHashMap<>();
-    private ConcurrentHashMap<String, OPCData> mOPCDaPointsMap = new ConcurrentHashMap<>();
+    private ConcurrentHashMap<String, ItemState> mOPCDaPointsMap = new ConcurrentHashMap<>();
 
 
     /**
@@ -100,7 +99,7 @@ public class OPCDAServiceImpl {
 
                         log.info("{} start subscribe", newPointName);
 
-                        mOPCDaPointsMap.put(opcPoint.getPointName(), OPCData.builder().build());
+                        mOPCDaPointsMap.put(opcPoint.getPointName(), new ItemState());
 
                         access.addItem(newPointName, (item, itemstate) -> {
                             int errorCode = itemstate.getErrorCode();
@@ -114,20 +113,25 @@ public class OPCDAServiceImpl {
                             }
                             synchronized (mOPCDaPointsMap) {
                                 //DA中订阅是按照定时计算的,存在重复的数据项,进行过滤
-                                OPCData previousData = mOPCDaPointsMap.get(opcPoint.getPointName());
+                                ItemState previousData = mOPCDaPointsMap.get(opcPoint.getPointName());
                                 Object currentData = object;
 
                                 //直接比较原始对象,避免字符串转换误差
-                                boolean isNewData = previousData == null ||
-                                        !Objects.equals(previousData.getData(), currentData);
+                                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 && 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()
@@ -138,7 +142,7 @@ public class OPCDAServiceImpl {
                                             .pointName(pointName)
                                             .build();
                                     // 使用put原子操作更新数据
-                                    mOPCDaPointsMap.put(opcPoint.getPointName(), opcData);
+                                    mOPCDaPointsMap.put(opcPoint.getPointName(), itemstate);
                                     //post给其他模块使用
                                     asyncEventBus.post(opcData);
                                     log.debug("DA,{},{}", item.getId(), itemstate);