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-api/src/main/java/org/jeecg/modules/dry/entity/DryFaultRecord.java                   |  102 +++++++++++
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java |  124 +++++++++++++
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/DryFaultRecordMapper.java             |   14 +
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java                 |   51 ++++-
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java                         |   19 +
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryFaultRecordService.java          |   15 +
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryRealTimeDataService.java         |    7 
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryFaultRecordServiceImpl.java  |   19 ++
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java    |    4 
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryFaultRecordController.java     |  167 ++++++++++++++++++
 jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java                                         |    6 
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/xml/DryFaultRecordMapper.xml          |    5 
 12 files changed, 510 insertions(+), 23 deletions(-)

diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java
index 4fb4240..addf80e 100644
--- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java
+++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java
@@ -54,6 +54,7 @@
 
   String TENANT_UP_PREFIX = "tenant/up";
   String TENANT_UP_PREFIX_REALTIME_DATA = TENANT_UP_PREFIX + "/realTime/data";
+  String TENANT_UP_PREFIX_FAULT_DATA = TENANT_UP_PREFIX + "/fault/data";
 
   String TENANT_UP_PREFIX_EQU = TENANT_UP_PREFIX + "/equipment";
 
@@ -67,8 +68,13 @@
 
 
   /**************************鏈嶅姟绔悜绉熸埛绔姹傛暟鎹甧nd*******************************/
+
+  /**************************start*******************************/
+  /**************************end*******************************/
+
   //redis缂撳瓨
   String MQTT_ONLINE_CLIENT = "mqtt:online:client::";
+  String MQTT_EQP_FAULT = "mqtt:eqp:fault";
 
 
 }
