对接n9e

This commit is contained in:
2025-07-09 16:34:21 +08:00
parent 6862ddbf58
commit cb312b1a74
4 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
package com.ycwl.basic.task;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import cn.hutool.http.HttpResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@EnableScheduling
public class N9eSyncTask {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Scheduled(fixedRate = 1000 * 60L)
public void syncN9e() {
// 构建Basic认证头
String auth = "Basic " + Base64.encode("user001:ccc26da7b9aba533cbb263a36c07dcc4");
// 构建请求体
JSONObject requestBody = new JSONObject();
JSONArray queries = new JSONArray();
JSONObject query = new JSONObject();
query.put("key", "group_ids");
query.put("op", "==");
JSONArray values = new JSONArray();
values.add(4);
query.put("values", values);
queries.add(query);
requestBody.put("queries", queries);
// 发送POST请求
HttpResponse response = HttpUtil.createPost("https://n9e.jerryyan.top/v1/n9e/target/list")
.header("Authorization", auth)
.header("Content-Type", "application/json")
.body(requestBody.toJSONString())
.execute();
JSONObject respData = JSON.parseObject(response.body());
if (StringUtils.isNotBlank(respData.getString("err"))) {
log.warn("N9E信息获取失败");
return;
}
JSONObject data = respData.getJSONObject("dat");
if (data.getInteger("total") <= 0) {
log.warn("N9E信息为空");
return;
}
JSONArray list = data.getJSONArray("list");
list.forEach(item -> {
JSONObject itemObj = (JSONObject) item;
String ident = itemObj.getString("ident");
Long updateAt = itemObj.getLong("update_at");
redisTemplate.opsForValue().set("ext_device:online:" + ident, updateAt.toString(), 1, TimeUnit.DAYS);
});
}
}