liusuyi
2025-10-14 39e8bb722006c55fb986a7394bfb4747d482b8c4
修改forest动态服务请求
1 files added
7 files modified
107 ■■■■■ changed files
ard-work/src/main/java/com/ard/work/component/forest/MyAddressSource.java 24 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java 4 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/forest/TyClient.java 25 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/websocket/interceptor/TyHandshakeInterceptor.java 6 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/service/IArdServerService.java 5 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/service/impl/ArdServerServiceImpl.java 18 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java 25 ●●●●● patch | view | raw | blame | history
ard-work/src/main/java/com/ard/work/component/forest/MyAddressSource.java
New file
@@ -0,0 +1,24 @@
package com.ard.work.component.forest;
import com.ard.work.domain.entity.ArdServerInfo;
import com.ard.work.service.IArdServerService;
import com.dtflys.forest.callback.AddressSource;
import com.dtflys.forest.http.ForestAddress;
import com.dtflys.forest.http.ForestRequest;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class MyAddressSource implements AddressSource {
    @Resource
    private IArdServerService ardServerService;
    @Override
    public ForestAddress getAddress(ForestRequest request) {
        String serverKey = (String) request.getVariableValue("serverId");
        ArdServerInfo serverInfo = ardServerService.selectArdServerManagementById(serverKey);
        // 返回 Forest 地址对象
        return new ForestAddress(serverInfo.getHost(), serverInfo.getHttpPort());
    }
}
ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java
@@ -22,8 +22,8 @@
    @Override
    public boolean beforeExecute(ForestRequest req) {
        log.debug("invoke Simple beforeExecute");
        String baseUrl = (String) req.getVariableValue("baseUrl");
        AccessTokenReceiver accessToken = ardServerService.getAccessTokenByBaseUrl(baseUrl);
        String serverId = (String) req.getVariableValue("serverId");
        AccessTokenReceiver accessToken = ardServerService.getAccessToken(serverId);
        req.addHeader("x-cid", accessToken.getCompanyId());
        req.addHeader("x-token", accessToken.getAccessToken());
        return true;  // 继续执行请求返回true
ard-work/src/main/java/com/ard/work/component/forest/TyClient.java
@@ -15,43 +15,44 @@
 * @author liusuyi
 * @date 2024年10月21日14:45
 **/
@Address(source = MyAddressSource.class)
public interface TyClient {
    /**
     * 获取云盒TOKEN
     */
    @Post("{baseUrl}" + TyConst.POST_TOKEN)
    TyHttpCommonReceiver<AccessTokenReceiver> getToken(@Var("baseUrl") String baseUrl, @JSONBody TokenDTO tokenDTO);
    @Post(TyConst.POST_TOKEN)
    TyHttpCommonReceiver<AccessTokenReceiver> getToken(@Var("serverId") String serverId, @JSONBody TokenDTO tokenDTO);
    /**
     * 获取云盒列表
     */
    @Get(value = "{baseUrl}" + TyConst.GET_BOX_LIST, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<List<BoxInfoReceiver>> getBoxList(@Var("baseUrl") String baseUrl);
    @Get(value = TyConst.GET_BOX_LIST, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<List<BoxInfoReceiver>> getBoxList(@Var("serverId") String serverId);
    /**
     * 获取直播地址
     */
    @Get(value = "{baseUrl}" + TyConst.GET_BOX_LIVE_URL, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<BoxStreamUrlReceiver> getBoxLiveUrl(@Var("baseUrl") String baseUrl,
    @Get(value = TyConst.GET_BOX_LIVE_URL, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<BoxStreamUrlReceiver> getBoxLiveUrl(@Var("serverId") String serverId,
                                                             @Var("boxSn") String boxSn);
    /**
     * 获取飞行历史
     */
    @Post(value = "{baseUrl}" + TyConst.POST_BOX_HISTORY, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<List<BoxHistoryReceiver>> getBoxHistory(@Var("baseUrl") String baseUrl,
    @Post(value = TyConst.POST_BOX_HISTORY, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<List<BoxHistoryReceiver>> getBoxHistory(@Var("serverId") String serverId,
                                                                 @JSONBody BoxHistoryDTO boxHistoryDTO);
    /**
     * 获取任务照片
     */
    @Post(value = "{baseUrl}" + TyConst.POST_BOX_PHOTOS, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<List<BoxPhotosReceiver>> getBoxPhotos(@Var("baseUrl") String baseUrl,
    @Post(value = TyConst.POST_BOX_PHOTOS, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<List<BoxPhotosReceiver>> getBoxPhotos(@Var("serverId") String serverId,
                                                               @JSONBody BoxPhotosDTO boxPhotosDTO);
    /**
     * 修改云盒设置
     */
    @Post(value = "{baseUrl}" + TyConst.POST_BOX_UPDATE, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<?> updateBoxSettings(@Var("baseUrl") String baseUrl, @JSONBody BoxSettingsDTO boxSettingsDTO);
    @Post(value = TyConst.POST_BOX_UPDATE, interceptor = TokenInterceptor.class)
    TyHttpCommonReceiver<?> updateBoxSettings(@Var("serverId") String serverId, @JSONBody BoxSettingsDTO boxSettingsDTO);
}
ard-work/src/main/java/com/ard/work/component/websocket/handler/TyMessageHandler.java
ard-work/src/main/java/com/ard/work/component/websocket/interceptor/TyHandshakeInterceptor.java
@@ -19,12 +19,6 @@
            String userId = segments[segments.length - 1];
            attributes.put("userId", userId);
        }
        //从header中获取userId
//        String userId = request.getHeaders().getFirst("userId");
//        if (userId != null)
//        {
//            attributes.put("userId", userId);
//        }
        return true;
    }
ard-work/src/main/java/com/ard/work/service/IArdServerService.java
@@ -64,8 +64,5 @@
     * 根据 id 获取 token
     */
    public AccessTokenReceiver getAccessToken(String id);
    /**
     * 根据 baseUrl 获取 token(给拦截器使用)
     */
    public AccessTokenReceiver getAccessTokenByBaseUrl(String baseUrl);
}
ard-work/src/main/java/com/ard/work/service/impl/ArdServerServiceImpl.java
@@ -114,8 +114,7 @@
                .timeStamp(timeStamp)
                .build();
        try {
            String baseUrl = "http://" + serverInfo.getHost() + ":" + serverInfo.getHttpPort();
            TyHttpCommonReceiver<AccessTokenReceiver> getTokenRes = tyClient.getToken(baseUrl, gtd);
            TyHttpCommonReceiver<AccessTokenReceiver> getTokenRes = tyClient.getToken(id, gtd);
            accessToken = getTokenRes.getContent();
        } catch (Exception e) {
            log.error("获取token失败:{}", e.getMessage());
@@ -133,19 +132,4 @@
        return new Date().getTime();
    }
    /**
     * 根据 baseUrl 获取 服务器http接口token(给拦截器使用)
     */
    @Override
    public AccessTokenReceiver getAccessTokenByBaseUrl(String baseUrl) {
        // 找到对应的 serverId
        List<ArdServerInfo> servers = selectArdServerManagementList(new ArdServerInfo());
        for (ArdServerInfo server : servers) {
            String _baseUrl = "http://" + server.getHost() + ":" + server.getHttpPort();
            if (_baseUrl.equals(baseUrl)) {
                return getAccessToken(server.getId());
            }
        }
        return null;
    }
}
ard-work/src/main/java/com/ard/work/service/impl/TyServiceImpl.java
@@ -113,8 +113,7 @@
    @Override
    public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSn);
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(baseUrl,boxSn);
        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(ardDroneInfo.getServerInfoId(),boxSn);
        return boxLiveUrl.getContent();
    }
@@ -126,8 +125,8 @@
    @Override
    public List<BoxHistoryReceiver> getBoxHistory(BoxHistoryDTO boxHistoryDTO) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxHistoryDTO.getBoxSn());
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(baseUrl,boxHistoryDTO);
        TyHttpCommonReceiver<List<BoxHistoryReceiver>> boxHistory = tyClient.getBoxHistory(ardDroneInfo.getServerInfoId(),
                boxHistoryDTO);
        return boxHistory.getContent();
    }
@@ -139,8 +138,7 @@
    @Override
    public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxPhotosDTO.getBoxSn());
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(baseUrl,boxPhotosDTO);
        TyHttpCommonReceiver<List<BoxPhotosReceiver>> boxPhotos = tyClient.getBoxPhotos(ardDroneInfo.getServerInfoId(),boxPhotosDTO);
        return boxPhotos.getContent();
    }
@@ -150,8 +148,7 @@
    @Override
    public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
        ArdDroneInfo ardDroneInfo = ardDroneService.selectArdDroneByboxSn(boxSettingsDTO.getBoxSn());
        String baseUrl="http://"+ardDroneInfo.getArdServerInfo().getHost()+":"+ardDroneInfo.getArdServerInfo().getHttpPort();
        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(baseUrl,boxSettingsDTO);
        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(ardDroneInfo.getServerInfoId(),boxSettingsDTO);
        return result.getState() == 1;
    }
@@ -845,14 +842,12 @@
     */
    @Override
    public List<BoxInfoReceiver> getBoxSnToList(String id) {
        ArdServerInfo ardServerInfos = ardServerService.selectArdServerManagementById(id);
        String baseUrl = "http://" + ardServerInfos.getHost() + ":" + ardServerInfos.getHttpPort();
        List<BoxInfoReceiver> lisbox = new ArrayList<>();
        try {
            lisbox = tyClient.getBoxList(baseUrl).getContent();
            lisbox = tyClient.getBoxList(id).getContent();
            return lisbox;
        } catch (Exception e) {
            log.error("获取云盒列表失败: {}", baseUrl, e);
            log.error("获取云盒列表失败: {}", id, e);
            return lisbox;
        }
    }
@@ -860,12 +855,12 @@
    private List<BoxInfoReceiver> getBoxList(List<ArdServerInfo> ls) {
        return ls.stream()
                .flatMap(ardServerInfo -> {
                    String baseUrl = "http://" + ardServerInfo.getHost() + ":" + ardServerInfo.getHttpPort();
                    try {
                        List<BoxInfoReceiver> boxes = tyClient.getBoxList(baseUrl).getContent();
                        List<BoxInfoReceiver> boxes = tyClient.getBoxList(ardServerInfo.getId()).getContent();
                        return Optional.ofNullable(boxes).orElse(Collections.emptyList()).stream();
                    } catch (Exception e) {
                        log.error("获取云盒列表失败: {}", baseUrl, e);
                        log.error("获取云盒列表失败: {}", ardServerInfo.getId(), e);
                        return Stream.empty();
                    }
                })