MrShi
5 天以前 98a1749e3e614da9ce8afbf3af7c474cd0bf6702
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
package com.doumee.core.device;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
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.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
 
public class ElectronicToolUitl {
 
    // 授权码 登录 后台获取, 联系你的甲方或者是销售
    private static String auth_code = "1f29d378fc6792d5d2b735877993ffb7";
    // 随机字符串 后台获取
    private static String nonce = "lvyOY7fcun719WkxF8ToVYatStgt";
    // 测试用采集器号  请使用真实设备采集器号
    private static String debug_collector = "19020618114";
    // 测试用电表号 请使用真实设备采集器号
    private static String debug_meter = "000066660942";
    // 异步通知地址(服务如果部署在内网,在公网无法直接访问到,需要在路由器上配置端口映射,或者配置内网穿透工具来实现访问) 此处仅为测试 示例
    private static String notify_url = "http://115.221.15.8:8055/notify";
    private static String api_url = "http://api1.tqdianbiao.com";
    private static String api2_url = "http://api2.tqdianbiao.com";
 
 
    public static void main(String[] args) {
// 注意 如需使用充值功能 可先了解 [参数维护模式](http://doc-api.tqdianbiao.com/doc/backend/data_sync_mode.html)
// 注意 有2个系统 授权码等参数  各自独立   接口域名分别为  https://iot.tqdianbiao.com/  http://api1.tqdianbiao.com
 
//        -------- 系统维护关键参数模式 接口-------------
//        采集器添加();
//        采集器删除();
//        查询N个采集器();
//        查询全部采集器();
//
//        电能表添加();
//        电能表删除();
//        电能表档案查询();
//
//        水表添加();
//        水表删除();
//        水表档案查询();
 
//        操作状态查询();
//        取消操作();
 
 
//        ------------ 接入方维护关键参数模式 数据接口-------------
//        抄电表数据();
//        设置电表参数();
//        电表拉闸();
//        电表合闸();
//        电表清零_同步模式();
//        电表清零_非同步模式();
//        电表开户_同步模式();
//        电表开户_非同步模式();
//        电表充值_同步模式();
//        电表充值_非同步模式();
 
//        抄水表数据();
//        水表关阀();
//        水表开阀();
//        水表清零();
//        Mbus水表充值_同步模式();
//        Mbus水表充值_非同步模式();
//        Lora水表充值_同步模式();
//        Lora水表充值_非同步模式();
 
        Lora普通预付费水表设置水价();
        Lora阶梯预付费水表设置水价();
 
//        第一套数据接口--------------
//        查询历史数据接口();
//        查询设备列表和当前状态();
//        查询采集器列表和当前状态();
//        查询价格档案();
//        查询用户档案();
//        查询参数档案();
//        查询电表当前状态数据();
//        查询水表当前状态数据();
 
    }
 
