 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.json.simple.JSONObject;

 public class AnprStateSend
   extends Thread
 {


   private String g_strTenel = "";
   private String g_strLane = "";

   private String g_strApiUrAnprStatusl = "";

   private final Logger logger = Logger.getLogger(AnprStateSend.class);


   public AnprStateSend(String str) {
     try {
       String propFile = "./config.properties";
       Properties props = new Properties();
       FileInputStream fis = new FileInputStream(propFile);
       props.load(new BufferedInputStream(fis));



       this.g_strTenel = props.getProperty("tennel_no"); //터널
       this.g_strLane = str; //차로

       this.g_strApiUrAnprStatusl = props.getProperty("api_url_status");

       props.clear();
       props = null;
     }
     catch (Exception e) {
       e.printStackTrace();
     }
   }

   @Override
   public void run() {
     runCarNoImageSend();
     this.logger.debug(String.format("=======================================================", new Object[0]));
   }


   public void runCarNoImageSend() {
     Map<String, String> cctvID = new HashMap<>();

     cctvID.put("101", "A10401001");
     cctvID.put("102", "A10301002");
     cctvID.put("103", "A10201003");
     cctvID.put("104", "A10101004");
     cctvID.put("105", "A10111005");
     cctvID.put("106", "A10211006");
     cctvID.put("107", "A10311007");
     cctvID.put("108", "A10411008");
     cctvID.put("301", "A10503001");
     cctvID.put("302", "A10403002");
     cctvID.put("303", "A10303003");
     cctvID.put("304", "A10203004");
     cctvID.put("307", "A10213005");
     cctvID.put("308", "A10313006");
     cctvID.put("309", "A10413007");
     cctvID.put("310", "A10513008");

     Date date_now = new Date(System.currentTimeMillis());
     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
     String strDate = sdf.format(date_now);

     this.logger.debug("===========" + strDate);


     List<String> temp = new ArrayList<>();
     temp.add("13");
     temp.add("11");
     temp.add("12");

     int n = (int)(Math.random() * 1000.0D) % 3;

     JSONObject jsonParam = new JSONObject();
     jsonParam.put("camId", cctvID.get(String.valueOf(this.g_strTenel) + this.g_strLane));
     jsonParam.put("colctTime", strDate);
     jsonParam.put("camConectSttus", "0");    //카메라접속상태
     jsonParam.put("rtuTp", temp.get(n % 3)); //온도:랜덤(11도12도13도)
     jsonParam.put("bxDoorSttus", "0");       //함체도어상태
     jsonParam.put("camSdCardSttus", "0");    //SD카드상태
     this.logger.debug("============== : " + jsonParam.toJSONString());
     callAnprCarnoSend(jsonParam.toJSONString());
     jsonParam.clear();
   }


   private void callAnprCarnoSend(String strJson) {
     HttpURLConnection connection = null;
     InputStream is = null;
     InputStreamReader isr = null;
     BufferedReader br = null;

     try {
       URL url = new URL(this.g_strApiUrAnprStatusl);
       this.logger.debug("==== 수집서버 주소  : " + this.g_strApiUrAnprStatusl);

       connection = (HttpURLConnection)url.openConnection();
       connection.setDoInput(true);
       connection.setDoOutput(true);
       connection.setUseCaches(false);
       connection.setRequestMethod("POST");
       connection.setConnectTimeout(30000);
       connection.setReadTimeout(30000);
       connection.setRequestProperty("Accept", "application/json");
       connection.setRequestProperty("Content-Type", "application/json");

       BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
       bos.write(strJson.getBytes("UTF-8"));
       bos.flush();
       bos.close();

       int resCode = connection.getResponseCode();
       StringBuffer sb = new StringBuffer();
       String inputLine = null;

       this.logger.debug("====  : " + resCode);
       if (resCode == 200) {
         is = connection.getInputStream();
         isr = new InputStreamReader(is, "UTF-8");
         br = new BufferedReader(isr);

         while ((inputLine = br.readLine()) != null) {
           sb.append(inputLine);
         }

         this.logger.debug("==== " + sb.toString());
       }
       else if (connection.getErrorStream() != null) {
         br = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8"));

         while ((inputLine = br.readLine()) != null) {
           sb.append(inputLine);
         }
         this.logger.debug("==== Http Error Resopnse : " + sb.toString());
       } else {
         this.logger.debug("==== Http Error Resopnse : " + sb.toString());
       }

     } catch (Exception e) {
       e.printStackTrace();
       this.logger.debug("==== Http Error Resopnse : ");
     } finally {
       try {
         if (is != null)
           is.close();
         if (isr != null)
           isr.close();
         if (br != null)
           br.close();
       } catch (Exception exception) {}
     }
   }
 }


