package com.youchain.Netty;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.exception.BadRequestException;
import com.youchain.exception.handler.ApiResult;
import io.swagger.annotations.Api;
import lombok.Data;
import net.wimpi.modbus.ModbusException;
import net.wimpi.modbus.ModbusIOException;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadMultipleRegistersRequest;
import net.wimpi.modbus.msg.ReadMultipleRegistersResponse;
import net.wimpi.modbus.msg.WriteMultipleRegistersRequest;
import net.wimpi.modbus.msg.WriteMultipleRegistersResponse;
import net.wimpi.modbus.net.TCPMasterConnection;
import net.wimpi.modbus.procimg.Register;
import net.wimpi.modbus.procimg.SimpleRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
// 自定义类来存储每个 IP 的配置信息和读取结果
@Data
class ModbusWrite {
    Long id;
    int modbusConfigId;
    int address;
    int values;
    public static List<ModbusWrite> convertToModbusWriteList(List<Map<String, Object>> queryResult) {
        List<ModbusWrite> modbusWriteList = new ArrayList<>();
        for (Map<String, Object> row : queryResult) {
            ModbusWrite modbusWrite = new ModbusWrite();
            // 从 Map 中获取 id 并转换为 Long 类型
            Object idObj = row.get("id");
            if (idObj != null) {
                modbusWrite.setId(Long.valueOf(idObj.toString()));
            }
            // 从 Map 中获取 modbusInfoId 并转换为 int 类型
            Object modbusConfigIdObj = row.get("modbusInfoId");
            if (modbusConfigIdObj != null) {
                modbusWrite.setModbusConfigId(Integer.parseInt(modbusConfigIdObj.toString()));
            }
            // 从 Map 中获取 address 并转换为 int 类型
            Object addressObj = row.get("address");
            if (addressObj != null) {
                modbusWrite.setAddress(Integer.parseInt(addressObj.toString()));
            }
            // 从 Map 中获取 value 并转换为 int 类型
            Object valuesObj = row.get("value");
            if (valuesObj != null) {
                modbusWrite.setValues(Integer.parseInt(valuesObj.toString()));
            }
            modbusWriteList.add(modbusWrite);
        }
        return modbusWriteList;
    }
}
@Data
class ModbusConfig {
    Long id;
    // 报错原因
    public String description;
    // 设备的 IP 地址
    String ip;
    // 设备的端口号
    int port;
    // 起始寄存器地址
    int startAddress;
    // 要读取的寄存器数量
    int numberOfRegisters;
    // 连接失败时的最大重试次数
    int maxRetries;
    // 存储读取到的寄存器值
    List<Integer> dataValues;
    // 数据的状态，normal 表示正常，abnormal 表示异常，warning 表示警告，maintenance 表示维护中
    String dataStatus="normal";
    // 用于存储要写入的值
    List<ModbusWrite> modbusWriteList;
    // 新增连接状态标记
    boolean isConnected = false;

    /**
     * 构造函数，用于初始化 Modbus 设备的配置信息。
     *
     * @param ip              设备的 IP 地址
     * @param port            设备的端口号
     * @param startAddress    起始寄存器地址
     * @param numberOfRegisters 要读取的寄存器数量
     * @param maxRetries      最大重试次数
     */
    public ModbusConfig(Long id,String ip, int port, int startAddress, int numberOfRegisters, int maxRetries, List<Integer> dataValues) {
        this.id = id;
        this.ip = ip;
        this.port = port;
        this.startAddress = startAddress;
        this.numberOfRegisters = numberOfRegisters;
        this.maxRetries = maxRetries;
        // 初始化存储寄存器值的列表
        this.dataValues =dataValues;
    }

    public ModbusConfig() {

    }

