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.
89 lines
3.1 KiB
89 lines
3.1 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Application.Services;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
|
|
namespace IoTCenter.Controllers
|
|
{
|
|
public class CommandController : Controller
|
|
{
|
|
private readonly ILogger<CommandController> _logger;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
private readonly IRepository<Sence> _senceRepo;
|
|
private readonly IHubContext<PageHub> _pageHubContext;
|
|
|
|
public CommandController(ILogger<CommandController> logger,
|
|
IHttpClientFactory httpClientFactory,
|
|
IRepository<Device> deviceRepo,
|
|
IRepository<Sence> senceRepo,
|
|
IHubContext<PageHub> pageHubContext)
|
|
{
|
|
this._logger = logger;
|
|
this._httpClientFactory = httpClientFactory;
|
|
this._deviceRepo = deviceRepo;
|
|
this._senceRepo = senceRepo;
|
|
this._pageHubContext = pageHubContext;
|
|
}
|
|
|
|
public IActionResult Exec(string connectionId, string node, string cmd, Guid id)
|
|
{
|
|
try
|
|
{
|
|
var query = string.Empty;
|
|
foreach (var item in Request.Query.Where(o => o.Key != "node" && o.Key != "cmd" && !o.Key.StartsWith("id")))
|
|
{
|
|
query = query.SetParam(item.Key, item.Value);
|
|
}
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.Exec), connectionId, id, cmd, query);
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Json(ApiResponse.Error(ex));
|
|
}
|
|
}
|
|
|
|
public IActionResult ExecAll(string connectionId, string node, string cmd, List<Guid> id)
|
|
{
|
|
try
|
|
{
|
|
var query = string.Empty;
|
|
foreach (var item in Request.Query.Where(o => o.Key != "node" && o.Key != "cmd" && !o.Key.StartsWith("id[")))
|
|
{
|
|
query = query.SetParam(item.Key, item.Value);
|
|
}
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.ExecAll), connectionId, id, cmd, query);
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Json(ApiResponse.Error(ex));
|
|
}
|
|
}
|
|
|
|
public IActionResult Sence(string connectionId, string node, Guid id)
|
|
{
|
|
try
|
|
{
|
|
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.Sence), connectionId, id);
|
|
return Json(ApiResponse.AsyncSuccess());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Json(ApiResponse.Error(ex));
|
|
}
|
|
}
|
|
}
|
|
} |