diff --git a/jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/entity/DryFaultRecord.java b/jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/entity/DryFaultRecord.java
new file mode 100644
index 0000000..a4ab146
--- /dev/null
+++ b/jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/entity/DryFaultRecord.java
@@ -0,0 +1,102 @@
+package org.jeecg.modules.dry.entity;
+
+import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
+import java.util.Date;
+import java.math.BigDecimal;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import lombok.Data;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.jeecgframework.poi.excel.annotation.Excel;
+import org.jeecg.common.aspect.annotation.Dict;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+/**
+ * @Description: dry_eqp_fault
+ * @Author: jeecg-boot
+ * @Date:   2024-11-13
+ * @Version: V1.0
+ */
+@Data
+@TableName("dry_fault_record")
+@Accessors(chain = true)
+@EqualsAndHashCode(callSuper = false)
+@ApiModel(value="dry_eqp_fault瀵硅薄", description="dry_eqp_fault")
+public class DryFaultRecord implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+	/**id*/
+	@TableId(type = IdType.ASSIGN_ID)
+    @ApiModelProperty(value = "id")
+    private String id;
+	/**宸ュ崟id*/
+	@Excel(name = "宸ュ崟id", width = 15)
+    @ApiModelProperty(value = "宸ュ崟id")
+    private String orderId;
+	/**鏁呴殰鍚嶇О*/
+	@Excel(name = "鏁呴殰鍚嶇О", width = 15)
+    @ApiModelProperty(value = "鏁呴殰鍚嶇О")
+    private String faultName;
+    /**鏁呴殰code*/
+    @Excel(name = "鏁呴殰code", width = 15)
+    @ApiModelProperty(value = "宸ュ崟id")
+    private String faultCode;
+	/**鏁呴殰绫诲瀷*/
+	@Excel(name = "鏁呴殰绫诲瀷", width = 15)
+    @ApiModelProperty(value = "鏁呴殰绫诲瀷")
+    private Integer faultType;
+	/**鏁呴殰鎻忚堪*/
+	@Excel(name = "鏁呴殰鎻忚堪", width = 15)
+    @ApiModelProperty(value = "鏁呴殰鎻忚堪")
+    private String faultDesc;
+	/**鏁呴殰鐘舵��*/
+	@Excel(name = "鏁呴殰鐘舵��", width = 15)
+    @ApiModelProperty(value = "鏁呴殰鐘舵��")
+    private Integer faultStatu;
+	/**寮�濮嬫椂闂�*/
+	@Excel(name = "寮�濮嬫椂闂�", width = 15, format = "yyyy-MM-dd HH:mm:ss")
+	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "寮�濮嬫椂闂�")
+    private Date startTime;
+	/**缁撴潫鏃堕棿*/
+	@Excel(name = "缁撴潫鏃堕棿", width = 15, format = "yyyy-MM-dd HH:mm:ss")
+	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "缁撴潫鏃堕棿")
+    private Date endTime;
+	/**鍒涘缓浜�*/
+    @ApiModelProperty(value = "鍒涘缓浜�")
+    private String createBy;
+	/**鍒涘缓鏃ユ湡*/
+	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
+    @DateTimeFormat(pattern="yyyy-MM-dd")
+    @ApiModelProperty(value = "鍒涘缓鏃ユ湡")
+    private Date createTime;
+	/**鏇存柊浜�*/
+    @ApiModelProperty(value = "鏇存柊浜�")
+    private String updateBy;
+	/**鏇存柊鏃ユ湡*/
+	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
+    @DateTimeFormat(pattern="yyyy-MM-dd")
+    @ApiModelProperty(value = "鏇存柊鏃ユ湡")
+    private Date updateTime;
+
+    public DryFaultRecord() {
+    }
+
+    public DryFaultRecord(String orderId, String faultName,Integer faultType, Date startTime, Date endTime) {
+        this.orderId = orderId;
+        this.faultName = faultName;
+        this.startTime = startTime;
+        this.endTime = endTime;
+        this.faultType = faultType;
+    }
+}
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryFaultRecordController.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryFaultRecordController.java
new file mode 100644
index 0000000..0e396cf
--- /dev/null
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryFaultRecordController.java
@@ -0,0 +1,167 @@
+package org.jeecg.modules.dry.controller;
+
+import java.util.Arrays;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.jeecg.common.api.vo.Result;
+import org.jeecg.common.system.query.QueryGenerator;
+
+import org.jeecg.modules.dry.entity.DryFaultRecord;
+import org.jeecg.modules.dry.service.IDryFaultRecordService;
+
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import lombok.extern.slf4j.Slf4j;
+
+import org.jeecg.common.system.base.controller.JeecgController;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import org.springframework.web.servlet.ModelAndView;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.jeecg.common.aspect.annotation.AutoLog;
+import org.apache.shiro.authz.annotation.RequiresPermissions;
+
+ /**
+ * @Description: dry_eqp_fault
+ * @Author: jeecg-boot
+ * @Date:   2024-11-13
+ * @Version: V1.0
+ */
+@Api(tags="dry_eqp_fault")
+@RestController
+@RequestMapping("/dry/dryEqpFault")
+@Slf4j
+public class DryFaultRecordController extends JeecgController<DryFaultRecord, IDryFaultRecordService> {
+	@Autowired
+	private IDryFaultRecordService dryEqpFaultService;
+	
+	/**
+	 * 鍒嗛〉鍒楄〃鏌ヨ
+	 *
+	 * @param dryEqpFault
+	 * @param pageNo
+	 * @param pageSize
+	 * @param req
+	 * @return
+	 */
+	//@AutoLog(value = "dry_eqp_fault-鍒嗛〉鍒楄〃鏌ヨ")
+	@ApiOperation(value="dry_eqp_fault-鍒嗛〉鍒楄〃鏌ヨ", notes="dry_eqp_fault-鍒嗛〉鍒楄〃鏌ヨ")
+	@GetMapping(value = "/list")
+	public Result<IPage<DryFaultRecord>> queryPageList(DryFaultRecord dryEqpFault,
+                                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
+                                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
+                                                       HttpServletRequest req) {
+		QueryWrapper<DryFaultRecord> queryWrapper = QueryGenerator.initQueryWrapper(dryEqpFault, req.getParameterMap());
+		Page<DryFaultRecord> page = new Page<DryFaultRecord>(pageNo, pageSize);
+		IPage<DryFaultRecord> pageList = dryEqpFaultService.page(page, queryWrapper);
+		return Result.OK(pageList);
+	}
+	
+	/**
+	 *   娣诲姞
+	 *
+	 * @param dryEqpFault
+	 * @return
+	 */
+	@AutoLog(value = "dry_eqp_fault-娣诲姞")
+	@ApiOperation(value="dry_eqp_fault-娣诲姞", notes="dry_eqp_fault-娣诲姞")
+	@RequiresPermissions("org.jeecg.modules.dry.mqtt:dry_eqp_fault:add")
+	@PostMapping(value = "/add")
+	public Result<String> add(@RequestBody DryFaultRecord dryEqpFault) {
+		dryEqpFaultService.save(dryEqpFault);
+		return Result.OK("娣诲姞鎴愬姛锛�");
+	}
+	
+	/**
+	 *  缂栬緫
+	 *
+	 * @param dryEqpFault
+	 * @return
+	 */
+	@AutoLog(value = "dry_eqp_fault-缂栬緫")
+	@ApiOperation(value="dry_eqp_fault-缂栬緫", notes="dry_eqp_fault-缂栬緫")
+	@RequiresPermissions("org.jeecg.modules.dry.mqtt:dry_eqp_fault:edit")
+	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
+	public Result<String> edit(@RequestBody DryFaultRecord dryEqpFault) {
+		dryEqpFaultService.updateById(dryEqpFault);
+		return Result.OK("缂栬緫鎴愬姛!");
+	}
+	
+	/**
+	 *   閫氳繃id鍒犻櫎
+	 *
+	 * @param id
+	 * @return
+	 */
+	@AutoLog(value = "dry_eqp_fault-閫氳繃id鍒犻櫎")
+	@ApiOperation(value="dry_eqp_fault-閫氳繃id鍒犻櫎", notes="dry_eqp_fault-閫氳繃id鍒犻櫎")
+	@RequiresPermissions("org.jeecg.modules.dry.mqtt:dry_eqp_fault:delete")
+	@DeleteMapping(value = "/delete")
+	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
+		dryEqpFaultService.removeById(id);
+		return Result.OK("鍒犻櫎鎴愬姛!");
+	}
+	
+	/**
+	 *  鎵归噺鍒犻櫎
+	 *
+	 * @param ids
+	 * @return
+	 */
+	@AutoLog(value = "dry_eqp_fault-鎵归噺鍒犻櫎")
+	@ApiOperation(value="dry_eqp_fault-鎵归噺鍒犻櫎", notes="dry_eqp_fault-鎵归噺鍒犻櫎")
+	@RequiresPermissions("org.jeecg.modules.dry.mqtt:dry_eqp_fault:deleteBatch")
+	@DeleteMapping(value = "/deleteBatch")
+	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
+		this.dryEqpFaultService.removeByIds(Arrays.asList(ids.split(",")));
+		return Result.OK("鎵归噺鍒犻櫎鎴愬姛!");
+	}
+	
+	/**
+	 * 閫氳繃id鏌ヨ
+	 *
+	 * @param id
+	 * @return
+	 */
+	//@AutoLog(value = "dry_eqp_fault-閫氳繃id鏌ヨ")
+	@ApiOperation(value="dry_eqp_fault-閫氳繃id鏌ヨ", notes="dry_eqp_fault-閫氳繃id鏌ヨ")
+	@GetMapping(value = "/queryById")
+	public Result<DryFaultRecord> queryById(@RequestParam(name="id",required=true) String id) {
+		DryFaultRecord dryEqpFault = dryEqpFaultService.getById(id);
+		if(dryEqpFault==null) {
+			return Result.error("鏈壘鍒板搴旀暟鎹�");
+		}
+		return Result.OK(dryEqpFault);
+	}
+
+    /**
+    * 瀵煎嚭excel
+    *
+    * @param request
+    * @param dryEqpFault
+    */
+    @RequiresPermissions("org.jeecg.modules.dry.mqtt:dry_eqp_fault:exportXls")
+    @RequestMapping(value = "/exportXls")
+    public ModelAndView exportXls(HttpServletRequest request, DryFaultRecord dryEqpFault) {
+        return super.exportXls(request, dryEqpFault, DryFaultRecord.class, "dry_eqp_fault");
+    }
+
+    /**
+      * 閫氳繃excel瀵煎叆鏁版嵁
+    *
+    * @param request
+    * @param response
+    * @return
+    */
+    @RequiresPermissions("org.jeecg.modules.dry.mqtt:dry_eqp_fault:importExcel")
+    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
+    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
+        return super.importExcel(request, response, DryFaultRecord.class);
+    }
+
+}
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java
index f786e32..09ee499 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java
@@ -3,7 +3,9 @@
 
 import ai.djl.modality.Classifications;
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.thread.ThreadUtil;
 import com.alibaba.druid.support.json.JSONUtils;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -97,6 +99,8 @@
                 mqttMessage.setQos(0);
                 mqttMessage.setPayload(JSONObject.toJSONString(realTimeDataVo).getBytes());
                 mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA,mqttMessage);
