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("Channels:Commands")!; using var redis = ConnectionMultiplexer.Connect(redisConfig.GetValue("Endpoint")!, opts => { opts.ClientName = redisConfig.GetValue("ClientName"); }); var packetChannel = redis.GetSubscriber().Subscribe(redisConfig.GetValue("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); } }