////private static Settings s_settings; public static Settings GetSettings() { var databasePath = DatabaseApi.GetActiveCompetition(); ////if (s_settings != null) ////{ //// return s_settings; ////} lock (syncLock) { ////if (s_settings != null) ////{ //// return s_settings; ////} var settingsfile = Path.Combine(databasePath, "FeltAdmin.xml"); if (File.Exists(settingsfile)) { var mySerializer = new XmlSerializer(typeof(Settings)); using (var myFileStream = new FileStream(settingsfile, FileMode.Open)) { return((Settings)mySerializer.Deserialize(myFileStream)); } } return(new Settings()); } }
internal static void CheckTmpFile(OrionSetupViewModel orionViewModel) { foreach (var orion in orionViewModel.OrionViewModels) { var path = orion.CommunicationSetup.SelectedPath; if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { return; } var tmpBasePath = DatabaseApi.GetActiveCompetition(); var tmpPath = Path.Combine(tmpBasePath, string.Format("Orion_{0}", orion.OrionId)); var filenameTmp = Path.Combine(tmpPath, ToOrionDataTmp); if (File.Exists(filenameTmp)) { var updFile = Path.Combine(path, ToOrionUPD); if (!File.Exists(updFile)) { var filename = Path.Combine(path, ToOrionData); var fileMoveError = false; try { File.Move(filenameTmp, filename); } catch (Exception ex) { Log.Error(ex, "Unable to move file " + filenameTmp + " to " + filename); fileMoveError = true; } if (!fileMoveError) { try { using (FileStream file = new FileStream(updFile, FileMode.Create, System.IO.FileAccess.Write)) { file.Write(s_updFileContent, 0, s_updFileContent.Length); file.Flush(true); file.Close(); } } catch (Exception ex) { Log.Error(ex, "Unable to create file " + updFile); try { File.Move(filename, filenameTmp); } catch (Exception ex1) { Log.Error(ex1, "Unable to move back file after UPD file create error " + filename + ", " + filenameTmp); } } } } } } }
public static void SaveSettings(Settings settings) { var databasePath = DatabaseApi.GetActiveCompetition(); var settingsfile = Path.Combine(databasePath, "FeltAdmin.xml"); var ser = new XmlSerializer(typeof(Settings)); using (var writer = new StreamWriter(settingsfile)) { ser.Serialize(writer, settings); } }
public List <OrionResult> GetOrionResult(OrionViewModel orionViewModel) { if (this.CheckForNewFile() == false) { return(null); } var filename = GetFilename(); var updFile = this.GetUPDFilename(); var allLines = m_fileHandler.ReadAllLines(filename); var id = "NoId"; var result = new List <OrionResult>(); foreach (var line in allLines) { if (string.IsNullOrWhiteSpace(line)) { continue; } var or = OrionResult.ParseFromOrion(line, orionViewModel); id = or.OrionId.ToString(); if (or.ShooterId > 0) { DatabaseApi.Save(or); result.Add(or); } } var dbroot = DatabaseApi.GetActiveCompetition(); var backupPath = Path.Combine(dbroot, "Backup"); if (!Directory.Exists(backupPath)) { Directory.CreateDirectory(backupPath); } var bkupfilename = Path.Combine(backupPath, string.Format("{0}_Orion_{1}_kmonew.txt", DateTime.Now.ToString("yyyyMMdd-HHmmss"), id)); try { File.Move(filename, bkupfilename); File.Delete(updFile); } catch (Exception ex) { Log.Error(ex, "Unable to move to backup: " + filename); } return(result); }
public List <MinneRegistration> GetMinneRegistrations() { var filename = GetFilename(); var updFile = this.GetUPDFilename(); var allLines = m_fileHandler.ReadAllLines(filename); var result = new Dictionary <string, MinneRegistration>(); foreach (var line in allLines) { var lp = this.GetMinneRegistrationFromLine(line); if (result.ContainsKey(lp.Key)) { result.Remove(lp.Key); } result.Add(lp.Key, lp); } var dbroot = DatabaseApi.GetActiveCompetition(); var backupPath = Path.Combine(dbroot, "Backup"); if (!Directory.Exists(backupPath)) { Directory.CreateDirectory(backupPath); } var bkupfilename = Path.Combine(backupPath, string.Format("{0}_Minne_kminew.txt", DateTime.Now.ToString("yyyyMMdd-hhmmss"))); try { File.Move(filename, bkupfilename); File.Delete(updFile); } catch (Exception ex) { Log.Error(ex, "Unable to move to backup: " + filename); } return(result.Values.ToList()); }
private static void WriteAllRegistrationsToOrion(string selectedPath, IEnumerable <OrionRegistration> allRegistrationsForThisOrion, int orionId) { var tmpBasePath = DatabaseApi.GetActiveCompetition(); var tmpPath = Path.Combine(tmpBasePath, string.Format("Orion_{0}", orionId)); if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } var filenameTmp = Path.Combine(tmpPath, ToOrionDataTmp); var allLinesForThisOrion = new List <string>(); foreach (var orionRegistration in allRegistrationsForThisOrion) { var line = orionRegistration.OrionInput; allLinesForThisOrion.Add(line); } // Feilhåndtering her, når maskin ikke er tilgjengelig.... File.AppendAllLines(filenameTmp, allLinesForThisOrion, Encoding.GetEncoding("ISO-8859-1")); ////var updFile = Path.Combine(selectedPath, ToOrionUPD); ////if (!File.Exists(updFile)) ////{ //// var filename = Path.Combine(selectedPath, ToOrionData); //// File.Move(filenameTmp, filename); //// using (FileStream file = new FileStream(updFile, FileMode.Create, System.IO.FileAccess.Write)) //// { //// file.Write(s_updFileContent, 0, s_updFileContent.Length); //// file.Flush(true); //// file.Close(); //// } ////} }
public static void WriteLeonResults(List <int> finishedShooters, List <OrionResult> orionResults, OrionSetupViewModel orionSetup, List <LeonPerson> leonPersons, List <MinneRegistration> minnePersons) { var tmpBasePath = DatabaseApi.GetActiveCompetition(); var tmpPath = Path.Combine(tmpBasePath, "LeonTmp"); var tmpPathMinne = Path.Combine(tmpBasePath, "MinneLeonTemp"); if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } if (!Directory.Exists(tmpPathMinne)) { Directory.CreateDirectory(tmpPathMinne); } var leonResultsFelt = new List <string>(); var leonResultsBane = new List <string>(); MinneRegistration minnePerson = null; LeonPerson leonPerson = null; foreach (var finishedShooter in finishedShooters) { var allResultsForShooter = orionResults.Where(o => o.ShooterId == finishedShooter); leonPerson = leonPersons.SingleOrDefault(l => l.ShooterId == finishedShooter); if (minnePersons != null) { minnePerson = minnePersons.SingleOrDefault(l => l.ShooterId == finishedShooter); } int sumFelt = 0; int sumBane = 0; List <string> allFeltSeries = new List <string>(); List <string> allMinneSeries = new List <string>(); foreach (var orion in orionSetup.OrionViewModels) { foreach (var rangeViewModel in orion.RangeViews) { if (rangeViewModel.RangeType == RangeType.Shooting) { var resultForThisRange = CalculateOrionAndRange.GetResultForThisRange(allResultsForShooter, orion, rangeViewModel); if (resultForThisRange == null) { if (rangeViewModel.ResultType == ResultType.Felt) { allFeltSeries.Add(string.Empty); } } else { if (resultForThisRange.ResultType == ResultType.Felt) { sumFelt += resultForThisRange.GetSum(); allFeltSeries.AddRange(resultForThisRange.Series); } else if (resultForThisRange.ResultType == ResultType.Bane) { sumBane += resultForThisRange.GetSum(); allMinneSeries.AddRange(resultForThisRange.Series); } } } } } ////foreach (var orionResult in allResultsForShooter) ////{ //// if (orionResult.ResultType == ResultType.Felt) //// { //// sumFelt += orionResult.GetSum(); //// allFeltSeries.AddRange(orionResult.Series); //// } //// else if (orionResult.ResultType == ResultType.Bane) //// { //// sumBane += orionResult.GetSum(); //// allMinneSeries.AddRange(orionResult.Series); //// } ////} if (allFeltSeries.Any() && leonPerson != null) { var allShots = string.Join(";", allFeltSeries).ToUpper(); var leonLine = string.Format( "{0};{1};{2};{3};{4};{5};{6};{7};{8}", leonPerson.Range, leonPerson.Team, leonPerson.Target, leonPerson.ShooterId, leonPerson.Name, leonPerson.ClubName, leonPerson.Class, sumFelt, allShots); leonResultsFelt.Add(leonLine); } if (allMinneSeries.Any() && minnePerson != null) { var allShots = string.Join(";", allMinneSeries).ToUpper(); var leonLine = string.Format( "{0};{1};{2};{3};{4};{5};{6};{7};{8}", minnePerson.Range, minnePerson.Team, minnePerson.Target, minnePerson.ShooterId, minnePerson.Name, minnePerson.ClubName, minnePerson.Class, sumBane, allShots); leonResultsBane.Add(leonLine); } var finishedPerson = new FinishedPerson { Name = leonPerson.Name, ShooterId = leonPerson.ShooterId, Target = leonPerson.Target, Team = leonPerson.Team }; DatabaseApi.Save(finishedPerson); } if (leonResultsFelt.Any()) { var filenameTmp = Path.Combine(tmpPath, ToLeonDataTmp); File.AppendAllLines(filenameTmp, leonResultsFelt, Encoding.GetEncoding("ISO-8859-1")); } if (leonResultsBane.Any()) { var filenameTmp = Path.Combine(tmpPathMinne, ToLeonDataTmp); File.AppendAllLines(filenameTmp, leonResultsBane, Encoding.GetEncoding("ISO-8859-1")); } }
internal static void CheckTmpFile(string path, bool minne = false) { if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { return; } var tmpBasePath = DatabaseApi.GetActiveCompetition(); string tmpDir; if (minne == false) { tmpDir = "LeonTmp"; } else { tmpDir = "MinneLeonTemp"; } var tmpPath = Path.Combine(tmpBasePath, tmpDir); var filenameTmp = Path.Combine(tmpPath, ToLeonDataTmp); if (File.Exists(filenameTmp)) { var updFile = Path.Combine(path, ToLeonUPD); if (!File.Exists(updFile)) { var filename = Path.Combine(path, ToLeonData); var fileMoveError = false; try { File.Move(filenameTmp, filename); } catch (Exception ex) { Log.Error(ex, "Unable to move file " + filenameTmp + " to " + filename); fileMoveError = true; } if (!fileMoveError) { try { using (FileStream file = new FileStream(updFile, FileMode.Create, System.IO.FileAccess.Write)) { file.Write(s_updFileContent, 0, s_updFileContent.Length); file.Flush(true); file.Close(); } } catch (Exception ex) { Log.Error(ex, "Unable to create file " + updFile); try { File.Move(filename, filenameTmp); } catch (Exception ex1) { Log.Error(ex1, "Unable to move back file after UPD file create error " + filename + ", " + filenameTmp); } } } } } }
public static void WriteLeonResults( List <int> finishedShooters, List <OrionResult> orionResults, OrionSetupViewModel orionSetup, List <LeonPerson> leonPersons, List <MinneRegistration> minnePersons) { var tmpBasePath = DatabaseApi.GetActiveCompetition(); var tmpPath = Path.Combine(tmpBasePath, "LeonTmp"); var tmpPathMinne = Path.Combine(tmpBasePath, "MinneLeonTemp"); if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } if (!Directory.Exists(tmpPathMinne)) { Directory.CreateDirectory(tmpPathMinne); } var leonResultsFelt = new List <string>(); var leonResultsBane = new List <string>(); MinneRegistration minnePerson = null; LeonPerson leonPerson = null; foreach (var finishedShooter in finishedShooters) { var allResultsForShooter = orionResults.Where(o => o.ShooterId == finishedShooter); leonPerson = leonPersons.SingleOrDefault(l => l.ShooterId == finishedShooter); if (minnePersons != null) { minnePerson = minnePersons.SingleOrDefault(l => l.ShooterId == finishedShooter); } int sumFelt = 0; int sumBane = 0; List <string> allFeltSeries = new List <string>(); List <string> allMinneSeries = new List <string>(); var allRanges = orionSetup.OrionViewModels.SelectMany(o => o.RangeViews).OrderBy(r => r.RangeId); ////foreach (var orion in orionSetup.OrionViewModels) ////{ foreach (var rangeViewModel in allRanges) { if (rangeViewModel.RangeType == RangeType.Shooting) { var resultForThisRange = CalculateOrionAndRange.GetResultForThisRange( allResultsForShooter, rangeViewModel.Parent, rangeViewModel); if (resultForThisRange == null) { if (rangeViewModel.ResultType == ResultType.Felt) { allFeltSeries.Add(string.Empty); } } else { if (resultForThisRange.ResultType == ResultType.Felt) { sumFelt += resultForThisRange.GetSum(); resultForThisRange.ValidSeries = resultForThisRange.CalculateValidSeriesForRange(rangeViewModel); allFeltSeries.AddRange(resultForThisRange.ValidSeries); } else if (resultForThisRange.ResultType == ResultType.Bane) { sumBane += resultForThisRange.GetSum(); foreach (var rawSerie in resultForThisRange.Series) { int len = rangeViewModel.CountingShoots; if (rawSerie.Length < rangeViewModel.CountingShoots) { len = rawSerie.Length; } string serieSingle = rawSerie.Substring(0, len); int slen = serieSingle.Length; while (slen < rangeViewModel.CountingShoots) { serieSingle = serieSingle + "0"; slen++; } allMinneSeries.Add(serieSingle); } } } } } ////} ////foreach (var orionResult in allResultsForShooter) ////{ //// if (orionResult.ResultType == ResultType.Felt) //// { //// sumFelt += orionResult.GetSum(); //// allFeltSeries.AddRange(orionResult.Series); //// } //// else if (orionResult.ResultType == ResultType.Bane) //// { //// sumBane += orionResult.GetSum(); //// allMinneSeries.AddRange(orionResult.Series); //// } ////} if (allFeltSeries.Any() && leonPerson != null) { var allShots = string.Join(";", allFeltSeries).ToUpper(); var leonLine = string.Format( "{0};{1};{2};{3};{4};{5};{6};{7};{8};", leonPerson.Range, leonPerson.Team, leonPerson.Target, leonPerson.ShooterId, leonPerson.Name, leonPerson.ClubName, leonPerson.Class, sumFelt, allShots); leonResultsFelt.Add(leonLine); } if (allMinneSeries.Any() && minnePerson != null) { var allShots = string.Join(";", allMinneSeries).ToUpper(); var leonLine = string.Format( "{0};{1};{2};{3};{4};{5};{6};{7};{8};", minnePerson.Range, minnePerson.Team, minnePerson.Target, minnePerson.ShooterId, minnePerson.Name, minnePerson.ClubName, minnePerson.Class, sumBane, allShots); leonResultsBane.Add(leonLine); } var finishedPerson = new FinishedPerson { Name = leonPerson.Name, ShooterId = leonPerson.ShooterId, Target = leonPerson.Target, Team = leonPerson.Team }; DatabaseApi.Save(finishedPerson); } lock (syncObject) { if (leonResultsFelt.Any()) { //var writefilenameTmp = Path.Combine(tmpPath, "WRITE"+ToLeonDataTmp); var filenameTmp = Path.Combine(tmpPath, ToLeonDataTmp); File.AppendAllLines(filenameTmp, leonResultsFelt, Encoding.GetEncoding("ISO-8859-1")); //if (File.Exists(filenameTmp)) //{ // Log.Error("File alredy exsist deliting {0}", filenameTmp); // File.Delete(filenameTmp); //} //File.Move(writefilenameTmp, filenameTmp); } if (leonResultsBane.Any()) { //var writefilenameTmp = Path.Combine(tmpPathMinne, "WRITE"+ToLeonDataTmp); var filenameTmp = Path.Combine(tmpPathMinne, ToLeonDataTmp); File.AppendAllLines(filenameTmp, leonResultsBane, Encoding.GetEncoding("ISO-8859-1")); //if (File.Exists(filenameTmp)) //{ // Log.Error("File alredy exsist deliting {0}", filenameTmp); // File.Delete(filenameTmp); //} //File.Move(writefilenameTmp, filenameTmp); } } }
internal static bool CheckTmpFile(string outputPath, string tmpDir) { if (string.IsNullOrWhiteSpace(outputPath) || !Directory.Exists(outputPath)) { return(false); } lock (syncObject) { var tmpBasePath = DatabaseApi.GetActiveCompetition(); var tmpPath = Path.Combine(tmpBasePath, tmpDir); var filenameTmp = Path.Combine(tmpPath, ToLeonDataTmp); if (!File.Exists(filenameTmp)) { return(false); } var updFile = Path.Combine(outputPath, ToLeonUPD); if (!File.Exists(updFile)) { var filename = Path.Combine(outputPath, ToLeonData); var backupDirName = "ToLeonBackup"; var backupDir = Path.Combine(tmpBasePath, backupDirName); var backupFilename = Path.Combine(backupDir, string.Format("{0}_Leon_kmonew.txt", DateTime.Now.ToString("yyyyMMdd-HHmmss"))); try { if (!Directory.Exists(backupDir)) { Directory.CreateDirectory(backupDir); } File.Copy(filenameTmp, backupFilename); } catch (Exception exCopy) { Log.Error(exCopy, "Unable to copy file " + filenameTmp + " to " + filename); } var fileMoveError = false; try { File.Move(filenameTmp, filename); } catch (Exception ex) { Log.Error(ex, "Unable to move file " + filenameTmp + " to " + filename); fileMoveError = true; } if (!fileMoveError) { try { using (FileStream file = new FileStream(updFile, FileMode.Create, System.IO.FileAccess.Write)) { file.Write(s_updFileContent, 0, s_updFileContent.Length); file.Flush(true); file.Close(); } return(true); } catch (Exception ex) { Log.Error(ex, "Unable to create file " + updFile); try { File.Move(filename, filenameTmp); } catch (Exception ex1) { Log.Error(ex1, "Unable to move back file after UPD file create error " + filename + ", " + filenameTmp); } } } } } return(false); }