package com.basic.security.manager.impl.es; import android.content.Context; import android.util.Log; import com.basic.security.manager.impl.sqlite.SlBaseManager; import com.basic.security.manager.impl.sqlite.SlDeviceSettingManager; import com.basic.security.model.ModelAdapter; import com.basic.security.utils.Constants; //import org.apache.http.client.ResponseHandler; //import org.apache.http.client.methods.HttpPost; //import org.apache.http.entity.StringEntity; //import org.apache.http.impl.client.BasicResponseHandler; //import org.apache.http.impl.client.CloseableHttpClient; //import org.apache.http.impl.client.HttpClients; import java.io.IOException; import java.io.InputStream; 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.*; 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 = SlBaseManager.findByTime("visit",recordTime); // System.out.println(recordTime+"====="); ModelAdapter deviceAdapter = SlDeviceSettingManager.getDeviceSetting(); if(modelAdapter.sqliteModel.size() != 0){ //拼接字符串之前也需要判断相对的字段是否有值 String jsonStr = "{\n" + //摄像机id,存设备id "\"videoReqNum\" : \""+modelAdapter.sqliteModel.get("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.sqliteModel.get("address")+"\",\n" + //年龄 "\"Age\" : \"\",\n" + //抓拍时间 "\"picDate\" : \""+formatTime(Long.parseLong(modelAdapter.sqliteModel.get("visit_time").toString()), "yyyy-MM-dd HH:mm:ss")+"\",\n" + //相似值 "\"likePer\" : \""+Constants.score+"\",\n" + //分析设备id,当前设备id "\"indeviceid\" : \""+modelAdapter.sqliteModel.get("device_id")+"\",\n" + //人员id "\"personId\" : \""+modelAdapter.sqliteModel.get("person_id")+"\",\n" + //分析设备名称,设备名称 "\"indevicename\" : \""+modelAdapter.sqliteModel.get("device_name")+"\",\n" + //是否报警,传空 "\"personIsHub\" : \"\",\n" + //设备id "\"videoIp\" : \""+modelAdapter.sqliteModel.get("device_id")+"\"\n" + "}"; /** * 将数据插入到ES集群 */ useOkHttp(modelAdapter.sqliteModel.get("id").toString(),jsonStr); } } /** * 使用传统的http请求: * @param url * @param json * @return */ public static String httpResp(String url,String json){ String returnValue = "这是默认返回值,接口调用失败"; // CloseableHttpClient httpClient = HttpClients.createDefault(); // ResponseHandler responseHandler = new BasicResponseHandler(); // try{ // //第一步:创建HttpClient对象 // httpClient = HttpClients.createDefault(); // // //第二步:创建httpPost对象 // HttpPost httpPost = new HttpPost("http://"+HOSTNAME+":"+HTTP_PORT+url); // // //第三步:给httpPost设置JSON格式的参数 // httpPost.setHeader("Content-type", "application/json"); // // StringEntity requestEntity = new StringEntity(json,"utf-8"); // requestEntity.setContentEncoding("UTF-8"); // httpPost.setHeader("Content-type", "application/json"); // httpPost.setEntity(requestEntity); // //第四步:发送HttpPost请求,获取返回值 // long start = System.currentTimeMillis(); //// log.info("开始时间: "+start); // returnValue = httpClient.execute(httpPost,responseHandler); //调接口获取返回值时,必须用此方法 // // long end = System.currentTimeMillis(); //// log.info("删除结束时间: "+end+"\n用时:"+(end-start)); // } // catch(Exception e) // { // e.printStackTrace(); // } // finally { // try { // httpClient.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // //第五步:处理返回值 return returnValue; } /** *使用OkHttp请求 */ public void useOkHttp(String id,String jsonStr){ try{ // Log.d("Http.params",jsonStr); 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(); // System.out.println(body); okHttpClient.newCall(request).enqueue(new Callback(){ @Override public void onFailure(Call call, IOException e) { Log.d("Http:call",""+call.request()+""); } @Override public void onResponse(Call call, Response response) throws IOException { Log.d("Http:call",""+response.code()+""); } }); }catch (Exception e){ e.printStackTrace(); } } /** * 同步管理平台上的同步过来的人员 */ public void syncPerson(){ try{ OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url("http://"+HOSTNAME+":"+HTTP_PORT+""+HTTP_URL+"") .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { System.out.println("获取ES数据失败!"); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()){ System.out.println("获取ES数据成功!"+response); } } }); }catch (Exception e){ e.printStackTrace(); } } }