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.
221 lines
6.7 KiB
221 lines
6.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Swashbuckle.AspNetCore.Annotations;
|
|
using Unosquare.RaspberryIO;
|
|
using Unosquare.RaspberryIO.Gpio;
|
|
|
|
namespace MediaService.Controllers
|
|
{
|
|
public class HomeController : Controller
|
|
{
|
|
private readonly IConfiguration _cfg;
|
|
private readonly IHostingEnvironment _env;
|
|
private readonly IRepository<FolderObject> _folderRepo;
|
|
private readonly IRepository<FileObject> _fileRepo;
|
|
private static object lockObject = new object();
|
|
|
|
public HomeController(IConfiguration cfg, IHostingEnvironment env, IRepository<FolderObject> folderRepo, IRepository<FileObject> fileRepo)
|
|
{
|
|
this._cfg = cfg;
|
|
this._env = env;
|
|
this._folderRepo = folderRepo;
|
|
this._fileRepo = fileRepo;
|
|
}
|
|
|
|
#region 视图
|
|
|
|
public IActionResult Index(Guid? parentId)
|
|
{
|
|
Pi.Spi.Channel0Frequency = SpiChannel.MinFrequency;
|
|
var request = System.Text.Encoding.ASCII.GetBytes("HELLO!");
|
|
var response = Pi.Spi.Channel0.SendReceive(request);
|
|
return View(this.Folder(parentId));
|
|
}
|
|
|
|
public IActionResult File(string path)
|
|
{
|
|
return View(model: path.Replace(Path.Combine(this._env.WebRootPath), "").Replace(@"\", "/"));
|
|
}
|
|
|
|
#endregion 视图
|
|
|
|
#region WebAPI
|
|
|
|
[HttpGet]
|
|
[Route("/folder")]
|
|
[SwaggerOperation("目录")]
|
|
public List<FolderObject> Folder([SwaggerParameter("上级Id")]Guid? parentId, [SwaggerParameter("刷新")]bool refresh = false)
|
|
{
|
|
if (!this._folderRepo.ReadOnlyTable().Any() || refresh)
|
|
{
|
|
this.Refresh();
|
|
}
|
|
var model = this._folderRepo.ReadOnlyTable().Include(o => o.Files)
|
|
.WhereIf(!parentId.HasValue, o => o.ParentId == null)
|
|
.WhereIf(parentId.HasValue, o => o.ParentId == parentId)
|
|
.ToList();
|
|
return model;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("/refresh")]
|
|
[SwaggerOperation("刷新")]
|
|
public string Refresh()
|
|
{
|
|
lock (lockObject)
|
|
{
|
|
try
|
|
{
|
|
var folders = this._folderRepo.Table().ToList();
|
|
foreach (var item in folders)
|
|
{
|
|
this._folderRepo.Delete(item);
|
|
}
|
|
this._folderRepo.SaveChanges();
|
|
var path = Path.Combine(this._env.WebRootPath, "upload");
|
|
this.UpdateFolder(path, null);
|
|
return "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("/play")]
|
|
[SwaggerOperation("播放")]
|
|
public string Play(string path)
|
|
{
|
|
try
|
|
{
|
|
this.Clear();
|
|
var process = new Process
|
|
{
|
|
StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = string.Format(this._cfg["cmd"], path),
|
|
Arguments = string.Format(this._cfg["arg"], path),
|
|
UseShellExecute = false
|
|
}
|
|
};
|
|
process.Start();
|
|
return "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("/stop")]
|
|
[SwaggerOperation("停止")]
|
|
public string Stop(string path)
|
|
{
|
|
try
|
|
{
|
|
this.Clear();
|
|
return "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ex.Message;
|
|
}
|
|
}
|
|
|
|
private void Clear()
|
|
{
|
|
var processes = Process.GetProcessesByName("omxplayer.bin");
|
|
Console.WriteLine($"process count: {processes.Count()}");
|
|
for (int i = 0; i < processes.Length; i++)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"kill {i}");
|
|
processes[i].Kill();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion WebAPI
|
|
|
|
#region private
|
|
|
|
private void UpdateFolder(string path, Guid? parentId)
|
|
{
|
|
var folder = this._folderRepo.Table().Include(o => o.Files).FirstOrDefault(o => o.Path == path);
|
|
if (folder == null)
|
|
{
|
|
folder = new FolderObject
|
|
{
|
|
Name = Path.GetFileName(path),
|
|
Path = path,
|
|
ParentId = parentId
|
|
};
|
|
this._folderRepo.Add(folder);
|
|
this._folderRepo.SaveChanges();
|
|
}
|
|
var filePaths = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly);
|
|
foreach (var file in folder.Files)
|
|
{
|
|
if (!filePaths.Contains(file.Path))
|
|
{
|
|
folder.Files.Remove(file);
|
|
}
|
|
}
|
|
foreach (var filePath in filePaths)
|
|
{
|
|
var file = folder.Files.FirstOrDefault(o => o.Path == filePath);
|
|
if (file == null)
|
|
{
|
|
file = new FileObject
|
|
{
|
|
Name = Path.GetFileName(filePath),
|
|
Path = filePath
|
|
};
|
|
folder.Files.Add(file);
|
|
}
|
|
}
|
|
this._folderRepo.SaveChanges();
|
|
var subFolders = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
|
|
foreach (var item in subFolders)
|
|
{
|
|
UpdateFolder(item, folder.Id);
|
|
}
|
|
}
|
|
|
|
#endregion private
|
|
|
|
#region test
|
|
|
|
public List<string> GetProcess(string id)
|
|
{
|
|
if (string.IsNullOrEmpty(id))
|
|
{
|
|
return Process.GetProcesses().Select(o => o.ProcessName).ToList();
|
|
}
|
|
else
|
|
{
|
|
return Process.GetProcessesByName(id).Select(o => o.ProcessName).ToList();
|
|
}
|
|
}
|
|
|
|
#endregion test
|
|
}
|
|
} |