Пример #1
0
        //Added Method for creating DataTypes
        // Abhilash 20 May 2020

        public static async Task <String> CreateDataType(
            HttpClient httpClient, Models.SensorDataType sensorDataType,
            ILogger logger, string includes = null)
        {
            // if (id == Guid.Empty)
            //   throw new ArgumentException("GetSpace requires a non empty guid as id");
            //.SpaceId = "00000000-0000-0000-0000-000000000000";


            var serializedContent = JsonConvert.SerializeObject(sensorDataType);
            var response          = await httpClient.PostAsync($"types/", new StringContent(serializedContent, Encoding.UTF8, "application/json"));

            logger.LogInformation($"Completed PostAsync for types creation - Abh2020.");
            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                logger.LogInformation($"Created New Data Type - Abh2020.");
                return(content);
            }

            return(null);
        }
Пример #2
0
        public static async Task <IEnumerable <ProvisionResults.Space> > CreateSpaces(
            HttpClient httpClient,
            ILogger logger,
            IEnumerable <SpaceDescription> descriptions,
            Guid parentId)
        {
            var spaceResults = new List <ProvisionResults.Space>();

            foreach (var description in descriptions)
            {
                var spaceId = await GetExistingSpaceOrCreate(httpClient, logger, parentId, description);

                if (spaceId != Guid.Empty)
                {
                    //Create Data Types - Added by Abhilash
                    //First one is Level
                    Models.SensorDataType dataTypeLevel = new Models.SensorDataType();
                    dataTypeLevel.Category     = "SensorDataType";
                    dataTypeLevel.Description  = "Level data type";
                    dataTypeLevel.Disabled     = false;
                    dataTypeLevel.Name         = "Level";
                    dataTypeLevel.FriendlyName = "Level";
                    dataTypeLevel.LogicOrder   = 0;
                    dataTypeLevel.SpaceId      = spaceId.ToString();

                    string responseLevel = await CreateDataType(httpClient, dataTypeLevel, logger);

                    logger.LogInformation($"Completed New Data Type Level - Abh2020.");

                    Models.SensorDataType dataTypeValvePos = new Models.SensorDataType();
                    dataTypeValvePos.Category     = "SensorDataType";
                    dataTypeValvePos.Description  = "ValvePosition data type";
                    dataTypeValvePos.Disabled     = false;
                    dataTypeValvePos.Name         = "ValvePosition";
                    dataTypeValvePos.FriendlyName = "ValvePosition";
                    dataTypeValvePos.LogicOrder   = 0;
                    dataTypeValvePos.SpaceId      = spaceId.ToString();

                    string responseValvePos = await CreateDataType(httpClient, dataTypeValvePos, logger);

                    logger.LogInformation($"Completed New Data Type ValvePosition - Abh2020.");


                    // This must happen before devices (or anyhting that could have devices like other spaces)
                    // or the device create will fail because a resource is required on an ancestor space
                    if (description.resources != null)
                    {
                        await CreateResources(httpClient, logger, description.resources, spaceId);
                    }

                    var devices = description.devices != null
                        ? await CreateDevices(httpClient, logger, description.devices, spaceId)
                        : Array.Empty <Models.Device>();

                    if (description.matchers != null)
                    {
                        await CreateMatchers(httpClient, logger, description.matchers, spaceId);
                    }

                    if (description.userdefinedfunctions != null)
                    {
                        await CreateUserDefinedFunctions(httpClient, logger, description.userdefinedfunctions, spaceId);
                    }

                    if (description.roleassignments != null)
                    {
                        await CreateRoleAssignments(httpClient, logger, description.roleassignments, spaceId);
                    }

                    var childSpacesResults = description.spaces != null
                        ? await CreateSpaces(httpClient, logger, description.spaces, spaceId)
                        : Array.Empty <ProvisionResults.Space>();

                    var sensors = await Api.GetSensorsOfSpace(httpClient, logger, spaceId);

                    spaceResults.Add(new ProvisionResults.Space()
                    {
                        Id      = spaceId,
                        Devices = devices.Select(device => new ProvisionResults.Device()
                        {
                            ConnectionString = device.ConnectionString,
                            HardwareId       = device.HardwareId,
                        }),
                        Sensors = sensors.Select(sensor => new ProvisionResults.Sensor()
                        {
                            DataType   = sensor.DataType,
                            HardwareId = sensor.HardwareId,
                        }),
                        Spaces = childSpacesResults,
                    });
                }
            }

            return(spaceResults);
        }