package com.zd.laboratory.service.impl;


import com.zd.common.core.web.domain.ResultData;
import com.zd.laboratory.domain.LabHardware;
import com.zd.laboratory.service.ControlService;
import com.zd.laboratory.service.ILabControlService;
import com.zd.laboratory.socket.command.Symbol;
import com.zd.system.api.laboratory.domain.Control;
import com.zd.system.api.laboratory.domain.FunctionStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 继电器控制
 * @Author: zhoupan
 * @Date: 2021/10/18/18:36
 * @Description:
 */
@Service
public class RelayControlService implements ControlService {

    @Autowired
    ILabControlService labControlService;

    @Override
    public ResultData execution(LabHardware labHardware, FunctionStatus functionStatus) {

        String s = com.zd.laboratory.socket.command.Control.sendCommand(Symbol.order.control, labHardware.getRelayCode(), functionStatus.getCmd(), labHardware.getBit());
        boolean equals = "下发成功".equals(s);
        if(equals)
        {
            //添加记录
            labControlService.autoCreateLabControlAndSave(labHardware,Symbol.order.control,Symbol.getByType(functionStatus.getCmd()));
        }
        return ResultData.result(equals,()->s);
    }

    @Override
    public Control.CommTypeEnums comTypeEnums() {
        return Control.CommTypeEnums.Relay;
    }
}
