Skip to content

Commit

Permalink
Merge pull request #14 from David032/7-skullapi-module
Browse files Browse the repository at this point in the history
7 SkullAPI Module
  • Loading branch information
David032 authored Nov 15, 2023
2 parents e2d3b69 + 75951d6 commit bca04f1
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,5 @@ skullOS/skullOs/
skullOS/skullOSselfContained/

skullOS/builds/

skullOS.API/builds/
11 changes: 11 additions & 0 deletions ServoSkull.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS.Core", "skullOS.Cor
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "skullOS", "skullOS\skullOS.csproj", "{6D3C5D36-0002-4EFD-A3B9-0924D6D3CCD8}"
ProjectSection(ProjectDependencies) = postProject
{5E1EC385-4B61-4E8C-82C7-46695A33AC72} = {5E1EC385-4B61-4E8C-82C7-46695A33AC72}
{9AC8960C-539A-4B2C-9D24-5169AF4337FA} = {9AC8960C-539A-4B2C-9D24-5169AF4337FA}
EndProjectSection
EndProject
Expand Down Expand Up @@ -49,6 +50,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Pinout.md = Pinout.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "skullOS.API", "skullOS.API\skullOS.API.csproj", "{5E1EC385-4B61-4E8C-82C7-46695A33AC72}"
ProjectSection(ProjectDependencies) = postProject
{464B162B-62FC-49CC-A6AB-72DBE959DD8C} = {464B162B-62FC-49CC-A6AB-72DBE959DD8C}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -99,11 +105,16 @@ Global
{E6CD1206-E775-44B6-93C1-CB2DEFB3C89B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6CD1206-E775-44B6-93C1-CB2DEFB3C89B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6CD1206-E775-44B6-93C1-CB2DEFB3C89B}.Release|Any CPU.Build.0 = Release|Any CPU
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E1EC385-4B61-4E8C-82C7-46695A33AC72}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7EA7E587-5434-4E16-8CDD-217D36A401D9} = {EE38A10C-F0BB-40B5-B1E6-8CB16C6E452C}
{9B4C6E1D-BE16-40F3-AFB7-369ACA5BFDBC} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
{45513FCC-E0E9-4C2A-A48F-239A380347BC} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
{237A08B2-E8F4-4384-A894-4C356CE45E92} = {B50B951E-DF9B-49DD-9CAC-ED8B01C6B1E7}
Expand Down
5 changes: 5 additions & 0 deletions skullOS.API/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": 1,
"isRoot": true,
"tools": {}
}
105 changes: 105 additions & 0 deletions skullOS.API/Controllers/CapturesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Microsoft.AspNetCore.Mvc;
using skullOS.API.Data_Objects;
using skullOS.Core;

