/*
 * 文 件 名:  ButtonSwitchController
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/4/15
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.ruoyi.ems.controller;

import com.huashe.common.exception.BusinessException;
import com.ruoyi.ems.handle.BaseDevHandler;
import com.ruoyi.ems.model.AbilityPayload;
import com.ruoyi.ems.model.CallResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 按钮开关接口
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/4/15]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@RestController
@CrossOrigin(allowedHeaders = "*", allowCredentials = "false")
@RequestMapping("/button-switch")
@Api(value = "ButtonSwitchController", description = "按钮开关控制接口")
public class ButtonSwitchController {
    /**
     * 日志
     */
    private static final Logger log = LoggerFactory.getLogger(CircuitBreakerController.class);

    @Qualifier("keka86BsHandler")
    @Resource
    private BaseDevHandler keka86BsHandler;

    /**
     * KEKA 86型开关能力调用
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/keka/86ButtonSwitchCall", method = RequestMethod.POST)
    @ApiOperation(value = "/keka/86ButtonSwitchCall", notes = "KEKA 86型按钮开关能力调用")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> keka86ButtonSwitchCall(@RequestBody AbilityPayload abilityPayload) {
        CallResponse<Void> res = null;

        try {
            res = keka86BsHandler.call(abilityPayload);
        }
        catch (BusinessException e) {
            log.warn(e.getMessage());
            res = new CallResponse<>(501, e.getMessage());
        }
        catch (Exception e) {
            log.error("keka86ButtonSwitchCall fail!", e);
            res = new CallResponse<>(501, "内部错误:" + e.getMessage());
        }

        return res;
    }
}