From 15206b16b0121ba7331496c0f0946e4004ad4507 Mon Sep 17 00:00:00 2001
From: zhuguifei <312353457@qq.com>
Date: 星期一, 18 十一月 2024 09:43:36 +0800
Subject: [PATCH] 添加故障上报、存储

---
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java |  124 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 119 insertions(+), 5 deletions(-)

diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
index e3f64dd..8910d51 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
@@ -1,10 +1,14 @@
 package org.jeecg.modules.dry.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.thread.ThreadUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.TypeReference;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.mina.core.service.IoAcceptor;
 import org.apache.mina.core.service.IoHandler;
 import org.apache.mina.core.session.IoSession;
@@ -20,6 +24,7 @@
 import org.jeecg.common.constant.MqttConstant;
 import org.jeecg.common.system.util.JwtUtil;
 import org.jeecg.common.system.vo.LoginUser;
+import org.jeecg.common.util.DateUtils;
 import org.jeecg.common.util.RedisUtil;
 import org.jeecg.common.util.SpringContextUtils;
 import org.jeecg.modules.dry.common.CacheConstants;
@@ -41,10 +46,8 @@
 import java.io.ObjectOutputStream;
 import java.net.Socket;
 import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -68,6 +71,9 @@
 
     @Autowired
     private IDryProdRecordService prodRecordService;
+
+    @Autowired
+    private IDryFaultRecordService faultRecordService;
 
 
     private String token;
@@ -336,7 +342,7 @@
     private void saveOrderTrendVo(DryOrderTrendVo trendVo, DryOrderVo orderVo) {
         //鍒ゆ柇 瀹炴椂鍚按鐜� 鎴� 瀹炴椂閲嶉噺鏈夋病鏈夊彉鍖栵紝鏈夊彉鍖栧垯鏇存柊
         if(orderVo.getTrendVo() == null && trendVo != null && trendVo.getWeight() > 0
-                || trendVo.getWeight() < orderVo.getTrendVo().getWeight()
+                || orderVo.getTrendVo()!=null &&  trendVo.getWeight() < orderVo.getTrendVo().getWeight()
                 ) {
             DryOrder byId = dryOrderService.getById(orderVo.getId());
             // 灏嗘渶鏂扮粨鏋滄洿鏂板埌宸ュ崟
@@ -485,4 +491,112 @@
     public Result<?> statisticsDataHandle(StatisticsDataVo statsDataVo) {
         return null;
     }
+
+    @Override
+    public Result<?> fitFultRecord(RealTimeDataVo vo) {
+        ThreadUtil.execute(() -> {
+            try {
+                //瑙f瀽瀛樺偍鎶ヨ鏁版嵁
+                List<DryFaultRecord> faultRecords1 = fitFault(vo.getEqp_fault(), vo.getWorkorder(), vo.getTenantid(), vo.getMachineid(), 1);
+                List<DryFaultRecord> faultRecords2 = fitFault(vo.getEqp_warning(), vo.getWorkorder(), vo.getTenantid(), vo.getMachineid(), 2);
+                faultRecords1.addAll(faultRecords2);
+                JSONObject json = new JSONObject();
+                json.put("data",JSON.toJSONString(faultRecords1));
+
+                if(faultRecords1.isEmpty())  return;
+                MqttMessage mqttMessage = new MqttMessage();
+                mqttMessage.setQos(0);
+                mqttMessage.setPayload((JSON.toJSONString(json).getBytes()));
+                mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_FAULT_DATA,mqttMessage);
+
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+
+        return null;
+    }
+
+
+    /**
+     * 瑙f瀽瀛樺偍鏁呴殰鏁版嵁
+     * TODO 淇濊瘉鍘熷瓙鎬�
+     * @param fault 鏁呴殰鏁版嵁
+     * @param orderId 宸ュ崟
+     * @param tenantId 绉熸埛
+     * @param machineId 璁惧
+     * @param faultType 鏁呴殰绫诲瀷
+     * @return 缁勮濂芥晠闅滄暟鎹�
+     */
+    private List<DryFaultRecord> fitFault(String fault, String orderId,Integer tenantId,String machineId,Integer faultType){
+        List<DryFaultRecord> result = new ArrayList<>();
+        //鏁版嵁鏍锋湰锛�"eqp_fault": "婊氱瓛闄嶈秴鏃�-鎶ヨ,椋庢満杩囨祦鎶ヨ,婊氱瓛鍗囪秴鏃�-鎶ヨ,椋庣鍗囨姤璀�",
+        System.err.println((faultType == 1 ? "绫诲瀷锛氭晠闅�" : "绫诲瀷锛氭姤璀�") +  DateUtils.formatDateTime()+"--"+fault);
+        //redis涓殑鏁呴殰
+        Map<Object, Object> rFauMap = redisUtil.hmget(MqttConstant.MQTT_EQP_FAULT);
+        Map<String, Object> redFauMap = rFauMap.entrySet().stream()
+                .collect(Collectors.toMap(
+                        entry -> entry.getKey().toString(),  // 閿浆鎹负瀛楃涓�
+                        entry -> entry.getValue()
+                ));
+
+        //娌℃湁鐢熸垚宸ュ崟鐨勬晠闅滄暟鎹笉瀛樺偍
+        if(StringUtils.isEmpty(orderId)){
+            return result;
+        }
+
+        if(StringUtils.isEmpty(fault) && rFauMap.isEmpty()){
+            return result;
+        }
+        //1.瑙f瀽鏁版嵁
+        String[] eqpFaults = fault.split(",");
+        Map<String,DryFaultRecord> realFauMap = new HashMap<>();
+        for (int i = 0; i < eqpFaults.length; i++) {
+            String eqpFault = eqpFaults[i];
+            //閬垮厤绌哄瓧绗︿覆
+            if(StringUtils.isEmpty(eqpFault)) continue;
+            //1.1妫�鏌qtt涓槸鍚﹀凡瀛樺湪杩欎釜鏁呴殰
+            String redisKey = String.format("%s_%s_%s", tenantId, machineId,eqpFault);
+            String  rFault = (String) redisUtil.get(redisKey);
+            //1.2濡傛灉redis涓嶅瓨鍦ㄥ垯瀛樺叆锛堝瓨鏁呴殰寮�濮嬶級
+            if(rFault ==null){
+                //缁勮缂撳瓨鏁版嵁
+                DryFaultRecord faultRecord = new DryFaultRecord(orderId,eqpFault,faultType,new Date(),null);
+                realFauMap.put(redisKey,faultRecord);
+            }
+
+        }
+
+        //1.3缂撳瓨鑷硆edis
+        //鍚堝苟鏁版嵁
+        realFauMap.forEach((key, value) -> redFauMap.putIfAbsent(key, value));
+        //娌℃湁鏂版晠闅滄暟鎹笉鐢ㄨ鐩�
+        if(!realFauMap.isEmpty()){
+            redisUtil.hmset(MqttConstant.MQTT_EQP_FAULT,redFauMap);
+        }
+
+
+
+        //2妫�娴嬪凡缁撴潫鐨勬晠闅�
+        //2.1濡傛灉瀹炴椂鏁版嵁涓嶅瓨鍦╮edis瀛樺湪鍒欎唬琛ㄦ晠闅滅粨鏉燂紝瀛樺叆鏁版嵁搴�
+        Map<Object, Object> curFauMap = redisUtil.hmget(MqttConstant.MQTT_EQP_FAULT);
+        curFauMap.keySet().stream()
+                //鐗瑰埆娉ㄦ剰锛屽涓姤璀︾被鍨嬪叡鐢ㄦ柟娉曢渶瑕佸尯鍒嗙被鍨�
+                .filter(key -> !realFauMap.containsKey(key) && ((DryFaultRecord)curFauMap.get(key)).getFaultType() == faultType)
+                .forEach(key -> {
+                    System.err.println((faultType == 1 ? "绫诲瀷锛氭晠闅�" : "绫诲瀷锛氭姤璀�") +  DateUtils.formatDateTime()+"瀛樺叆鏁版嵁搴�");
+                    DryFaultRecord record = (DryFaultRecord)redFauMap.get(key);
+                    record.setEndTime(new Date());
+                    faultRecordService.save(record);
+                    redisUtil.hdel(MqttConstant.MQTT_EQP_FAULT,key);
+
+                    result.add(record);
+
+                });
+
+        return result;
+    }
+
+
+
 }

--
Gitblit v1.9.3