package com.unis.detectionnum.server.service.impl;

import java.util.Date;
import java.util.List;

import com.unis.detectionnum.server.entity.DeviceInfoDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.unis.detectionnum.common.vo.DeviceCameraParamVO;
import com.unis.detectionnum.common.vo.DeviceInfoQueryVO;
import com.unis.detectionnum.common.vo.DeviceInfoVO;
import com.unis.detectionnum.common.vo.DeviceParamVO;
import com.unis.detectionnum.common.vo.DeviceProbeParamVO;
import com.unis.detectionnum.server.domain.DeviceInfoDomain;
import com.unis.detectionnum.server.domain.DeviceParamDomain;
import com.unis.detectionnum.server.service.DeviceParamService;
import com.unis.detectionnum.server.utils.Modbus4jUtils;
import com.unis.detectionnum.server.utils.Register;

/**
 * @Package: com.unis.detectionnum.server.service.impl
 * @Author: LiuFeng
 * @Description:设备参数service实现类
 * @Date: Created in 2019/8/15
 */
@Service("deviceParamService")
public class DeviceParamServiceImpl implements DeviceParamService{

	@Autowired
	private DeviceParamDomain  paramDomain;
	
	@Autowired
	private DeviceInfoDomain infoDomain;
	
	@Override
	public int readDeviceParam(DeviceInfoQueryVO deviceInfoQueryVO) throws ModbusTransportException, ErrorResponseException, ModbusInitException {
		
		String slaveIp = Register.SLAVE_IP;
		Integer slavePort = Register.SLAVE_PORT;
		Integer slaveId = Register.SLAVE_ID;
		DeviceInfoVO  infoVO = infoDomain.getDeviceInfoByQuery(deviceInfoQueryVO);
		if(infoVO != null) {
			slaveIp = infoVO.getSbIpdz();
			slavePort = Integer.parseInt(infoVO.getSbDkh());
		}
		
		Number fcLengthX = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.FC_LENGTH_X, DataType.TWO_BYTE_INT_SIGNED);
		Number fcWidthY = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.FC_WIDTH_Y, DataType.TWO_BYTE_INT_SIGNED);
		Number grainDensity = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.GRAIN_DENSITY, DataType.TWO_BYTE_INT_SIGNED);
		Number probeX = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.PROBE_X, DataType.TWO_BYTE_INT_SIGNED);
		Number probeY = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.PROBE_Y, DataType.TWO_BYTE_INT_SIGNED);
		Number probeZ = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.PROBE_Z, DataType.TWO_BYTE_INT_SIGNED);
		Number probeVersion = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.PROBE_VERSION, DataType.TWO_BYTE_INT_SIGNED);
		Number motorX = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.MOTOR_X, DataType.TWO_BYTE_INT_SIGNED);
		Number motorY = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.MOTOR_Y, DataType.TWO_BYTE_INT_SIGNED);
		Number volumeCompensation = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.VOLUME_COMPENSATION, DataType.TWO_BYTE_INT_SIGNED);
		Number areaCompensation = Modbus4jUtils.readHoldingRegister(slaveIp, slavePort, slaveId, Register.AREA_COMPENSATION, DataType.TWO_BYTE_INT_SIGNED);

		DeviceParamVO paramVO = new DeviceParamVO();
		paramVO.setFcLengthX(fcLengthX.intValue());
		paramVO.setFcWidthY(fcWidthY.intValue());
		paramVO.setGrainDensity(grainDensity.intValue());
		paramVO.setProbeX(probeX.intValue());
		paramVO.setProbeY(probeY.intValue());
		paramVO.setProbeZ(probeZ.intValue());
		paramVO.setProbeVersion(probeVersion.intValue()+"");
		paramVO.setMotorX(motorX.intValue());
		paramVO.setMotorY(motorY.intValue());
		paramVO.setVolumeCompensation(volumeCompensation.intValue());
		paramVO.setAreaCompensation(areaCompensation.intValue());
		paramVO.setCreateDate(new Date());
		paramVO.setSbCode(infoVO.getSbCode());
		return paramDomain.saveDeviceParam(paramVO);
	}

	@Override
	public void writeDeviceParam(DeviceParamVO deviceParamVO, DeviceInfoQueryVO deviceInfoQueryVO) throws ModbusTransportException, ErrorResponseException, ModbusInitException {
		String ip = Register.SLAVE_IP;
		Integer port = Register.SLAVE_PORT;
		Integer slaveId = Register.SLAVE_ID;
		DeviceInfoVO  infoVO = infoDomain.getDeviceInfoByQuery(deviceInfoQueryVO);
		if(infoVO != null) {
			ip = infoVO.getSbIpdz();
			port = Integer.parseInt(infoVO.getSbDkh());
		}

		if(StringUtils.isNoneBlank(infoVO.getSbCode()) ) {
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.FC_LENGTH_X, deviceParamVO.getFcLengthX());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.FC_WIDTH_Y, deviceParamVO.getFcWidthY());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.GRAIN_DENSITY, deviceParamVO.getGrainDensity());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.PROBE_X, deviceParamVO.getProbeX());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.PROBE_Y, deviceParamVO.getProbeY());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.PROBE_Z, deviceParamVO.getProbeZ());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.VOLUME_COMPENSATION, deviceParamVO.getVolumeCompensation());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.AREA_COMPENSATION, deviceParamVO.getAreaCompensation());
			Modbus4jUtils.writeRegister(ip, port, slaveId, Register.PROBE_PARAM_SAVE, 1);
		}

	}

	/**
	 * <p>Title: saveProbeParam</p> 
	 * <p>Description: </p> 
	 * @param deviceProbeVO
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#saveProbeParam(com.unis.detectionnum.common.vo.DeviceProbeParamVO) 
	 */
	@Override
	public int saveProbeParam(DeviceProbeParamVO deviceProbeVO) {
		
		return paramDomain.saveProbeParam(deviceProbeVO);
	}

	/**
	 * <p>Title: updateProbeParam</p> 
	 * <p>Description: </p> 
	 * @param deviceProbeVO
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#updateProbeParam(com.unis.detectionnum.common.vo.DeviceProbeParamVO) 
	 */
	@Override
	public int updateProbeParam(DeviceProbeParamVO deviceProbeVO) {
		
		return paramDomain.updateProbeParam(deviceProbeVO);
	}

	/**
	 * <p>Title: getDevicePageList</p> 
	 * <p>Description: </p> 
	 * @param deviceInfoQueryVO
	 * @param pageNum
	 * @param pageSize
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#getDevicePageList(com.unis.detectionnum.common.vo.DeviceInfoQueryVO, java.lang.Integer, java.lang.Integer) 
	 */
	@Override
	public PageInfo<DeviceInfoVO> getDevicePageList(DeviceInfoQueryVO deviceInfoQueryVO, Integer pageNum,
			Integer pageSize) {
		
		return paramDomain.getDevicePageList(deviceInfoQueryVO,pageNum,pageSize);
	}

	/**
	 * <p>Title: getDeviceProbeParam</p> 
	 * <p>Description: </p> 
	 * @param sbCode
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#getDeviceProbeParam(java.lang.String) 
	 */
	@Override
	public DeviceProbeParamVO getDeviceProbeParam(String sbCode) {
		
		return paramDomain.getDeviceProbeParam(sbCode);
	}

	/**
	 * <p>Title: deleteDeviceParam</p> 
	 * <p>Description: </p> 
	 * @param sbCode
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#deleteDeviceParam(java.lang.String) 
	 */
	@Override
	public int deleteProbeParam(String sbCode) {
		
		return paramDomain.deleteProbeParam(sbCode);
	}

	/**
	 * <p>Title: saveCameraParam</p> 
	 * <p>Description: </p> 
	 * @param deviceCameraVO
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#saveCameraParam(com.unis.detectionnum.common.vo.DeviceCameraParamVO) 
	 */
	@Override
	public int saveCameraParam(DeviceCameraParamVO deviceCameraVO) {
		
		return paramDomain.saveCameraParam(deviceCameraVO);
	}

	/**
	 * <p>Title: updateCameraParam</p> 
	 * <p>Description: </p> 
	 * @param deviceCameraVO
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#updateCameraParam(com.unis.detectionnum.common.vo.DeviceCameraParamVO) 
	 */
	@Override
	public int updateCameraParam(DeviceCameraParamVO deviceCameraVO) {
		// TODO Auto-generated method stub
		return paramDomain.updateCameraParam(deviceCameraVO);
	}

	/**
	 * <p>Title: getDeviceCameraParam</p> 
	 * <p>Description: </p> 
	 * @param sbCode
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#getDeviceCameraParam(java.lang.String) 
	 */
	@Override
	public DeviceCameraParamVO getDeviceCameraParam(String sbCode) {
		
		return paramDomain.getDeviceCameraParam(sbCode);
	}

	/**
	 * <p>Title: deleteCameraParam</p> 
	 * <p>Description: </p> 
	 * @param sbCode
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#deleteCameraParam(java.lang.String) 
	 */
	@Override
	public int deleteCameraParam(String sbCode) {
		// TODO Auto-generated method stub
		return paramDomain.deleteCameraParam(sbCode);
	}

	/**
	 * <p>Title: getDeviceNo</p> 
	 * <p>Description: </p> 
	 * @param cfbh
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#getDeviceNo(java.lang.String) 
	 */
	@Override
	public String getDeviceNo(String cfbh) {
		
		return paramDomain.getDeviceNo(cfbh);
	}

	/**
	 * <p>Title: getDeviceProbe</p> 
	 * <p>Description: </p> 
	 * @param infoQueryVO
	 * @return 
	 * @see com.unis.detectionnum.server.service.DeviceParamService#getDeviceProbe(com.unis.detectionnum.common.vo.DeviceInfoQueryVO) 
	 */
	@Override
	public DeviceInfoVO getDeviceProbe(DeviceInfoQueryVO infoQueryVO) {
		
		return infoDomain.getDeviceInfoByQuery(infoQueryVO);
	}

	@Override
	public List<DeviceInfoDO> selectListAll() {
		return infoDomain.selectListAll();
	}

}
