public async Task <int> GetSpyFileNumber(SpyFileType type) { var fileNames = new List <string>(); UInt32 getNumCommand; if (type == SpyFileType.FileTypeCAN1) { getNumCommand = 0x00000003; } else if (type == SpyFileType.FileTypeCAN2) { getNumCommand = 0x00000004; } else if (type == SpyFileType.FileTypeK) { getNumCommand = 0x00000005; } else { throw new Exception("FileType not implemented!"); } var res = await SendReceiveCommand(getNumCommand); return((int)res.responseData); }
public async Task <byte[]> GetSpyFile(SpyFileType type, string fileName) { byte[] fileNameBuff = new byte[20]; UInt32 getFileCommand; if (type == SpyFileType.FileTypeCAN1) { getFileCommand = 0x0000000C; } else if (type == SpyFileType.FileTypeCAN2) { getFileCommand = 0x0000000D; } else if (type == SpyFileType.FileTypeK) { getFileCommand = 0x0000000E; } else { throw new Exception("FileType not implemented!"); } if (fileName.Length <= fileNameBuff.Length) { Encoding.ASCII.GetBytes(fileName).CopyTo(fileNameBuff, 0); } else { throw new Exception("Nome del file troppo lungo!"); } var res = await SendReceiveCommand(getFileCommand, fileNameBuff); return(res.responseBuff); }
public FileListPage(SpyFileType fileType) { // Appena creata la pagina deve contenere la lista vuota; // lo spinner e' attivo mentre la pagina aspetta il messaggio dal ViewModel // di aggiornamento corretto della lista, che ferma lo spinner // In generale poi la lista e' composta da un nome del file e una pulsante "Download" // che quando premuto esegue un command? InitializeComponent(); MessagingCenter.Subscribe <FileListViewModel, string>(this, "DownloadFilesListError", async(sender, message) => { await DisplayAlert("CANAnalyzer", "An error occurred during the files list download.", "Ok"); }); MessagingCenter.Subscribe <FileListViewModel, string>(this, "DownloadFileError", async(sender, message) => { await DisplayAlert("CANAnalyzer", "An error occurred during the file download.", "Ok"); }); _viewModel = new FileListViewModel(fileType); BindingContext = _viewModel; _fileType = fileType; _isDataLoaded = false; }
public FileListViewModel(SpyFileType fileType) { files = new List <SpyFile>(); isDownloadingList = false; isDownloadedList = false; isDownloadingFile = false; downloadFileProgress = 0; downloadFileCommand = new Command <string>(async(fileName) => { try { Device.BeginInvokeOnMainThread(() => { foreach (SpyFile file in Files) { file.IsDownloadingFile = true; } IsDownloadingFile = true; }); var fileContent = await AnalyzerDevice.GetSpyFile(fileType, fileName); // Devo condividere fileContent, che sarà un byte[] var filePath = Path.Combine(FileSystem.CacheDirectory, fileName); File.WriteAllBytes(filePath, fileContent); await Share.RequestAsync(new ShareFileRequest { Title = Path.GetFileNameWithoutExtension(fileName), File = new ShareFile(filePath) }); } catch (Exception ex) { MessagingCenter.Send <FileListViewModel, string>(this, "DownloadFileError", ex.Message); } Device.BeginInvokeOnMainThread(() => { foreach (SpyFile file in Files) { file.IsDownloadingFile = false; } IsDownloadingFile = true; DownloadFileProgress = 0; }); }); MessagingCenter.Subscribe <IAnalyzerDevice, double>(this, "DownloadFileProgress", (sender, progress) => { Device.BeginInvokeOnMainThread(() => { DownloadFileProgress = progress; }); }); }
public async Task DownloadFilesList(SpyFileType fileType) { Device.BeginInvokeOnMainThread(() => { IsDownloadingList = true; }); try { var fileNum = await AnalyzerDevice.GetSpyFileNumber(fileType); if (fileNum > 0) { var fileNames = await AnalyzerDevice.GetSpyFileNames(fileType); var fileSizes = await AnalyzerDevice.GetSpyFileSizes(fileType); Device.BeginInvokeOnMainThread(() => { for (int fileIndex = 0; fileIndex < fileNum; fileIndex++) { Files.Add(new SpyFile { FileName = fileNames[fileIndex], FileSize = fileSizes[fileIndex], ItemTappedCommand = downloadFileCommand, IsDownloadingFile = false }); } }); } Device.BeginInvokeOnMainThread(() => { IsDownloadedList = true; }); } catch (Exception ex) { MessagingCenter.Send <FileListViewModel, string>(this, "DownloadFilesListError", ex.Message); } Device.BeginInvokeOnMainThread(() => { IsDownloadingList = false; }); }
public async Task <List <int> > GetSpyFileSizes(SpyFileType type) { var fileSizes = new List <int>(); UInt32 getNumCommand; UInt32 getFileSizeCommand; if (type == SpyFileType.FileTypeCAN1) { getNumCommand = 0x00000003; getFileSizeCommand = 0x00000009; } else if (type == SpyFileType.FileTypeCAN2) { getNumCommand = 0x00000004; getFileSizeCommand = 0x0000000A; } else if (type == SpyFileType.FileTypeK) { getNumCommand = 0x00000005; getFileSizeCommand = 0x0000000B; } else { throw new Exception("FileType not implemented!"); } var getNumResponse = await SendReceiveCommand(getNumCommand); var filesNum = getNumResponse.responseData; for (UInt32 fileIndex = 0; fileIndex < filesNum; fileIndex++) { var fileIndexBuf = new byte[4]; ArrConverter.SetBufferFromUInt32(fileIndex, fileIndexBuf, 0); var getFileSizeResponse = await SendReceiveCommand(getFileSizeCommand, fileIndexBuf); UInt32 fileSize = getFileSizeResponse.responseData; fileSizes.Add((int)fileSize); } return(fileSizes); }
public async Task <List <string> > GetSpyFileNames(SpyFileType type) { var fileNames = new List <string>(); UInt32 getNumCommand; UInt32 getFileNameCommand; if (type == SpyFileType.FileTypeCAN1) { getNumCommand = 0x00000003; getFileNameCommand = 0x00000006; } else if (type == SpyFileType.FileTypeCAN2) { getNumCommand = 0x00000004; getFileNameCommand = 0x00000007; } else if (type == SpyFileType.FileTypeK) { getNumCommand = 0x00000005; getFileNameCommand = 0x00000008; } else { throw new Exception("FileType not implemented!"); } var getNumResponse = await SendReceiveCommand(getNumCommand); var filesNum = getNumResponse.responseData; for (UInt32 fileIndex = 0; fileIndex < filesNum; fileIndex++) { var fileIndexBuf = new byte[4]; ArrConverter.SetBufferFromUInt32(fileIndex, fileIndexBuf, 0); var getFileNameResponse = await SendReceiveCommand(getFileNameCommand, fileIndexBuf); var fileName = Encoding.ASCII.GetString(getFileNameResponse.responseBuff); fileNames.Add(fileName); } return(fileNames); }