    /**
     * 查询设备列表和当前状态
      */
    private static void queryDeviceList() {
        // 查询设备列表和当前状态
        String url = api_url+"/Api/Meter";  // "https://iot.tqdianbiao.com/Api/Meter"
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
 
        url = url + "?" + urlParams;
 
        String resp = HttpClientGet(url);
 
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询采集器列表和当前状态
     */
    private static void queryCollectorList() {
        // 查询采集器列表和当前状态
        String url = api_url+"/Api/Collector";
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
 
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
 
        url = url + "?" + urlParams;
 
        String resp = HttpClientGet(url);
 
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询价格档案
     */
    private static void queryPrice() {
        // 查询价格档案
        String url = api_url+"/Api/Price";
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
        url = url + "?" + urlParams;
        String resp = HttpClientGet(url);
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询用户档案
     */
    private static void queryUser() {
        // 查询用户档案
        String url = api_url+"/Api/User";
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
        url = url + "?" + urlParams;
        String resp = HttpClientGet(url);
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询参数档案
     */
    private static void queryParam() {
        // 查询参数档案
        String url = api_url+"/Api/Param";
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
 
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
 
        url = url + "?" + urlParams;
 
        String resp = HttpClientGet(url);
 
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询电表当前状态数据
     */
    private static void queryEleMeterState() {
        // 查询电表当前状态数据
        String url = api_url+"/Api/EleMeterState";
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
 
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
 
        url = url + "?" + urlParams;
 
        String resp = HttpClientGet(url);
 
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询水表当前状态数据
     */
    private static void queryWaterMeterState() {
        // 查询水表当前状态数据
        String url = api_url+"/Api/WaterMeterState";
        Map<String, Object> params = new HashMap<>();
        params.put("auth", auth_code);
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
        url = url + "?" + urlParams;
        String resp = HttpClientGet(url);
        simpleRequestPrint(url, resp);
    }
 
    /**
     * 查询历史数据接口
     */
    private static void queryDataRequest() {
        // 查询历史数据接口
        String url = api_url+"/Api/DataRequest";
        Map<String, Object> params = new HashMap<>();
        params.put("auth",          auth_code);
        params.put("type",          "json");
        params.put("functionids",   "3,4,5");
        params.put("start_time",    "2020-01-01 00:00:00");
        params.put("end_time",      "2020-06-01 00:00:00");
        params.put("offset",        0);
        params.put("limit",         100);
 
        // Map<String, Object> 转成url参数
        String urlParams = getUrlParams(params);
        url = url + "?" + urlParams;
        String resp = HttpClientGet(url);
        simpleRequestPrint(url, resp);
 
    }
 
    private static void simpleRequestPrint(String url, String resp) {
        String name=Thread.currentThread().getStackTrace()[2].getMethodName();
        System.out.println(name);
 
        System.out.println("请求参数:" + url);
        System.out.println("返回数据:" + resp);
    }
 
    private static String HttpClientGet(String url) {
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse Response = client.execute(httpGet);
            HttpEntity entity = Response.getEntity();
            String resp = EntityUtils.toString(entity, "UTF-8");
            Response.close();
            return  resp;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return "";
        }
    }
 
    private static String getUrlParams(Map<String, Object> map) {
        if (map == null || map.size() == 0) {
            return "";
        }
        List<String> list = new ArrayList<>();
        map.forEach((key, value)->{
            try {
                list.add(key + "=" + URLEncoder.encode(value.toString(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        });
        return String.join("&", list);
    }
 
    /**
     * 电表开户_同步模式
     */
    private static void openAcount() {
        String url = api2_url+"/Api_v2/ele_security/open_acount";
 
        // 请求内容,调用接口所需要的数据(电表开户,同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
    }
 
    /**
     * 电表开户_非同步模式
     */
    private static void 电表开户_非同步模式() {
        String url = api2_url+"/Api_v2/ele_security/open_acount";
 
        // 请求内容,调用接口所需要的数据(电表开户,非同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
        params.put("account_id", "123456");
        params.put("count", "1");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 电表充值_同步模式() {
        String url = api2_url+"/Api_v2/ele_security/recharge";
 
        // 请求内容,调用接口所需要的数据(电表充值,同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 电表充值_非同步模式() {
        String url = api2_url+"/Api_v2/ele_security/recharge";
 
        // 请求内容,调用接口所需要的数据(电表充值,非同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
        params.put("account_id", "123456");
        params.put("count", "2");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
    }
 
    private static void Lora普通预付费水表设置水价() {
        String url = api2_url+"/Api_v2/water_write/price";
 
        // 请求内容,调用接口所需要的数据(Lora普通预付费水表设置水价)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("p1", "1");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "10000000031");
        item.put("address", "20040900000003");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void Lora阶梯预付费水表设置水价() {
        String url = api2_url+"/Api_v2/water_write/price";
 
        // 请求内容,调用接口所需要的数据(Lora阶梯预付费水表设置水价)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("p1", "1");
        params.put("p2", "2");
        params.put("p3", "3");
        params.put("p4", "40");
        params.put("p5", "50");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "10000000031");
        item.put("address", "20040900000003");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void Lora水表充值_同步模式() {
        String url = api2_url+"/Api_v2/water_security/recharge";
 
        // 请求内容,调用接口所需要的数据(Lora水表充值,同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "10000000031");
        item.put("address", "20040900000003");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void Lora水表充值_非同步模式() {
        String url = api2_url+"/Api_v2/water_security/recharge";
 
        // 请求内容,调用接口所需要的数据(Lora水表充值,非同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
        params.put("count", "2");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "10000000031");
        item.put("address", "20040900000003");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void Mbus水表充值_同步模式() {
        String url = api2_url+"/Api_v2/water_security/recharge";
 
        // 请求内容,调用接口所需要的数据(Mbus水表充值,同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "88020206100");
        item.put("address", "C1E81000007859");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void Mbus水表充值_非同步模式() {
        String url = api2_url+"/Api_v2/water_security/recharge";
 
        // 请求内容,调用接口所需要的数据(Mbus水表充值,非同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("money", "100");
        params.put("price", "5");
        params.put("count", "2");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "88020206100");
        item.put("address", "C1E81000007859");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 水表清零() {
        String url = api2_url+"/Api_v2/water_security/reset";
 
        // 请求内容,调用接口所需要的数据(水表清零)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "88020206100");
        item.put("address", "C1E81000007859");
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 电表清零_非同步模式() {
        String url = api2_url+"/Api_v2/ele_security/reset";
 
        // 请求内容,调用接口所需要的数据(电表清零,非同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("account_id", "123456");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("params", params);
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 电表清零_同步模式() {
        String url = api2_url+"/Api_v2/ele_security/reset";
 
        // 请求内容,调用接口所需要的数据(电表清零,同步模式)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
 
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 电表拉闸() {
        String url = api2_url+"/Api_v2/ele_control";
 
        // 请求内容,调用接口所需要的数据(电表拉闸)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("type", 10);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 电表合闸() {
        String url = api2_url+"/Api_v2/ele_control";
 
        // 请求内容,调用接口所需要的数据(电表合闸)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("type", 11);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 水表关阀() {
        String url = api2_url+"/Api_v2/water_control";
 
        // 请求内容,调用接口所需要的数据(水表关阀)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "88020206100");
        item.put("address", "C1E81000007859");
        item.put("type", 53);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 水表开阀() {
        String url = api2_url+"/Api_v2/water_control";
 
        // 请求内容,调用接口所需要的数据(水表开阀)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "88020206100");
        item.put("address", "C1E81000007859");
        item.put("type", 43);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 设置电表参数() {
        String url = api2_url+"/Api_v2/ele_write";
 
        // 请求内容,调用接口所需要的数据(一级报警值)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("p1", "300");
 
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("type", 24);
        item.put("params", params);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 抄电表数据() {
        String url = api2_url+"/Api_v2/ele_read";
 
        // 请求内容,调用接口所需要的数据(抄电表数据)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        item.put("type", 3);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static void 抄水表数据() {
        String url = api2_url+"/Api_v2/water_read";
 
        // 请求内容,调用接口所需要的数据(抄水表数据)
        List<Map<String, Object>> req = new ArrayList<>();
        Map<String, Object> item = new HashMap<>();
        item.put("opr_id", generateOperateId());
        item.put("time_out", 0);
        item.put("must_online", true);
        item.put("retry_times", 1);
        item.put("cid", "88020206100");
        item.put("address", "C1E81000007859");
        item.put("type", 42);
        req.add(item);
 
        String request_content = JSON.toJSONString(req);
        testApiAsync(url, request_content);
 
    }
 
    private static String generateOperateId() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
    private static void 电能表档案查询()
    {
        String url = api2_url+"/Api_v2/ele_meter/query";
        // 请求内容,调用接口所需要的数据(查询指定的采集器下的电能表信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "12345678901");     // 查询其他区域的采集器下的电能表(未找到采集器)
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "29020618114");     // 查询不存在的采集器下的电能表(未找到采集器)
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "19020618114");     // 查询本区域已经添加的采集器下的电能表(得到正确结果:"address":["000066660942"])
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "1902061811411");   // 查询不合法的采集器下的电能表(只支持11/12位采集器号)
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
 
    private static void 电能表删除()
    {
        String url = api2_url+"/Api_v2/ele_meter/delete";
        // 请求内容,调用接口所需要的数据(删除指定的电能表信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "19020618114");
        item.put("address", "0000666609");      // 不合法的表号
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "1902061811");          // 不合法的采集器号
        item.put("address", "000066660942");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "12345678901");         // 非本区域采集器号
        item.put("address", "000066660942");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "11335577990");
        item.put("address", "000066660942");    // 不存在的采集器号
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void 电能表添加()
    {
        String url = api2_url+"/Api_v2/ele_meter/add";
        // 请求内容,调用接口所需要的数据(添加指定的电能表信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "19020618114");
        item.put("address", "0000666609");      // 不合法的表号
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "1902061811");          // 不合法的采集器号
        item.put("address", "000066660942");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "12345678901");         // 非本区域采集器号
        item.put("address", "000066660942");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "11335577990");
        item.put("address", "000066660942");    // 不存在的采集器号
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
    private static void 水表档案查询()
    {
        String url = api2_url+"/Api_v2/water_meter/query";
        // 请求内容,调用接口所需要的数据(查询指定的采集器下的水表信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "19020618114");
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
 
    private static void 水表删除()
    {
        String url = api2_url+"/Api_v2/water_meter/delete";
        // 请求内容,调用接口所需要的数据(删除指定的水表信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void 水表添加()
    {
        String url = api2_url+"/Api_v2/water_meter/add";
        // 请求内容,调用接口所需要的数据(添加指定的水表信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "19020618114");
        item.put("address", "000066660942");
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void 操作状态查询()
    {
        String url = api2_url+"/Api_v2/request/status";
        // 请求内容,调用接口所需要的数据(操作状态查询)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("opr_id", "123456");
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void 取消操作()
    {
        String url = api2_url+"/Api_v2/request/cancel";
        // 请求内容,调用接口所需要的数据(取消操作)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("opr_id", "123456");
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
 
    private static void 采集器删除()
    {
        String url = api2_url+"/Api_v2/collector/delete";
        // 请求内容,调用接口所需要的数据(删除指定的采集器信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "12345678901");     // 删除其他区域的采集器
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "29020618114");     // 删除不存在的采集器
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "19020618114");     // 删除本区域已经添加的采集器
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "1902061811411");   // 删除不合法的采集器
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void 采集器添加()
    {
        String url = api2_url+"/Api_v2/collector/add";
        // 请求内容,调用接口所需要的数据(添加指定的采集器信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "12345678901");     // 采集器号被其他区域用户添加过的
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "19020618114");     // 多次调用均会返回添加成功
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void 查询全部采集器()
    {
        String url = api2_url+"/Api_v2/collector/query";
        // 请求内容,调用接口所需要的数据(查询所有采集器信息)
        String request_content = "[]";
 
        testApi(url, request_content);
    }
    private static void 查询N个采集器()
    {
        String url = api2_url+"/Api_v2/collector/query";
        // 请求内容,调用接口所需要的数据(查询指定的采集器信息)
        List<Map<String, String>> req = new ArrayList<>();
        Map<String, String> item = new HashMap<>();
        item.put("cid", "100000000101");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "10000000101");
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "1000000101");      // 采集器号不符合规范
        req.add(item);
        item = new HashMap<>();
        item.put("cid", "50000000101");     // 不存在的采集器
        req.add(item);
        String request_content = JSON.toJSONString(req);
 
        testApi(url, request_content);
    }
 
    private static void testApi(String url, String request_content)
    {
        String name=Thread.currentThread().getStackTrace()[2].getMethodName();
        System.out.println(name);
 
        String response = request(url, request_content);
        printResponse(response);
    }
 
    private static void testApiAsync(String url, String request_content)
    {
        String name=Thread.currentThread().getStackTrace()[2].getMethodName();
        System.out.println(name);
 
        String response = requestAsync(url, request_content);
        printResponse(response);
    }
 
    // 打印响应内容
    private static void printResponse(String response) {
        JSONObject jsonObject = JSON.parseObject(response);
 
        String status = jsonObject.getString("status");
        if(!"SUCCESS".equals(status)) {
            System.out.println(jsonObject.getString("error_msg"));
        } else {
            String response_content = jsonObject.getString("response_content");
            System.out.println("response_content: " + response_content);
            JSONArray contentArray = JSON.parseArray(response_content);
            int index = 1;
            System.out.println("返回结果:");
            for(int i = 0; i < contentArray.size(); ++i) {
                System.out.println("[" + index++ + "]");
                JSONObject contentObject = contentArray.getJSONObject(i);
                Set<String> keySet = contentObject.keySet();
                for(String key: keySet) {
                    System.out.println(key + ": " + contentObject.get(key));
                }
 
            }
        }
    }
 
    // 请求接口
    private static String request(String url, String request_content){
        // 时间戳
        String timestamp = String.valueOf(new Date().getTime()/1000);
 
        // 用于签名的内容
        Map<String, String> data = new HashMap<>();
        data.put("timestamp", timestamp);
        data.put("auth_code", auth_code);
        data.put("request_content", request_content);
 
        // 获取签名
        String sign = getSign(data);
 
        data.put("sign", sign);
 
        try {
            return sendHttpRequest(url, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    // 请求接口
    private static String requestAsync(String url, String request_content){
        // 时间戳
        String timestamp = String.valueOf(new Date().getTime()/1000);
 
        // 用于签名的内容
        Map<String, String> data = new HashMap<>();
        data.put("timestamp", timestamp);
        data.put("auth_code", auth_code);
        data.put("request_content", request_content);
        data.put("notify_url", notify_url);
 
        // 获取签名
        String sign = getSign(data);
 
        data.put("sign", sign);
 
        try {
            return sendHttpRequest(url, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    // 生成签名字符串
    private static String getSign(Map<String, String> data)
    {
        // 获取关键字列表
        List<String> keys = new ArrayList<>(data.keySet());
        // 关键字列表排序
        keys.sort(Comparator.naturalOrder());
        StringBuilder sb = new StringBuilder();
        for (String key : keys)
        {
            // 取各个字段内容拼接字符串
            sb.append(data.get(key));
        }
        // 加上双方约定随机字符串
        String txt = sb.toString() + nonce;
 
        // 计算哈希值
        return getMD5(txt);
    }
 
    // md5加密
    private static String getMD5(String password) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        byte[] byteArray = password.getBytes(StandardCharsets.UTF_8);
 
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
 
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
 
    // 发送http请求
    private static String sendHttpRequest(String url, Map<String, String> bodyMap) throws Exception {
        System.out.println("请求地址:" + url);
        System.out.println("发送参数:" + bodyMap.toString());
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost postRequest = new HttpPost(url);
 
        List<NameValuePair> nvps = new ArrayList<>();
 
        for(String key : bodyMap.keySet()) {
            nvps.add(new BasicNameValuePair(key,bodyMap.get(key)));
        }
        postRequest.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
 
 
        int retry = 3;
        HttpResponse execute = null;
        while(retry-- > 0) {
            try {
                execute = client.execute(postRequest);
                break;
            } catch (Exception e) {
                Thread.sleep(5000);
            }
        }
        if(execute == null) {
            throw new Exception("接口请求失败");
        }
        String resp = EntityUtils.toString(execute.getEntity(), "UTF-8");
        System.out.println("接口返回:" + resp);
        return resp;
    }
 
}