/**
 * 核心服务接口sdk（golang版）
 *
 * @desc 啪啪运动 核心服务接口sdk
 * ---------------------------------------------------------------------
 * @author		unphp <unphp@qq.com>
 * @date		2018-12-18
 * @copyright	core-utils
 * ---------------------------------------------------------------------
 */

package cooper

import (
	"net/url"
)

//NewGateAp ...
func (that *CooperClient) NewGateApOpen() *GateApOpenRequest {
	return &GateApOpenRequest{
		Client:     that,
		apiAddress: "/gateAp/open",
	}
}

//GateApRequest 唯一id生成器
type GateApOpenRequest struct {
	Client     *CooperClient
	DeviceId   string `json:"device_id"` //设备ID
	AcsRes     string `json:"acs_res"`   //继电器 0:进门，1：出门，2：警报（火警）
	AcsIndex   string `json:"act_index"` //读头的位置(非必填)
	Time       string `json:"time"`      //开门保持时间(非必填，默认5秒)
	apiAddress string
}

func (that *GateApOpenRequest) SetApiAddress(apiAddress string) *GateApOpenRequest {
	that.apiAddress = apiAddress
	return that
}

func (that *GateApOpenRequest) SetDeviceId(deviceId string) *GateApOpenRequest {
	that.DeviceId = deviceId
	return that
}

func (that *GateApOpenRequest) SetAcsRes(acsRes string) *GateApOpenRequest {
	that.AcsRes = acsRes
	return that
}

func (that *GateApOpenRequest) SetTime(time string) *GateApOpenRequest {
	that.Time = time
	return that
}

//Params ...
func (that *GateApOpenRequest) Params() (url.Values, error) {
	var m = make(url.Values)
	m.Set("device_id", ToString(that.DeviceId))
	m.Set("acs_res", ToString(that.AcsRes))
	m.Set("time", ToString(that.Time))
	return m, nil
}

//Do ...
func (that *GateApOpenRequest) Do() (bool, error) {
	_, _err := that.Client.Post(that.apiAddress, that)
	if _err != nil {
		return false, _err
	}
	return true, nil
}