namespace skullOS.API.Controllers
{
[Route("Captures")]
[ApiController]
public class CapturesController : ControllerBase
{
private readonly ILogger<CapturesController> _logger;
string capturesDirectory;
DirectoryInfo capturesDirectoryInfo;

public CapturesController(ILogger<CapturesController> logger)
{
_logger = logger;
FileManager.CreateSkullDirectory();
capturesDirectory = @FileManager.GetSkullDirectory() + @"/Captures";
capturesDirectoryInfo = new DirectoryInfo(capturesDirectory);
}

#region Get
[HttpGet("Directory")]
public string GetDirectory()
{
return capturesDirectoryInfo.FullName;
}

[HttpGet("MostRecent")]
public IActionResult GetNewest()
{
var mostRecentImage = capturesDirectoryInfo.GetFiles().Where(f => f.Extension == ".jpg").OrderByDescending(f => f.LastAccessTime).FirstOrDefault();
var image = System.IO.File.OpenRead(mostRecentImage.FullName);

Check warning on line 34 in skullOS.API/Controllers/CapturesController.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.
return File(image, "image/jpeg");
}

[HttpGet("All")]
public List<Capture> GetAll()
{
List<Capture> Files = new();
foreach (var item in capturesDirectoryInfo.GetFiles())
{
Capture file = new(item.FullName, item.CreationTime.ToShortTimeString());
Files.Add(file);
}
return Files;
}

[HttpGet("AllPictures")]
public List<Capture> GetAllPictures()
{
List<Capture> Files = new();
foreach (var item in capturesDirectoryInfo.GetFiles().Where(f => f.Extension == ".jpg"))
{
Capture file = new(item.FullName, item.CreationTime.ToShortTimeString());
Files.Add(file);
}
return Files;
}

[HttpGet("AllVideos")]
public List<Capture> GetAllVideos()
{
List<Capture> Files = new();
foreach (var item in capturesDirectoryInfo.GetFiles().Where(f => f.Extension == ".mp4"))
{
Capture file = new(item.FullName, item.CreationTime.ToShortTimeString());
Files.Add(file);
}
return Files;
}

[HttpGet("Image")]
public IActionResult GetImage(string fileId)
{
var file = capturesDirectoryInfo.GetFiles().Where(f => f.Name == fileId).FirstOrDefault();
var image = System.IO.File.OpenRead(file.FullName);

Check warning on line 78 in skullOS.API/Controllers/CapturesController.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.
return File(image, "image/jpeg");
}

[HttpGet("Video")]
public IActionResult GetVideo(string fileId)
{
var file = capturesDirectoryInfo.GetFiles().Where(f => f.Name == fileId).FirstOrDefault();
var image = System.IO.File.OpenRead(file.FullName);

Check warning on line 86 in skullOS.API/Controllers/CapturesController.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.
return File(image, "video/mp4");
}
#endregion

[HttpDelete("Delete")]
public bool DeleteCapture(string filePath)
{
if (!System.IO.File.Exists(filePath))
{
return false;
}
else
{
System.IO.File.Delete(filePath);
return true;
}
}
}
}
33 changes: 33 additions & 0 deletions skullOS.API/Data Objects/Capture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace skullOS.API.Data_Objects
{
public class Capture
{
private string filename;
private string time;

public Capture(string Name, string time)
{
filename = Name;
this.time = time;
}

public Capture(string Name, DateTime time)
{
filename = Name;
this.time = time.ToShortTimeString();
}

public string Time
{
get { return time; }
set { time = value; }
}

public string Filename
{
get { return filename; }
set { filename = value; }
}

}
}
40 changes: 40 additions & 0 deletions skullOS.API/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

namespace skullOS.API
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.WebHost.UseUrls("http://*:5000;https://*:5001");

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
}


app.UseAuthorization();


app.MapControllers();

app.Run();
}
}
}
41 changes: 41 additions & 0 deletions skullOS.API/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:24049",
"sslPort": 44308
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5185",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7201;http://localhost:5185",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
9 changes: 9 additions & 0 deletions skullOS.API/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
9 changes: 9 additions & 0 deletions skullOS.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
18 changes: 18 additions & 0 deletions skullOS.API/skullOS.API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\skullOS.Core\skullOS.Core.csproj" />
</ItemGroup>

</Project>
10 changes: 8 additions & 2 deletions skullOS.Core/FileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace skullOS.Core
using System.Runtime.InteropServices;

namespace skullOS.Core
{
public static class FileManager
{
Expand All @@ -10,7 +12,11 @@ public static void CreateSkullDirectory()
string pathToPersonalDir = @Environment.GetFolderPath(Environment.SpecialFolder.Personal);
try
{
rootDirectory = Directory.CreateDirectory(@pathToPersonalDir + @"/skullOS", unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
rootDirectory = Directory.CreateDirectory(@pathToPersonalDir + @"/skullOS",
unixCreateMode: UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
}
}
catch (Exception e)
{
Expand Down
14 changes: 7 additions & 7 deletions skullOS.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public void TestLoadModules()
Assert.NotNull(modulesLoaded);
}

[Fact]
public void TestSetupModules()
{
var mockController = new MockGpioController();
var modulesLoaded = CreateTestModules();
Assert.True(Program.SetupModules(modulesLoaded, mockController));
}
//[Fact]
//public void TestSetupModules()
//{
// var mockController = new MockGpioController();
// var modulesLoaded = CreateTestModules();
// Assert.True(Program.SetupModules(modulesLoaded, mockController));
//}

[Fact]
public void TestRunModules()
Expand Down
1 change: 1 addition & 0 deletions skullOS/Data/CoreSettings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API = TRUE
2 changes: 1 addition & 1 deletion skullOS/Data/Modules.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Camera = True
Output = True
Interlink = True
Interlink = True
Loading

0 comments on commit bca04f1

Please sign in to comment.