Skip to content

Commit

Permalink
move to really separate TLUpdate.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
superfloh247 committed May 22, 2024
1 parent 51159e5 commit e76d3ec
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 32 deletions.
31 changes: 26 additions & 5 deletions TLUpdate/Program.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
3 changes: 2 additions & 1 deletion TLUpdate/TLUpdate.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -46,6 +46,7 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tools.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
158 changes: 158 additions & 0 deletions TLUpdate/Tools.cs
Original file line number Diff line number Diff line change
@@ -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";
}
}
}

25 changes: 0 additions & 25 deletions TeslaLogger/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion TeslaLogger/UpdateTeslalogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e76d3ec

Please sign in to comment.