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.
134 lines
5.1 KiB
134 lines
5.1 KiB
using Application.Domain.Entities;
|
|
using Hangfire;
|
|
using Hangfire.Storage;
|
|
using Infrastructure.Application.Services.Settings;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
|
|
namespace IoTCenter.Api.Controllers
|
|
{
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
|
[ApiController]
|
|
public class SiteController : ControllerBase
|
|
{
|
|
private readonly IConfiguration _cfg;
|
|
private readonly ILogger<OrganController> _logger;
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
private readonly IRepository<OrganSceneTimer> _organSceneTimerRepo;
|
|
private readonly ISettingService _settingService;
|
|
private readonly IRepository<User> _userRepo;
|
|
|
|
public SiteController(IConfiguration cfg,
|
|
IHttpClientFactory httpClientFactory,
|
|
IRepository<OrganSceneTimer> organSceneTimerRepo,
|
|
ILogger<OrganController> logger,
|
|
ISettingService settingService,
|
|
IRepository<User> userRepo)
|
|
{
|
|
this._cfg = cfg;
|
|
this._httpClientFactory = httpClientFactory;
|
|
this._organSceneTimerRepo = organSceneTimerRepo;
|
|
this._logger = logger;
|
|
this._settingService = settingService;
|
|
this._userRepo = userRepo;
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult GetSite()
|
|
{
|
|
try
|
|
{
|
|
var username = User.Identity.IsAuthenticated ? this.HttpContext.User.Identity.Name : null;
|
|
var roles = new List<string>();
|
|
var permissions = new List<string>();
|
|
if (User.Identity.IsAuthenticated)
|
|
{
|
|
roles = this._userRepo.ReadOnlyTable()
|
|
.Where(o => o.UserName == username)
|
|
.SelectMany(o => o.UserRoles)
|
|
.Select(o => o.Role.Name)
|
|
.ToList();
|
|
permissions = this._userRepo.ReadOnlyTable()
|
|
.Where(o => o.UserName == username)
|
|
.SelectMany(o => o.UserRoles)
|
|
.Select(o => o.Role)
|
|
.SelectMany(o => o.RolePermissions)
|
|
.Select(o => o.Permission)
|
|
.Select(o => o.Number)
|
|
.ToList();
|
|
}
|
|
return Ok(new
|
|
{
|
|
logo = this._settingService.GetSetting("logo").Value,
|
|
name = this._settingService.GetSetting("name").Value,
|
|
copyright = this._settingService.GetSetting("copyright").Value,
|
|
version = Helper.Instance.GetVersion(),
|
|
username,
|
|
roles,
|
|
permissions,
|
|
sso = this._settingService.GetSetting("sso")?.Value
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this._logger.LogError(ex, ex.Message);
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult UpdateTimer()
|
|
{
|
|
using var conn = JobStorage.Current.GetConnection();
|
|
var jobs = conn.GetRecurringJobs();
|
|
foreach (var job in jobs)
|
|
{
|
|
RecurringJob.RemoveIfExists(job.Id);
|
|
}
|
|
var timers = this._organSceneTimerRepo.ReadOnlyTable().ToList();
|
|
foreach (var timer in timers)
|
|
{
|
|
var url = _cfg.GetConnectionString("JobServer") + $"/RecurringJob/AddOrUpdate";
|
|
var data = new
|
|
{
|
|
timer.Id,
|
|
url = $"{_cfg.GetConnectionString("JobCallBack")}/{timer.Id}",
|
|
cron = timer.Cron
|
|
};
|
|
try
|
|
{
|
|
this._logger.LogDebug($"update job server");
|
|
var client = _httpClientFactory.CreateClient();
|
|
using var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
|
|
var result = client.PostAsync(url, content).Result;
|
|
this._logger.LogDebug($"{result.StatusCode}:{result.Content.ReadAsStringAsync().Result}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
this._logger.LogError(ex.ToString());
|
|
}
|
|
}
|
|
return Ok();
|
|
}
|
|
|
|
private void RemoveJobs()
|
|
{
|
|
var client = _httpClientFactory.CreateClient();
|
|
var url = _cfg.GetConnectionString("JobServer") + $"/RecurringJob/RemoveAll";
|
|
using var content = new StringContent("", Encoding.UTF8, "application/json");
|
|
var result = client.PostAsync(url, content).Result;
|
|
this._logger.LogDebug($"{result.StatusCode}:{result.Content.ReadAsStringAsync().Result}");
|
|
}
|
|
}
|
|
} |