package com.iwunu.nbiot.lock.aepclient;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.ctg.ag.sdk.biz.AepDeviceCommandClient;
import com.ctg.ag.sdk.biz.aep_device_command.CancelCommandRequest;
import com.ctg.ag.sdk.biz.aep_device_command.CancelCommandResponse;
import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandRequest;
import com.ctg.ag.sdk.biz.aep_device_command.CreateCommandResponse;
import com.ctg.ag.sdk.biz.aep_device_command.QueryCommandRequest;
import com.ctg.ag.sdk.biz.aep_device_command.QueryCommandResponse;
import com.ctg.ag.sdk.core.constant.Scheme;
import com.iwunu.nbiot.lock.config.AepMqConfig;

@Component
public class AepCommandClient {
	@Autowired
	private AepMqConfig aepMqConfig;
	/**
	 **发送指令
	 * @param param:deviceId设备ID && 指令
	 * @return
	 */
	public JSONObject  sendMessagePost(JSONObject param) {
		String result = null;
		AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(aepMqConfig.getAppKey()).appSecret(aepMqConfig.getAppSecret()).scheme(Scheme.HTTPS).build();
		CreateCommandRequest request = new CreateCommandRequest();
		request.setParamMasterKey(aepMqConfig.getMasterApikey());
        JSONObject obj =aepMqConfig.postParamJson();
        obj.put("deviceId", param.get("deviceId"));
        param.remove("deviceId");
        param.put("dataType", 2);
        obj.put("content", param);
        request.setBody(obj.toString().getBytes());
        try {
            CreateCommandResponse response = client.CreateCommand(request);
			 if (response.getStatusCode() != 200) {
	                return null;
	            }
			 result = new String(response.getBody());
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.shutdown();
		}
		return JSONObject.parseObject(result);
	}
	/**
	 **查询发送的指令
	 * @param param:deviceId &&  commandId
	 * @return
	 */
	public JSONObject  sendMessageGet(JSONObject param) {
		String result = null;
		AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(aepMqConfig.getAppKey()).appSecret(aepMqConfig.getAppSecret()).scheme(Scheme.HTTPS).build();
		QueryCommandRequest request = new QueryCommandRequest();
		request.setParamMasterKey(aepMqConfig.getMasterApikey());
		request.setParamDeviceId(param.get("deviceId"));
		request.setParamProductId(aepMqConfig.getProductId());
		request.setParamCommandId(param.get("commandId"));
		try {
			 QueryCommandResponse response =client.QueryCommand(request);
			 if (response.getStatusCode() != 200) {
	                return null;
	            }
			 result = new String(response.getBody());
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.shutdown();
		}
		return JSONObject.parseObject(result);
	}
	/**
	 **取消指令
	 * @param param
	 * @return
	 */
	public JSONObject  sendMessageCancel(JSONObject param) {
		String result = null;
		AepDeviceCommandClient client = AepDeviceCommandClient.newClient().appKey(aepMqConfig.getAppKey()).appSecret(aepMqConfig.getAppSecret()).scheme(Scheme.HTTPS).build();
		CancelCommandRequest request = new CancelCommandRequest();
		request.setParamMasterKey(aepMqConfig.getMasterApikey());
		JSONObject obj =aepMqConfig.getParamJson();
		obj.put("deviceId", param.get("deviceId"));
		obj.put("commandId",param.get("commandId"));
		request.setBody(obj.toString().getBytes());
		try {
			 CancelCommandResponse response =client.CancelCommand(request);
			 if (response.getStatusCode() != 200) {
	                return null;
	            }
			 result = new String(response.getBody());
			 
			 
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.shutdown();
		}
		return JSONObject.parseObject(result);
	}
}
