Create basic API to mock sunrise-sunset.org API for arduino controller

This commit is contained in:
Robert Marshall 2023-09-12 16:22:38 +01:00 committed by Robert Marshall
commit 5588c88726
10 changed files with 323 additions and 0 deletions

25
.dockerignore Normal file
View file

@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

24
.drone.yml Normal file
View file

@ -0,0 +1,24 @@
kind: pipeline
name: default
clone:
skip_verify: true
steps:
- name: docker
image: plugins/docker
settings:
repo: docker.robware.uk/fishtank-api
registry: docker.robware.uk
tags: latest
dockerfile: FishTankApi/Dockerfile
- name: notify
image: drillster/drone-email
settings:
host: 192.168.1.3
skip_verify: true
from: build@robware.uk
when:
status:
- changed
- failure

57
.gitignore vendored Normal file
View file

@ -0,0 +1,57 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/
# Visual Studio Code
.vscode
# Rider
.idea
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
#build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn
# Visual Studio 2015
.vs/
#lcov files
lcov.info
#compiled and minified assets
*.css
*.min.css
*.min.js
#nodejs
node_modules
output
.tmp
*.ncrunchsolution
/_NCrunch_Website
*.DotSettings
*.cache
.nuke/temp

30
FishTankApi.sln Normal file
View file

@ -0,0 +1,30 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishTankApi", "FishTankApi\FishTankApi.csproj", "{35D0BA11-444C-412C-81EE-C4D32567A3F2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B002B63D-676D-47C0-99A3-EF088AAF5F1D}"
ProjectSection(SolutionItems) = preProject
.drone.yml = .drone.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{35D0BA11-444C-412C-81EE-C4D32567A3F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35D0BA11-444C-412C-81EE-C4D32567A3F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35D0BA11-444C-412C-81EE-C4D32567A3F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35D0BA11-444C-412C-81EE-C4D32567A3F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {183B90B8-D73C-4100-8B27-193523A48436}
EndGlobalSection
EndGlobal

22
FishTankApi/Dockerfile Normal file
View file

@ -0,0 +1,22 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["FishTankApi/FishTankApi.csproj", "FishTankApi/"]
RUN dotnet restore "FishTankApi/FishTankApi.csproj"
COPY . .
WORKDIR "/src/FishTankApi"
RUN dotnet build "FishTankApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "FishTankApi.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FishTankApi.dll"]

View file

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>618597c8-de36-4286-9c26-6c2c452d1c65</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

83
FishTankApi/Program.cs Normal file
View file

@ -0,0 +1,83 @@
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.MapGet("/light", (int startHour, int endHour) =>
{
var year = DateTime.Today.Year;
var month = DateTime.Today.Month;
var day = DateTime.Today.Day;
return new SunriseSunsetOrgMock
{
Results = new()
{
AstronomicalTwilightBegin = new DateTimeOffset(year, month, day, startHour, 0, 0, TimeSpan.Zero),
Sunrise = new DateTimeOffset(year, month, day, startHour + 1, 0, 0, TimeSpan.Zero),
Sunset = new DateTimeOffset(year, month, day, endHour - 1, 0, 0, TimeSpan.Zero),
AstronomicalTwilightEnd = new DateTimeOffset(year, month, day, endHour, 0, 0, TimeSpan.Zero)
},
Status = "ok"
};
})
.WithOpenApi();
app.Run();
public class SunriseSunsetOrgMock
{
[JsonPropertyName("results")]
public Results Results { get; set; }
[JsonPropertyName("status")]
public string Status { get; set; }
}
public class Results
{
[JsonPropertyName("sunrise")]
public DateTimeOffset Sunrise { get; set; }
[JsonPropertyName("sunset")]
public DateTimeOffset Sunset { get; set; }
[JsonPropertyName("solar_noon")]
public DateTimeOffset? SolarNoon { get; set; }
[JsonPropertyName("day_length")]
public long? DayLength { get; set; }
[JsonPropertyName("civil_twilight_begin")]
public DateTimeOffset? CivilTwilightBegin { get; set; }
[JsonPropertyName("civil_twilight_end")]
public DateTimeOffset? CivilTwilightEnd { get; set; }
[JsonPropertyName("nautical_twilight_begin")]
public DateTimeOffset? NauticalTwilightBegin { get; set; }
[JsonPropertyName("nautical_twilight_end")]
public DateTimeOffset? NauticalTwilightEnd { get; set; }
[JsonPropertyName("astronomical_twilight_begin")]
public DateTimeOffset AstronomicalTwilightBegin { get; set; }
[JsonPropertyName("astronomical_twilight_end")]
public DateTimeOffset AstronomicalTwilightEnd { get; set; }
}

View file

@ -0,0 +1,48 @@
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5110"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7088;http://localhost:5110"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52356",
"sslPort": 44365
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}