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;
|
}
|
}
|