fuliqi
2024-08-30 4edf419e4beed7e17c99aecc7860691c2b9a7196
宇视SDK代码,OSD过滤ip
6个文件已修改
3个文件已添加
30638 ■■■■■ 已修改文件
include/NetDEVSDK.h 30394 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libs/jna.jar 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/api/DH/module/LoginModule.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/api/HK/HKApi.java 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/api/YS/NetDemo.java 34 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/api/YS/YSApi.java 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/api/YS/YSInit.java 57 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/api/YS/lib/NetDEVSDKLib.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ycl-server/src/main/java/com/ycl/task/OsdTask.java 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
include/NetDEVSDK.h
New file
Diff too large
libs/jna.jar
Binary files differ
ycl-server/src/main/java/com/ycl/api/DH/module/LoginModule.java
@@ -130,7 +130,6 @@
        // 登陆句柄
        LLong m_hLoginHandle = new LLong(0);
        m_hLoginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
        log.info("大华登录"+m_hLoginHandle);
        if (m_hLoginHandle.longValue() == 0) {
//            System.err.printf("Login Device[%s] Port[%d]Failed. %s\n", m_strIp, m_nPort, ToolKits.getErrorCodePrint());
        } else {
ycl-server/src/main/java/com/ycl/api/HK/HKApi.java
@@ -8,16 +8,14 @@
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -35,27 +33,30 @@
public class HKApi {
    public static OSDResult getOsdByIP(String ip, String userName, String password) {
        OSDResult osdResult = new OSDResult();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String hostUrl = "http://" + ip;
        try {
            //获取OSD
            HttpResponse OSDResponse = getHttpResponse(httpClient, hostUrl + ApiConstants.HK_OSD_PATH, userName, password);
            String OSDString = EntityUtils.toString(OSDResponse.getEntity(), "utf-8");
            String OSDString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_PATH, userName, password);
            if (OSDString == null) return null;
            //解析xml
            parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null);
            parseXMl(OSDString, "TextOverlay", "displayText", osdResult, null,ip);
            //获取Time
            HttpResponse TimeResponse = getHttpResponse(httpClient, hostUrl + ApiConstants.HK_OSD_TIME, userName, password);
            String timeString = getHttpResponse(hostUrl + ApiConstants.HK_OSD_TIME, userName, password);
            if (timeString == null) return null;
            Date date = new Date();
            String timeString = EntityUtils.toString(TimeResponse.getEntity(), "utf-8");
            //解析xml
            parseXMl(timeString, "Time", "localTime", osdResult, date);
            parseXMl(timeString, "Time", "localTime", osdResult, date,ip);
        } catch (Exception e) {
            return null;
        }
        return osdResult;
    }
    private static void parseXMl(String OSDString, String tagName1, String tagName2, OSDResult osdResult, Date date) throws ParserConfigurationException, SAXException, IOException {
    private static void parseXMl(String OSDString, String tagName1, String tagName2, OSDResult osdResult, Date date,String ip) throws ParserConfigurationException, SAXException, IOException {
        try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(OSDString)));
@@ -88,13 +89,21 @@
                osdResult.setOsdTime(Date.from(dateTime.toInstant()));
            }
        }
        } catch (Exception e) {
           log.error("ip:{}出现异常,{}",ip,e.getMessage());
           throw new RuntimeException(e.getMessage());
        }
    }
    private static HttpResponse getHttpResponse(CloseableHttpClient httpClient, String url, String userName, String password) throws URISyntaxException, IOException {
    private static String getHttpResponse(String url, String userName, String password) throws URISyntaxException, IOException {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(new URI(url).getHost(), new URI(url).getPort()),
                new UsernamePasswordCredentials(userName, password));
        // 绑定凭证提供者到HttpClient
        CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        HttpGet httpGet = new HttpGet(url);
@@ -104,15 +113,16 @@
                .setSocketTimeout(1000)
                .build();
        httpGet.setConfig(requestConfig);
        // 绑定凭证提供者到HttpClient
        httpClient = HttpClientBuilder.create()
                .setDefaultCredentialsProvider(credsProvider)
                .build();
        try {
            return httpClient.execute(httpGet);
        } catch (Exception e){
            throw new RuntimeException("海康OSD执行失败");
        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                return EntityUtils.toString(response.getEntity(), "utf-8");
            } else {
                return null;
        }
        } catch (Exception e) {
            throw new RuntimeException("海康OSD执行失败", e);
        }
    }
}
ycl-server/src/main/java/com/ycl/api/YS/NetDemo.java
@@ -787,27 +787,27 @@
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NetDemo window = new NetDemo();
                    window.frame.setResizable(false);
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
//    public static void main(String[] args) {
//        EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                try {
//                    NetDemo window = new NetDemo();
//                    window.frame.setResizable(false);
//                    window.frame.setVisible(true);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }
    /**
     * Create the application.
     */
    public NetDemo() {
        initialize();
        init();
    }
