干燥机配套车间生产管理系统/云平台服务端
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
@@ -3,22 +3,30 @@
import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.MqttConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.dry.api.EmqxApi;
import org.jeecg.modules.dry.entity.DryEquipment;
import org.jeecg.modules.dry.entity.DryShop;
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.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;
@Slf4j
@Component
@Scope("prototype")
public class MqttSampleCallback implements MqttCallback {
@@ -35,6 +43,15 @@
  @Autowired
  private IDryRealTimeDataService realTimeDataService;
  @Autowired
  private IDryEquipmentService equipmentService;
  @Autowired
  private IDryShopService dryShopService;
  @Override
@@ -174,7 +191,26 @@
        }
        break;
      case MqttConstant.TENANT_UP_PREFIX_EQU:
        try {
          DryEquipment equipment = (DryEquipment) messageJson.get("equipment");
          DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(equipment.getTenantId() + "", equipment.getCode());
          if (dryEquipment == null) {
            equipmentService.save(equipment);
          }
          // 获取设备所属车间
          DryShop shop = (DryShop) messageJson.get("shop");
          shop.setTenantId(equipment.getTenantId());
          dryShopService.save(shop);
        } catch (Exception e) {
          e.printStackTrace();
        }
    }
  }
@@ -218,6 +254,30 @@
        break;
      case MqttConstant.SERVICE_REQ_EQU_TOPIC:
        log.debug("收到设备详细信息查询请求");
        // 根据设备编码查询设备信息
        String code = messageJson.getString("code");
        DryEquipment equipmentVo = equipmentService.selectByTenantIdEquipmentId(TenantContext.getTenant(),code);
        // 根据设备车间id查询车间信息
        DryShop shop = dryShopService.getById(equipmentVo.getShopId());
        JSONObject res = new JSONObject();
        res.put("tenant", TenantContext.getTenant());
        res.put("equipment", equipmentVo);
        res.put("shop", shop);
        try {
          MqttMessage sendMessage = new MqttMessage(res.toJSONString().getBytes());
          sendMessage.setQos(0);
          mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_EQU, sendMessage);
          // baseCommonService.addLog(res.toString(), CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_2);
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
    }
  }