    /**
     * 清空当前存储的结果，以便存储新的读取数据。
     */
    public void clearData() {
        dataValues.clear();
    }
    /* 处理jdbc查询结果*/
    public static List<ModbusConfig> convertToModbusConfigList(List<Map<String, Object>> queryResult) {
        List<ModbusConfig> configList = new ArrayList<>();
        for (Map<String, Object> row : queryResult) {
            Long id = Long.valueOf(row.get("id")+"");
            String ip = (String) row.get("ip");
            Object portObj = row.get("port");
            int port = 0;
            if (portObj instanceof Number) {
                port = ((Number) portObj).intValue();
            }
            Object startAddressObj = row.get("startAddress");
            int startAddress = 0;
            if (startAddressObj instanceof Number) {
                startAddress = ((Number) startAddressObj).intValue();
            }
            Object numberOfRegistersObj = row.get("numberOfRegisters");
            int numberOfRegisters = 0;
            if (numberOfRegistersObj instanceof Number) {
                numberOfRegisters = ((Number) numberOfRegistersObj).intValue();
            }
            Object maxRetriesObj = row.get("maxRetries");
            int maxRetries = 0;
            if (maxRetriesObj instanceof Number) {
                maxRetries = ((Number) maxRetriesObj).intValue();
            }
            // 处理 dataValues 字段
            Object dataValuesObj = row.get("dataValues");
            List<Integer> dataValues = new ArrayList<>();
            if (dataValuesObj != null && dataValuesObj instanceof String) {
                String dataValuesStr = (String) dataValuesObj;
                if (!dataValuesStr.isEmpty()) {
                    try {
                        // 使用 fastjson 解析 JSON 数组
                        JSONArray jsonArray = JSON.parseArray(dataValuesStr);
                        for (int i = 0; i < jsonArray.size(); i++) {
                            dataValues.add(jsonArray.getInteger(i));
                        }
                    } catch (Exception e) {
                        System.err.println("无法解析 dataValues 中的值: " + dataValuesStr);
                    }
                }
            }
            ModbusConfig config = new ModbusConfig(id,ip, port, startAddress, numberOfRegisters, maxRetries,dataValues);
            configList.add(config);
        }
        return configList;
    }
}

/**
 * JamodReadModbusTCP 类用于通过 Modbus TCP 协议从多个设备读取寄存器数据。
 * 支持为每个设备配置不同的端口、起始地址、数据长度和重试次数，
 * 并将读取到的数据存储在对应的 ModbusConfig 对象中。
 */
@RestController
@RequestMapping("/api/jamod")
@Api(tags = "jamod测试")
public class JamodReadModbusTCP {
    // 存储每个 Modbus 设备的配置信息
    private static List<ModbusConfig> CONFIG_LIST = new ArrayList<>();
    @Autowired
    private JdbcQueryExample jdbcQueryExample;

    // Modbus 从站 ID
    private static final int SLAVE_ID = 1;

