3.7
fxl
2023-03-07 52cffc4ab8e9787a6f233295502c7c9788dddae1
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.hxzkoa.udp;
import com.hxzkoa.util.Config;
import com.hxzkoa.util.HttpUtil;
import com.hxzkoa.util.ModifyConfig;
import com.sun.jna.Library;
import com.sun.jna.Native;
 
import net.sf.json.JSONObject;
 
public class DellJAN {
 
    static boolean start=true;
    static int youxiao=0;
    static int suanfaversion=0;
 
 
 
 
    /**
     * 动态链接库编译时选择的平台。如果通过x86平台编译,那么只能使用32位jdk环境加载,如果要使用64位jdk,必须使用x64平台编译。
     * 
     * 接口内部需要一个公共静态常量:INSTANCE,通过这个常量,就可以获得这个接口的实例,从而使用接口的方法,也就是调用外部dll/so的函数。
     *  该常量通过Native.loadLibrary()这个API函数获得,该函数有2个参数:
     *  
     *  第一个参数是动态链接库dll/so的名称,但不带.dll或.so这样的后缀,这符合JNI的规范,因为带了后缀名就不可以跨操作系统平台了。
     *  搜索动态链接库路径的顺序是:先从当前类的当前文件夹找,如果没有找到,再在工程当前文件夹下面找win32/win64文件夹,
     *  找到后搜索对应的dll文件,如果 找不到再到WINDOWS下面去搜索,再找不到就会抛异常了。
     *  第二个参数是本接口的Class类型。JNA通过这个Class类型,根据指定的.dll/.so文件,动态创建接口的实例。
     *  该实例由JNA通过反射自动生成。
     *  
     *  接口中只需要定义你要用到的函数或者公共变量,不需要的可以不定义,注意参数和返回值的类型,应该和链接库中的函数类型保持一致。
     *  定义好接口后,就可以使用接口中的函数即相应dll/so中的函数了
     */
 
    public interface DLibrary extends Library {
        //此处我的jdk版本为64位,故加载64位的Dll
        DLibrary INSTANCE = (DLibrary) Native.loadLibrary("JNA",DLibrary.class);
        //JNA.dll 中定义的函数
 
        /**初始化
         * @param anchornum 系统基站总数*/
        public int SuanFa_Init(int anchornum);
 
 
        /**添加基站*/       
        public int  anchor_register(
                int an_id,
                int an_px, 
                int an_py, 
                int an_pz, 
                int group, //基站所属的组
                int range, //基站的量程
                int dimension,//基站的维度
                int nearby_anchor_number, // 临近基站id数目
                int[] nearby_anchor_id_list); // 临近基站id数组
 
 
        /**添加区域*/
        public  int add_area(
                int id, //区域编号
                int num, // 顶点数目
                int[] px, //x顶点集合
                int[] py, //y顶点集合
                int[] entrance, // 可入
                int[] exit); // 可出
 
        /**删除区域*/
        public  int delete_area( // 如果不成功返回-1
                int id); // 要删除的基站id
 
        /** 返回区域内基站*/
        public  int get_anchor_in_area( // 返回区域内基站数目
                int area_id, // 输入区域id
                int[] an_id); // 返回基站id
 
 
        /**返回时间片函数
         * @param timestamp时间戳
         * @param 标签id
         * @param 标签的频率*/
        public int slotted_aloha(int timestamp, int tagid, int tag_freq);
 
        /**
         * @param timestamp时间戳 (小时*3600+分钟*60)*1000 
         * @param an_x基站X坐标
         * @param an_y基站y坐标
         * @param an_z基站z坐标
         * @param dis 标签到基站距离
         * @param  anchor_num 基站数量
         * @param  anchor_id基站id
         * @param  tag_id 标签id
         * @param  tag_heigt 标签高度 s
         * @param  tag_hz标签频率
         * @param  tagspeed_limit速度限制
         * @param  anchour_distance基站的量程
         * @param  dimension 算法选择1是1维2是二维
         * @param  stationary_flag静止状态
         * @param  tag_pos 返回的标签坐标
         * @param  power第一路径信号强度集合
         * @param  power_diff第二路信号强度集合*/
 
        public int get_position(
                int timestamp, 
                int[]  an_x, 
                int[] an_y, 
                int[] an_z,
                int[] dis, 
                int anchor_num, 
                int[] anchor_id, 
                int tag_id, 
                int tag_height, 
                int tag_hz, 
                int tagspeed_limit,
                int[] anchour_distance, 
                int[] dimension ,
                int stationary_flag,                
                int[] power,
                int[] power_diff,
                int[] zu,
                int[] tag_pos_out
                );
 
        /**删除时间片接口函数,返回值0正常成功删除,其他异常。
         * @param 参数为tagid(标签id)和
         *  @param tag_freq(标签频率)*/
        public int delete_tag_from_slotted_aloha(int tagid, int tag_freq); 
 
 
 
 
    }
 
