You've already forked FrameTour-BE
对接n9e
This commit is contained in:
11
src/main/java/com/ycwl/basic/mapper/ExtraDeviceMapper.java
Normal file
11
src/main/java/com/ycwl/basic/mapper/ExtraDeviceMapper.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.ycwl.basic.mapper;
|
||||||
|
|
||||||
|
import com.ycwl.basic.model.pc.device.resp.DeviceRespVO;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ExtraDeviceMapper {
|
||||||
|
List<DeviceRespVO> listExtraDeviceByScenicId(Long scenicId);
|
||||||
|
}
|
@@ -22,7 +22,9 @@ import com.ycwl.basic.service.pc.ScenicAccountService;
|
|||||||
import com.ycwl.basic.utils.ApiResponse;
|
import com.ycwl.basic.utils.ApiResponse;
|
||||||
import com.ycwl.basic.utils.JwtTokenUtil;
|
import com.ycwl.basic.utils.JwtTokenUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@@ -51,6 +53,10 @@ public class AppScenicServiceImpl implements AppScenicService {
|
|||||||
private ScenicAccountService scenicAccountService;
|
private ScenicAccountService scenicAccountService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private DeviceRepository deviceRepository;
|
private DeviceRepository deviceRepository;
|
||||||
|
@Autowired
|
||||||
|
private ExtraDeviceMapper extraDeviceMapper;
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApiResponse<PageInfo<ScenicAppVO>> pageQuery(ScenicReqQuery scenicReqQuery) {
|
public ApiResponse<PageInfo<ScenicAppVO>> pageQuery(ScenicReqQuery scenicReqQuery) {
|
||||||
@@ -126,6 +132,24 @@ public class AppScenicServiceImpl implements AppScenicService {
|
|||||||
deviceRespVO.setUpdateAt(null);
|
deviceRespVO.setUpdateAt(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
List<DeviceRespVO> extraDeviceList = extraDeviceMapper.listExtraDeviceByScenicId(scenicId);
|
||||||
|
for (DeviceRespVO deviceRespVO : extraDeviceList) {
|
||||||
|
if (redisTemplate.hasKey("ext_device:online:"+deviceRespVO.getNo())) {
|
||||||
|
String onlineTs = redisTemplate.opsForValue().get("ext_device:online:"+deviceRespVO.getNo());
|
||||||
|
if (!StringUtils.isNumeric(onlineTs)) {
|
||||||
|
deviceRespVO.setOnline(0);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Long ts = Long.parseLong(onlineTs);
|
||||||
|
Date keepaliveAt = new Date(ts*1000);
|
||||||
|
deviceRespVO.setUpdateAt(keepaliveAt);
|
||||||
|
deviceRespVO.setKeepaliveAt(keepaliveAt);
|
||||||
|
deviceRespVO.setOnline(keepaliveAt.getTime() > System.currentTimeMillis()-1000*60*5 ? 1 : 0);
|
||||||
|
} else {
|
||||||
|
deviceRespVO.setOnline(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
deviceRespVOList.addAll(0, extraDeviceList);
|
||||||
return ApiResponse.success(deviceRespVOList);
|
return ApiResponse.success(deviceRespVOList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
src/main/java/com/ycwl/basic/task/N9eSyncTask.java
Normal file
70
src/main/java/com/ycwl/basic/task/N9eSyncTask.java
Normal 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
11
src/main/resources/mapper/ExtraDeviceMapper.xml
Normal file
11
src/main/resources/mapper/ExtraDeviceMapper.xml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="com.ycwl.basic.mapper.ExtraDeviceMapper">
|
||||||
|
<select id="listExtraDeviceByScenicId" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO">
|
||||||
|
select d.id, d.ident as no, d.scenic_id, d.name, d.status, s.name as scenic_name
|
||||||
|
from extra_device d
|
||||||
|
left join scenic s on d.scenic_id = s.id
|
||||||
|
where d.scenic_id = #{scenicId}
|
||||||
|
and d.status = 1
|
||||||
|
</select>
|
||||||
|
</mapper>
|
Reference in New Issue
Block a user