using System.ComponentModel;
using Admin.Bodk.Device.Entities.equipment;
using Admin.NET.Core;
using Admin.NET.Core.Service;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
namespace Admin.Bodk.Device.Services;
///
/// M9相关接口
///
[ApiDescriptionSettings(Order = 2)]
public class M9Service: IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _m9;
private readonly string _baseUri = "http://192.168.88.111:8080/";
ApiService _apiService = new ApiService();
public M9Service(SqlSugarRepository m9)
{
_m9 = m9;
}
///
/// 增加Ip
///
///
///
[UnitOfWork]
[ApiDescriptionSettings(Name = "AddIp"), HttpGet]
[DisplayName("增加Ip")]
public async Task AddEquipment([FromBody] M9Input input)
{
if(input is null) throw Oops.Oh("参数不能为空");
if (input.IPInterfaces is null || input.IPInterfaces.Count == 0) throw Oops.Oh("IP不能为空");
foreach (var Interface in input.IPInterfaces)
{
M9 m9Input = input.Adapt();
m9Input.InterfaceName = Interface.InterfaceName;
m9Input.IPAddresses = Interface.IPAddresses;
try
{
Console.WriteLine("开始插入数据");
await _m9.AsInsertable(m9Input).ExecuteReturnEntityAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
///
/// 读取coils
///
///
///
[UnitOfWork]
[ApiDescriptionSettings(Name = "ReadCoils"), HttpGet]
[DisplayName("读取coils")]
public async Task GetReadCoils(string slaveId, string startAddress, string numCoils)
{
string url = $"{_baseUri}api/modbus/read-coils?slaveId={slaveId}&startAddress={startAddress}&numCoils={numCoils}";
try
{
return await _apiService.GetApiResponseAsync(url);
}
catch (HttpRequestException e)
{
throw Oops.Oh(e.Message);
}
}
///
/// 写入coils
///
[UnitOfWork]
[ApiDescriptionSettings(Name = "WriteCoils"), HttpGet]
[DisplayName("写入coils")]
public async Task WriteCoils(string slaveId, string startAddress)
{
string url = $"{_baseUri}api/modbus/write-coils?slaveId={slaveId}&startAddress={startAddress}";
try
{
return await _apiService.GetApiResponseAsync(url);
}
catch (HttpRequestException e)
{
throw Oops.Oh(e.Message);
}
}
///
/// 读取HoldingRegisters
///
[UnitOfWork]
[ApiDescriptionSettings(Name = "ReadHoldingRegisters"), HttpGet]
[DisplayName("读取HoldingRegisters")]
public async Task ReadHoldingRegisters(string slaveId, string startAddress, string numRegisters)
{
string url = $"{_baseUri}api/modbus/read-holding-registers?slaveId={slaveId}" +
$"&startAddress={startAddress}&numRegisters={numRegisters}";
try
{
return await _apiService.GetApiResponseAsync(url);
}
catch (HttpRequestException e)
{
throw Oops.Oh(e.Message);
}
}
///
/// 写入HoldingRegisters
///
[UnitOfWork]
[ApiDescriptionSettings(Name = "WriteHoldingRegisters"), HttpGet]
[DisplayName("写入HoldingRegisters")]
public async Task WriteHoldingRegisters(string slaveId, string startAddress)
{
string url = $"{_baseUri}api/modbus/write-holding-registers?slaveId={slaveId}" +
$"&startAddress={startAddress}";
try
{
return await _apiService.GetApiResponseAsync(url);
}
catch (HttpRequestException e)
{
throw Oops.Oh(e.Message);
}
}
}