فهرست منبع

采集端添加小数位数控制

wangxiaofei 3 روز پیش
والد
کامیت
20d48912c9

+ 18 - 15
taphole-iron/src/main/java/com/sckj/iron/socketio/DeviceEventListener.java

@@ -37,7 +37,6 @@ 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;
@@ -238,6 +237,9 @@ public class DeviceEventListener extends EventListener { //
     //redis保存最多数量数据
     private static final int MAX_REDIS_COUNT = 50;
 
+    //上次出铁量
+    private OPCData mIronOPCData;
+
     @PostConstruct
     public void init() {
         taskExecutor.submit(() -> {
@@ -408,15 +410,7 @@ public class DeviceEventListener extends EventListener { //
      */
     @Subscribe
     public void onMessageEvent(OPCData opcData) {
-        Object obj = opcData.getData();
-        if (NumberUtils.isCreatable(obj.toString())) {
-            String s = obj.toString().split("\\.")[0];
-//            double value = ((Number) obj).doubleValue();
-            //String formattedValue = String.format("%.2f", value);
-            opcData.setData(Integer.parseInt(s));
-        }
-
-
+        log.debug("subscribe info:{}",opcData);
         //异步保存OPC数据
         taskExecutor.submit(() -> {
             opcDataService.save(opcData);
@@ -687,9 +681,8 @@ public class DeviceEventListener extends EventListener { //
         //出铁超时报警
         if ("1".equals(scheduleTappingTimeoutWarn.getStatus())) {
             scheduledTaskManager.addTask(scheduleTappingTimeoutWarn.getName(), scheduleTappingTimeoutWarn.getDelay(), scheduleTappingTimeoutWarn.getPeriod(), TimeUnit.SECONDS, () -> {
-                int seconds = mSecondsElapsed.get();
                 // log.info("已出铁时间(秒):{},标准出铁时间(秒):{}", seconds, STANDARD_IRON_TIME.get());
-                if (seconds > STANDARD_IRON_TIME.get()) {
+                if (getIronElapsedMinute() > STANDARD_IRON_TIME.get()) {
                     PushData.send2Warn(WarnData.warnTappingTimeout("出铁时间超时", tappingTimeoutAlramUrl));
                     taskExecutor.submit(() -> {
                         exceptionLogService.add(TExceptionLogCreateValidate.builder().exceptionType("2").exceptionDesc("出铁时间超过设定时间").build());
@@ -922,13 +915,18 @@ public class DeviceEventListener extends EventListener { //
                 RealtimeData ironWeight = new RealtimeData();
                 ironWeight.setDesc("铁水流量");
                 ironWeight.setUnit("t");
-                BigDecimal bigDecimalNew = new BigDecimal(opcData.getData().toString());
+                if(ObjectUtils.isEmpty(mIronOPCData) || !opcData.getData().equals(mIronOPCData.getData())){
+                    mIronOPCData = opcData;
+                }
+                BigDecimal bigDecimalNew = new BigDecimal(mIronOPCData.getData().toString());
                 mTotalWeight = mTotalWeight.add(bigDecimalNew);
                 ironWeight.setValue(mTotalWeight);
                 mRealtimeData.put(IRON_WEIGHT, ironWeight);
 //                log.info(">>>>>>>>>>>>>{}:{},total:{}", opcData.getPointName(), opcData.getData(), totalWeight.toPlainString());
                 ironWeight.setTime(LocalDateUtils.formatDate(opcData.getServerTime()));
 
+
+
                 mContext.setVariable(ExpressionConstants.rtIronWeight, mTotalWeight.doubleValue());
 
             } else if (opcData.getPointName().contains(SubscribeTagConstants.TAG_FLUSH_STATUS(opcData.getServerType()))) {
@@ -1265,7 +1263,7 @@ public class DeviceEventListener extends EventListener { //
         if (null == validateClientOnline(client)) {
             return;
         }
-        getIronTimeNo();
+
     }
 
     /***
@@ -1280,7 +1278,12 @@ public class DeviceEventListener extends EventListener { //
         }
         setStepResult(mSteps);
         PushData.send2Operation(mSteps, ironLoading1.get());
-
+        //推送实时数据
+        PushData.send2RealtimeData(mRealtimeData);
+        //推送实时状态
+        PushData.send2RealtimeStatus(mRealtimeStatus);
+        //获取出铁日期和次数
+        getIronTimeNo();
     }
 
 

+ 35 - 26
taphole-opc/src/main/java/com/sckj/opc/dataservice/HDServiceImpl.java

@@ -22,14 +22,19 @@ import com.sckj.opc.service.OPCServerServiceImpl;
 import com.sckj.opc.service.THdTagServiceImpl;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.PreDestroy;
 import javax.annotation.Resource;
+import java.text.DecimalFormat;
 import java.time.LocalDateTime;
-import java.util.*;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 
@@ -178,36 +183,40 @@ public class HDServiceImpl {
                 HDDataProvider dp = new HDDataProvider(connection);
                 HDRecord record = dp.querySnapshotByTagID(hdTag.getTagId());
                 if (record != null) {
-                    HDRecord previousData = mOPCDaPointsMap.get(hdTag.getTagName());
-
-                    //直接比较原始对象,避免字符串转换误差
-                    boolean isNewData = previousData == null ||
-                            !Objects.equals(previousData.getValueStr(), record.getValueStr());
-
-                    if (isNewData) {
-                        OPCData build = OPCData.builder()
-                                .data(record.getValueStr())
-                                .pointName(hdTag.getTagName())
-                                .belongTagID(record.getBelongTagID())
-                                .sourceTime(new Date(record.getSecond() * 1000))
-                                .serverTime(new Date(record.getSecond() * 1000))
-                                .serverType(opcServer.getType())
-                                .build();
-                        asyncEventBus.post(build);
-                        //使用put原子操作更新数据
-                        mOPCDaPointsMap.put(hdTag.getTagName(), record);
-
-                        if (null != previousData) {
-                            log.info("{}({})当前数据:{},上个数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr(), previousData.getValueStr());
+                    Object data = record.getValueStr();
+                    if (NumberUtils.isCreatable(data.toString())) {
+                        // 如果 digits 为 null,默认设为 0
+                        int decimalDigits = (hdTag.getDigits() != null) ? hdTag.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(record.getValueStr());
+                        if (formatString.contains(".")) {
+                            data = Double.parseDouble(formatString);
                         } else {
-                            log.info("{}({})当前数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr());
+                            data = Integer.parseInt(formatString);
                         }
-                    }
 
+                    }
 
+                    OPCData build = OPCData.builder()
+                            .data(data)
+                            .pointName(hdTag.getTagName())
+                            .belongTagID(record.getBelongTagID())
+                            .sourceTime(new Date(record.getSecond() * 1000))
+                            .serverTime(new Date(record.getSecond() * 1000))
+                            .serverType(opcServer.getType())
+                            .dataType(hdTag.getTagType())
+                            .build();
+                    asyncEventBus.post(build);
+                    log.info("{}({})当前数据:{}", hdTag.getTagName(), hdTag.getTagDesc(), record.getValueStr());
                 }
-
-
             } catch (Exception e) {
                 // e.printStackTrace();
                 log.error("{}({})获取数据异常:{}", hdTag.getTagName(), hdTag.getTagDesc(), e.getMessage());

+ 34 - 38
taphole-opc/src/main/java/com/sckj/opc/dataservice/OPCDAServiceImpl.java

@@ -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();

+ 3 - 0
taphole-opc/src/main/java/com/sckj/opc/entity/OPCPoint.java

@@ -57,4 +57,7 @@ public class OPCPoint {
     @ApiModelProperty(value = "创建人")
     private String createBy;
 
+    @ApiModelProperty(value = "小数位")
+    private Integer digits;
+
 }

+ 3 - 0
taphole-opc/src/main/java/com/sckj/opc/entity/THdTag.java

@@ -63,4 +63,7 @@ public class THdTag implements Serializable {
     @ApiModelProperty(value = "更新时间")
     private Date updateTime;
 
+    @ApiModelProperty(value = "小数位")
+    private Integer digits;
+
 }