package com.dcs.equipment.exception.device;

import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.UnknownHostException;

/**
 * 设备异常转换工具类
 * @author: wangpx
 * @date: 2025-01-02 10:00
 */
public class DeviceExceptionConverter {
    private static final Logger logger = LoggerFactory.getLogger(DeviceExceptionConverter.class);

    /**
     * 转换连接相关异常
     */
    public static DeviceConnectionException convertConnectionException(Exception e, String deviceAddress, Integer devicePort) {
        String message;
        if (e instanceof UnknownHostException) {
            message = String.format("设备地址解析失败: %s:%d - 请检查主机地址是否正确", deviceAddress, devicePort);
        } else if (e instanceof ModbusIOException) {
            message = String.format("设备网络连接失败: %s:%d - 请检查网络连接和设备状态", deviceAddress, devicePort);
        } else {
            message = String.format("设备连接异常: %s:%d - %s", deviceAddress, devicePort, e.getMessage());
        }
        
        logger.error(message, e);
        DeviceConnectionException exception = new DeviceConnectionException(message, deviceAddress, devicePort);
        exception.initCause(e); // 保存原始异常信息
        return exception;
    }

    /**
     * 转换协议相关异常
     */
    public static DeviceProtocolException convertProtocolException(Exception e, String functionCode, Integer slaveId, Integer offset, Integer quantity) {
        String message;
        if (e instanceof ModbusProtocolException) {
            message = String.format("Modbus协议异常 - 功能码:%s, 从机地址:%d, 偏移量:%d, 数量:%d - %s", 
                functionCode, slaveId, offset, quantity, e.getMessage());
        } else if (e instanceof ModbusNumberException) {
            message = String.format("Modbus参数异常 - 功能码:%s, 从机地址:%d, 偏移量:%d, 数量:%d - %s", 
                functionCode, slaveId, offset, quantity, e.getMessage());
        } else {
            message = String.format("设备协议异常 - 功能码:%s, 从机地址:%d, 偏移量:%d, 数量:%d - %s", 
                functionCode, slaveId, offset, quantity, e.getMessage());
        }
        
        logger.error(message, e);
        return new DeviceProtocolException(message, functionCode, slaveId, offset, quantity);
    }

    /**
     * 转换参数相关异常
     */
    public static DeviceParameterException convertParameterException(String parameterName, Object parameterValue, String expectedRange) {
        String message = String.format("参数验证失败 - 参数名:%s, 参数值:%s, 期望范围:%s", 
            parameterName, parameterValue, expectedRange);
        
        logger.error(message);
        return new DeviceParameterException(message, parameterName, parameterValue, expectedRange);
    }

    /**
     * 转换超时相关异常
     */
    public static DeviceTimeoutException convertTimeoutException(String deviceAddress, Integer devicePort, Long timeoutDuration, String operationType) {
        String message = String.format("设备操作超时 - 地址:%s:%d, 超时时间:%dms, 操作类型:%s", 
            deviceAddress, devicePort, timeoutDuration, operationType);
        
        logger.error(message);
        return new DeviceTimeoutException(message, deviceAddress, devicePort, timeoutDuration, operationType);
    }

    /**
     * 转换通用设备异常
     */
    public static DeviceException convertDeviceException(Exception e) {
        String message = String.format("设备操作异常: %s", e.getMessage());
        
        logger.error(message, e);
        return new DeviceException(message);
    }

    /**
     * 转换IO异常
     */
    public static DeviceIOException convertIOException(Exception e) {
        String message = String.format("设备IO异常: %s", e.getMessage());
        
        logger.error(message, e);
        return new DeviceIOException(message);
    }
} 