干燥机配套车间生产管理系统/云平台服务端
zhuguifei
2024-11-18 15206b16b0121ba7331496c0f0946e4004ad4507
添加故障上报、存储
已添加6个文件
已修改6个文件
533 ■■■■■ 文件已修改
jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/entity/DryFaultRecord.java 102 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryFaultRecordController.java 167 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/DryFaultRecordMapper.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mapper/xml/DryFaultRecordMapper.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java 51 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryFaultRecordService.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/IDryRealTimeDataService.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryFaultRecordServiceImpl.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java 124 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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 @@
  /**************************服务端向租户端请求数据end*******************************/
  /**************************start*******************************/
  /**************************end*******************************/
  //redis缓存
  String MQTT_ONLINE_CLIENT = "mqtt:online:client::";
  String MQTT_EQP_FAULT = "mqtt:eqp:fault";
}
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;
    }
}
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);
    }
}
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) {
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> {
}
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>
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角色)启动时查询所有设备并缓存到redis
   */
  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解析(注意配置文件中clientid格式  ä¾‹ï¼š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);
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解析(注意配置文件中clientid格式  ä¾‹ï¼š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 {
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> {
}
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);
}
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 {
}
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 {
                //解析存储报警数据
                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;
    }
    /**
     * è§£æžå­˜å‚¨æ•…障数据
     * 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.解析数据
        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检查mqtt中是否已存在这个故障
            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缓存至redis
        //合并数据
        realFauMap.forEach((key, value) -> redFauMap.putIfAbsent(key, value));
        //没有新故障数据不用覆盖
        if(!realFauMap.isEmpty()){
            redisUtil.hmset(MqttConstant.MQTT_EQP_FAULT,redFauMap);
        }
        //2检测已结束的故障
        //2.1如果实时数据不存在redis存在则代表故障结束,存入数据库
        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;
    }
}