1
0

Initial commit

This commit is contained in:
2023-06-21 13:51:38 +02:00
commit 957670ce42
45 changed files with 1894 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>$(SolutionName.Replace(" ", "_")).$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Remove="publish\**" />
<EmbeddedResource Remove="publish\**" />
<None Remove="publish\**" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.70" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DataModels\DataModels.csproj" />
<ProjectReference Include="..\Utils\Utils.csproj" />
</ItemGroup>
</Project>

167
ConsoleLog/Program.cs Normal file
View File

@@ -0,0 +1,167 @@
using System.Globalization;
using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using MoniteurBaie.DataModels;
using MoniteurBaie.Utils;
using StackExchange.Redis;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var redisConfig = config.GetSection("MoniteurBaie:Redis");
var commandsChannel = redisConfig.GetValue<string>("Channels:Commands")!;
using var redis = ConnectionMultiplexer.Connect(redisConfig.GetValue<string>("Endpoint")!, opts =>
{
opts.ClientName = redisConfig.GetValue<string>("ClientName");
});
var packetChannel = redis.GetSubscriber().Subscribe(redisConfig.GetValue<string>("Channels:Packets")!);
packetChannel.OnMessage(OnPacketMessage);
redis.GetSubscriber().Subscribe(commandsChannel).OnMessage(OnCommandMessage);
var inputThread = new Thread(DoInput)
{
IsBackground = true
};
inputThread.Start();
using var cancel = new AutoResetEvent(false);
void ctrlC(object? sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
Console.CancelKeyPress -= ctrlC;
cancel.Set();
}
Console.CancelKeyPress += ctrlC;
cancel.WaitOne();
static void OnPacketMessage(ChannelMessage channelMessage)
{
var message = channelMessage.Message.ToString();
Console.WriteLine("<< " + message);
PrintPacket(message);
}
static void OnCommandMessage(ChannelMessage channelMessage)
{
Console.WriteLine("<< " + channelMessage.Message);
}
static void PrintPacket(string serializedPacket)
{
var sb = new StringBuilder();
BatteryControllerPacket? batCtrlPacket;
try
{
batCtrlPacket = JsonSerializer.Deserialize(serializedPacket, BatteryControllerPacketContext.Default.BatteryControllerPacket);
}
catch (Exception ex)
{
Console.Error.WriteLine("Invalid packet.");
Console.Error.WriteLine(ex);
return;
}
if (batCtrlPacket is null)
{
Console.Error.WriteLine("Null packet.");
return;
}
SerialDataPacket packet;
try
{
packet = PacketParser.ParseSerialDataPacket(batCtrlPacket.SerialData);
}
catch (Exception ex)
{
Console.Error.WriteLine("Invalid data packet.");
Console.Error.WriteLine(ex);
throw;
}
sb.AppendLine($"==== [{batCtrlPacket.Timestamp:yyyy/MM/dd HH:mm:ss)}] ====");
sb.AppendLine();
sb.AppendLine("-- Tensions --");
sb.AppendLine();
sb.AppendLine($"{packet.VBat:F2} // {packet.VB1:F2} / {packet.VB2:F2} / {packet.VB3:F2} / {packet.VB4:F2} / {packet.VB5:F2} / {packet.VB6:F2}");
sb.AppendLine($"Min : {packet.VBmin:F2}");
sb.AppendLine($"Max : {packet.VBmax:F2}");
sb.AppendLine($"Diff : {packet.VBmax - packet.VBmin:F2}");
sb.AppendLine();
sb.AppendLine("-- Puissance --");
sb.AppendLine();
sb.AppendLine($"Courant : {packet.Curr:F2}");
sb.AppendLine($"Puissance : {packet.Power:F2}");
sb.AppendLine($"Energie : {packet.Energy:F2}");
sb.AppendLine();
sb.AppendLine("-- Températures --");
sb.AppendLine();
sb.AppendLine($"Alimentation : {packet.Temp_alim:F2}");
sb.AppendLine($"Chargeur : {packet.Temp_cha:F2}");
sb.AppendLine($"Batterie : {packet.Temp_bat:F2}");
sb.AppendLine();
sb.AppendLine("-- Relais --");
sb.AppendLine();
sb.AppendLine($"1000W : {packet.S_PowRelay.ToAnsi()}");
sb.AppendLine($"120W : {packet.S_ChaRelay.ToAnsi()}");
sb.AppendLine($"Batterie-carte : {packet.S_BatRelay1.ToAnsi()}");
sb.AppendLine($"Batterie-puissance : {packet.S_BatRelay_State.ToAnsi()}");
sb.AppendLine();
sb.AppendLine("-- Défauts --");
sb.AppendLine();
sb.AppendLine($"Défaut général : {packet.DF_GENERAL.ToAnsi()}");
sb.AppendLine();
sb.AppendLine($"Température alim : {packet.DF_TEMP_ALIM.ToAnsi()} / {packet.MEM_DF_TEMP_ALIM.ToAnsi()}");
sb.AppendLine($"Température batterie : {packet.DF_TEMP_BAT.ToAnsi()} / {packet.MEM_DF_TEMP_BAT.ToAnsi()}");
sb.AppendLine($"Température chargeur : {packet.DF_TEMP_CHA.ToAnsi()} / {packet.MEM_DF_TEMP_CHA.ToAnsi()}");
sb.AppendLine($"Mesure tension incohérente : {packet.DF_V_INCOHERENT.ToAnsi()}");
sb.AppendLine($"Mauvaise cellule : {packet.DF_BAD_CELL.ToAnsi()}");
sb.AppendLine($"Surintensité : {packet.DF_OVERCURRENT.ToAnsi()}");
sb.AppendLine($"Surintensité critique : {packet.DF_OVERCURRENT_STOP.ToAnsi()}");
sb.AppendLine($"Stop général : {packet.DF_STOP_GENERAL.ToAnsi()}");
sb.AppendLine($"Surtension cellule : {packet.DF_CELL_OVERVOLTAGE.ToAnsi()} / {packet.MEM_DF_CELL_OVERVOLTAGE.ToAnsi()}");
sb.AppendLine($"Equilibrage : {packet.DF_UNBALANCE.ToAnsi()} / {packet.MEM_DF_UNBALANCE.ToAnsi()}");
sb.AppendLine();
sb.AppendLine("-- Informations --");
sb.AppendLine();
sb.AppendLine($"Décharge : {packet.DECHARGE.ToAnsi()} / {packet.MEM_DECHARGE.ToAnsi()}");
sb.AppendLine($"Décharge forcée : {packet.Flag_decharge.ToAnsi()}");
sb.AppendLine();
sb.AppendLine($"Buzzer stop : {packet.Buzzer_stop.ToAnsi()}");
sb.AppendLine($"Compteur demande coupure batterie : {packet.Compteur_demande_coupure_batterie}");
sb.AppendLine($"Compteur demande coupure totale : {packet.Compteur_demande_coupure_totale}");
Console.WriteLine(sb.ToString());
}
void DoInput()
{
while (true)
{
var line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
{
continue;
}
Console.WriteLine(">> " + line);
redis.GetSubscriber().Publish(commandsChannel, line);
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>linux-arm64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,12 @@
{
"MoniteurBaie": {
"Redis": {
"Endpoint": "mercedes.hbsha.re:6379",
"ClientName": "Console",
"Channels": {
"Packets": "batCtrlPackets",
"Commands": "batCtrlCommands"
}
}
}
}