liusuyi
2024-10-22 ce309155d4a0d750f786ca242b36bba8fc0bbf14
ard-work/src/main/java/com/ard/tianyu/service/impl/TyServiceImpl.java
@@ -12,7 +12,6 @@
import com.ard.tianyu.netty.command.RegisterCommand;
import com.ard.tianyu.netty.TyNettyTcpClient;
import com.ard.tianyu.service.ITyService;
import com.ard.vtdu.util.ArdTool;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
@@ -44,7 +43,7 @@
    /**
     * 获取天宇token信息
     *
     * @return
     * @return AccessTokenReceiver
     */
    @Override
    public AccessTokenReceiver getAccessToken() {
@@ -54,31 +53,31 @@
    /**
     * 获取云盒列表
     *
     * @return
     * @return List<BoxInfoReceiver>
     */
    @Override
    public List<BoxReceiver> getBoxList() {
    public List<BoxInfoReceiver> getBoxList() {
        //请求云盒列表
        TyHttpCommonReceiver<List<BoxReceiver>> boxListReveiver = tyClient.getBoxList(tyConfiguration.getAccessToken().convertFromDTO());
        TyHttpCommonReceiver<List<BoxInfoReceiver>> boxListReveiver = tyClient.getBoxList(tyConfiguration.getAccessToken().convertFromDTO());
        return boxListReveiver.getContent();
    }
    /**
     * 获取直播地址
     *
     * @return
     * @return BoxStreamUrlReceiver
     */
    @Override
    public PullStreamUrlReceiver getBoxLiveUrl(String boxSn) {
    public BoxStreamUrlReceiver getBoxLiveUrl(String boxSn) {
        TyHttpCommonReceiver<PullStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn, tyConfiguration.getAccessToken().convertFromDTO());
        TyHttpCommonReceiver<BoxStreamUrlReceiver> boxLiveUrl = tyClient.getBoxLiveUrl(boxSn, tyConfiguration.getAccessToken().convertFromDTO());
        return boxLiveUrl.getContent();
    }
    /**
     * 获取飞行历史
     *
     * @return
     * @return List<BoxHistoryReceiver>
     */
    @Override
    public List<BoxHistoryReceiver> getBoxHistory(BoxHistoryDTO boxHistoryDTO) {
@@ -89,7 +88,7 @@
    /**
     * 获取盒子图片
     *
     * @return
     * @return List<BoxPhotosReceiver>
     */
    @Override
    public List<BoxPhotosReceiver> getBoxPhotos(BoxPhotosDTO boxPhotosDTO) {
@@ -102,7 +101,7 @@
     */
    @Override
    public Boolean updateBoxSettings(BoxSettingsDTO boxSettingsDTO) {
        TyHttpCommonReceiver<String> result = tyClient.updateBoxSettings(boxSettingsDTO, tyConfiguration.getAccessToken().convertFromDTO());
        TyHttpCommonReceiver<?> result = tyClient.updateBoxSettings(boxSettingsDTO, tyConfiguration.getAccessToken().convertFromDTO());
        return result.getState() == 1;
    }
@@ -115,34 +114,28 @@
        RegisterCommand registerMessage = new RegisterCommand(tyToken.getCompanyId(), tyToken.getAccessToken().getBytes());
        this.sendMessage(tyConfiguration.getIpPort(), registerMessage);
    }
    /**
     * 发送消息
     */
    public boolean sendMessage(String key, BaseCommand msg) {
        //将从用户接收的信息发送到天宇 commandCache
        boolean cmdResult = false;
    public void sendMessage(String key, BaseCommand msg) {
        try {
            ChannelFuture future = TyNettyTcpClient.channels.get(key).getFuture();
            Channel channel = future.channel();
            if (future != null && channel.isActive()) {
            if (channel.isActive()) {
                InetSocketAddress ipSocket = (InetSocketAddress) channel.remoteAddress();
                String host = ipSocket.getHostString();
                int port = ipSocket.getPort();
                // 创建 ByteBuf 并编码消息
                ByteBuf buffer = Unpooled.buffer();
                msg.encode(buffer); // 确保对消息进行编码
                channel.writeAndFlush(buffer); // 发送编码后的数据
                cmdResult = true;
                log.info("向天宇服务器" + host + ":" + port + "发送数据" + ",命令码:" + String.format("0x%02X", msg.getCmdCode() & 0xFF));
                log.info("向天宇服务器【{}:{}】发送数据,命令码:{}", host, port, String.format("0x%02X", msg.getCmdCode() & 0xFF));
            } else {
                log.error("与天宇服务器链接断开,无法发送数据!");
            }
        } catch (Exception e) {
            log.info("Failed to publish the message. {}", "转发信息");
            e.printStackTrace();
        } finally {
            return cmdResult;
            log.error("发送消息异常:{}", e.getMessage());
        }
    }
@@ -155,7 +148,7 @@
    @Scheduled(fixedRate = 5000)
    public void sendHeartBeat() {
        long timestamp = System.currentTimeMillis();
        log.info("定时向天宇发送⼼跳,时间戳{}", timestamp);
        log.debug("定时向天宇发送⼼跳,时间戳{}", timestamp);
        HeartbeatCommand heartbeatMessage = new HeartbeatCommand(timestamp);
        this.sendMessage(tyConfiguration.getIpPort(), heartbeatMessage);
    }