import request from '@/utils/request'
//获取设备id索引列表
export function getEquipmentIndexList() {
    return request({
        url: '/hnyz/param/getIndexList',
        method: 'get',
        headers: {
            'Content-Type': 'application/json'
        },
    })
}
//dataType:数据类型 (1线圈, 2浮点数, 3整型)
//functionCode:功能码 (1读线圈, 3读保持寄存器,4读输入寄存器)
//quantity:读取数量
//获取数据
export function getValues({ dataArr, startAddress }) {
    return request({
        url: '/modbus/getValues?data=' + dataArr + '&slaveId=1&startAddress=' + startAddress + '&dataType=1&functionCode=1&quantity=1',
        method: 'get',
        headers: {
            'Content-Type': 'application/json'
        },
    })
}

// 设置单个线圈
export function setCoil({ dataArr, startAddress }) {
    return request({
        url: '/modbus/setCoil?data='+dataArr+'&slaveId=1&startAddress='+startAddress+'&dataType=1&functionCode=1&quantity=1',
        method: 'post',
        headers: {
            'Content-Type': 'application/json' 
        },
    })
}

export function setValues({ protocol, dataType, resource, lengthOrQos, data, offsetOrIndex, unitId, ipAddress, port }) {
    return request({
        url: '/protocol/setValues',
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        data: {
            protocol: protocol,                    // MODBUS_TCP 协议
            unitId: unitId,                      // 从站 ID
            dataType: dataType,                 // 1 表示布尔型
            resource: resource,                  // 功能码，05 是写单个线圈
            offsetOrIndex: offsetOrIndex,   // 起始地址
            lengthOrQos: lengthOrQos,                // 数量
            data: data,                 // 写入的值，例如 [true]
            ipAddress: ipAddress,    // 你的 Modbus TCP 服务地址
            port: port                      // 默认端口
        }
    })
}



//设置多个线圈
export function setCoils({ dataArr, startAddress, quantity }) {
    return request({
        url: '/modbus/setCoils?data=' + dataArr + '&slaveId=1&startAddress=' + startAddress + '&dataType=1&functionCode=1&quantity=' + quantity,
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
    })
}

//设置单个整型
export function setIntRegister({ dataArr, startAddress }) {
    return request({
        url: '/modbus/setIntRegister?data=' + dataArr + '&slaveId=1&startAddress=' + startAddress + '&dataType=3&functionCode=3&quantity=1',
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
    })
}

//设置单个浮点数
export function setFloatRegister({ dataArr, startAddress }) {
    return request({
        url: '/modbus/setFloatRegister?data=' + dataArr + '&slaveId=1&startAddress=' + startAddress + '&dataType=2&functionCode=3&quantity=1',
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
    })
}