package com.njcn.roma.client;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

public class TestNorthApi {

    private static final String ApicHost = "10.10.10.1:1443";
    private static final String appId = "roma.link.test";
    private static final String appKey = "ic+HsXPRCA****";

    private static final String deviceId="D6111KOQk";


    //测命令下发时用到的，替换成自己的serviceName和commandName
    private static final String serviceName="service";
    private static final String commandName="command";


    /**
     * 创建ssl连接，设置客户端信任所有证书
     * @return CloseableHttpClient
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    private static CloseableHttpClient createSSLClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true).build();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[]{"TLSv1.2"}, null,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }

    /**
     * 设置请求时的header
     * @param httpUriRequest
     */
    private static void setSSLHeader(HttpUriRequest httpUriRequest) {
        long time = System.currentTimeMillis();
        String authorization = DigestUtils.sha256Hex(appId + appKey + time);
        httpUriRequest.addHeader("Content-Type", "application/json");
        httpUriRequest.addHeader("Authorization", authorization);
        httpUriRequest.addHeader("timestamp", String.valueOf(time));
    }
    /**
     * 解析响应结果
     * @param httpUriRequest
     * @return
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws KeyManagementException
     * @throws IOException
     */
    private static HttpEntity getSSLResponse(HttpUriRequest httpUriRequest) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        CloseableHttpClient httpClient = createSSLClient();
        CloseableHttpResponse response = httpClient.execute(httpUriRequest);
        HttpEntity responseEntity = response.getEntity();
        System.out.println("响应状态为:" + response.getStatusLine());
        if (responseEntity != null) {
            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
            System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
        }
        return responseEntity;
    }

    /**
     * 命令下发
     *
     * @param serviceName
     * @param commandName
     * @param cmdComtent
     * @throws JSONException
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     * @throws KeyManagementException
     */
    public static void deviceCommands(String serviceName, String commandName, JSONObject cmdComtent) throws JSONException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        String url = "https://" + ApicHost + "/iot/1.0/deviceCommands?appId=" + appId;
        HttpPost httpPost = new HttpPost(url);  //创建post请求

        JSONObject cmdContent = new JSONObject();
        cmdContent.put("serviceId", serviceName);
        cmdContent.put("method", commandName);
        cmdContent.put("paras", cmdComtent);
        JSONObject cmdBody = new JSONObject();
        cmdBody.put("command", cmdContent);
        cmdBody.put("deviceId", deviceId);

        setSSLHeader(httpPost);	//设置请求的header
        StringEntity stringEntity = new StringEntity(cmdBody.toString(), "utf-8");
        httpPost.setEntity(stringEntity);//设置请求的body
        getSSLResponse(httpPost);//获取响应结果

    }


    public static void main(String[] args) throws Exception {
        JSONObject commandContent = new JSONObject();
        commandContent.put("status", "on");
        deviceCommands(serviceName, commandName, commandContent);

    }
}