package com.dcs.equipment.controller;

import com.dcs.common.core.domain.AjaxResult;
import com.dcs.equipment.domain.Condition;
import com.dcs.equipment.domain.DeviceRequest;
import com.dcs.equipment.service.ModbusTcpService;
import com.dcs.hnyz.domain.EquipmentParam;
import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.dcs.common.core.domain.AjaxResult.success;

/**
 * @author: wangpx
 * @date: 2025-03-07 16:54
 */
@Api(tags = "ModbusTCP模块")
@RestController
@RequestMapping("/modbus")
public class ModbusTcpController {
    @Autowired
    private ModbusTcpService service;

    private static List<EquipmentParamFormVO> VirtualData = new ArrayList<EquipmentParamFormVO>() {{
        EquipmentParamFormVO equipmentFormVo1 = new EquipmentParamFormVO();
        add(equipmentFormVo1);
    }};

    @GetMapping("/getValues")
    @ApiOperation(value = "modbus tcp 获取数据")
    public AjaxResult getValues(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        List<Object> values = service.getValues(request);
        return success(values);
    }

    @PostMapping("/setCoils")
    @ApiOperation(value = "modbus tcp 设置多个线圈数据")
    public void setCoils(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setCoils(request);
    }

    @PostMapping("/setCoil")
    @ApiOperation(value = "modbus tcp 设置单个线圈数据")
    public void setCoil(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setCoil(request);
    }

    @PostMapping("/setIntRegisters")
    @ApiOperation(value = "modbus tcp 设置多个整型数据")
    public void setInts(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setIntRegisters(request);
    }

    @PostMapping("/setIntRegister")
    @ApiOperation(value = "modbus tcp 设置单个整型数据")
    public void setInt(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setIntRegister(request);
    }

    @PostMapping("/setFloatRegisters")
    @ApiOperation(value = "modbus tcp 设置多个浮点数据")
    public void setFloats(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setFloatRegisters(request);
    }

    @PostMapping("/setFloatRegister")
    @ApiOperation(value = "modbus tcp 设置单个浮点数据")
    public void setFloat(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) DeviceRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setFloatRegister(request);
    }

    @PostMapping("/and")
    @ApiOperation(value = "执行与操作")
    public AjaxResult processIfAllConditionsMet(@ApiParam(name = "目标设备列表", value = "目标设备列表", required = true) List<EquipmentParam> targets, @ApiParam(name = "条件设备列表", value = "条件设备列表", required = false) List<Condition> conditions) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        return success(service.processIfAllConditionsMet(targets, conditions));
    }

    @PostMapping("/or")
    @ApiOperation(value = "执行或操作")
    public AjaxResult processIfAnyConditionMet(@ApiParam(name = "目标设备列表", value = "目标设备列表", required = true) List<EquipmentParam> targets, @ApiParam(name = "条件设备列表", value = "条件设备列表", required = false) List<Condition> conditions) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        return success(service.processIfAnyConditionMet(targets, conditions));
    }

}
