From e76d3ecab170ff266043e9844454e5d18a5ab24b Mon Sep 17 00:00:00 2001 From: Flo Date: Wed, 22 May 2024 18:14:26 +0200 Subject: [PATCH] move to really separate TLUpdate.exe --- TLUpdate/Program.cs | 31 +++++- TLUpdate/TLUpdate.csproj | 3 +- TLUpdate/Tools.cs | 158 +++++++++++++++++++++++++++++++ TeslaLogger/Program.cs | 25 ----- TeslaLogger/UpdateTeslalogger.cs | 2 +- 5 files changed, 187 insertions(+), 32 deletions(-) create mode 100644 TLUpdate/Tools.cs diff --git a/TLUpdate/Program.cs b/TLUpdate/Program.cs index beaf13c36..bfd406d05 100644 --- a/TLUpdate/Program.cs +++ b/TLUpdate/Program.cs @@ -1,15 +1,36 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.IO; namespace TLUpdate { internal class Program { - static void Main(string[] args) + static void Main(string[] _) { + Console.WriteLine(" *** TLUpdate MAIN ***"); + try + { + Tools.CopyFilesRecursively(new DirectoryInfo("/etc/teslalogger/git/TeslaLogger/bin"), new DirectoryInfo("/etc/teslalogger"), "TeslaLogger.exe"); + + Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/TeslaLogger.exe", "/etc/teslalogger/TeslaLogger.exe"); + + Console.WriteLine("End update"); + + if (Tools.IsMono()) + { + Console.WriteLine("Rebooting ..."); + Tools.ExecMono("reboot", ""); + } + else + { + Console.WriteLine("Restarting ..."); + Environment.Exit(0); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } } } } diff --git a/TLUpdate/TLUpdate.csproj b/TLUpdate/TLUpdate.csproj index e9cd6412e..78a7f893f 100644 --- a/TLUpdate/TLUpdate.csproj +++ b/TLUpdate/TLUpdate.csproj @@ -1,5 +1,5 @@  - + Debug @@ -46,6 +46,7 @@ + diff --git a/TLUpdate/Tools.cs b/TLUpdate/Tools.cs new file mode 100644 index 000000000..614d09213 --- /dev/null +++ b/TLUpdate/Tools.cs @@ -0,0 +1,158 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using System.Text; + +namespace TLUpdate +{ + public class Tools + { + public Tools() + { + } + + public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target, string excludeFile = null) + { + if (source != null && target != null) + { + try + { + foreach (DirectoryInfo dir in source.GetDirectories()) + { + CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name)); + } + + foreach (FileInfo file in source.GetFiles()) + { + if (excludeFile != null && file.Name == excludeFile) + { + Console.WriteLine($"CopyFilesRecursively: skip {excludeFile}"); + } + else + { + string p = Path.Combine(target.FullName, file.Name); + Console.WriteLine("Copy '" + file.FullName + "' to '" + p + "'"); + File.Copy(file.FullName, p, true); + } + } + } + catch (Exception ex) + { + Console.WriteLine("CopyFilesRecursively Exception: " + ex.ToString()); + } + } + else + { + Console.WriteLine($"CopyFilesRecursively: source or target is null - source:{source} target:{target}"); + } + } + + public static void CopyFile(string srcFile, string directory) + { + try + { + Console.WriteLine("Copy '" + srcFile + "' to '" + directory + "'"); + File.Copy(srcFile, directory, true); + } + catch (Exception ex) + { + Console.WriteLine("CopyFile Exception: " + ex.ToString()); + } + } + + public static string ExecMono(string cmd, string param, bool logging = true, bool stderr2stdout = false, int timeout = 0) + { + Console.WriteLine("Exec_mono: " + cmd + " " + param); + + StringBuilder sb = new StringBuilder(); + + bool bTimeout = false; + + try + { + if (!Tools.IsMono()) + { + return ""; + } + + using (Process proc = new Process()) + { + proc.EnableRaisingEvents = false; + proc.StartInfo.UseShellExecute = false; + proc.StartInfo.RedirectStandardOutput = true; + proc.StartInfo.RedirectStandardError = true; + proc.StartInfo.FileName = cmd; + proc.StartInfo.Arguments = param; + + proc.Start(); + + do + { + if (!proc.HasExited) + { + proc.Refresh(); + + if (timeout > 0 && (DateTime.Now - proc.StartTime).TotalSeconds > timeout) + { + proc.Kill(); + bTimeout = true; + } + } + } + while (!proc.WaitForExit(100)); + + string line = proc.StandardOutput.ReadToEnd().Replace('\r', '\n'); + + if (logging && line.Length > 0) + { + Console.WriteLine(" " + line); + } + + sb.AppendLine(line); + line = proc.StandardError.ReadToEnd().Replace('\r', '\n'); + + if (logging && line.Length > 0) + { + if (stderr2stdout) + { + Console.WriteLine(" " + line); + } + else + { + Console.WriteLine("Error: " + line); + } + } + + sb.AppendLine(line); + } + } + catch (Exception ex) + { + Console.WriteLine("Exception " + cmd + " " + ex.Message); + return "Exception"; + } + return bTimeout ? "Timeout! " + sb.ToString() : sb.ToString(); + } + + public static bool IsMono() + { + return GetMonoRuntimeVersion() != "NULL"; + } + + public static string GetMonoRuntimeVersion() + { + Type type = Type.GetType("Mono.Runtime"); + if (type != null) + { + MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); + if (displayName != null) + { + return displayName.Invoke(null, null).ToString(); + } + } + return "NULL"; + } + } +} + diff --git a/TeslaLogger/Program.cs b/TeslaLogger/Program.cs index 9d7697a55..263179690 100644 --- a/TeslaLogger/Program.cs +++ b/TeslaLogger/Program.cs @@ -31,31 +31,6 @@ public enum TLMemCacheKey private static void Main(string[] _) { - // TLUpdate.exe main - if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.Equals("TLUpdate")) - { - Console.WriteLine(" *** TLUpdate MAIN ***"); - try - { - // do not kill Tools.ExecMono("pkill", "TeslaLogger.exe"); - - Tools.CopyFilesRecursively(new DirectoryInfo("/etc/teslalogger/git/TeslaLogger/bin"), new DirectoryInfo("/etc/teslalogger"), "TeslaLogger.exe"); - - Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/TeslaLogger.exe", "/etc/teslalogger/TeslaLogger.exe"); - - Console.WriteLine("End update"); - - Console.WriteLine("Rebooting"); - - Tools.ExecMono("reboot", ""); - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } - } - - // TeslaLogger.exe main try { try diff --git a/TeslaLogger/UpdateTeslalogger.cs b/TeslaLogger/UpdateTeslalogger.cs index 69b006ee9..6e8d4620f 100644 --- a/TeslaLogger/UpdateTeslalogger.cs +++ b/TeslaLogger/UpdateTeslalogger.cs @@ -1453,7 +1453,7 @@ public static async void DownloadUpdateAndInstall() { try { - Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/TeslaLogger.exe", "/etc/teslalogger/TLUpdate.exe"); + Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/TLUpdate.exe", "/etc/teslalogger/TLUpdate.exe"); foreach (Car car in Car.Allcars) { car.CurrentJSON.ToKVS();