package com.dcs.equipment.controller;

import com.dcs.common.core.domain.AjaxResult;
import com.dcs.dcs.domain.vo.DcsEquipmentFormVo;
import com.dcs.equipment.domain.Condition;
import com.dcs.equipment.domain.ModbusRequest;
import com.dcs.equipment.domain.vo.DataTwinEquipmentVo;
import com.dcs.equipment.service.ModbusTcpService;
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.BeanUtils;
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
@RestController
@RequestMapping("/modbus")
public class ModbusTcpController {
    @Autowired
    private ModbusTcpService service;

    @GetMapping("/updateEquipmentCodes")
    @ApiOperation(value = "更新设备列表")
    public AjaxResult updateEquipmentCodes() {
        service.updateEquipmentCodes();
        return success();
    }

    @GetMapping("/getDataTwinEquipmentValues")
    @ApiOperation(value = "数据孪生获取设备数据")
    public AjaxResult getDataTwinEquipmentValues() {
        List<DcsEquipmentFormVo> equipmentFormVoList = service.getDataTwinEquipmentValues();
        List<DataTwinEquipmentVo> dataTwinEquipmentVoList = new ArrayList<>();
        for (DcsEquipmentFormVo equipmentFormVo : equipmentFormVoList) {
            DataTwinEquipmentVo target = new DataTwinEquipmentVo();
            BeanUtils.copyProperties(equipmentFormVo, target);
            dataTwinEquipmentVoList.add(target);
        }
        return success(dataTwinEquipmentVoList);
    }

    @GetMapping("/getValues")
    @ApiOperation(value = "modbus tcp 获取数据")
    public AjaxResult getValues(@ApiParam(name = "modbus请求参数", value = "modbus请求参数", required = true) ModbusRequest 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) ModbusRequest request) throws ModbusProtocolException, ModbusNumberException, ModbusIOException {
        service.setCoils(request);
    }

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

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

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

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

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

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

}
