diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +**/.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 +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/ShellyExporter.sln b/ShellyExporter.sln new file mode 100644 index 0000000..da9161b --- /dev/null +++ b/ShellyExporter.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShellyExporter", "ShellyExporter\ShellyExporter.csproj", "{B776CDAB-5CD2-48A6-AFD6-20AD763C498B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B776CDAB-5CD2-48A6-AFD6-20AD763C498B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B776CDAB-5CD2-48A6-AFD6-20AD763C498B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B776CDAB-5CD2-48A6-AFD6-20AD763C498B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B776CDAB-5CD2-48A6-AFD6-20AD763C498B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {73A9AF68-001D-4498-B27C-514CC2FCC04C} + EndGlobalSection +EndGlobal diff --git a/ShellyExporter/.config/dotnet-tools.json b/ShellyExporter/.config/dotnet-tools.json new file mode 100644 index 0000000..e3cadb9 --- /dev/null +++ b/ShellyExporter/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "8.0.0", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/ShellyExporter/Dockerfile b/ShellyExporter/Dockerfile new file mode 100644 index 0000000..e38e365 --- /dev/null +++ b/ShellyExporter/Dockerfile @@ -0,0 +1,25 @@ +#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:8.0 AS base +USER app +WORKDIR /app +EXPOSE 8080 +EXPOSE 8081 + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["ShellyExporter/ShellyExporter.csproj", "ShellyExporter/"] +RUN dotnet restore "./ShellyExporter/./ShellyExporter.csproj" +COPY . . +WORKDIR "/src/ShellyExporter" +RUN dotnet build "./ShellyExporter.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./ShellyExporter.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "ShellyExporter.dll"] \ No newline at end of file diff --git a/ShellyExporter/Metrics.cs b/ShellyExporter/Metrics.cs new file mode 100644 index 0000000..3365922 --- /dev/null +++ b/ShellyExporter/Metrics.cs @@ -0,0 +1,37 @@ +using System.Text; +using Newtonsoft.Json; + +namespace ShellyExporter +{ + public class Metrics + { + public static async Task Get(string path, string name) + { + var sb = new StringBuilder(); + + var client = new HttpClient(); + var response = await client.GetStringAsync(path); + + dynamic responseObject = JsonConvert.DeserializeObject(response); + + if (responseObject == null) + return sb; + + if (responseObject.meters != null) + { + sb.AppendLine($"meters_power_wats{{item=\"{name}\"}} {responseObject.meters[0].power}"); + sb.AppendLine($"meters_overpower_wats{{item=\"{name}\"}} " + responseObject.meters[0].overpower); + sb.AppendLine($"meters_is_valid{{item=\"{name}\"}} " + (responseObject.meters[0].is_valid == "True" ? 1 : 0)); + sb.AppendLine($"meters_timestamp{{item=\"{name}\"}} " + responseObject.meters[0].timestamp); + sb.AppendLine($"meters_power_total{{item=\"{name}\"}} " + responseObject.meters[0].total); + } + + if (responseObject.temperature != null) + { + sb.AppendLine($"temperature{{item=\"{name}\"}} " + responseObject.temperature); + } + + return sb; + } + } +} diff --git a/ShellyExporter/Program.cs b/ShellyExporter/Program.cs new file mode 100644 index 0000000..5094f57 --- /dev/null +++ b/ShellyExporter/Program.cs @@ -0,0 +1,51 @@ +using System.Text; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Extensions.Hosting; +using ShellyExporter; +using System.Xml.Linq; + +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(); + +if (!builder.Environment.IsDevelopment()) +{ + builder.WebHost.UseKestrel(); + builder.WebHost.UseUrls(builder.Configuration["Kst:Url"]); +} + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); +app.MapControllers(); + +app.Map("/metrics", () => +{ + var sb = new StringBuilder(); + + foreach (var host in builder.Configuration.GetSection("Hosts").GetChildren()) + { + sb.Append(Metrics.Get($"http://{host.Value}/status", host.Path.Replace("Hosts:", "")).Result); + } + + return sb.ToString(); +}); + + + + +app.Run(); diff --git a/ShellyExporter/Properties/launchSettings.json b/ShellyExporter/Properties/launchSettings.json new file mode 100644 index 0000000..1546f20 --- /dev/null +++ b/ShellyExporter/Properties/launchSettings.json @@ -0,0 +1,52 @@ +{ + "profiles": { + "http": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5197" + }, + "https": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7217;http://localhost:5197" + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", + "environmentVariables": { + "ASPNETCORE_HTTPS_PORTS": "8081", + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true, + "useSSL": true + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:37965", + "sslPort": 44300 + } + } +} \ No newline at end of file diff --git a/ShellyExporter/ShellyExporter.csproj b/ShellyExporter/ShellyExporter.csproj new file mode 100644 index 0000000..2468209 --- /dev/null +++ b/ShellyExporter/ShellyExporter.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + true + c4df3427-71fa-4233-816b-8788d2eaa613 + Linux + + + + + + + + + + + + + + + Always + + + + diff --git a/ShellyExporter/ShellyExporter.http b/ShellyExporter/ShellyExporter.http new file mode 100644 index 0000000..5a408af --- /dev/null +++ b/ShellyExporter/ShellyExporter.http @@ -0,0 +1,6 @@ +@ShellyExporter_HostAddress = http://localhost:5197 + +GET {{ShellyExporter_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/ShellyExporter/appsettings.Development.json b/ShellyExporter/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/ShellyExporter/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ShellyExporter/appsettings.json b/ShellyExporter/appsettings.json new file mode 100644 index 0000000..dc3c34e --- /dev/null +++ b/ShellyExporter/appsettings.json @@ -0,0 +1,16 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "Kst": { + "Url": "http://192.168.111.77:5050" + }, + "Hosts": { + "david": "192.168.1.60", + "nekaj": "192.168.1.60" + } +}