diff --git a/Models/BaseStationSettings.cs b/Models/BaseStationSettings.cs index 1a46d10..3b2ca29 100644 --- a/Models/BaseStationSettings.cs +++ b/Models/BaseStationSettings.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using ArduinoUploader.Hardware; @@ -18,6 +19,8 @@ public struct Config public List SupportedBoards { get; set; } public List SupportedMotorShields { get; set; } public string DisplayName { get; set; } + + } public struct Dependency @@ -26,6 +29,8 @@ public struct Dependency public string Link { get; set; } public string FileName { get; set; } + + public string checksum { get; set; } } public static class BaseStationSettings @@ -41,7 +46,7 @@ public static class BaseStationSettings Dependencies = new List() { new Dependency{Name = "platformio", FileName = "get-platformio.py", Link = "https://raw.githubusercontent.com/platformio/platformio/develop/scripts/get-platformio.py" }, - new Dependency{Name = "python", FileName = "python.zip", Link = "https://www.python.org/ftp/python/3.8.2/python-3.8.2-embed-amd64.zip" } + new Dependency{Name = "python", FileName = "python.zip", Link = "https://www.python.org/ftp/python/3.8.2/python-3.8.2-embed-amd64.zip", checksum = "1a98565285491c0ea65450e78afe6f8d" } }, InitCommand = "platformio init", BuildCommand = "platformio run", @@ -63,10 +68,10 @@ public static class BaseStationSettings { Name = "BaseStationClassic", DisplayName = "Base Station Classic", - Git = "https://github.com/DCC-EX/BaseStationClassic.git", + Git = "https://github.com/DCC-EX/BaseStation-Classic.git", Dependencies = new List() { - new Dependency{ Name = "arduino-1.8.12", FileName = "arduino-1.8.12.zip", Link = "https://downloads.arduino.cc/arduino-1.8.12-windows.zip" } + new Dependency{ Name = "arduino-1.8.12", FileName = "arduino-1.8.12.zip", Link = "https://downloads.arduino.cc/arduino-1.8.12-windows.zip", checksum = "92471d21a38c33a8095dc243cd7e8e28" } }, //BuildCommand = $@"{ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\arduino-builder -compile -logger=machine -hardware { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware -tools { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\tools-builder -tools { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -built-in-libraries { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\libraries -fqbn=arduino:avr:uno -ide-version=10812 -build-path { Directory.GetCurrentDirectory()}\BaseStationClassic\Build -warnings=all -build-cache { Directory.GetCurrentDirectory()}\BaseStationClassic\Cache -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino5.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.avrdude.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -verbose { Directory.GetCurrentDirectory()}\BaseStationClassic\DCCpp\DCCpp.ino", BuildCommand = $@"-compile -logger=machine -hardware { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware -tools { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\tools-builder -tools { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -built-in-libraries { Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\libraries -fqbn={{0}} -ide-version=10812 -build-path { Directory.GetCurrentDirectory()}\BaseStationClassic\Build -warnings=all -build-cache { Directory.GetCurrentDirectory()}\BaseStationClassic\Cache -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino5.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.arduinoOTA-1.3.0.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.avrdude.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -prefs=runtime.tools.avrdude-6.3.0-arduino17.path={ Directory.GetCurrentDirectory()}\arduino-1.8.12\arduino-1.8.12\hardware\tools\avr -verbose { Directory.GetCurrentDirectory()}\BaseStationClassic\DCCpp\DCCpp.ino", @@ -124,5 +129,17 @@ public static string GetWiringDiagram(ArduinoModel model, MotorShieldType type) } return wiringDiagram; } + + public static string CalculateMD5(string filename) + { + using (var md5 = MD5.Create()) + { + using (var stream = File.OpenRead(filename)) + { + var hash = md5.ComputeHash(stream); + return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); + } + } + } } } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 7fa5886..bb52dd6 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -51,5 +51,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")] diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 9d80bb6..7bf2a9a 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -18,7 +18,7 @@ namespace BaseStationInstaller.ViewModels { - public class MainWindowViewModel : ViewModelBase + public class MainWindowViewModel : ViewModelBase, IArduinoUploaderLogger { public MainWindowViewModel() { @@ -327,20 +327,28 @@ private async Task DownloadPreReqs() { if (!Directory.Exists($@".\{dep.Name}") && dep.FileName.Contains(".zip")) { - ZipFile.ExtractToDirectory($@"./{dep.FileName}", $@"./{dep.Name}"); + if (dep.checksum.Equals(BaseStationSettings.CalculateMD5($@"./{dep.Name}"))) { + ZipFile.ExtractToDirectory($@"./{dep.FileName}", $@"./{dep.Name}"); + } + else + { + File.Delete($@"./{dep.Name}"); + Busy = false; + RefreshingPorts = false; + Thread.Sleep(5000); + Status = "Prequisite download failed please try again"; + CommandManager.InvalidateRequerySuggested(); + return; + } } } } Status = $"Gitting {SelectedConfig.DisplayName}"; - await GitCode(); if (SelectedConfig.Name == "BaseStation") { GetPlatformIO(); } - else if (SelectedConfig.Name == "BaseStationClassic") - { - CompileSketch(); - } + await GitCode(); } private void GetPlatformIO() @@ -369,6 +377,15 @@ private void GetPlatformIO() private void CompileSketch() { + Status = "Changing MotorShield options"; + Progress = 5; + string[] config = File.ReadAllLines(@".\BaseStationClassic\DCCpp\Config.h"); + Progress = 10; + config[16] = $"#define MOTOR_SHIELD_TYPE {(int)SelectedMotorShield.ShieldType}"; + Progress = 15; + File.WriteAllLines(@".\BaseStationClassic\DCCpp\Config.h", config); + Progress = 20; + Thread.Sleep(1000); Status = "Compiling Base Station Classic Sketch"; if (!Directory.Exists(@".\BaseStationClassic\Build")) @@ -387,38 +404,38 @@ private void CompileSketch() start.WindowStyle = ProcessWindowStyle.Hidden; Process process = new Process(); process.StartInfo = start; - //process.Start(); - //process.WaitForExit(); + process.Start(); + process.WaitForExit(); Progress = 50; start.Arguments = String.Format($@"{SelectedConfig.BuildCommand}", SelectedBoard.FQBN); Console.WriteLine(start.Arguments); - //process.Start(); - //process.WaitForExit(); + process.Start(); + process.WaitForExit(); Status = $"Compilation Complete"; RefreshingPorts = true; Thread.Sleep(1000); Status = $"Uploading to {SelectedComPort}"; Progress = 75; + if (File.Exists(@"./upload.log")) + { + File.Delete(@"./upload.log"); + } ArduinoSketchUploader uploader = new ArduinoSketchUploader( new ArduinoSketchUploaderOptions() { FileName = $@"{Directory.GetCurrentDirectory()}\BaseStationClassic\Build\DCCpp.ino.hex", PortName = SelectedComPort, ArduinoModel = SelectedBoard.Platform - }); + },this); try { uploader.UploadSketch(); - Status = "Upload Completed Successfully"; + Status = "Upload Completed Successfully. Please check upload.log for more details"; } catch (Exception e) { - Status = "Upload Failed!!! Please check upload.log for more details"; - if (File.Exists(@"./upload.log")) - { - File.Delete(@"./upload.log"); - } - File.WriteAllText(@"./upload.log", $"Message: {e.Message} {Environment.NewLine} StackTrace: {e.StackTrace}"); + Status = "Upload Failed!!! Please check upload.log for more details"; + File.AppendAllText(@"./upload.log", $"Message: {e.Message} {Environment.NewLine} StackTrace: {e.StackTrace}"); } Progress = 100; Thread.Sleep(1000); @@ -449,19 +466,69 @@ private void Client_DownloadFileCompleted(object sender, System.ComponentModel.A private async Task GitCode() { + CloneOptions options = new CloneOptions(); + options.RepositoryOperationCompleted = new LibGit2Sharp.Handlers.RepositoryOperationCompleted(GotCode); Progress = 0; if (!Directory.Exists($"./{SelectedConfig.Name}")) { - Repository.Clone(SelectedConfig.Git, $"./{SelectedConfig.Name}"); + Repository.Clone(SelectedConfig.Git, $"./{SelectedConfig.Name}", options); + } else + { + if (!Repository.IsValid($"./{SelectedConfig.Name}") ) + { + Directory.Delete($"./{SelectedConfig.Name}",true); + Repository.Clone(SelectedConfig.Git, $"./{SelectedConfig.Name}", options); + } + else + { + CompileSketch(); + } } } + private void GotCode(RepositoryOperationContext context) + { + if (SelectedConfig.Name.Equals("BaseStationClassic")) { + CompileSketch(); + } + } + private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { Progress = e.ProgressPercentage; } - #endregion + public void Error(string message, Exception e) + { + File.AppendAllText(@"./upload.log", $"Message: {e.Message} {Environment.NewLine} StackTrace: {e.StackTrace}"); + } + + public void Warn(string message) + { + File.AppendAllText(@"./upload.log", $"{message}{Environment.NewLine}"); + } + + public void Info(string message) + { + Status = message; + File.AppendAllText(@"./upload.log", $"{message}{Environment.NewLine}"); + } + + public void Debug(string message) + { +#if DEBUG + File.AppendAllText(@"./upload.log", $"{message}"); +#endif + } + + public void Trace(string message) + { +#if DEBUG + File.AppendAllText(@"./upload.log", $"{message}"); +#endif + } + +#endregion } }