+                //澶勭悊鏁呴殰淇℃伅
+                dryRealTimeDataService.fitFultRecord(realTimeDataVo);
             }
 
         } catch (MqttException e) {
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/DryFaultRecordMapper.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/DryFaultRecordMapper.java
new file mode 100644
index 0000000..321ca4c
--- /dev/null
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/DryFaultRecordMapper.java
@@ -0,0 +1,14 @@
+package org.jeecg.modules.dry.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.jeecg.modules.dry.entity.DryFaultRecord;
+
+/**
+ * @Description: dry_eqp_fault
+ * @Author: jeecg-boot
+ * @Date:   2024-11-13
+ * @Version: V1.0
+ */
+public interface DryFaultRecordMapper extends BaseMapper<DryFaultRecord> {
+
+}
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/xml/DryFaultRecordMapper.xml b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/xml/DryFaultRecordMapper.xml
new file mode 100644
index 0000000..c02ee05
--- /dev/null
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/xml/DryFaultRecordMapper.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="org.jeecg.modules.dry.mapper.DryFaultRecordMapper">
+
+</mapper>
\ No newline at end of file
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java
index 21a69b6..41d3db4 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java
@@ -97,6 +97,9 @@
             // 璁㈤槄绉熸埛瀹炴椂鏁版嵁
             mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA);
             System.out.println("admin璁㈤槄" + MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA);
