//public async Task SaveAsync() //{ // this.BBSDataArray = BBSDataList.ToArray(); // StorageFolder localFolder = ApplicationData.Current.LocalFolder; // try // { // var storageItem = await localFolder.CreateFileAsync(bbsFileName); // if (storageItem != null) // { // using (StreamWriter writer = new StreamWriter(new FileStream(storageItem.Path, FileMode.OpenOrCreate))) // { // XmlSerializer serializer = new XmlSerializer(typeof(BBSDefinitions)); // serializer.Serialize(writer, this); // } // } // else // { // _logHelper.Log(LogLevel.Error, $"File not found {bbsFileName}"); // } // } // catch (Exception e) // { // _logHelper.Log(LogLevel.Error, $"Error saving {bbsFileName} {e}"); // } //} public static async void SaveAsync(BBSDefinitions bBSDefinitions) { StorageFolder localFolder = ApplicationData.Current.LocalFolder; try { var storageItem = await localFolder.CreateFileAsync(bbsFileName, CreationCollisionOption.ReplaceExisting); if (storageItem != null) { using (StreamWriter writer = new StreamWriter(new FileStream(storageItem.Path, FileMode.Create))) { XmlSerializer serializer = new XmlSerializer(typeof(BBSDefinitions)); serializer.Serialize(writer, bBSDefinitions); } } else { _logHelper.Log(LogLevel.Error, $"File not found {bbsFileName}"); } } catch (Exception e) { _logHelper.Log(LogLevel.Error, $"Error saving {bbsFileName} {e}"); } }
public async Task OpenAsync() { StorageFolder localFolder = ApplicationData.Current.LocalFolder; try { StorageFile bbsDataFile; ulong size = 0; var storageItem = await localFolder.TryGetItemAsync(bbsFileName); if (storageItem != null) { Windows.Storage.FileProperties.BasicProperties basicProperties = await storageItem.GetBasicPropertiesAsync(); size = basicProperties.Size; } if (storageItem is null || size == 0) { // Copy the file from the install folder to the local folder var assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); bbsDataFile = await assetsFolder.GetFileAsync(bbsFileName); if (bbsDataFile != null) { await bbsDataFile.CopyAsync(localFolder, bbsFileName, NameCollisionOption.ReplaceExisting); } } bbsDataFile = await localFolder.GetFileAsync(bbsFileName); using (FileStream reader = new FileStream(bbsDataFile.Path, FileMode.Open)) { XmlSerializer serializer = new XmlSerializer(typeof(BBSDefinitions)); _instance = (BBSDefinitions)serializer.Deserialize(reader); } } catch (FileNotFoundException e) { _logHelper.Log(LogLevel.Error, $"Open BBSData file failed: {e.Message}"); } catch (Exception e) { _logHelper.Log(LogLevel.Error, $"Error opening {e.Message} {e}"); } }
public static async Task CreateFromBulletinAsync(PacketMessage bbsBulletin) { string[] subjectElements = bbsBulletin.Subject.Split(' ', StringSplitOptions.RemoveEmptyEntries); string version = subjectElements[3]; string bulletin = bbsBulletin?.MessageBody; if (bulletin is null) { return; } try { int start = bulletin.IndexOf("W1XSC"); int stop = bulletin.IndexOf('*', start); stop = bulletin.IndexOf('\r', stop); string bbsInfo = bulletin.Substring(start, stop - start); BBSData bbsData; var lines = bbsInfo.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); int bbsCount = lines.Length; BBSData[] bbsdataArray = new BBSData[lines.Length + 1]; for (int i = 0; i < lines.Length; i++) { var bbs = lines[i].Split(new char[] { ',', ' ', '\t', '*' }, StringSplitOptions.RemoveEmptyEntries); bbsData = new BBSData(); bbsData.Name = bbs[0]; bbsData.ConnectName = bbs[1]; bbsData.Frequency1 = bbs[2]; bbsData.Frequency2 = bbs[3]; if (bbs.Length > 4) { bbsData.Frequency3 = bbs[4]; } bbsdataArray[i] = bbsData; } // Add location start = bulletin.IndexOf("W1XSC", stop); bbsInfo = bulletin.Substring(start); lines = bbsInfo.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < bbsCount; i++) { var callsign = lines[i].Split(new string[] { ",", " ", "\t" }, StringSplitOptions.RemoveEmptyEntries); bbsdataArray[i].Description = callsign[1]; } // Add training BBS bbsData = new BBSData(); bbsData.Name = "W5XSC"; bbsData.ConnectName = "W5XSC-1"; bbsData.Description = "Used for training and testing"; bbsData.Frequency1 = "144.910"; bbsData.Frequency3 = "433.450"; bbsdataArray[bbsCount] = bbsData; BBSDefinitions bbsDefinitions = new BBSDefinitions(); bbsDefinitions.RevisionTime = bbsBulletin.JNOSDate; bbsDefinitions.BBSDataArray = bbsdataArray; BBSDefinitions.SaveAsync(bbsDefinitions); // Also save to Assets. Make a copy first StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile bbsDataFile = await localFolder.GetFileAsync(bbsFileName); if (bbsDataFile != null) { StorageFolder assetsFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets"); StorageFile storageFile = await assetsFolder.GetFileAsync(bbsFileName); string bbsCopyFileName = bbsFileName + " - Copy"; await storageFile.CopyAsync(assetsFolder, bbsCopyFileName, NameCollisionOption.ReplaceExisting); await bbsDataFile.CopyAsync(assetsFolder, bbsFileName, NameCollisionOption.ReplaceExisting); } } catch (Exception e) { _logHelper.Log(LogLevel.Error, $"Error in CreateFromBulletinAsync(): {e.Message}"); } }