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();
|
}
|
}
|
}
|