/// <summary> /// Attempts to load image, video and audio assets in the project's 'Assets/SkillAssets' folder, or the overridden storage folder, to the robot's system /// </summary> /// <param name="forceReload">force the system to upload all assets, whether they exist or not</param> /// <param name="assetFolder">pass in to override the default location</param> /// <returns></returns> public async Task LoadAssets(bool forceReload = false, StorageFolder assetFolder = null) { try { await RefreshAssetLists(); //Load the assets in the Assets/SkillAssets folder to the robot if they are missing or if ReloadAssets is passed in StorageFolder skillAssetFolder = assetFolder ?? await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\SkillAssets"); IList <StorageFile> assetFileList = (await skillAssetFolder.GetFilesAsync()).ToList(); foreach (StorageFile storageFile in assetFileList) { if (forceReload || (!AudioList.Any(x => x.Name == storageFile.Name) && !ImageList.Any(x => x.Name == storageFile.Name) && !VideoList.Any(x => x.Name == storageFile.Name))) { StorageFile file = await skillAssetFolder.GetFileAsync(storageFile.Name); IRandomAccessStreamWithContentType stream = await file.OpenReadAsync(); byte[] contents = new byte[stream.Size]; await stream.AsStream().ReadAsync(contents, 0, contents.Length); if (storageFile.Name.EndsWith(".mp3") || storageFile.Name.EndsWith(".wav") || storageFile.Name.EndsWith(".wma") || storageFile.Name.EndsWith(".aac")) { if ((await _misty.SaveAudioAsync(storageFile.Name, contents, false, true)).Status == ResponseStatus.Success) { AudioList.Add(new AudioDetails { Name = storageFile.Name, SystemAsset = false }); _misty.SkillLogger.LogInfo($"Uploaded audio asset '{storageFile.Name}'"); } else { _misty.SkillLogger.Log($"Failed to upload audio asset '{storageFile.Name}'"); } } else if (storageFile.Name.EndsWith(".mp4") || storageFile.Name.EndsWith(".wmv")) { if ((await _misty.SaveVideoAsync(storageFile.Name, contents, false, true)).Status == ResponseStatus.Success) { VideoList.Add(new VideoDetails { Name = storageFile.Name, SystemAsset = false }); _misty.SkillLogger.LogInfo($"Uploaded video asset '{storageFile.Name}'"); } else { _misty.SkillLogger.Log($"Failed to upload video asset '{storageFile.Name}'"); } } else if (storageFile.Name.EndsWith(".jpg") || storageFile.Name.EndsWith(".jpeg") || storageFile.Name.EndsWith(".png") || storageFile.Name.EndsWith(".gif")) { if ((await _misty.SaveImageAsync(storageFile.Name, contents, false, true, 0, 0)).Status == ResponseStatus.Success) { ImageList.Add(new ImageDetails { Name = storageFile.Name, SystemAsset = false }); _misty.SkillLogger.LogInfo($"Uploaded image asset '{storageFile.Name}'"); } else { _misty.SkillLogger.Log($"Failed to upload image asset '{storageFile.Name}'"); } } else { _misty.SkillLogger.Log($"Unknown extension for asset '{storageFile.Name}', could not load to robot."); } } } } catch (Exception ex) { _misty.SkillLogger.Log("Error loading assets.", ex); } }
private async Task LoadAssets() { //Get the current assets on the robot IGetAudioListResponse audioListResponse = await _misty.GetAudioListAsync(); if (audioListResponse.Status == ResponseStatus.Success && audioListResponse.Data.Count() > 0) { _audioList = audioListResponse.Data; } IGetImageListResponse imageListResponse = await _misty.GetImageListAsync(); if (imageListResponse.Status == ResponseStatus.Success && imageListResponse.Data.Count() > 0) { _imageList = imageListResponse.Data; } IGetVideoListResponse videoListResponse = await _misty.GetVideoListAsync(); if (videoListResponse.Status == ResponseStatus.Success && videoListResponse.Data.Count() > 0) { _videoList = videoListResponse.Data; } //Load the assets in the Assets/SkillAssets folder to the robot if they are missing or if ReloadAssets is passed in StorageFolder skillAssetFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets\SkillAssets"); IList <StorageFile> assetFileList = (await skillAssetFolder.GetFilesAsync()).ToList(); foreach (StorageFile storageFile in assetFileList) { if (_reloadAssets || (!_audioList.Any(x => x.Name == storageFile.Name) && !_imageList.Any(x => x.Name == storageFile.Name) && !_videoList.Any(x => x.Name == storageFile.Name))) { StorageFile file = await skillAssetFolder.GetFileAsync(storageFile.Name); IRandomAccessStreamWithContentType stream = await file.OpenReadAsync(); byte[] contents = new byte[stream.Size]; await stream.AsStream().ReadAsync(contents, 0, contents.Length); if (storageFile.Name.EndsWith(".mp3") || storageFile.Name.EndsWith(".wav") || storageFile.Name.EndsWith(".wma") || storageFile.Name.EndsWith(".aac")) { if ((await _misty.SaveAudioAsync(storageFile.Name, contents, false, true)).Status == ResponseStatus.Success) { _audioList.Add(new AudioDetails { Name = storageFile.Name, SystemAsset = false }); _misty.SkillLogger.LogInfo($"Uploaded audio asset '{storageFile.Name}'"); } else { _misty.SkillLogger.Log($"Failed to upload audio asset '{storageFile.Name}'"); } } else if (storageFile.Name.EndsWith(".mp4") || storageFile.Name.EndsWith(".wmv")) { if ((await _misty.SaveVideoAsync(storageFile.Name, contents, false, true)).Status == ResponseStatus.Success) { _videoList.Add(new VideoDetails { Name = storageFile.Name, SystemAsset = false }); _misty.SkillLogger.LogInfo($"Uploaded video asset '{storageFile.Name}'"); } else { _misty.SkillLogger.Log($"Failed to upload video asset '{storageFile.Name}'"); } } else if (storageFile.Name.EndsWith(".jpg") || storageFile.Name.EndsWith(".jpeg") || storageFile.Name.EndsWith(".png") || storageFile.Name.EndsWith(".gif")) { if ((await _misty.SaveImageAsync(storageFile.Name, contents, false, true, 0, 0)).Status == ResponseStatus.Success) { _imageList.Add(new ImageDetails { Name = storageFile.Name, SystemAsset = false }); _misty.SkillLogger.LogInfo($"Uploaded image asset '{storageFile.Name}'"); } else { _misty.SkillLogger.Log($"Failed to upload image asset '{storageFile.Name}'"); } } else { _misty.SkillLogger.Log($"Unknown extension for asset '{storageFile.Name}', could not load to robot."); } } } }