package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.opc;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesEquipVariableRwExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.mes.model.MesEquipVariableRwModel;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "设备数据变量读写API")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/opc")
@Slf4j
public class MesEquipVariableRwExtController {

    @Autowired
    private IMesEquipVariableRwExtService equipVariableRwExtService;

    @PostMapping("/write-variable")
    @ApiOperation(value = "写值")
    public ResultBean writeVariable(MesEquipVariableRwModel mesOpcLinkModel) {
        try {
            return ResultBean.success("操作成功").setResultObject(equipVariableRwExtService.writeVariable(mesOpcLinkModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/read-variable")
    @ApiOperation(value = "读值")
    public ResultBean readVariable(MesEquipVariableRwModel mesOpcLinkModel) {
        try {
            return ResultBean.success("读取成功").setResultObject(equipVariableRwExtService.readVariable(mesOpcLinkModel));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
