private async Task OnExecuteAsync() { HttpClient httpClient = await HttpClientHelper.GetHttpClientAsync(DigitalTwinsApiEndpoint, AadInstance, Tenant, DigitalTwinsResourceId, ClientId, ClientSecret); ProvisioningDescription provisioningDescription = ProvisioningHelper.LoadSmartHotelProvisioning(); await RemoveAllExistingDevicesAsync(httpClient, provisioningDescription); IReadOnlyCollection <Space> rootSpaces = await GetRootSpacesAsync(httpClient, provisioningDescription); await RemoveAllExistingResourcesAsync(httpClient, rootSpaces); await RemoveAllExistingRootSpacesAsync(httpClient, rootSpaces); if (RemoveEndpoints) { await RemoveAllExistingEndpoints(httpClient); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Press Enter to continue..."); Console.ReadLine(); }
public List <MyListBoxItem> GetChildElements(ProvisioningDescription provisioningDescription, MyListBoxItem item) { if (item.description == Constants.ServiceName.simpleService) { return(null); } List <MyListBoxItem> arrayOfChild = new List <MyListBoxItem>(); var complexService = provisioningDescription.servicesDescription.complexServiceList.Where(s => s.name == item.name).FirstOrDefault(); var partServicesArray = complexService.partList.partService; if (partServicesArray != null) { var partServices = partServicesArray.ToList(); foreach (var p in partServices) { var service = GetServiceByName(provisioningDescription, p.name); if (service != null) { arrayOfChild.Add(service); } else { arrayOfChild.Add(new MyListBoxItem(p.name, Constants.ServiceName.servisNotInList)); } } } return(arrayOfChild); }
private static void OutputTotalNumberOfSpaces(ProvisioningDescription provisioningDescription) { int numberOfSpaces = provisioningDescription.spaces.Count; foreach (SpaceDescription space in provisioningDescription.spaces) { numberOfSpaces += GetTotalNumberOfChildSpaces(space); } if (numberOfSpaces > 995) { string warningMessage; if (numberOfSpaces < 1000) { warningMessage = "This is extremely close to the API's 1,000 object limit of the" + " Digital Twins public preview and may result in the demo not working."; } else { warningMessage = "This is OVER the API's 1,000 object limit of the" + " Digital Twins public preview and WILL result in the demo not working."; } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Loaded {numberOfSpaces} spaces to provision." + $"{Environment.NewLine}{warningMessage}{Environment.NewLine}We recommend shrinking the topology size"); Console.ResetColor(); } else { Console.WriteLine($"Loaded {numberOfSpaces} spaces to provision."); } }
private async Task OnExecuteAsync() { try { string fullUserObjectIdsFilePath = Path.GetFullPath(UserObjectIdsFile); string userAadObjectIdsString = await File.ReadAllTextAsync(fullUserObjectIdsFilePath); UserAadObjectIdsDescription userAadObjectIds = JsonConvert.DeserializeObject <UserAadObjectIdsDescription>(userAadObjectIdsString); if (!userAadObjectIds.AreRequiredValuesFilled()) { Console.WriteLine($"The {nameof( UserObjectIdsFile )} must have all the required properties filled." + " (Head Of Operations, Hotel Brand 1 Manager, Hotel 1 Manager, and Hotel 1 Employee)"); return; } HttpClient httpClient = await HttpClientHelper.GetHttpClientAsync(DigitalTwinsApiEndpoint, AadInstance, Tenant, DigitalTwinsResourceId, ClientId, ClientSecret); string fullPathToDigitalTwinsProvisioningFile = Path.GetFullPath(DigitalTwinsProvisioningFile); directoryContainingDigitalTwinsProvisioningFile = Path.GetDirectoryName(fullPathToDigitalTwinsProvisioningFile); Console.WriteLine("Loading the provisioning files..."); ProvisioningDescription provisioningDescription = ProvisioningHelper.LoadSmartHotelProvisioning(fullPathToDigitalTwinsProvisioningFile); Console.WriteLine("Successfully loaded provisioning files."); Console.WriteLine("Creating spaces and endpoints..."); await CreateSpacesAsync(httpClient, provisioningDescription.spaces, Guid.Empty, Guid.Empty, userAadObjectIds); Console.WriteLine(); Console.WriteLine(); Console.WriteLine($"Created {_spacesCreatedCount} spaces..."); Console.WriteLine(); Console.WriteLine(); await CreateEndpointsAsync(httpClient, provisioningDescription.endpoints); if (!string.IsNullOrEmpty(OutputFile)) { IDictionary <string, List <DeviceDescription> > allDevices = provisioningDescription.spaces.GetAllDeviceDescriptionsByDeviceIdPrefix(string.Empty); await File.WriteAllTextAsync(OutputFile, JsonConvert.SerializeObject(new SortedDictionary <string, List <DeviceDescription> >(allDevices))); } Console.WriteLine(); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"Error occurred during Digital Twins provisioning: {ex}"); } }
public List <ServiceForList> GetServiceList(ProvisioningDescription provisioningDescription) { var serviceList = new List <ServiceForList>(); serviceList.AddRange(provisioningDescription.servicesDescription.simpleServiceList.Select(s => new ServiceForList(s.name, false)).OrderBy(o => o.name)); serviceList.AddRange(provisioningDescription.servicesDescription.complexServiceList.Select(s => new ServiceForList(s.name, true)).OrderBy(o => o.name)); return(serviceList); }
private async Task OnExecuteAsync() { try { if (RemoveDevices) { _actionMessage = "Removing"; _actionMessagePastTense = "Removed"; } _iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(IoTHubRegistryConnectionString); _registryManager = RegistryManager.CreateFromConnectionString(IoTHubRegistryConnectionString); Console.WriteLine("Loading the provisioning files..."); ProvisioningDescription provisioningDescription = ProvisioningHelper.LoadSmartHotelProvisioning(DigitalTwinsProvisioningFile); Console.WriteLine("Successfully loaded provisioning files."); if (!string.IsNullOrEmpty(OutputFile)) { var sw = Stopwatch.StartNew(); IDictionary <string, List <DeviceDescription> > allDevices = provisioningDescription.spaces.GetAllDeviceDescriptionsByDeviceIdPrefix(string.Empty); Console.WriteLine($"{_actionMessage} IoT Hub devices..."); Console.WriteLine(); Console.WriteLine(); IDictionary <string, string> deviceConnectionStringsByPrefix = await CreateIoTHubDevicesAndGetConnectionStringsAsync(allDevices); sw.Stop(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine($"{_actionMessagePastTense} IoT Hub devices successfully in {sw.Elapsed.TotalMinutes} minutes."); if (!RemoveDevices) { await File.WriteAllTextAsync(OutputFile, JsonConvert.SerializeObject( new SortedDictionary <string, string>(deviceConnectionStringsByPrefix))); } } Console.WriteLine(); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine($"Error occurred during IoT Hub Device provisioning: {ex}"); } }
public List <MyListBoxItem> getServicesList(ProvisioningDescription provisioningDescription, string packageName) { var package = provisioningDescription.packageDescription.packageList.Where(p => p.name == packageName).FirstOrDefault(); var packageServicesName = package.serviceList.Select(s => s.name).ToList(); List <MyListBoxItem> listOfServices = new List <MyListBoxItem>(); foreach (var serviceName in packageServicesName) { listOfServices.Add(ServiceHelper.Instance.GetServiceByName(provisioningDescription, serviceName)); } return(listOfServices); }
public MyListBoxItem GetServiceByName(ProvisioningDescription provisioningDescription, string name) { if (provisioningDescription.servicesDescription.complexServiceList.Any(s => s.name == name)) { var service = provisioningDescription.servicesDescription.complexServiceList.Where(s => s.name == name).FirstOrDefault(); return(new MyListBoxItem(service.name, Constants.ServiceName.complexService)); } if (provisioningDescription.servicesDescription.simpleServiceList.Any(s => s.name == name)) { var service = provisioningDescription.servicesDescription.simpleServiceList.Where(s => s.name == name).FirstOrDefault(); return(new MyListBoxItem(service.name, Constants.ServiceName.simpleService)); } return(null); }
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()); }
private async Task OnExecuteAsync() { HttpClient httpClient = await HttpClientHelper.GetHttpClientAsync(DigitalTwinsApiEndpoint, AadInstance, Tenant, DigitalTwinsResourceId, ClientId, ClientSecret); ProvisioningDescription provisioningDescription = ProvisioningHelper.LoadSmartHotelProvisioning(); await CreateSpacesAsync(httpClient, provisioningDescription.spaces, Guid.Empty, Guid.Empty); await CreateEndpointsAsync(httpClient, provisioningDescription.endpoints); if (!string.IsNullOrEmpty(OutputFile)) { IDictionary <string, List <DeviceDescription> > allDevices = provisioningDescription.spaces.GetAllDeviceDescriptions(); await File.WriteAllTextAsync(OutputFile, JsonConvert.SerializeObject(allDevices)); } Console.WriteLine(); Console.WriteLine(); }
private void OpenProvisioningDescription_Click(object sender, EventArgs e) { OpenFileDialog openProvisioningDescriptionFileDialog = new OpenFileDialog(); //openProvisioningDescriptionFileDialog.InitialDirectory = "c:\\"; openProvisioningDescriptionFileDialog.Filter = "All files (*.*)|*.*|xml files (*.xml)|*.xml"; openProvisioningDescriptionFileDialog.FilterIndex = 2; openProvisioningDescriptionFileDialog.RestoreDirectory = true; if (openProvisioningDescriptionFileDialog.ShowDialog() == DialogResult.OK) { try { provisioningDescription = XmlHelper.Instance.Deserialize <ProvisioningDescription>(openProvisioningDescriptionFileDialog.FileName); } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }
private void button_OpenProvisioningDescription(object sender, RoutedEventArgs e) { System.Windows.Forms.OpenFileDialog openProvisioningDescriptionFileDialog = new System.Windows.Forms.OpenFileDialog(); //openProvisioningDescriptionFileDialog.InitialDirectory = "c:\\"; openProvisioningDescriptionFileDialog.Filter = "All files (*.*)|*.*|xml files (*.xml)|*.xml"; openProvisioningDescriptionFileDialog.FilterIndex = 2; openProvisioningDescriptionFileDialog.RestoreDirectory = true; if (openProvisioningDescriptionFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { provisioningDescription = XmlHelper.Instance.Deserialize <ProvisioningDescription>(openProvisioningDescriptionFileDialog.FileName); packageList = provisioningDescription.packageDescription.packageList.OrderBy(o => o.name).ToList(); serviceList = ServiceHelper.Instance.GetServiceList(provisioningDescription); FillList(); } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }
public bool isSimpleService(ProvisioningDescription provisioningDescription, string serviceName) { return(provisioningDescription.servicesDescription.simpleServiceList.Any(s => s.name == serviceName)); }
private static async Task RemoveAllExistingDevicesAsync(HttpClient httpClient, ProvisioningDescription provisioningDescription) { IDictionary <string, List <DeviceDescription> > deviceDictionary = provisioningDescription.spaces.GetAllDeviceDescriptions(); IEnumerable <DeviceDescription> allDefinedDevices = deviceDictionary.Values.SelectMany(v => v); IReadOnlyCollection <Device> existingDevices = await allDefinedDevices.GetExistingDevicesAsync(httpClient); if (existingDevices == null || !existingDevices.Any()) { return; } Console.WriteLine(); Console.WriteLine($"Removing {existingDevices.Count} devices with the following hardware ids:" + $" {string.Join( ", ", existingDevices.Select( d => d.HardwareId ) )}"); foreach (Device deviceToRemove in existingDevices) { bool success = await deviceToRemove.DeleteDeviceAsync(httpClient, JsonSerializerSettings); if (!success) { Console.WriteLine($"Failed to remove Device, please try manually. (Id: {deviceToRemove.Id})"); } } Console.WriteLine(); }
private bool GenerateSiteProvisioningFile(List <Brand> brands, string outputDirectory) { string siteFilename = Path.Combine(outputDirectory, $"{OutputFilePrefix}_Site_Provisioning.yaml"); if (File.Exists(siteFilename)) { var overwrite = Prompt.GetYesNo("A site provisioning file already exists," + " would you like to overwrite it and any brand provisioning" + $" files that already exist? ({siteFilename})", false); if (!overwrite) { return(false); } } var p = new ProvisioningDescription(); p.AddEndpoint(new EndpointDescription { type = "EventHub", eventTypes = new List <string> { "DeviceMessage" } }); var tenantSpace = new SpaceDescription { name = "SmartHotel 360 Tenant", description = "This is the root node for the SmartHotel360 IoT Demo", friendlyName = "SmartHotel 360 Tenant", type = "Tenant", keystoreName = "SmartHotel360 Keystore" }; SpaceDescription desiredTenantSpace = tenantSpace; p.AddSpace(tenantSpace); if (!string.IsNullOrWhiteSpace(SubTenantName)) { var subtenantSpace = new SpaceDescription { name = SubTenantName, description = $"This is the root node for the {SubTenantName} sub Tenant", friendlyName = SubTenantName, type = "Tenant" }; tenantSpace.AddSpace(subtenantSpace); desiredTenantSpace = subtenantSpace; } else { tenantSpace.AddResource(new ResourceDescription { type = "IoTHub" }); } desiredTenantSpace.AddType(new TypeDescription { name = "Classic", category = "SensorType" }); desiredTenantSpace.AddType(new TypeDescription { name = "HotelBrand", category = "SpaceType" }); desiredTenantSpace.AddType(new TypeDescription { name = "Hotel", category = "SpaceType" }); desiredTenantSpace.AddType(new TypeDescription { name = "VIPFloor", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = "QueenRoom", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = "KingRoom", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = "SuiteRoom", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = "VIPSuiteRoom", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = "ConferenceRoom", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = "GymRoom", category = "SpaceSubType" }); desiredTenantSpace.AddType(new TypeDescription { name = BlobDescription.FloorplanFileBlobSubType, category = "SpaceBlobSubType" }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.DeviceIdPrefixName, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Prefix used in sending Device Method calls to the IoT Hub." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.DisplayOrder, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.UInt, description = "Order to display spaces" }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.MinTemperatureAlertThreshold, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.Int, description = "Alert if the temperature goes below this value." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.MaxTemperatureAlertThreshold, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.Int, description = "Alert if the temperature goes above this value." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.ImagePath, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Path of the image to display for the space." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.ImageBlobId, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Id of the image blob for the space." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.DetailedImagePath, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Path of the detailed image to display for the space." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.DetailedImageBlobId, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Id of the detailed image blob for the space." }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.Latitude, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Geo Position" }); desiredTenantSpace.AddPropertyKey(new PropertyKeyDescription { name = PropertyKeyDescription.Longitude, primitiveDataType = PropertyKeyDescription.PrimitiveDataType.String, description = "Geo Position" }); desiredTenantSpace.AddUser("Head Of Operations"); var matcherTemperature = new MatcherDescription { name = "Matcher Temperature", dataTypeValue = "Temperature" }; desiredTenantSpace.AddMatcher(matcherTemperature); var temperatureProcessor = new UserDefinedFunctionDescription { name = "Temperature Processor", matcherNames = new List <string>(), script = "../UserDefinedFunctions/temperatureThresholdAlert.js" }; temperatureProcessor.matcherNames.Add(matcherTemperature.name); desiredTenantSpace.AddUserDefinedFunction(temperatureProcessor); desiredTenantSpace.AddRoleAssignment(new RoleAssignmentDescription { roleId = RoleAssignment.RoleIds.SpaceAdmin, objectName = temperatureProcessor.name, objectIdType = RoleAssignment.ObjectIdTypes.UserDefinedFunctionId }); foreach (Brand brand in brands) { string brandFilename = GetBrandProvisioningFilename(brand); desiredTenantSpace.AddSpaceReference(new SpaceReferenceDescription { filename = brandFilename }); } var yamlSerializer = new Serializer(); string serializedProvisioningDescription = yamlSerializer.Serialize(p); File.WriteAllText(siteFilename, serializedProvisioningDescription); Console.WriteLine($"Successfully created site provisioning file: {siteFilename}"); return(true); }