liulingling.177216
2024-08-26 349f1cfc5fa77fbc636d542df0d8050fddec48c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.dingzhuo.energy.project.conglomeratepush.mqtt;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.dingzhuo.energy.common.core.lang.UUID;
import com.dingzhuo.energy.project.gateway.domain.GatewayHbtLog;
import com.dingzhuo.energy.project.gateway.domain.GatewaySetting;
import com.dingzhuo.energy.project.gateway.service.IGatewayHbtLogService;
import com.dingzhuo.energy.project.gateway.service.IGatewaySettingService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Iterator;
 
/**
 * MQTT回调函数
 *MqttClientCallback 回调函数类中可能不能用 @Autowired 注入的, 解决办法  SpringContextHolder 类引入
 * @author zhw
 * @since 2022/04/05
 */
@Slf4j
@Component
public class InitCallback implements MqttCallback {
 
  @Autowired
  private IGatewayHbtLogService gatewayHbtLogService;
 
  @Autowired
  private IGatewaySettingService gatewaySettingService;
 
  /**
   * MQTT 断开连接会执行此方法
   */
  @Override
  public void connectionLost(Throwable cause)
  {
    log.error(cause.getMessage(), cause);
  }
 
  /**
   * publish发布成功后会执行到这里
   */
  @Override
  public void deliveryComplete(IMqttDeliveryToken token) {
    //发布完成后 记录日志信息 并断开连接
  }
 
  /**
   * subscribe订阅后得到的消息会执行到这里
   */
  @Override
  public void messageArrived(String topic, MqttMessage message) {
    String result = new String(message.getPayload(), StandardCharsets.UTF_8);
     try {
//      JSONObject jsonObject = JSON.parseObject(result);
//       Date hbtTime =  jsonObject.getDate("ts");
//       Iterator<String> keys =  jsonObject.getJSONObject("d").keySet().iterator();
//       if(keys.hasNext())
//       {
//         String key = keys.next();
//         //更新实时状态
//         GatewaySetting gatewaySetting = new GatewaySetting();
//         gatewaySetting.setGatewayNum(key);
//         gatewaySetting.setHbtTime(hbtTime);
//         gatewaySettingService.updateGatewaySettingByNum(gatewaySetting);
//
//         //记录日志表
//         GatewayHbtLog gatewayHbtLog = new GatewayHbtLog();
//         gatewayHbtLog.setId(UUID.fastUUID().toString());
//         gatewayHbtLog.setGatewayNo(key);
//         gatewayHbtLog.setHbtTime(hbtTime);
//         gatewayHbtLog.setContent(result);
//         gatewayHbtLogService.insertGatewayHbtLog(gatewayHbtLog);
//       }
 
    } catch (JSONException e) {
      log.error("JSON Format Parsing Exception : {}", result);
    }
  }
}