a
554325746@qq.com
2019-12-25 7340eb0b160eacbbd0f3c2289e3ac6150da235f3
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
package com.basic.security.manager.es;
 
import android.util.Log;
 
import com.basic.security.manager.BaseManager;
import com.basic.security.manager.DeviceManager;
import com.basic.security.model.ModelAdapter;
import com.basic.security.utils.Constants;
 
import java.io.IOException;
import java.util.Properties;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
 
import static com.basic.security.utils.DateUtil.formatTime;
 
public class EsBaseManager {
    //ES集群中某个节点
    private static String HOSTNAME;
    //连接端口号
    private static String HTTP_PORT;
    //连接url
    private static String HTTP_URL;
    //同步人员的url
    private static String PERSON_URL;
 
    public EsBaseManager() {
        Properties properties = new Properties();
        try {
            properties.load(EsBaseManager.class.getResourceAsStream("/assets/es_config.properties"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        HOSTNAME = properties.getProperty("HOSTNAME");
        HTTP_PORT = properties.getProperty("HTTP_PORT");
        HTTP_URL = properties.getProperty("HTTP_URL");
        PERSON_URL = properties.getProperty("PERSON_URL");
    }
 
    //编写一个方法,当每次有记录保存到访问记录中的时候,调用!
    public void queryAndSend(String recordTime) {
        //首先根据参数从数据库中查询这条数据
        ModelAdapter modelAdapter = new ModelAdapter();
        modelAdapter = BaseManager.findByTime("visit", recordTime);
        ModelAdapter deviceAdapter = DeviceManager.getDevice();
        if (modelAdapter.model.size() != 0) {
            //拼接字符串之前也需要判断相对的字段是否有值
            String jsonStr = "{\n" +
                    //摄像机id,存设备id
                    "\"videoReqNum\" : \"" + modelAdapter.getString("device_id") + "\",\n" +
                    //性别,默认值1
                    "\"Gender\" : \"1\",\n" +
                    //内容,保留字段
                    "\"content\" : \"\",\n" +
                    //比对时间,比对完成时间
                    "\"likeDate\" : \"" + formatTime(Long.parseLong(String.valueOf(Constants.compareFeatureTime)), "yyyy-MM-dd HH:mm:ss") + "\",\n" +
                    //抓拍地址:设备地址
                    "\"picAddress\" : \"" + deviceAdapter.getString("address") + "\",\n" +
                    //年龄
                    "\"Age\" : \"\",\n" +
                    //抓拍时间
                    "\"picDate\" : \"" + formatTime(Long.parseLong(modelAdapter.getString("visit_time").toString()), "yyyy-MM-dd HH:mm:ss") + "\",\n" +
                    //相似值
                    "\"likePer\" : \"" + Constants.score + "\",\n" +
                    //分析设备id,当前设备id
                    "\"indeviceid\" : \"" + modelAdapter.getString("device_id") + "\",\n" +
                    //人员id
                    "\"personId\" : \"" + modelAdapter.getString("person_id") + "\",\n" +
                    //分析设备名称,设备名称
                    "\"indevicename\" : \"" + modelAdapter.getString("device_name") + "\",\n" +
                    //是否报警,传空
                    "\"personIsHub\" : \"\",\n" +
                    //设备id
                    "\"videoIp\" : \"" + modelAdapter.getString("device_id") + "\"\n" +
                    "}";
            //将数据插入到ES集群
            useOkHttp(modelAdapter.getString("id").toString(), jsonStr);
        }
    }
 
    //使用OkHttp请求
    public void useOkHttp(String id, String jsonStr) {
        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            MediaType JSON = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(JSON, jsonStr);
            Request request = new Request.Builder()
                    .url("http://" + HOSTNAME + ":" + HTTP_PORT + "" + HTTP_URL + "" + id + "")
                    .put(body)
                    .build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                public void onFailure(Call call, IOException e) {
                    Log.d("Http:call", "" + call.request() + "");
                }
 
                public void onResponse(Call call, Response response) {
                    Log.d("Http:call", "" + response.code() + "");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}