package com.doumee.core.conditoner;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.conditoner.model.ConditionerConstant;
import com.doumee.core.conditoner.model.request.*;
import com.doumee.core.conditoner.model.response.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 智精灵空调平台 HTTP 工具类。
*
* 协议:GET 读取(Query 参数);POST 控制/写入(JSON Body)。
* 成功标识 {@code code=200}。登录后公共参数 {@code kt_token}、{@code kt_dwid}、{@code kt_sonid}。
*
*/
@Slf4j
public class ConditionerUtil {
private static final int HTTP_RETRY = 3;
private ConditionerUtil() {
}
// ==================== 二、基础控制 ====================
public static ConditionerBaseResponse login() {
return login(null);
}
public static ConditionerBaseResponse login(LoginRequest req) {
LoginRequest r = req != null ? req : new LoginRequest();
JSONObject body = new JSONObject();
body.put("username", StringUtils.isNotBlank(r.getUsername()) ? r.getUsername() : ConditionerConstant.username);
body.put("password", StringUtils.isNotBlank(r.getPassword()) ? r.getPassword() : ConditionerConstant.password);
try {
String raw = doPostRaw("/login", body.toJSONString());
ConditionerBaseResponse resp = parseObjectResponse(raw, LoginDataResponse.class);
applyLoginSession(resp);
return resp;
} catch (Exception e) {
log.error("conditioner login failed", e);
return null;
}
}
public static ConditionerBaseResponse> getDevList(ConditionerSessionRequest req) {
return getList("/getDevList", req, DeviceStatusResponse.class);
}
public static ConditionerBaseResponse getDevOne(GetDevOneRequest req) {
if (req == null) {
return null;
}
req.fillSessionDefaults();
try {
String raw = doGetRaw("/getDevOne", toQueryMap(req));
return parseObjectResponse(raw, DeviceStatusResponse.class);
} catch (Exception e) {
log.error("conditioner getDevOne failed", e);
return null;
}
}
public static ConditionerBaseResponse