    /**删除时间片函数* 
     * @param 参数为tagid(标签id)和
     *@param tag_freq(标签频率)*/
 
    public static int delete_tag_from_slotted_aloha(int tagid, int tag_freq) {      
 
        int succ=DLibrary.INSTANCE.delete_tag_from_slotted_aloha(tagid, tag_freq);
        String filePath = Config.getBaowenConfig();
        String baowenSwitch = ModifyConfig.readData(filePath,"baowenSwitch").toString(); //进入查看报文页面打开开关
        String debugSwitch = ModifyConfig.readData(filePath,"debugSwitch").toString();;//调试页面打开
        if(baowenSwitch.contentEquals("1")&& debugSwitch.contentEquals("1")) {
            StringBuffer text_str=new StringBuffer();
            text_str.append("delete_tag_from_slotted_aloha:"+tagid+","+tag_freq+",return:"+succ);
//          String postUrl = Config.getPostUrl();
//          String debugUrl=postUrl+"debugMessage.do";
            String messageJson = "{\"result\":\""+ text_str + "\"}";
            JSONObject json = JSONObject.fromObject(messageJson);
//          HttpUtil.doPost(debugUrl, json, "UTF-8");
            text_str=new StringBuffer();
        }
 
        return succ;
    }
 
    /**返回时间片函数
     * @param timestamp时间戳
     * @param 标签id
     * @param 标签的频率*/
    public static int get_slotted_aloha(int timestamp, int tagid, int tag_freq) {
 
        int slotted_aloha=DLibrary.INSTANCE.slotted_aloha(timestamp, tagid, tag_freq);
        String filePath = Config.getBaowenConfig();
        String baowenSwitch = ModifyConfig.readData(filePath,"baowenSwitch").toString(); //进入查看报文页面打开开关
        String debugSwitch = ModifyConfig.readData(filePath,"debugSwitch").toString();;//调试页面打开
        if(baowenSwitch.contentEquals("1")&& debugSwitch.contentEquals("1")) {
            StringBuffer text_str=new StringBuffer();
            text_str.append("get_slotted_aloha:"+timestamp+", "+tagid+", "+tag_freq+",return:"+slotted_aloha);
            String postUrl = Config.getPostUrl();
//          String debugUrl=postUrl+"debugMessage.do";
            String messageJson = "{\"result\":\""+ text_str + "\"}";
            JSONObject json = JSONObject.fromObject(messageJson);
//          HttpUtil.doPost(debugUrl, json, "UTF-8");
            text_str=new StringBuffer();
        }
 
        return slotted_aloha;
    }
 
    /**算法库初始化*/
    public static void suanfa_chushi(int anchornum) {
        if(start) {
            suanfaversion=DLibrary.INSTANCE.SuanFa_Init(anchornum);
            start=false;
        }
    }
 
 
    /**告诉算法库基站的相关信息*/
    public static int  anchor_register(
            int an_id,
            int an_px, 
            int an_py, 
            int an_pz, 
            int group, //基站所属的组
            int range, //基站的量程
            int dimension,//基站的维度
            int nearby_anchor_number, // 临近基站id数目
            int[] nearby_anchor_id_list) {// 临近基站id数组
//      Open_soft_dialog.getJt_are().append(GetNowTime.now()+":初始化算法库...\n");
 
        int num=DLibrary.INSTANCE.anchor_register(an_id, 
                an_px, 
                an_py, 
                an_pz, 
                group, 
                range, 
                dimension, 
                nearby_anchor_number, 
                nearby_anchor_id_list);
 
        if(true) {
 
            StringBuffer near_anchorid=new StringBuffer();
            for(int i=0;i<nearby_anchor_id_list.length;i++) {
 
                if(i==(nearby_anchor_id_list.length-1)) {
                    near_anchorid.append(nearby_anchor_id_list[i]);
                }else {
                    near_anchorid.append(nearby_anchor_id_list[i]+",");
                }
            }
 
 
 
            StringBuffer text_str=new StringBuffer();
            text_str.append("anchor_register: "+an_id+", "+an_px+", "
                    +an_pz+", "+group+", "+range+", "+dimension+", "
                    +nearby_anchor_number+", ("+near_anchorid+"), result:"+num);
 
            String postUrl = Config.getPostUrl();
//          String debugUrl=postUrl+"debugMessage.do";
            String messageJson = "{\"result\":\""+ text_str + "\"}";
            JSONObject json = JSONObject.fromObject(messageJson);
//          HttpUtil.doPost(debugUrl, json, "UTF-8");
            
            text_str=null;
            near_anchorid=null;
        }
 
        return num;
    }
 
 
    /**添加区域*/
    public static  int add_area(
            int id, //区域编号
            int num, // 顶点数目
            int[] px, //x顶点集合
            int[] py, //y顶点集合
            int[] entrance, // 可入
            int[] exit) {// 可出
 
        int add_area=DLibrary.INSTANCE.add_area(id, num, px, py, entrance, exit);
//      Open_soft_dialog.getJt_are().append(GetNowTime.now()+":初始化算法库区域...\n");
 
        if(true) {
            StringBuffer px1=new StringBuffer();
            StringBuffer py1=new StringBuffer();
            StringBuffer entrance1=new StringBuffer();
            StringBuffer exit1=new StringBuffer();
            for(int i=0;i<px.length;i++) {
 
                if(i==(px.length-1)) {
                    px1.append(px[i]);
                }else {
                    px1.append(px[i]+",");
                }
            }
 
            for(int i=0;i<py.length;i++) {
 
                if(i==(py.length-1)) {
                    py1.append(py[i]);
                }else {
                    py1.append(py[i]+",");
                }
            }
 
            for(int i=0;i<entrance.length;i++) {
 
                if(i==(entrance.length-1)) {
                    entrance1.append(entrance[i]);
                }else {
                    entrance1.append(entrance[i]+",");
                }
            }
 
            for(int i=0;i<exit.length;i++) {
 
                if(i==(exit.length-1)) {
                    exit1.append(exit[i]);
                }else {
                    exit1.append(exit[i]+",");
                }
            }
 
 
            StringBuffer text_str=new StringBuffer();
            text_str.append("add_area: "+id+num+", ("+px1+"), ("+py1+"), ("+entrance1+"), ("+exit1+"), result:"+add_area);
 
            String postUrl = Config.getPostUrl();
//          String debugUrl=postUrl+"debugMessage.do";
            String messageJson = "{\"result\":\""+ text_str + "\"}";
            JSONObject json = JSONObject.fromObject(messageJson);
//          HttpUtil.doPost(debugUrl, json, "UTF-8");
 
            text_str=null;
            px1=null;
            py1=null;
            entrance1=null;
            exit1=null;
        }
 
        return add_area;
    }
 
