package com.yuchen.saas.park.service.util.iot.client;

import com.thinkunion.sdk.iot.IoTClient;
import com.thinkunion.sdk.iot.bean.BaseRequest;
import com.thinkunion.sdk.iot.bean.BaseResponse;
import com.thinkunion.sdk.iot.exception.IotClientException;
import com.thinkunion.sdk.iot.exception.IotServerException;
import com.yuchen.saas.integration.feign.api.config.IotOpenApiConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 刘通
 * @date 2020/12/25 0:14
 */
@Component
public class IotClientUtils {

    @Resource
    private IotOpenApiConfig iotOpenApiConfig;

    @Autowired
    private IoTClient initClient(){
        return new IoTClient(iotOpenApiConfig.getUri(), iotOpenApiConfig.getAccessKey(), iotOpenApiConfig.getAccessSecret());
    }

    public <T extends BaseResponse> T connect(BaseRequest<T> request) {
        try {
            return initClient().send(request);
        } catch (IotServerException e) {
            e.printStackTrace();
        } catch (IotClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}