1
zhanghua
2024-09-26 c775c6953d9759e70f08acbfa8f6d7490aaae3d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.netsdk.demo.customize;
 
import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.*;
import com.netsdk.lib.utils.Initialization;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author 291189
 * @version 1.0
 * @description GIP220929012 
 * @date 2022/10/26 16:13
 */
public class AttachGyroDemo extends Initialization {
 
    NetSDKLib.LLong lAttachHandle=new   NetSDKLib.LLong();
 
    static List<Pointer> pointers=new ArrayList<>();
    /**
     * 订阅陀螺仪数据接口
     */
    public void attachGyro(){
 
        String dwUser="{"+"ip:"+ipAddr+",port:"+port+"}";
 
// CLIENT_AttachGyro接口入参
        NET_IN_ATTACH_GYRO input=new NET_IN_ATTACH_GYRO();
        Pointer dwUserData
                = GetStringToPointer(dwUser, encode);
        input.cbNotifyGyroData=NotifyGyroData.getInstance();
        input.dwUser=dwUserData;
         Pointer inputPointer =new Memory(input.size());
 
         inputPointer.clear(input.size());
 
         ToolKits.SetStructDataToPointer(input,inputPointer,0);
 
       //CLIENT_AttachGyro接口出参
         NET_OUT_ATTACH_GYRO outPut=new NET_OUT_ATTACH_GYRO();
 
        Pointer outPutPointer =new Memory(outPut.size());
 
        outPutPointer.clear(outPut.size());
 
        ToolKits.SetStructDataToPointer(outPut,outPutPointer,0);
 
        lAttachHandle = netSdk.CLIENT_AttachGyro(loginHandle,inputPointer,outPutPointer,3000 );
 
        if(lAttachHandle.longValue()!=0){
            System.out.println(" CLIENT_AttachGyro Success");
            pointers.add(dwUserData);
        }else {
            System.out.println("CLIENT_AttachGyro Failed!LastError "+
                    ToolKits.getErrorCode());
        }
    }
 
    /**
     * 取消陀螺仪数据订阅接口
     */
    public void detachGyro(){
 
        if(lAttachHandle.longValue()!=0){
 
            boolean b
                    = netSdk.CLIENT_DetachGyro(lAttachHandle);
 
            if(b){
                System.out.println(" CLIENT_DetachGyro Success");
 
            }else {
                System.out.println("CLIENT_DetachGyro Failed!LastError "+
                        ToolKits.getErrorCode());
            }
 
        }
 
 
    }
 
    /**
     *   订阅陀螺仪数据接口回调函数原型, lAttachGyroHandle为CLIENT_AttachGyro接口的返回值
     */
    private static class NotifyGyroData implements NetSDKLib.fNotifyGyroData {
        private final File picturePath;
        private static NotifyGyroData instance;
 
        private NotifyGyroData() {
            picturePath = new File("./AnalyzerPicture/");
            if (!picturePath.exists()) {
                picturePath.mkdirs();
            }
        }
 
        public static NotifyGyroData getInstance() {
            if (instance == null) {
                synchronized (NotifyGyroData.class) {
                    if (instance == null) {
                        instance = new NotifyGyroData();
                    }
                }
            }
            return instance;
        }
 
 
        @Override
        public void invoke(NetSDKLib.LLong lAttachGyroHandle, Pointer pstuGyroDataInfo, Pointer dwUser) {
 
            String s = GetPointerDataToString(dwUser,encode);
            System.out.println("dwUser:"+s);
            NET_NOTIFY_GYRO_DATA_INFO msg=new NET_NOTIFY_GYRO_DATA_INFO();
 
            ToolKits.GetPointerData(pstuGyroDataInfo,msg);
 
            System.out.println("msg:"+msg.toString());
 
 
        }
    }
 
    public static Pointer GetStringToPointer(String src,String charset) {
        Pointer pointer = null;
        try {
            byte[] b = src.getBytes(charset);
 
            pointer = new Memory(b.length + 1);
            pointer.clear(b.length + 1);
 
            pointer.write(0, b, 0, b.length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return pointer;
    }
 
    public static String GetPointerDataToString(Pointer pointer,String charset) {
        String str = "";
        if (pointer == null) {
            return str;
        }
 
        int length = 0;
        byte[] bufferPlace = new byte[1];
 
        for (int i = 0; i < 2048; i++) {
            pointer.read(i, bufferPlace, 0, 1);
            if (bufferPlace[0] == '\0') {
                length = i;
                break;
            }
        }
 
        if (length > 0) {
            byte[] buffer = new byte[length];
            pointer.read(0, buffer, 0, length);
            try {
                str = new String(buffer, charset).trim();
            } catch (UnsupportedEncodingException e) {
                return str;
            }
        }
 
        return str;
    }
    public void RunTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();
        menu.addItem(new CaseMenu.Item(this , "订阅陀螺仪数据接口" , "attachGyro"));
        menu.addItem(new CaseMenu.Item(this , "取消陀螺仪数据订阅接口" , "detachGyro"));
        menu.run();
    }
 
    private static String ipAddr             = "10.55.157.161";
    private static int    port                 = 37777;
    private static String user                 = "admin";
    private static String password             = "admin123";
 
    public static void main(String[] args){
        InitTest(ipAddr, port, user, password);
        AttachGyroDemo attachGyroDemo=new AttachGyroDemo();
        attachGyroDemo.RunTest();
        LoginOut();
    }
}