+            // 璁㈤槄绉熸埛鎶ヨ鏁版嵁
+            mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_FAULT_DATA);
+            System.out.println("admin璁㈤槄" + MqttConstant.TENANT_UP_PREFIX_FAULT_DATA);
             mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_EQU);
             System.out.println("admin璁㈤槄" + MqttConstant.TENANT_UP_PREFIX_EQU);
             initClients();
@@ -150,7 +153,7 @@
    * 鏈嶅姟绔紙admin瑙掕壊锛夊惎鍔ㄦ椂鏌ヨ鎵�鏈夎澶囧苟缂撳瓨鍒皉edis
    */
   private void initClients() {
-    redisUtil.del(MqttConstant.MQTT_ONLINE_CLIENT);
+    redisUtil.removeAll(MqttConstant.MQTT_ONLINE_CLIENT);
 
     JSONObject clients = emqxApi.queryEmqx(EmqxApi.CMD_CLIENTS);
     //TODO 鏍规嵁emqx杩斿洖缂栧啓瀹炰綋绫�
@@ -171,11 +174,15 @@
         //鏄惁杩炴帴
         Boolean connected = obj.getBoolean("connected");
         item.put("connected", connected);
-        //
-        String[] info = clientid.split("-");
-        item.put("type", info[0]);
-        item.put("tenantId", info[1]);
-        item.put("code", info[2]);
+        //鏍规嵁clientid瑙f瀽(娉ㄦ剰閰嶇疆鏂囦欢涓璫lientid鏍煎紡  渚嬶細client-1000)
+        try {
+          String[] info = clientid.split("-");
+          item.put("type", info[0]);
+          item.put("tenantId", info[1]);
+          //item.put("code", info[2]);
+        }catch (Exception e){
+          e.printStackTrace();
+        }
 
         if (connected) {
           redisUtil.set(MqttConstant.MQTT_ONLINE_CLIENT + clientid, item);
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
index e9e23cf..1efee35 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
@@ -2,7 +2,9 @@
 
 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 lombok.extern.slf4j.Slf4j;
 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
 import org.eclipse.paho.client.mqttv3.MqttCallback;
@@ -16,17 +18,17 @@
 import org.jeecg.modules.dry.api.EmqxApi;
 import org.jeecg.modules.dry.entity.DryEqpType;
 import org.jeecg.modules.dry.entity.DryEquipment;
+import org.jeecg.modules.dry.entity.DryFaultRecord;
 import org.jeecg.modules.dry.entity.DryShop;
-import org.jeecg.modules.dry.service.IDryEqpTypeService;
-import org.jeecg.modules.dry.service.IDryEquipmentService;
-import org.jeecg.modules.dry.service.IDryRealTimeDataService;
-import org.jeecg.modules.dry.service.IDryShopService;
+import org.jeecg.modules.dry.service.*;
 import org.jeecg.modules.dry.vo.DryEquipmentVo;
 import org.jeecg.modules.dry.vo.RealTimeDataVo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Scope;
 import org.springframework.stereotype.Component;
+
+import java.util.List;
 
 @Slf4j
 @Component
@@ -56,16 +58,20 @@
   @Autowired
   private IDryShopService dryShopService;
 
+  @Autowired
+  private IDryFaultRecordService faultRecordService;
+
 
 
 
   @Override
   public void connectionLost(Throwable throwable) {
     System.err.println("杩炴帴鏂紑锛氾細鎺夌嚎");
+    System.err.println("杩炴帴鏂紑锛氾細"+throwable.toString());
   }
 
   @Override
-  public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
+  public void messageArrived(String topic, MqttMessage mqttMessage) {
     System.out.println("鏀跺埌娑堟伅: \n  topic锛�" + topic + "\n  Qos锛�" + mqttMessage.getQos() + "\n  payload锛�"
       + new String(mqttMessage.getPayload()));
 
@@ -90,12 +96,15 @@
             item.put("clientid", clientid);
             //鏄惁杩炴帴
             item.put("connected", true);
-            //
-            String[] info = clientid.split("-");
-            item.put("type", info[0]);
-            item.put("tenantId", info[1]);
-            item.put("code", info[2]);
-
+            //鏍规嵁clientid瑙f瀽(娉ㄦ剰閰嶇疆鏂囦欢涓璫lientid鏍煎紡  渚嬶細client-1000)
+            try {
+              String[] info = clientid.split("-");
+              item.put("type", info[0]);
+              item.put("tenantId", info[1]);
+              //item.put("code", info[2]);
+            }catch (Exception e){
+              e.printStackTrace();
+            }
             redisUtil.set(MqttConstant.MQTT_ONLINE_CLIENT + clientid, item);
             System.err.println(String.format("璁惧: %s涓婄嚎", clientid));
           }
@@ -150,7 +159,7 @@
       messageJson.put("timestamp", DateUtils.zone2Str(messageJson.get("timestamp").toString()));
     }
     // 瀹炴椂鏁版嵁涓婁紶澶绻佷笖鏁版嵁鍐呭瓒呰繃瀛楁澶у皬涓嶈褰曟棩蹇�
-    if (!topic.equals(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA)){
+    if (!topic.equals(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA) && !topic.equals(MqttConstant.TENANT_UP_PREFIX_FAULT_DATA)){
       baseCommonService.addLog(message, CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_1);
     }
 
@@ -203,6 +212,24 @@
         });
 
         break;
