public override async Task <List <string> > GetFilesOnDevice(bool refresh = true, int timeoutInMs = 30000) { if (SerialPort == null) { throw new Exception("SerialPort not intialized"); } if (FilesOnDevice.Count == 0 || refresh == true) { var timeOutTask = Task.Delay(timeoutInMs); EventHandler <MeadowMessageEventArgs> handler = null; var tcs = new TaskCompletionSource <bool>(); bool started = false; handler = (s, e) => { if (e.MessageType == MeadowMessageType.FileListTitle) { FilesOnDevice.Clear(); FileCrcs.Clear(); started = true; } else if (started == false) { //ignore everything until we've started a new file list request return; } if (e.MessageType == MeadowMessageType.FileListMember) { SetFileNameFromMessage(e.Message); } if (e.MessageType == MeadowMessageType.Concluded) { tcs.SetResult(true); } }; DataProcessor.OnReceiveData += handler; await MeadowFileManager.ListFiles(this); await Task.WhenAny(new Task[] { timeOutTask, tcs.Task }); DataProcessor.OnReceiveData -= handler; } return(FilesOnDevice); }
public override async Task <(List <string> files, List <UInt32> crcs)> GetFilesAndCrcs(int timeoutInMs = 60000) { if (SerialPort == null) { throw new Exception("SerialPort not intialized"); } var timeOutTask = Task.Delay(timeoutInMs); EventHandler <MeadowMessageEventArgs> handler = null; var tcs = new TaskCompletionSource <bool>(); var started = false; handler = (s, e) => { Console.WriteLine($"Msg: {e.MessageType}"); if (e.MessageType == MeadowMessageType.FileListTitle) { FilesOnDevice.Clear(); FileCrcs.Clear(); started = true; } else if (started == false) { //ignore everything until we've started a new file list request return; } if (e.MessageType == MeadowMessageType.FileListCrcMember) { SetFileAndCrcsFromMessage(e.Message); } if (e.MessageType == MeadowMessageType.Concluded) { tcs.TrySetResult(true); } }; DataProcessor.OnReceiveData += handler; await MeadowFileManager.ListFilesAndCrcs(this).ConfigureAwait(false); await Task.WhenAny(new Task[] { timeOutTask, tcs.Task }); DataProcessor.OnReceiveData -= handler; return(FilesOnDevice, FileCrcs); }