public void Evaluate(int spreadMax) { var touchDevice = FInput[0] ?? TouchDevice.Empty; if (touchDevice != FTouchDevice) { Unsubscribe(); FTouchDevice = touchDevice; Subscribe(); } var notifications = FEnumerator.MoveNext() ? FEnumerator.Current : FEmptyList; FEventTypeOut.SliceCount = notifications.Count; PositionPixelOut.SliceCount = notifications.Count; PositionInProjectionSpaceOut.SliceCount = notifications.Count; PositionInNormalizedProjectionOut.SliceCount = notifications.Count; PositionOut.SliceCount = notifications.Count; IdOut.SliceCount = notifications.Count; PrimaryOut.SliceCount = notifications.Count; ContactAreaOut.SliceCount = notifications.Count; DeviceIDOut.SliceCount = notifications.Count; for (int i = 0; i < notifications.Count; i++) { var n = notifications[i]; FEventTypeOut[i] = n.Kind; Vector2D inNormalizedProjection, inProjection; SpaceHelpers.MapFromPixels(n.Position, n.Sender, n.ClientArea, out inNormalizedProjection, out inProjection); PositionOut[i] = MouseExtensions.GetLegacyMousePositon(n.Position, n.ClientArea); PositionInProjectionSpaceOut[i] = inProjection; PositionInNormalizedProjectionOut[i] = inNormalizedProjection; PositionPixelOut[i] = new Vector2D(n.Position.X, n.Position.Y); IdOut[i] = n.Id; PrimaryOut[i] = n.Primary; ContactAreaOut[i] = new Vector2D(n.ContactArea.Width, n.ContactArea.Height); DeviceIDOut[i] = n.TouchDeviceID; } }
private static async Task <IReadOnlyCollection <Space> > GetRootSpacesAsync(HttpClient httpClient, ProvisioningDescription provisioningDescription) { var rootSpaces = new List <Space>(); foreach (SpaceDescription spaceDescription in provisioningDescription.spaces) { Space existingSpace = await SpaceHelpers.GetUniqueSpaceAsync(httpClient, spaceDescription.name, Guid.Empty, JsonSerializerSettings); if (existingSpace != null) { rootSpaces.Add(existingSpace); } } return(rootSpaces.AsReadOnly()); }
public void Evaluate(int spreadMax) { var gestureDevice = FInput[0] ?? GestureDevice.Empty; if (gestureDevice != FGestureDevice) { Unsubscribe(); FGestureDevice = gestureDevice; Subscribe(); } var notifications = FEnumerator.MoveNext() ? FEnumerator.Current : FEmptyList; var gestures = notifications.Where(g => (g.Kind == FGestureFilterKind) || ((g.Kind == GestureNotificationKind.GestureEnd) && (IdOut.Contains(g.Id)))) .ToList(); UseGestures(gestures); PositionOut.SliceCount = gestures.Count; PositionInProjectionSpaceOut.SliceCount = gestures.Count; PositionInNormalizedProjectionOut.SliceCount = gestures.Count; PositionPixelOut.SliceCount = gestures.Count; IdOut.SliceCount = gestures.Count; DeviceIDOut.SliceCount = gestures.Count; for (int i = 0; i < gestures.Count; i++) { var g = gestures[i]; Vector2D inNormalizedProjection, inProjection; SpaceHelpers.MapFromPixels(g.Position, g.Sender, g.ClientArea, out inNormalizedProjection, out inProjection); PositionOut[i] = MouseExtensions.GetLegacyMousePositon(g.Position, g.ClientArea); PositionInProjectionSpaceOut[i] = inProjection; PositionInNormalizedProjectionOut[i] = inNormalizedProjection; PositionPixelOut[i] = new Vector2D(g.Position.X, g.Position.Y); IdOut[i] = g.Id; DeviceIDOut[i] = g.GestureDeviceID; } }
private async Task CreateRoleAssignmentsAsync(HttpClient httpClient, IList <RoleAssignmentDescription> roleAssignmentDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"RoleAssignments must have a {nameof( spaceId )}"); } string spacePath = await SpaceHelpers.GetSpaceFullPathAsync(httpClient, spaceId); foreach (RoleAssignmentDescription roleAssignmentDescription in roleAssignmentDescriptions) { string objectId; switch (roleAssignmentDescription.objectIdType) { case RoleAssignment.ObjectIdTypes.UserDefinedFunctionId: objectId = (await UserDefinedFunctionHelpers.FindUserDefinedFunctionAsync(httpClient, roleAssignmentDescription.objectName, spaceId))?.Id; break; default: throw new ArgumentOutOfRangeException( $"{nameof( RoleAssignment )} with {nameof( RoleAssignmentDescription.objectName )} must" + $" have a known {nameof( RoleAssignmentDescription.objectIdType )}" + $" but instead has {roleAssignmentDescription.objectIdType}"); } if (objectId != null) { var roleAssignment = roleAssignmentDescription.ToDigitalTwins(objectId, spacePath); var existingRoleAssignment = await roleAssignment.GetUniqueRoleAssignmentAsync(httpClient); if (existingRoleAssignment == null) { await roleAssignment.CreateRoleAssignmentAsync(httpClient, JsonSerializerSettings); } else { Console.WriteLine($"{nameof( RoleAssignment )} already exists, so skipping creation."); } } } }
private async Task CreateUserRoleAssignmentsAsync(HttpClient httpClient, IList <string> users, Guid spaceId, UserAadObjectIdsDescription userAadObjectIds) { if (spaceId == Guid.Empty) { throw new ArgumentException($"User Role Assignment must have a {nameof( spaceId )}"); } foreach (string user in users) { if (userAadObjectIds.TryGetValue(user, out string oid)) { string spacePath = await SpaceHelpers.GetSpaceFullPathAsync(httpClient, spaceId); if (string.IsNullOrWhiteSpace(spacePath)) { Console.WriteLine($"Unable to get the full path for the current space. Cannot create a role assignment. (Space Id: {spaceId})"); continue; } var roleAssignment = new RoleAssignment { RoleId = RoleAssignment.RoleIds.User, ObjectId = oid, ObjectIdType = RoleAssignment.ObjectIdTypes.UserId, TenantId = Tenant, Path = spacePath }; var existingRoleAssignment = await roleAssignment.GetUniqueRoleAssignmentAsync(httpClient); if (existingRoleAssignment == null) { await roleAssignment.CreateRoleAssignmentAsync(httpClient, JsonSerializerSettings); } else { Console.WriteLine($"{nameof( RoleAssignment )} already exists, so skipping creation."); } } } }
private async Task CreatePropertiesAsync(HttpClient httpClient, IList <PropertyDescription> properties, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Property must have a {nameof( spaceId )}"); } Space space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId, "Properties"); var propertiesToCreate = properties.Where(pd => !space.Properties.Any(ep => ep.Name.Equals(pd.name, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (PropertyDescription propertyDescription in propertiesToCreate) { Property property = propertyDescription.ToDigitalTwins(); await property.CreatePropertyAsync(spaceId, httpClient, JsonSerializerSettings); } }
private async Task CreatePropertyKeysAsync(HttpClient httpClient, IList <PropertyKeyDescription> propertyKeys, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"PropertyKey must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <PropertyKey> existingPropertyKeys = await space.GetExistingPropertyKeysAsync(httpClient); var propertyKeysToCreate = propertyKeys.Where(pkd => !existingPropertyKeys.Any(epk => epk.Name.Equals(pkd.name, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (PropertyKeyDescription propertyKeyDescription in propertyKeysToCreate) { PropertyKey propertyKey = propertyKeyDescription.ToDigitalTwins(spaceId); await propertyKey.CreatePropertyKeyAsync(spaceId, httpClient, JsonSerializerSettings); } }
private static async Task CreateMatchersAsync(HttpClient httpClient, IList <MatcherDescription> matchers, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Matchers must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <Matcher> existingMatchers = await space.GetExistingMatchersAsync(httpClient); var matchersToCreate = matchers.Where(md => !existingMatchers.Any(em => em.Name.Equals(md.name, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (MatcherDescription matcherDescription in matchersToCreate) { Matcher matcher = matcherDescription.ToDigitalTwins(spaceId); await matcher.CreateMatcherAsync(httpClient, JsonSerializerSettings); } }
private static async Task CreateTypesAsync(HttpClient httpClient, IList <TypeDescription> typeDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Types must have a {nameof( spaceId )}"); } var space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId); IReadOnlyCollection <Type> existingTypes = await space.GetExistingTypesAsync(httpClient); var typesToCreate = typeDescriptions.Where(td => !existingTypes.Any(et => et.Name.Equals(td.name, StringComparison.OrdinalIgnoreCase) && et.Category.Equals(td.category, StringComparison.OrdinalIgnoreCase))) .ToArray(); foreach (TypeDescription typeDescription in typesToCreate) { Type type = typeDescription.ToDigitalTwins(spaceId); await type.CreateTypeAsync(httpClient, JsonSerializerSettings); } }
private async Task CreateSpaceAsync(HttpClient httpClient, SpaceDescription spaceDescription, Guid parentId, Guid keystoreId, UserAadObjectIdsDescription userAadObjectIds) { Space spaceToCreate = spaceDescription.ToDigitalTwins(parentId); Space existingSpace = await SpaceHelpers.GetUniqueSpaceAsync(httpClient, spaceToCreate.Name, parentId, JsonSerializerSettings); var spaceId = !string.IsNullOrWhiteSpace(existingSpace?.Id) ? Guid.Parse(existingSpace.Id) : await CreateSpaceAsync(httpClient, spaceToCreate); Console.WriteLine(); if (spaceId != Guid.Empty) { Guid keystoreIdToUseForChildren = keystoreId; // Keystore creation must happen first to ensure that devices down the tree can get their SaS tokens from it if (!string.IsNullOrWhiteSpace(spaceDescription.keystoreName)) { Keystore existingKeystore = await KeyStoresHelper.GetUniqueKeystoreAsync(httpClient, spaceDescription.keystoreName, spaceId, JsonSerializerSettings); Guid createdKeystoreId = !string.IsNullOrWhiteSpace(existingKeystore?.Id) ? Guid.Parse(existingKeystore.Id) : await CreateKeystoreAsync(httpClient, spaceDescription.keystoreName, spaceId); if (createdKeystoreId != Guid.Empty) { keystoreIdToUseForChildren = createdKeystoreId; } } // Resources must be created next to ensure that devices created down the tree will succeed if (spaceDescription.resources != null) { await CreateResourcesAsync(httpClient, spaceDescription.resources, spaceId); } // Types must be created next to ensure that devices/sensors created down the tree will succeed if (spaceDescription.types != null) { await CreateTypesAsync(httpClient, spaceDescription.types, spaceId); } if (spaceDescription.devices != null) { await CreateDevicesAsync(httpClient, spaceDescription.devices, keystoreIdToUseForChildren, spaceId); } if (spaceDescription.matchers != null) { await CreateMatchersAsync(httpClient, spaceDescription.matchers, spaceId); } if (spaceDescription.userDefinedFunctions != null) { await CreateUserDefinedFunctionsAsync(httpClient, spaceDescription.userDefinedFunctions, spaceId); } if (spaceDescription.roleAssignments != null) { await CreateRoleAssignmentsAsync(httpClient, spaceDescription.roleAssignments, spaceId); } if (spaceDescription.users != null) { await CreateUserRoleAssignmentsAsync(httpClient, spaceDescription.users, spaceId, userAadObjectIds); } if (spaceDescription.propertyKeys != null) { await CreatePropertyKeysAsync(httpClient, spaceDescription.propertyKeys, spaceId); } if (spaceDescription.properties != null) { await CreatePropertiesAsync(httpClient, spaceDescription.properties, spaceId); } if (spaceDescription.spaces != null) { await CreateSpacesAsync(httpClient, spaceDescription.spaces, spaceId, keystoreIdToUseForChildren, userAadObjectIds); } } }
private async Task CreateBlobAsync(HttpClient httpClient, IList <BlobDescription> blobDescriptions, Guid spaceId) { if (spaceId == Guid.Empty) { throw new ArgumentException($"Blob must have a {nameof( spaceId )}"); } Space space = await SpaceHelpers.GetSpaceAsync(httpClient, spaceId, "Properties"); Property imageBlobIdProperty = space.Properties.FirstOrDefault(p => p.Name == PropertyKeyDescription.ImageBlobId); Property detailedImageBlobIdProperty = space.Properties.FirstOrDefault(p => p.Name == PropertyKeyDescription.DetailedImageBlobId); foreach (BlobDescription blobDescription in blobDescriptions) { Property desiredBlobIdProperty; string desiredImagePathPropertyName; string desitedImageBlobIdPropertyName; if (blobDescription.isPrimaryBlob) { desiredBlobIdProperty = imageBlobIdProperty; desiredImagePathPropertyName = PropertyKeyDescription.ImagePath; desitedImageBlobIdPropertyName = PropertyKeyDescription.ImageBlobId; } else { desiredBlobIdProperty = detailedImageBlobIdProperty; desiredImagePathPropertyName = PropertyKeyDescription.DetailedImagePath; desitedImageBlobIdPropertyName = PropertyKeyDescription.DetailedImageBlobId; } Metadata metadata = blobDescription.ToDigitalTwinsMetadata(spaceId); var multipartContent = new MultipartFormDataContent("USER_DEFINED_BOUNDARY"); var metadataContent = new StringContent(JsonConvert.SerializeObject(metadata), Encoding.UTF8, "application/json"); metadataContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8"); multipartContent.Add(metadataContent, "metadata"); string blobContentFilePath = blobDescription.filepath; if (!Path.IsPathFullyQualified(blobContentFilePath)) { blobContentFilePath = Path.Combine(_directoryContainingDigitalTwinsProvisioningFile, blobContentFilePath); } using (FileStream s = File.OpenRead(blobContentFilePath)) { var blobContent = new StreamContent(s); blobContent.Headers.ContentType = MediaTypeHeaderValue.Parse(blobDescription.contentType); multipartContent.Add(blobContent, "contents"); Console.WriteLine(); HttpRequestMessage request; if (desiredBlobIdProperty == null) { Console.WriteLine($"Creating new blob for Space ({spaceId}) from file: {blobDescription.filepath}"); request = new HttpRequestMessage(HttpMethod.Post, "spaces/blobs") { Content = multipartContent }; } else { Console.WriteLine($"Updating blob for Space ({spaceId}) from file: {blobDescription.filepath}"); request = new HttpRequestMessage(HttpMethod.Patch, $"spaces/blobs/{desiredBlobIdProperty.Value}") { Content = multipartContent }; } var response = await httpClient.SendAsync(request); Guid blobId = await response.GetIdAsync(); if (desiredBlobIdProperty == null) { var uriBuilder = new UriBuilder($"{httpClient.BaseAddress}spaces/blobs/{blobId}/contents/latest"); var imagePathProperty = new Property { Name = desiredImagePathPropertyName, Value = uriBuilder.Uri.ToString() }; await imagePathProperty.CreateOrUpdatePropertyAsync(spaceId, httpClient, JsonSerializerSettings); imageBlobIdProperty = new Property { Name = desitedImageBlobIdPropertyName, Value = blobId.ToString() }; await imageBlobIdProperty.CreateOrUpdatePropertyAsync(spaceId, httpClient, JsonSerializerSettings); } } } }
public virtual void OnImportsSatisfied() { FGestureFilterKind = SetGestureKindFilter(); PositionPixelOut.SliceCount = 0; PositionInProjectionSpaceOut.SliceCount = 0; PositionInNormalizedProjectionOut.SliceCount = 0; PositionOut.SliceCount = 0; IdOut.SliceCount = 0; DeviceIDOut.SliceCount = 0; FSubscription = new Subscription <GestureDevice, GestureNotification>( gestureDevice => { return(gestureDevice.Notifications.Where(g => (g.Kind == FGestureFilterKind) || (g.Kind == GestureNotificationKind.GestureEnd))); }, (gestureDevice, g) => { var index = IdOut.IndexOf(g.Id); var isFilterMatch = g.Kind == FGestureFilterKind; UseGesture(g, isFilterMatch, index); if (isFilterMatch) { Vector2D inNormalizedProjection, inProjection; SpaceHelpers.MapFromPixels(g.Position, g.Sender, g.ClientArea, out inNormalizedProjection, out inProjection); var normalizedPosition = MouseExtensions.GetLegacyMousePositon(g.Position, g.ClientArea); var inPixels = new Vector2D(g.Position.X, g.Position.Y); if (index < 0) { PositionPixelOut.Add(inPixels); PositionInProjectionSpaceOut.Add(inProjection); PositionInNormalizedProjectionOut.Add(inNormalizedProjection); PositionOut.Add(normalizedPosition); IdOut.Add(g.Id); DeviceIDOut.Add(g.GestureDeviceID); } else { PositionPixelOut[index] = inPixels; PositionInProjectionSpaceOut[index] = inProjection; PositionInNormalizedProjectionOut[index] = inNormalizedProjection; PositionOut[index] = normalizedPosition; IdOut[index] = g.Id; DeviceIDOut[index] = g.GestureDeviceID; } } else if (index >= 0) { PositionPixelOut.RemoveAt(index); PositionInProjectionSpaceOut.RemoveAt(index); PositionInNormalizedProjectionOut.RemoveAt(index); PositionOut.RemoveAt(index); IdOut.RemoveAt(index); DeviceIDOut.RemoveAt(index); } }, FScheduler ); }
public void OnImportsSatisfied() { PositionPixelOut.SliceCount = 0; PositionInProjectionSpaceOut.SliceCount = 0; PositionInNormalizedProjectionOut.SliceCount = 0; PositionOut.SliceCount = 0; IdOut.SliceCount = 0; PrimaryOut.SliceCount = 0; ContactAreaOut.SliceCount = 0; IsNewOut.SliceCount = 0; DeviceIDOut.SliceCount = 0; FSubscription = new Subscription <TouchDevice, TouchNotification>( touchDevice => { return(touchDevice.Notifications); }, (touchDevice, n) => { Vector2D inNormalizedProjection, inProjection; SpaceHelpers.MapFromPixels(n.Position, n.Sender, n.ClientArea, out inNormalizedProjection, out inProjection); var normalizedPosition = MouseExtensions.GetLegacyMousePositon(n.Position, n.ClientArea); var inPixels = new Vector2D(n.Position.X, n.Position.Y); var contactArea = new Vector2D(n.ContactArea.Width, n.ContactArea.Height); var index = IdOut.IndexOf(n.Id); var primary = n.Primary; var deviceid = n.TouchDeviceID; switch (n.Kind) { case TouchNotificationKind.TouchDown: if (index < 0) { IdOut.Add(n.Id); PositionPixelOut.Add(inPixels); PositionInProjectionSpaceOut.Add(inProjection); PositionInNormalizedProjectionOut.Add(inNormalizedProjection); PositionOut.Add(normalizedPosition); PrimaryOut.Add(primary); ContactAreaOut.Add(contactArea); IsNewOut.Add(true); DeviceIDOut.Add(deviceid); } break; case TouchNotificationKind.TouchUp: if (index >= 0) { IdOut.RemoveAt(index); PositionPixelOut.RemoveAt(index); PositionInProjectionSpaceOut.RemoveAt(index); PositionInNormalizedProjectionOut.RemoveAt(index); PositionOut.RemoveAt(index); PrimaryOut.RemoveAt(index); ContactAreaOut.RemoveAt(index); IsNewOut.RemoveAt(index); DeviceIDOut.RemoveAt(index); } break; case TouchNotificationKind.TouchMove: if (index >= 0) { PositionPixelOut[index] = inPixels; PositionInProjectionSpaceOut[index] = inProjection; PositionInNormalizedProjectionOut[index] = inNormalizedProjection; PositionOut[index] = normalizedPosition; PrimaryOut[index] = primary; ContactAreaOut[index] = contactArea; IsNewOut[index] = false; } break; default: throw new NotImplementedException(); } }, FScheduler ); }