Skip to content

Commit

Permalink
This is Renacci
Browse files Browse the repository at this point in the history
Formatted readme

Renamed InverterStatusFactory

c
  • Loading branch information
Yety committed Jan 31, 2024
1 parent f2ae940 commit 8494c48
Show file tree
Hide file tree
Showing 26 changed files with 1,003 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
**/appsettings.*.json
LICENSE
README.md
48 changes: 48 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

.DS_Store
appsettings.*.json
.idea/
#Ignore thumbnails created by Windows
Thumbs.db
#Ignore files built by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.lib
*.sbr
obj/
[Rr]elease*/
_ReSharper*/
.vs/
#Nuget packages folder
packages/
9 changes: 9 additions & 0 deletions Common/Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
114 changes: 114 additions & 0 deletions Common/HexToDecimalExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System.Text;

namespace Renacci
{
public static class HexToDecimalExtensions
{
// Disclaimer
// All these tools thanks to answers on StackOverflow.
// Some may have been modified for the purpose of this project.


/// <summary>
/// Converts a HEX string into Decimal number.
/// </summary>
/// <param name="hexValue">String must not contain dashes or any other seperators. Must not start with 0x.</param>
/// <returns></returns>
public static decimal HexToDecimal(this string hexValue)
{
long result = 0;
long.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, null, out result);
return result;
}

/// <summary>
/// Converts a HEX string into human readable ASCII.
/// </summary>
/// <param name="hexString">String must not contain dashes or any other seperators</param>
/// <returns></returns>
public static string HexToASCII(this string hexString)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexString.Length; i += 2)
{
string hs = hexString.Substring(i, 2);
sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
}
return sb.ToString();
}


public static byte[] ToByteArray(this string value)
{
byte[] bytes = null;
if (String.IsNullOrEmpty(value))
bytes = new byte[0];
else
{
int string_length = value.Length;
int character_index = (value.StartsWith("0x", StringComparison.Ordinal)) ? 2 : 0; // Does the string define leading HEX indicator '0x'. Adjust starting index accordingly.
int number_of_characters = string_length - character_index;

bool add_leading_zero = false;
if (0 != (number_of_characters % 2))
{
add_leading_zero = true;

number_of_characters += 1; // Leading '0' has been striped from the string presentation.
}

bytes = new byte[number_of_characters / 2]; // Initialize our byte array to hold the converted string.

int write_index = 0;
if (add_leading_zero)
{
bytes[write_index++] = FromCharacterToByte(value[character_index], character_index);
character_index += 1;
}

for (int read_index = character_index; read_index < value.Length; read_index += 2)
{
byte upper = FromCharacterToByte(value[read_index], read_index, 4);
byte lower = FromCharacterToByte(value[read_index + 1], read_index + 1);

bytes[write_index++] = (byte)(upper | lower);
}
}

return bytes;
}

//Solely used by ConvertToByteArray
private static byte FromCharacterToByte(char character, int index, int shift = 0)
{
byte value = (byte)character;
if (((0x40 < value) && (0x47 > value)) || ((0x60 < value) && (0x67 > value)))
{
if (0x40 == (0x40 & value))
{
if (0x20 == (0x20 & value))
value = (byte)(((value + 0xA) - 0x61) << shift);
else
value = (byte)(((value + 0xA) - 0x41) << shift);
}
}
else if ((0x29 < value) && (0x40 > value))
value = (byte)((value - 0x30) << shift);
else
throw new InvalidOperationException(String.Format("Character '{0}' at index '{1}' is not valid alphanumeric character.", character, index));

return value;
}

public static string ByteArrayToString(byte[] ba, int numBytes)
{
StringBuilder hex = new StringBuilder(numBytes * 2);
var take = ba.Take(numBytes);
foreach (byte b in take)
hex.AppendFormat("{0:X2}, ", b);
return hex.ToString();
}


}
}
14 changes: 14 additions & 0 deletions Common/RandomByteGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Security.Cryptography;

namespace Renacci;

public class RandomByteGenerator
{
public byte[] GenerateRandomBytes(int numberOfBytes)
{
byte[] randomBytes = new byte[numberOfBytes];
using var generator = RandomNumberGenerator.Create();
generator.GetBytes(randomBytes);
return randomBytes;
}
}
9 changes: 9 additions & 0 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
13 changes: 13 additions & 0 deletions Core/GetStatusUseCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Core;