+      // 鎺ユ敹璁惧鎶ヨ鏁版嵁
+      case MqttConstant.TENANT_UP_PREFIX_FAULT_DATA:
+        ThreadUtil.execute(() -> {
+          try {
+
+            JSONObject jsonObject = JSON.parseObject(message);
+            List<DryFaultRecord> faultRecords = JSON.parseArray(jsonObject.get("data").toString(), DryFaultRecord.class);
+            System.err.println(faultRecords.toString());
+            faultRecordService.saveBatch(faultRecords);
+
+          } catch (Exception e) {
+            e.printStackTrace();
+          }
+        });
+
+        break;
+
+
       case MqttConstant.TENANT_UP_PREFIX_EQU:
         ThreadUtil.execute(() -> {
           try {
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryFaultRecordService.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryFaultRecordService.java
new file mode 100644
index 0000000..6173b97
--- /dev/null
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryFaultRecordService.java
@@ -0,0 +1,15 @@
+package org.jeecg.modules.dry.service;
+
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import org.jeecg.modules.dry.entity.DryFaultRecord;
+
+/**
+ * @Description: dry_eqp_fault
+ * @Author: jeecg-boot
+ * @Date:   2024-11-13
+ * @Version: V1.0
+ */
+public interface IDryFaultRecordService extends IService<DryFaultRecord> {
+
+}
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryRealTimeDataService.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryRealTimeDataService.java
index 713ad91..60a6bbd 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryRealTimeDataService.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryRealTimeDataService.java
@@ -15,4 +15,11 @@
     Result<?> queryWorkshopStatistics(RealTimeDataVo realTimeDataVo);
 
     Result<?> statisticsDataHandle(StatisticsDataVo statsDataVo);
+
+    /**
+     * 閫氳繃瀹炴椂鏁版嵁鏀堕泦澶勭悊鏁呴殰淇℃伅
+     * @param realTimeDataVo
+     * @return
+     */
+    Result<?> fitFultRecord(RealTimeDataVo realTimeDataVo);
 }
diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryFaultRecordServiceImpl.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryFaultRecordServiceImpl.java
new file mode 100644
index 0000000..ce045e9
--- /dev/null
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryFaultRecordServiceImpl.java
@@ -0,0 +1,19 @@
+package org.jeecg.modules.dry.service.impl;
+
+
+import org.jeecg.modules.dry.entity.DryFaultRecord;
+import org.jeecg.modules.dry.mapper.DryFaultRecordMapper;
+import org.jeecg.modules.dry.service.IDryFaultRecordService;
+import org.springframework.stereotype.Service;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+
+/**
+ * @Description: dry_eqp_fault
+ * @Author: jeecg-boot
+ * @Date:   2024-11-13
+ * @Version: V1.0
+ */
+@Service
+public class DryFaultRecordServiceImpl extends ServiceImpl<DryFaultRecordMapper, DryFaultRecord> implements IDryFaultRecordService {
+
+}
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