• 大小: 220KB
    文件类型: .rar
    金币: 2
    下载: 3 次
    发布日期: 2021-05-11
  • 语言: Java
  • 标签: netty4  onvif  

资源简介

基于netty客户端请求onvif接口,包含获取设备信息,流地址,事件等

资源截图

代码片段和文件信息

package com.aoirse.netty4;

import com.aorise.util.OnvifUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@Slf4j
@UtilityClass
public class OnvifNettyClient {

    private static String host = “主机地址“;
    private static int port = 80;
    private static int time = 5;

    private Channel channel;
    private EventLoopGroup group;
    private  Bootstrap bootstrap;


    public void connect() {
        group = new NioEventLoopGroup();
        try {
            bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
//                    .option(ChannelOption.SO_KEEPALIVE true)
                    .handler(new ChannelInitializer() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
                            ch.pipeline().addLast(new HttpResponseDecoder());
                            // 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
                            ch.pipeline().addLast(new HttpRequestEncoder());
                            ch.pipeline().addLast(“aggregator“ new HttpobjectAggregator(1024 * 1024));
                        }
                    });
            ChannelFuture f = bootstrap.connect(host port).sync();
            channel = f.channel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public  String getRequestResult(T tString serviceUrl String tempaltePathString...headerArgs) throws Exception {
        String msg = OnvifUtil.generateTemplate(t tempaltePathheaderArgs);
        FullHttpRequest request = createRequest(hostserviceUrl msg);
        //重连
        if(!channel.isOpen()){
            channel=bootstrap.connect(host port).sync().channel();
        }
        channel.writeAndFlush(request);
        ChannelHandler onvifNettyHandler1 = channel.pipeline().get(“onvifNettyHandler“);
        if(onvifNettyHandler1!=null){
            channel.pipeline().remove(“onvifNettyHandler“);
        }
        CountDownLatch countDownLatch=new CountDownLatch(1);
        OnvifNettyHandler onvifNettyHandler = new OnvifNettyHandler(countDownLatch);
        channel.pipeline().addLast(“onvifNettyHandler“ onvifNettyHandler);
        if(!countDownLatch.await(time TimeUnit.SECONDS)){
            //无论如何要关闭连接
            channel.close();
         

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件          1  2018-11-27 17:55  onviftest\.gradle\4.1\fileChanges\last-build.bin

     文件      18647  2018-11-27 17:55  onviftest\.gradle\4.1\fileHashes\fileHashes.bin

     文件         17  2018-11-27 17:55  onviftest\.gradle\4.1\fileHashes\fileHashes.lock

     文件         17  2018-11-19 17:42  onviftest\.gradle\4.1\taskHistory\taskHistory.lock

     文件          0  2018-11-19 16:32  onviftest\.gradle\buildOutputCleanup\built.bin

     文件         51  2018-11-19 16:32  onviftest\.gradle\buildOutputCleanup\cache.properties

     文件          2  2018-11-27 17:55  onviftest\.gradle\buildOutputCleanup\cache.properties.lock

     文件        247  2018-12-10 08:52  onviftest\.idea\compiler.xml

     文件        200  2018-11-28 10:05  onviftest\.idea\encodings.xml

     文件        666  2018-11-19 16:33  onviftest\.idea\gradle.xml

     文件        332  2018-11-19 16:31  onviftest\.idea\misc.xml

     文件      84540  2018-12-10 13:41  onviftest\.idea\workspace.xml

     文件       1773  2018-11-27 17:54  onviftest\build.gradle

     文件       1550  2018-12-10 10:08  onviftest\out\production\classes\com\aoirse\netty4\OnvifNettyClient$1.class

     文件       5981  2018-12-10 10:08  onviftest\out\production\classes\com\aoirse\netty4\OnvifNettyClient.class

     文件       2219  2018-12-10 11:32  onviftest\out\production\classes\com\aoirse\netty4\OnvifNettyHandler.class

     文件       5055  2018-12-04 15:25  onviftest\out\production\classes\com\aorise\filter\OnvifNamespaceMapper.class

     文件      11115  2018-12-10 11:30  onviftest\out\production\classes\com\aorise\service\OnvifService.class

     文件       1498  2018-12-05 10:23  onviftest\out\production\classes\com\aorise\util\DateUtil.class

     文件       2508  2018-11-28 10:05  onviftest\out\production\classes\com\aorise\util\JAXBUtil.class

     文件       2751  2018-11-28 10:05  onviftest\out\production\classes\com\aorise\util\MapUtil.class

     文件       5249  2018-12-10 10:08  onviftest\out\production\classes\com\aorise\util\OnvifUtil.class

     文件       2328  2018-11-28 10:05  onviftest\out\production\classes\com\aorise\util\ReflectionUtil.class

     文件       1539  2018-11-29 15:47  onviftest\out\production\classes\com\aorise\util\TemplateUtil.class

     文件       2741  2018-12-05 11:48  onviftest\out\production\classes\com\aorise\webservice\request\model\CreatePullPointSubscription.class

     文件       2692  2018-11-29 09:56  onviftest\out\production\classes\com\aorise\webservice\request\model\CreatePullPointSubscriptionFilter.class

     文件       2296  2018-11-29 17:11  onviftest\out\production\classes\com\aorise\webservice\request\model\CreatePullPointSubscriptionFilterexpressionOrContent.class

     文件       3972  2018-11-29 15:49  onviftest\out\production\classes\com\aorise\webservice\request\model\DeviceSecurity.class

     文件       1786  2018-12-04 11:18  onviftest\out\production\classes\com\aorise\webservice\request\model\GetCapabilities.class

     文件       1117  2018-12-04 11:18  onviftest\out\production\classes\com\aorise\webservice\request\model\GetDeviceInformation.class

............此处省略266个文件信息

评论

共有 条评论