干燥机配套车间生产管理系统/云平台服务端
zhuguifei
3 天以前 b38019aae593a66c16f7e75d6e37d14eb8d2c42e
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java
@@ -15,6 +15,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.*;
@@ -36,6 +37,8 @@
  private String mqttClientId;
  @Value(value = "${jeecg.mqtt.role}")
  private String role;
  @Value(value = "${jeecg.mqtt.enable}")
  private boolean enable;
  @Autowired
  private MqttSampleCallback mqttSampleCallback;
@@ -59,6 +62,7 @@
   * mqtt连接配置
   */
  private void conn() {
    if (!isEnable()) return;
    MemoryPersistence persistence = new MemoryPersistence();
    MqttConnectOptions mqttConnOpt = new MqttConnectOptions();
    mqttConnOpt.setUserName(mqttName);
@@ -98,8 +102,8 @@
            mqttClient.subscribe(MqttConstant.MOBILE_UP);
            System.err.println("admin订阅" + MqttConstant.MOBILE_UP);
            // 订阅租户实时数据
            mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA);
            System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA);
            mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA_EQP);
            System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA_EQP);
            // 订阅租户报警数据
            mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REAL_FAULT_DATA);
            System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REAL_FAULT_DATA);
@@ -132,6 +136,7 @@
  //重连
  private void reconn() {
    if (!isEnable()) return;
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
@@ -174,15 +179,17 @@
      for (int i = 0; i < data.size(); i++) {
        JSONObject obj = data.getJSONObject(i);
        JSONObject item = new JSONObject();
        //clientid
        String clientid = obj.getString("clientid");
        item.put("clientid", clientid);
        //TODO 校验租户id是否存在
        if(!clientid.matches("^[^-]+-[^-]+-[^-]+$"))  continue;
        //username
        item.put("username", obj.get("username"));
        //连接时间
        String st = obj.getString("connected_at");
        String upTime = DateUtils.zone2Str(st);
        item.put("connectedAt", upTime);
        //clientid
        String clientid = obj.getString("clientid");
        item.put("clientid", clientid);
        //是否连接
        Boolean connected = obj.getBoolean("connected");
        item.put("connected", connected);
@@ -191,7 +198,7 @@
          String[] info = clientid.split("-");
          item.put("type", info[0]);
          item.put("tenantId", info[1]);
          //item.put("code", info[2]);
          item.put("code", info[2]);
          if (connected) {
            redisUtil.hset(String.format(MqttConstant.MQTT_ONLINE_CLIENT,item.get("tenantId").toString()) , clientid, item);