Files
ThingsGateway/framework/ThingsGateway.ApiController/Controllers/RpcControler.cs

98 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
using Furion;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.ComponentModel;
using ThingsGateway.Admin.Application;
using ThingsGateway.Admin.Core;
using ThingsGateway.Application;
using ThingsGateway.Foundation;
namespace ThingsGateway.ApiController;
/// <summary>
/// 设备控制
/// </summary>
[ApiDescriptionSettings(CateGoryConst.ThingsGatewayOpenApi, Order = 200)]
[Route("openApi/rpc")]
[Description("变量写入")]
[OpenApiPermission]
[LoggingMonitor]
[Authorize(AuthenticationSchemes = "Bearer")]
public class RpcControler : IDynamicApiController
{
readonly IServiceScopeFactory _scopeFactory;
/// <inheritdoc cref="RpcControler"/>
public RpcControler(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
var serviceScope = _scopeFactory.CreateScope();
RpcCore = serviceScope.ServiceProvider.GetService<RpcSingletonService>();
CollectDeviceHostService = ServiceHelper.GetBackgroundService<CollectDeviceWorker>();
}
CollectDeviceWorker CollectDeviceHostService { get; set; }
RpcSingletonService RpcCore { get; set; }
/// <summary>
/// 控制采集线程启停
/// </summary>
/// <returns></returns>
[HttpPost("configDeviceThread")]
[Description("控制采集线程启停")]
public void ConfigDeviceThread(long id, bool isStart)
{
CollectDeviceHostService.ConfigDeviceThread(id, isStart);
}
/// <summary>
/// 重启采集线程
/// </summary>
/// <returns></returns>
[HttpPost("upDeviceThread")]
[Description("重启采集线程")]
public async Task UpDeviceThread(long id)
{
if (id <= 0)
{
await CollectDeviceHostService.RestartDeviceThreadAsync();
}
else
{
await CollectDeviceHostService.UpDeviceThreadAsync(id, false);
}
}
/// <summary>
/// 写入多个变量
/// </summary>
[HttpPost("writeVariables")]
[Description("写入变量")]
public async Task<Dictionary<string, OperResult>> WriteDeviceMethods(Dictionary<string, string> objs)
{
var result = await RpcCore.InvokeDeviceMethodAsync($"WebApi-{UserManager.UserAccount}-{App.HttpContext.Connection.RemoteIpAddress.MapToIPv4()}", objs);
return result;
}
}