操控平台后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.3 KiB

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;
/// <summary>
/// M9相关接口
/// </summary>
[ApiDescriptionSettings(Order = 2)]
public class M9Service: IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<M9> _m9;
private readonly string _baseUri = "http://192.168.88.111:8080/";
ApiService _apiService = new ApiService();
public M9Service(SqlSugarRepository<M9> m9)
{
_m9 = m9;
}
/// <summary>
/// 增加Ip
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[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<M9>();
m9Input.InterfaceName = Interface.InterfaceName;
m9Input.IPAddresses = Interface.IPAddresses;
try
{
Console.WriteLine("开始插入数据");
await _m9.AsInsertable(m9Input).ExecuteReturnEntityAsync();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
/// <summary>
/// 读取coils
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[UnitOfWork]
[ApiDescriptionSettings(Name = "ReadCoils"), HttpGet]
[DisplayName("读取coils")]
public async Task<ResultData> 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);
}
}
/// <summary>
/// 写入coils
/// </summary>
[UnitOfWork]
[ApiDescriptionSettings(Name = "WriteCoils"), HttpGet]
[DisplayName("写入coils")]
public async Task<ResultData> 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);
}
}
/// <summary>
/// 读取HoldingRegisters
/// </summary>
[UnitOfWork]
[ApiDescriptionSettings(Name = "ReadHoldingRegisters"), HttpGet]
[DisplayName("读取HoldingRegisters")]
public async Task<ResultData> 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);
}
}
/// <summary>
/// 写入HoldingRegisters
/// </summary>
[UnitOfWork]
[ApiDescriptionSettings(Name = "WriteHoldingRegisters"), HttpGet]
[DisplayName("写入HoldingRegisters")]
public async Task<ResultData> 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);
}
}
}