    // 存储所有 Modbus TCP 连接的线程安全列表
    private static CopyOnWriteArrayList<TCPMasterConnection> connections = new CopyOnWriteArrayList<>();
    // 用于定时执行读取任务的调度执行器
    private static ScheduledExecutorService executorService;
    // 用于同步连接操作的锁对象
    private final Object connectionLock = new Object();
    /**
     * 向 Modbus TCP 服务写入值
     * @param connection 要使用的 TCPMasterConnection 对象
     * @param config 设备的配置信息
     * @param slaveId Modbus 从站 ID
     */
    private void writeRegisters(TCPMasterConnection connection, ModbusConfig config, int slaveId) {
        synchronized (connectionLock) {
            if (!connection.isConnected()) {
                try {
                    connectWithRetries(connection, config);
                } catch (Exception e) {
                    System.err.println("重新连接到 " + config.ip + " 失败 - " + translateExceptionMessage(e.getMessage()));
                    return;
                }
            }
        }
        List<ModbusWrite> modbusWriteList = config.modbusWriteList;
        for (ModbusWrite modbusWrite : modbusWriteList){
            try {
                /* 写入值*/
                List<Integer> writeValues = Collections.singletonList(modbusWrite.getValues());
                /* 写入地址*/
                int writeAddress = modbusWrite.getAddress();
                if (writeValues == null || writeValues.isEmpty()) {
                    return;
                }

                SimpleRegister[] registers = new SimpleRegister[writeValues.size()];
                for (int i = 0; i < writeValues.size(); i++) {
                    registers[i] = new SimpleRegister(writeValues.get(i));
                }

                WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(writeAddress, registers);
                request.setUnitID(slaveId);

                ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);
                transaction.setRequest(request);
                transaction.execute();

                WriteMultipleRegistersResponse response = (WriteMultipleRegistersResponse) transaction.getResponse();
                if (response != null) {
                    String updateSql=" UPDATE modbus_write_log  "
                            +" SET  "
                            +"     `status` = 'success',failure_reason='', "
                            +"     execution_time = ROUND( "
                            +"         (UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(write_time)),  "
                            +"         3 "
                            +"     ) "
                            +" WHERE id = "+modbusWrite.id+";";
                    jdbcQueryExample.executeUpdate(updateSql);
                    System.out.println("成功向 " + config.ip + " 写入值");
                }
            } catch (ModbusIOException e) {
                synchronized (connectionLock) {
                    System.err.println("Modbus IO 异常，针对 " + config.ip + " - " + translateExceptionMessage(e.getMessage()));
                }
            } catch (ModbusException e) {
                String updateSql=" UPDATE modbus_write_log  "
                        +" SET  "
                        +"     `status` = 'retrying',retry_count = retry_count + 1 "
                        +" WHERE id ="+modbusWrite.id+";";
                jdbcQueryExample.executeUpdate(updateSql);
                System.err.println("Modbus 事务异常，针对 " + config.ip + " - " + translateExceptionMessage(e.getMessage()));
            }
        }
    }

    /**
     * 启动定时任务
     */
    @PostMapping("/start")
    @AnonymousAccess
    public void startTask() {
        // 初始化配置列表
        String sql = "SELECT id 'id',device_name, device_ip 'ip', device_port 'port', " +
                "start_address 'startAddress', data_length 'numberOfRegisters', " +
                "max_retries 'maxRetries', data_values 'dataValues' " +
                "FROM modbus_info WHERE enabled = ?";
        List<Map<String, Object>> queryResult = jdbcQueryExample.sqlQuery(sql, 1);
        CONFIG_LIST = ModbusConfig.convertToModbusConfigList(queryResult);
        // 初始化连接
        initializeConnections();

        // 注册关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));

        // 启动定时任务
        executorService = Executors.newScheduledThreadPool(1);
        executorService.scheduleAtFixedRate(() -> {
            System.out.println("开始读取数据...");
            for (ModbusConfig config : CONFIG_LIST) {
                TCPMasterConnection connection = getConnectionByIp(config.ip);
                if (connection != null) {
                    config.clearData();
                    ModbusConfig updatedConfig = readRegisters(connection, config, SLAVE_ID);
                    if (updatedConfig != null) {
                        // 处理数据更新逻辑（原有读取后的处理逻辑）
                        if ("warning".equals(updatedConfig.dataStatus)) {
                            String updateSql = "update modbus_info set data_status='warning',description='"
                                    + updatedConfig.description + "',last_read_time=NOW() where id="
                                    + updatedConfig.id + ";";
                            jdbcQueryExample.executeUpdate(updateSql);
                        } else if ("normal".equals(updatedConfig.dataStatus)) {
                            String updateSql = "update modbus_info set data_status='normal',data_values='"
                                    + updatedConfig.dataValues + "',description='',last_read_time=NOW() where id="
                                    + updatedConfig.id + ";";
                            jdbcQueryExample.executeUpdate(updateSql);

                            // 读取成功后处理写入逻辑
                            String sqlWrite = "select id,modbus_info_id 'modbusInfoId',register_address 'address',data_values 'value' " +
                                    "from modbus_write_log where `status` in ('unprocessed','retrying') and modbus_info_id=?";
                            List<Map<String, Object>> queryWrite = jdbcQueryExample.sqlQuery(sqlWrite, updatedConfig.id);
                            List<ModbusWrite> modbusWriteList = ModbusWrite.convertToModbusWriteList(queryWrite);
                            config.modbusWriteList = modbusWriteList;
                            writeRegisters(connection, config, SLAVE_ID);
                        }
                    }
                }
            }
        }, 0, 1, TimeUnit.SECONDS);
    }

    /**
     * 关闭定时任务
     */
    @PostMapping("/stop")
    @AnonymousAccess
    public void stopTask() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
            try {
                if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("等待定时任务执行器终止时被中断。");
            }
        }

        // 关闭所有连接
        for (TCPMasterConnection connection : connections) {
            if (connection.isConnected()) {
                try {
                    connection.close();
                    System.out.println("已关闭 " + connection.getAddress() + " 的连接。");
                } catch (Exception e) {
                    System.err.println("关闭 " + connection.getAddress() + " 的连接时出错: " + translateExceptionMessage(e.getMessage()));
                }
            }
        }
        // 清空配置信息
        CONFIG_LIST.clear();
    }

    /**
     * 根据 IP 地址从连接列表中查找对应的 TCPMasterConnection 对象。
     *
     * @param ipAddress 要查找的 IP 地址
     * @return 对应的 TCPMasterConnection 对象，如果未找到则返回 null
     */
    private static TCPMasterConnection getConnectionByIp(String ipAddress) {
        for (TCPMasterConnection connection : connections) {
            if (connection.getAddress().getHostAddress().equals(ipAddress)) {
                return connection;
            }
        }
        return null;
    }

    /**
     * 初始化与所有配置的 Modbus 设备的连接。
     * 遍历 CONFIG_LIST 中的每个设备配置信息，创建连接并尝试建立连接。
     */
    private void initializeConnections() {
        for (ModbusConfig config : CONFIG_LIST) {
            try {
                InetAddress inetAddress = InetAddress.getByName(config.ip);
                TCPMasterConnection connection = new TCPMasterConnection(inetAddress);
                connection.setPort(config.port);

                // 尝试连接，成功则标记状态并添加连接
                connectWithRetries(connection, config);

                config.isConnected = true;
                connections.add(connection);
            } catch (Exception e) {
                // 初始化连接失败，标记状态为未连接
                config.isConnected = false;

                String des="初始化连接失败，后续将重试 - IP: " + config.ip + "，错误: " + translateExceptionMessage(e.getMessage());
                System.err.println(des);
            }
        }
    }

    /**
     * 尝试连接到 Modbus 设备，若连接失败则进行重试，最多重试 maxRetries 次。
     *
     * @param connection 要连接的 TCPMasterConnection 对象
     * @param config     设备的配置信息
     * @throws Exception 如果尝试 maxRetries 次后仍无法连接
     */
    private void connectWithRetries(TCPMasterConnection connection, ModbusConfig config) throws Exception {
        int retries = 0;
        while (retries < config.maxRetries) {
            try {
                // 尝试建立连接
                connection.connect();
                System.out.println("已连接到 " + config.ip);
                /*连接成功修改状态*/
                String updateSql = "update modbus_info set is_connected=0 where id="
                        + config.id + ";";
                jdbcQueryExample.executeUpdate(updateSql);
                return;
            } catch (Exception e) {
                retries++;
                System.err.println("第 " + retries + " 次尝试连接到 " + config.ip + " 失败: " + translateExceptionMessage(e.getMessage()));
                if (retries == config.maxRetries) {
                    // 达到最大重试次数，抛出异常
                    String updateSql = "update modbus_info set is_connected=1,data_values='"
                            + "',description='"+"尝试 " + config.maxRetries + " 次后仍无法连接到 " + config.ip + "。"+e.getMessage()+"' where id="
                            + config.id + ";";
                    jdbcQueryExample.executeUpdate(updateSql);
                    throw new Exception("尝试 " + config.maxRetries + " 次后仍无法连接到 " + config.ip + "。");
                }
                try {
                    // 重试前暂停 1 秒
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    System.err.println("连接重试期间睡眠被中断。");
                    throw new Exception("连接重试被中断。");
                }
            }
        }
    }

    /**
     * 从指定的 Modbus 设备读取寄存器数据。
     * 如果连接断开，会尝试重新连接。
     * 读取到的数据会存储在对应的 ModbusConfig 对象中。
     *
     * @param connection 要使用的 TCPMasterConnection 对象
     * @param config     设备的配置信息
     * @param slaveId    Modbus 从站 ID
     * @return 读取到的寄存器数据列表，如果读取失败则返回 null
     */
    private ModbusConfig readRegisters(TCPMasterConnection connection, ModbusConfig config, int slaveId) {
        // 同步操作，确保同一时间只有一个线程可以检查和修改连接状态
        synchronized (connectionLock) {
            if (!connection.isConnected()) {
                try {
                    // 尝试重新连接到设备
                    connectWithRetries(connection, config);
                } catch (Exception e) {
                    System.err.println("重新连接到 " + config.ip + " 失败 - " + translateExceptionMessage(e.getMessage()));
                    return null;
                }
            }
        }

        try {
            // 创建一个读取多个寄存器的请求
            ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(config.startAddress, config.numberOfRegisters);
            // 设置从站 ID
            request.setUnitID(slaveId);

            // 创建一个 Modbus TCP 事务
            ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection);
            // 设置请求到事务中
            transaction.setRequest(request);
            // 执行事务
            transaction.execute();

            // 获取事务的响应
            ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) transaction.getResponse();
            if (response != null) {
                // 获取响应中的寄存器数组
                Register[] registers = response.getRegisters();
                // 用于临时存储读取到的寄存器值
                List<Integer> values = new ArrayList<>();
                for (Register register : registers) {
                    // 将每个寄存器的值添加到临时列表中
                    values.add(register.getValue());
                }
                // 更新配置中的数据
                config.dataValues = values;
                config.dataStatus="normal";// 数据正常
            }
        } catch (ModbusIOException e) {
            // 同步操作，确保同一时间只有一个线程可以处理连接异常
            synchronized (connectionLock) {
                System.err.println("Modbus IO 异常，针对 " + config.ip + " - " + translateExceptionMessage(e.getMessage()));
                try {
                    // 关闭当前连接
                    connection.close();
                    // 尝试重新连接到设备
                    connectWithRetries(connection, config);
                } catch (Exception re) {
                    System.err.println("重新连接到 " + config.ip + " 失败 - " + translateExceptionMessage(re.getMessage()));
                }
            }
        } catch (ModbusException e) {
            config.dataStatus="warning";// 数据报错
            config.description="Modbus 事务异常，针对 " + config.ip + " - " + translateExceptionMessage(e.getMessage());
//            System.err.println("Modbus 事务异常，针对 " + config.ip + " - " + translateExceptionMessage(e.getMessage()));
        }
        /* 修改表值*/

        return config;
    }
    /**
     * 程序关闭时的清理操作。
     * 关闭定时任务执行器和所有 Modbus TCP 连接。
     */
    private void shutdown() {
        if (executorService != null && !executorService.isShutdown()) {
            // 发起关闭定时任务执行器的请求
            executorService.shutdown();
            try {
                // 等待执行器在 5 秒内正常关闭
                if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
                    // 若 5 秒内未关闭，则强制关闭
                    executorService.shutdownNow();
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("等待定时任务执行器终止时被中断。");
            }
        }

        // 关闭所有连接
        for (TCPMasterConnection connection : connections) {
            if (connection.isConnected()) {
                try {
                    // 关闭连接
                    connection.close();
                    System.out.println("已关闭 " + connection.getAddress() + " 的连接。");
                } catch (Exception e) {
                    System.err.println("关闭 " + connection.getAddress() + " 的连接时出错: " + translateExceptionMessage(e.getMessage()));
                }
            }
        }
    }

    /**
     * 将异常信息翻译成中文，目前只是简单返回原信息。
     * 可根据实际情况添加更多翻译规则。
     *
     * @param message 原始异常信息
     * @return 翻译后的异常信息
     */
    private String translateExceptionMessage(String message) {
        return message;
    }

    /**
     * 获取指定 IP 地址对应的设备的最新读取数据。
     *
     * @param ipAddress 要获取数据的设备的 IP 地址
     * @return 存储最新读取数据的列表，如果 IP 地址对应的配置不存在则返回 null
     */
    public List<Integer> getLatestData(String ipAddress) {
        // 遍历配置列表，查找对应 IP 的配置信息
        for (ModbusConfig config : CONFIG_LIST) {
            if (config.ip.equals(ipAddress)) {
                // 返回配置中存储的数据的副本
                return new ArrayList<>(config.dataValues);
            }
        }
        return null;
    }
    // 添加新配置的方法
    @PostMapping("/addConFig")
    @AnonymousAccess
    public synchronized ResponseEntity<Object> addModbusConfig(@RequestBody Long id)  {
        String sql = "SELECT id 'id',device_name, device_ip 'ip', device_port 'port', " +
                "start_address 'startAddress', data_length 'numberOfRegisters', " +
                "max_retries 'maxRetries', data_values 'dataValues' " +
                "FROM modbus_info WHERE id=?";
        List<Map<String, Object>> queryResult = jdbcQueryExample.sqlQuery(sql, id);
        if (queryResult.size()>0){
            ModbusConfig newConfig = ModbusConfig.convertToModbusConfigList(queryResult).get(0);
            // 检查是否已存在相同配置（示例：根据IP和端口判断）
            if (CONFIG_LIST.stream().anyMatch(c ->
                    c.getIp().equals(newConfig.getIp()) &&
                            c.getPort() == newConfig.getPort())) {
                System.err.println("配置已存在，跳过添加: " + newConfig.getIp() + ":" + newConfig.getPort());
//                return;
            }else {
                // 添加新配置
                CONFIG_LIST.add(newConfig);
            }
            if (getConnectionByIp(newConfig.ip)==null){
                // 初始化连接
                initializeConnection(newConfig);
            }
        }else {throw new BadRequestException("未查询到配置信息");}
        ApiResult apiResult=new ApiResult();
        return new ResponseEntity<>(apiResult.success("添加成功"), HttpStatus.OK);
    }
    // 初始化单个配置的连接
    private void initializeConnection(ModbusConfig config) {
        try {
            // 创建连接对象
            TCPMasterConnection connection = new TCPMasterConnection(
                    InetAddress.getByName(config.getIp())
            );
            connection.setPort(config.getPort());

            // 尝试连接
            connectWithRetries(connection, config);

            // 保存连接
            connections.add(connection);
            config.setConnected(true);
            System.out.println("成功连接新设备: " + config.getIp() + ":" + config.getPort());
        } catch (Exception e) {
            config.setConnected(false);
            throw new RuntimeException("异常: " + config.getIp() + ":" + config.getPort(), e);
        }
    }

    /**
     * 添加写入操作
     * @param ip IP
     * @param address 地址
     * @param values 数据
     */
    public synchronized void addWrite(String ip, int address, int values) {
        String sql = "SELECT id " +
                "FROM modbus_info WHERE device_ip= '"+ip+"'";
        List<Map<String, Object>> modbusInfos = jdbcQueryExample.sqlQuery(sql);
        if (modbusInfos.size()>0){
            Long modbus_info_id = Long.valueOf(modbusInfos.get(0).get("id").toString());
            // 如果已有任务，直接跳过
            String sqlWrite = "select id,modbus_info_id 'modbusInfoId',register_address 'address',data_values 'value' " +
                    "from modbus_write_log where `status` in ('unprocessed','retrying') " +
                    "and modbus_info_id=? " +
                    "and register_address=? " +
                    "and data_values=?";
            List<Map<String, Object>> queryWrite = jdbcQueryExample.sqlQuery(sqlWrite, modbus_info_id,address,values);
            if (queryWrite.size()<1) {
                String insertSql = "INSERT INTO `wms`.`modbus_write_log` ( `modbus_info_id`, `register_address`, `data_values`, `write_time`, `status`,`failure_reason`) " +
                        "VALUES ( ?,? , ?, NOW(), 'unprocessed', '');";
                // 添加写入操作
                jdbcQueryExample.executeUpdate(insertSql, modbus_info_id, address, values);
            }
        }
    }
    public String getRead(String ip,int address) {
        for (ModbusConfig config : CONFIG_LIST) {
            if (config.getIp().equals(ip)) {
                if (config.getDataValues() != null && config.getDataValues().size() >= (address-1)) {
                    return config.getDataValues().get(address)+""; // 索引从0开始
                } else {
                    System.err.println("dataValues 集合中没有足够的数据。");
                    return "dataValues 集合中没有足够的数据。";
                }
            }
        }
        System.err.println("未找到 IP 地址为 " + ip + " 的 ModbusConfig 配置。");
        return "未找到 IP 地址为 " + ip + " 的 ModbusConfig 配置。";
    }
}