-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move to really separate TLUpdate.exe
- Loading branch information
1 parent
51159e5
commit e76d3ec
Showing
5 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters