干燥机配套车间生产管理系统/云平台服务端
guifei zhu
2024-08-21 2297b10a86a233155006297d451b142d3f7ce3db
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
package org.jeecg.modules.dry.api;
 
import com.alibaba.fastjson.JSONObject;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
 
/**
 * emqx rest api
 * 入参出参参考:https://docs.emqx.com/zh/emqx/v5.7/admin/api-docs.html
 */
@Component
public class EmqxApi {
  @Value(value = "${jeecg.mqtt.emqx_api_key}")
  private String emqxApiKey;
  @Value(value = "${jeecg.mqtt.emqx_secret_key}")
  private String emqxSecretKey;
  @Value(value = "${jeecg.mqtt.emqx_base}")
  private String emqxBaseUrl;
 
  //所有客户端
  public static final String CMD_CLIENTS = "/clients";
  //查询一个客户端状态
  public static final String CMD_CLIENTS_CLIENT = "/clients/%s";
 
  /**
   * EMQX接口
   *
   * @return
   */
  public JSONObject queryEmqx(String cmd) {
    try {
      OkHttpClient client = new OkHttpClient();
      Request request = new Request.Builder()
        .url(emqxBaseUrl + cmd)
        .header("Content-Type", "application/json")
        .header("Authorization", Credentials.basic(emqxApiKey, emqxSecretKey))
        .build();
 
      Response response = client.newCall(request).execute();
      String res = response.body().string();
      return JSONObject.parseObject(res);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}