namespace Renacci.UseCases;

public class GetStatusUseCase(IStatusRepository _statusRepository)
{

public async Task<InverterStatus> GetStatus()
{
var status = await _statusRepository.GetStatus();
return status;
}
}
8 changes: 8 additions & 0 deletions Core/IStatusRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Renacci;

namespace Core;

public interface IStatusRepository
{
Task<InverterStatus> GetStatus();
}
68 changes: 68 additions & 0 deletions Core/InverterStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
public class InverterStatus
{
public double Pv1Vol { get; set; }
public double Pv1Cur { get; set; }
public double Pv1Power { get; set; }
public double Pv2Vol { get; set; }
public double Pv2Cur { get; set; }
public double Pv2Power { get; set; }
public double IntTemp { get; set; }
public double ChargerTemp { get; set; }
public double BoostTemp { get; set; }
public double InvTemp { get; set; }
public double BatteryVol { get; set; }
public double BatteryCur { get; set; }
public double BatteryPower { get; set; }
public double BmsVol { get; set; }
public double BmsCur { get; set; }
public double BmsTemp { get; set; }
public double Soc { get; set; }
public double Soh { get; set; }
public int BatteryStatus { get; set; }
public int CycleTime { get; set; }
public int AhNum { get; set; }
public int InvState { get; set; }
public int BmsState { get; set; }
public int MeterState { get; set; }
public int Alarm { get; set; }
public double GridVol { get; set; }
public double GridSVol { get; set; }
public double GridTVol { get; set; }
public double InvCur { get; set; }
public double InvSCur { get; set; }
public double InvTCur { get; set; }
public double InvPower { get; set; }
public double InvSPower { get; set; }
public double InvTPower { get; set; }
public double GridFre { get; set; }
public double GridSFre { get; set; }
public double GridTFre { get; set; }
public double EpsVol { get; set; }
public double EpsSVol { get; set; }
public double EpsTVol { get; set; }
public double EpsCur { get; set; }
public double EpsSCur { get; set; }
public double EpsTCur { get; set; }
public double EpsPower { get; set; }
public double EpsSPower { get; set; }
public double EpsTPower { get; set; }
public double EpsFre { get; set; }
public double MeterRPower { get; set; }
public double MeterSPower { get; set; }
public double MeterTPower { get; set; }
public double MeterPower { get; set; }
public double Meter2RPower { get; set; }
public double Meter2SPower { get; set; }
public double Meter2TPower { get; set; }
public double Meter2Power { get; set; }
public double Meter3RPower { get; set; }
public double Meter3SPower { get; set; }
public double Meter3TPower { get; set; }
public double Meter3Power { get; set; }
public double LoadRPower { get; set; }
public double LoadSPower { get; set; }
public double LoadTPower { get; set; }
public double LoadPower { get; set; }

// Constructor and other methods (if necessary) go here
}
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["RenacciBackend/RenacciBackend.csproj", "RenacciBackend/"]
COPY ["RenacModbus/RenacModbus.csproj", "RenacModbus/"]
COPY ["Common/Common.csproj", "Common/"]
COPY ["Core/Core.csproj", "Core/"]
RUN dotnet restore "RenacciBackend/RenacciBackend.csproj"
COPY . .
WORKDIR "/src/RenacciBackend"
RUN dotnet build "RenacciBackend.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "RenacciBackend.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RenacciBackend.dll"]
11 changes: 11 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Renacci
Serving the modbus of your Renac inverter as REST API.

This is totally work in progress!

## Run
To run this in docker use:

```docker run -it --rm -p 8080:8080 -e RenacModbusOptions__Host='<MODBUS-ADAPTER-IP>' -e RenacModbusOptions__Port=<MODBUS-ADAPTER-PORT> -e RenacModbusOptions__DeviceId=<INVERTER-DEVICE-ID> ghcr.io/yety/renacci:main```

Then call: http://localhost:8080/api/Status/GetStatus
Loading

0 comments on commit 8494c48

Please sign in to comment.