//    public NetDemo() {
//        initialize();
//        init();
//    }
    public void init() {
        String strLogPath = "./sdklog/";
ycl-server/src/main/java/com/ycl/api/YS/YSApi.java
New file
@@ -0,0 +1,64 @@
package com.ycl.api.YS;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.ycl.api.DH.utils.DHApi;
import com.ycl.api.YS.lib.NetDEVSDKLib;
import com.ycl.api.YS.login.Login;
import com.ycl.platform.domain.result.OSDResult;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
import static com.ycl.api.YS.lib.NetDEVSDKLib.NETDEV_LOGIN_PROTO_E.NETDEV_LOGIN_PROTO_ONVIF;
@Slf4j
public class YSApi {
    public static OSDResult getOsd(String ip, String userName, String password){
        log.info("ip:{},用户:{},密码:{}",ip,userName,password);
        //登录
        NetDEVSDKLib.NETDEV_DEVICE_LOGIN_INFO_S stDevLoginInfo = new NetDEVSDKLib.NETDEV_DEVICE_LOGIN_INFO_S();
        NetDEVSDKLib.NETDEV_SELOG_INFO_S stSELogInfo = new NetDEVSDKLib.NETDEV_SELOG_INFO_S();
//        System.arraycopy(userName.getBytes(), 0, stDevLoginInfo.szUserName, 0, userName.getBytes().length);
//        System.arraycopy(password.getBytes(), 0, stDevLoginInfo.szPassword, 0, password.getBytes().length);
//        System.arraycopy(ip.getBytes(), 0, stDevLoginInfo.szIPAddr, 0, ip.getBytes().length);
        stDevLoginInfo.szUserName = userName.getBytes();
        stDevLoginInfo.szPassword = password.getBytes();
        stDevLoginInfo.szIPAddr = ip.getBytes();
        stDevLoginInfo.dwPort = 80;
        //登录协议
        stDevLoginInfo.dwLoginProto = NETDEV_LOGIN_PROTO_ONVIF;
        log.info("SDK传参ip:{},用户:{},密码:{}",stDevLoginInfo.szIPAddr,stDevLoginInfo.szUserName,stDevLoginInfo.szPassword);
        Pointer lpUserID  = YSInit.netdevsdk.NETDEV_Login_V30(stDevLoginInfo, stSELogInfo);
        log.info("登录返回id:{}",lpUserID);
        if(lpUserID !=null) {
            log.info("登录成功");
            //获取osd
            NetDEVSDKLib.NETDEV_VIDEO_OSD_CFG_S stOSDInfo = new NetDEVSDKLib.NETDEV_VIDEO_OSD_CFG_S();
            stOSDInfo.write();
            IntByReference dwBytesReturned = new IntByReference();
            boolean bRet = YSInit.netdevsdk.NETDEV_GetDevConfig(lpUserID, 0, NetDEVSDKLib.NETDEV_CONFIG_COMMAND_E.NETDEV_GET_OSDCFG, stOSDInfo.getPointer(), 616, dwBytesReturned);
            if (!bRet) {
                log.error("error code"+YSInit.netdevsdk.NETDEV_GetLastError());
                return null;
            }
            stOSDInfo.read();
            for (NetDEVSDKLib.NETDEV_OSD_TEXT_OVERLAY_S osd : stOSDInfo.astTextOverlay) {
                String text = new String(osd.szOSDText, StandardCharsets.UTF_8);
                log.info("text:" + text);
            }
            //登出
            YSInit.netdevsdk.NETDEV_Logout(lpUserID);
        }else {
            log.error("error code"+YSInit.netdevsdk.NETDEV_GetLastError());
        }
        return null;
    }
    public static void demo(){
        Login.loginIn();
    }
}
ycl-server/src/main/java/com/ycl/api/YS/YSInit.java
New file
@@ -0,0 +1,57 @@
package com.ycl.api.YS;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.ycl.api.YS.lib.NetDEVSDKLib;
import com.ycl.api.YS.util.Common;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
@Component
@Slf4j
public class YSInit {
    @Value("${spring.profiles.active}")
    private String activeEnv;
    public static NetDEVSDKLib netdevsdk = NetDEVSDKLib.NETDEVSDK_INSTANCE;
    public static String strPicturePath = "."+File.separator+"Picture"+File.separator;
    @PostConstruct
    void init() {
        if("prod".equals(activeEnv)) {
            String strLogPath = "./sdklog/";
            boolean bRet = netdevsdk.NETDEV_SetLogPath(strLogPath);
            if(false == bRet){
                System.out.printf("NETDEV_SetLogPath failed:%d", netdevsdk.NETDEV_GetLastError());
            }
            bRet = netdevsdk.NETDEV_Init();
            if(false == bRet){
                System.out.printf("Initialize failed:%d", netdevsdk.NETDEV_GetLastError());
                return;
            }
            try {
                Common.updateStructureByReferenceMethod = Structure.class.getDeclaredMethod("updateStructureByReference", Class.class, Structure.class, Pointer.class);
                Common.updateStructureByReferenceMethod.setAccessible(true);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            File file=new File(strPicturePath);
            if(!file.exists()){
                file.mkdir();
            }
        }
    }
    @PreDestroy
    void clean(){
        if("prod".equals(activeEnv)) {
            netdevsdk.NETDEV_Cleanup();
        }
    }
}
ycl-server/src/main/java/com/ycl/api/YS/lib/NetDEVSDKLib.java
@@ -1,8 +1,10 @@
package com.ycl.api.YS.lib;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
@@ -128,6 +130,7 @@
     * @brief 设备登录信息
     * @attention
     */
    @Structure.FieldOrder({"byRes", "dwDeviceType" ,"dwLoginProto","dwPort","szIPAddr","szPassword","szUserName"})
    public class NETDEV_DEVICE_LOGIN_INFO_S extends Structure
    {
        public byte[]      szIPAddr = new byte[NETDEV_LEN_260];    /* IP地址/域名 */
@@ -144,6 +147,7 @@
     * @brief 安全登录信息
     * @attention
     */
    @Structure.FieldOrder({"byRes", "dwSELogCount","dwSELogTime"})
    public class NETDEV_SELOG_INFO_S extends Structure {
        public int         dwSELogCount;             /* 安全登录次数 */
        public int         dwSELogTime;              /* 安全登录时间 */
@@ -5974,6 +5978,7 @@
     * @brief  区域 Area
     * @attention 无 None
     */
    @Structure.FieldOrder({"dwLocateX", "dwLocateY"})
    public class NETDEV_AREA_SCOPE_S extends Structure {
        public int  dwLocateX;             /** 顶点x坐标值[0,10000] * Coordinates of top point x [0,10000] */
        public int  dwLocateY;             /** 顶点y坐标值[0,10000] * Coordinates of top point y [0,10000] */
@@ -5983,29 +5988,39 @@
     * @brief OSD时间配置信息 OSD time configuration information
     * @attention 无 None
     */
    @Structure.FieldOrder({"bEnableFlag", "bWeekEnableFlag","stAreaScope","udwDateFormat","udwTimeFormat"})
    public class NETDEV_OSD_TIME_S extends Structure {
        public int                    bEnableFlag;        /** 时间OSD使能, BOOL_TRUE为使能,BOOL_FALSE为非使能 * Enable time OSD, BOOL_TRUE means enable and BOOL_FALSE means disable */
        public int                    bWeekEnableFlag;    /** 是否显示星期(预留) * Display week or not (reserved) */
        public NETDEV_AREA_SCOPE_S     stAreaScope;        /** 区域坐标 * Area coordinates */
        public int                  udwTimeFormat;      /** 时间OSD时间格式,详见 NETDEV_OSD_TIME_FORMAT_CAP_E * Time OSD format, see NETDEV_OSD_TIME_FORMAT_E */
        public int                  udwDateFormat;      /** 日期OSD时间格式,详见 NETDEV_OSD_DATE_FORMAT_CAP_E * Date OSD format, see NETDEV_OSD_TIME_FORMAT_E */
        public NETDEV_OSD_TIME_S() {
            // 初始化代码
        }
    }
    /**
     * @brief OSD字符叠加信息 OSD text overlay information
     * @attention
     */
    @Structure.FieldOrder({"bEnableFlag", "byRes","stAreaScope","szOSDText"})
    public class NETDEV_OSD_TEXT_OVERLAY_S extends Structure {
        public int                  bEnableFlag;                /** OSD字符叠加使能, BOOL_TRUE为使能,BOOL_FALSE为非使能 * Enable OSD text overlay, BOOL_TRUE means enable and BOOL_FALSE means disable */
        public NETDEV_AREA_SCOPE_S    stAreaScope;                /** OSD字符叠加区域坐标 * OSD text overlay area coordinates */
        public byte[]                szOSDText=new byte[NETDEV_OSD_TEXT_MAX_LEN];    /** OSD字符叠加名称字符串 * OSD text overlay name strings */
        public byte[]                byRes=new byte[4];                               /* 保留字段  Reserved */
        public NETDEV_OSD_TEXT_OVERLAY_S() {
            // 初始化代码
        }
    }
    /**
     * @brief 通道OSD的基本属性信息 Basic channel OSD information
     * @attention
     */
    @Structure.FieldOrder({"astTextOverlay", "stNameOSD","stTimeOSD","wTextNum"})
    public class NETDEV_VIDEO_OSD_CFG_S extends Structure {
        public NETDEV_OSD_TIME_S         stTimeOSD;        /* 通道的时间OSD信息  Information of channel time OSD */
        public NETDEV_OSD_TEXT_OVERLAY_S stNameOSD;        /* 通道的名称OSD信息  Information of channel name OSD */
ycl-server/src/main/java/com/ycl/task/OsdTask.java
@@ -7,6 +7,8 @@
import com.ycl.api.DH.module.LoginModule;
import com.ycl.api.DH.utils.DHApi;
import com.ycl.api.HK.HKApi;
import com.ycl.api.YS.YSApi;
import com.ycl.api.YS.YSInit;
import com.ycl.platform.domain.entity.TMonitor;
import com.ycl.platform.domain.result.OSDResult;
import com.ycl.platform.domain.result.UY.MonitorQualifyResult;
@@ -49,6 +51,10 @@
    public String DHUserName;
    @Value("${DH.password}")
    public String DHPassword;
    @Value("${YS.userName}")
    public String YSUserName;
    @Value("${YS.password}")
    public String YSPassword;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
@@ -72,6 +78,9 @@
     */
    public void getOSD() {
        log.info("开始获取OSD");
//            YSApi.getOsd("51.95.66.42",YSUserName,YSPassword);
        DHApi.getOsd("51.95.67.189",DHUserName,DHPassword);
        //批量修改海康品牌集合
        List<String> hkList = new ArrayList<>();
        //批量修改大华品牌集合
@@ -84,9 +93,10 @@
        List<MonitorQualifyResult> oneMachineFileResults = mongoTemplate.find(query, MonitorQualifyResult.class);
        //需要考核的数据
        List<String> serialNumbers = oneMachineFileResults.stream().map(result -> result.getSerialNumber().getValue()).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(serialNumbers)) {
        List<TMonitor> monitors = monitorMapper.selectByNumbers(serialNumbers);
        monitors = monitors.stream().filter(monitor -> monitor.getId()<7000).collect(Collectors.toList());
            monitors = monitors.stream().filter(monitor -> !StringUtils.isEmpty(monitor.getIp()) && !"127.0.0.1".equals(monitor.getIp())).collect(Collectors.toList());
//            monitors = monitors.stream().filter(monitor -> monitor.getId() < 7000).collect(Collectors.toList());
        log.info("处理数据大小{}",monitors.size());
        for (TMonitor monitor : monitors) {
            executorService.submit(() -> {
@@ -126,8 +136,6 @@
        } catch (InterruptedException e) {
            // 当前线程在等待过程中被中断
            executorService.shutdownNow();
            // 处理中断,例如重新抛出异常或记录日志
            Thread.currentThread().interrupt(); // 保持中断状态
        }
        //结束api执行,开始校验
        List<OsdCheckResult> checkResults = new ArrayList<>();
@@ -162,6 +170,7 @@
            monitorMapper.batchUpdateDeviceType(dhList,DeviceType.DH.getType());
        }
        //TODO:宇视
        }
        log.info("结束获取OSD");
    }
@@ -244,6 +253,7 @@
        OSDResult hkosd = HKApi.getOsdByIP(monitor.getIp(), HKUserName, HKPassword);
        if (hkosd != null) {
            hkosd.setSerialNumber(monitor.getSerialNumber());
            log.info("海康调用成功"+hkosd);
            synchronized (hkList) {
                hkList.add(monitor.getSerialNumber());
            }
@@ -253,6 +263,7 @@
        OSDResult dhosd = DHApi.getOsd(monitor.getIp(), DHUserName, DHPassword);
        if (dhosd != null) {
            dhosd.setSerialNumber(monitor.getSerialNumber());
            log.info("大华调用成功"+dhosd);
            synchronized (dhList) {
                dhList.add(monitor.getSerialNumber());
            }