package com.hwclouds.romalink;

import com.cloud.apigateway.sdk.utils.Client;
import com.cloud.apigateway.sdk.utils.Request;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.IOException;

public class DeviceControlDemo
{
    public static void main(String[] args)
    {
        // API认证的appKey
        String appKey = "";
        // API认证的appSecret
        String appSecret = "";
        // 要发送控制消息的设备客户端ID
        String clientId = "";
        // 要发送控制消息的设备的sub权限的Topic
        String subTopic = "";
        // 租户的项目ID，在我的凭证里面获取
        String projectId = "";
        // 发送控制消息的API的访问地址
        String host = "";
        // 要给设备发送的消息内容
        String payload = "hello world.";
        String url = "https://" + host + "/v1.0/" + projectId + "/device";

        controlDevice(url, appKey, appSecret, clientId, subTopic, payload);
    }

    private static void controlDevice(String url, String appKey, String appSecret, String clientId, String topic,
            String body)
    {
        Request request = new Request();
        try
        {
            request.setKey(appKey);
            request.setSecret(appSecret);
            request.setUrl(url);
            request.addHeader("clientId", clientId);
            request.addHeader("topic", topic);
            request.setBody(body);
            request.setMethod("POST");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
            return;
        }

        CloseableHttpClient client = null;
        try
        {
            HttpRequestBase signedRequest = Client.sign(request);
            for (Header h : signedRequest.getAllHeaders())
            {
                System.out.println(h.getName() + ":" + h.getValue());
            }
            System.out.println("------------");
            SSLContext sslContext =
                    SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS().build();
            SSLConnectionSocketFactory sslSocketFactory =
                    new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
            client = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
            HttpResponse response = client.execute(signedRequest);

            System.out.println(response.getStatusLine().toString());

            Header[] resHeaders = response.getAllHeaders();
            for (Header h : resHeaders)
            {
                System.out.println(h.getName() + ":" + h.getValue());
            }

            HttpEntity resEntity = response.getEntity();
            if (resEntity != null)
            {
                System.out.println(System.getProperty("line.separator") + EntityUtils.toString(resEntity, "UTF-8"));
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            try
            {
                if (client != null)
                {
                    client.close();
                }
            }
            catch (IOException e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
}