    /**删除区域*/
    public  static int delete_area( // 如果不成功返回-1
            int id) {
 
        int delete_area=DLibrary.INSTANCE.delete_area(id);
 
        return delete_area;
    }
 
    /** 返回区域内基站*/
    public  int get_anchor_in_area( // 返回区域内基站数目
            int area_id, // 输入区域id
            int[] an_id) {// 返回基站id
        int get_anchor_in_area=DLibrary.INSTANCE.get_anchor_in_area(area_id, an_id);
 
        return get_anchor_in_area;
    };
 
 
 
 
 
    /**
     * @param timestamp时间戳 (小时*3600+分钟*60)*1000 
     * @param an_x基站X坐标
     * @param an_y基站y坐标
     * @param an_z基站z坐标
     * @param dis 标签到基站距离
     * @param  anchor_num 基站数量
     * @param  anchor_id基站id
     * @param  tag_id 标签id
     * @param  tag_heigt 标签高度 s
     * @param  tag_hz标签频率
     * @param  tagspeed_limit速度限制
     * @param  anchour_distance基站的量程
     * @param  dimension 算法选择1是1维2是二维
     * @param  stationary_flag静止状态
     * @param  tag_pos 返回的标签坐标
     * @param  power第一路径信号强度集合
     * @param  power_diff第二路信号强度集合
     * @param  zu基站所在的组*/
    public static int[] get_tag_pos(
            int timestamp, 
            int[]  an_x, 
            int[] an_y, 
            int[] an_z,
            int[] dis, 
            int anchor_num, 
            int[] anchor_id, 
            int tag_id, 
            int tag_height, 
            int tag_hz,
            int tagspeed_limit, 
            int[] anchour_distance, 
            int[] dimension,
            int stationary_flag,
            int[] power,
            int[] power_diff,
            int[] zu
            ) {
 
 
        /**经过DNS计算后输出的结果*/
        int[] tag_pos_out= {0,0};
 
        youxiao=DLibrary.INSTANCE.get_position(
                timestamp, 
                an_x, 
                an_y, 
                an_z,
                dis,  
                anchor_num,  
                anchor_id, 
                tag_id,  
                tag_height, 
                tag_hz, 
                tagspeed_limit, 
                anchour_distance,
                dimension,
                stationary_flag,
                power,
                power_diff,
                zu,
                tag_pos_out); 
 
 
        //      System.out.println("输出位置结果"+tag_pos_out[0]+";"+tag_pos_out[1]);
 
        return tag_pos_out;
    }
 
 
 
    public static int getYouxiao() {
        return youxiao;
    }
 
 
 
    public static String getSuanfaversion() {
 
        int v=suanfaversion/1000;
        int banben=suanfaversion % 1000;
        String version="V"+v+"."+banben;
        //      System.out.println(suanfaversion);
 
        return version;
    }
 
 
}