package com.reyun.check.phonemsg;
/**
 * 打电话接口
 */

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.atomic.AtomicInteger;

public class PhoneMsgInfo
{
    private static final Logger logger = Logger.getLogger(PhoneMsgInfo.class);
    String Servicetoken;
    String receiver;
    String type;
    static String phone_url;

    public PhoneMsgInfo() {
        this.receiver = "q20180308144739930";
        this.Servicetoken = "e15057cc527dd4bdc054dce19a1316e9";
        this.type="phone";
        this.phone_url="http://www.linkedsee.com/alarm/cloudchannel";
    }

    public void sendMsg(String description)
            throws JSONException
    {
        AtomicInteger phonetime=new AtomicInteger(0);
        if(phonetime.get()==0) {
            JSONObject jsobj1 = new JSONObject();
            jsobj1.put("receiver", this.receiver);
            jsobj1.put("content", description);
            jsobj1.put("type", this.type);
            jsobj1.put("title","notice_phone");
            post(jsobj1);
            phonetime.getAndIncrement();
        }
    }

    public String post(JSONObject json) {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(this.phone_url);


        post.setHeader("Servicetoken", this.Servicetoken);
        post.addHeader("Content-Type", "application/json");

        String result = "";

        try {

            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);

            // 发送请求
            HttpResponse httpResponse = client.execute(post);

            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                strber.append(line + "\n");
            inStream.close();

            result = strber.toString();
            logger.info(result);

            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                logger.info("请求服务器成功，做相应处理");

            } else {

                logger.info("请求服务端失败");

            }


        } catch (Exception e) {
            logger.error("请求异常");
            throw new RuntimeException(e);
        }

        return result;
    }
}
