public async Task <IActionResult> PutFider([FromRoute] int id, [FromBody] Fider fider)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != fider.Id)
            {
                return(BadRequest());
            }

            _context.Entry(fider).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FiderExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PostFider([FromBody] Fider fider)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Fiders.Add(fider);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetFider", new { id = fider.Id }, fider));
        }
Ejemplo n.º 3
0
        private void CreateFider()
        {
            Fider fider = new Fider
            {
                Id      = Faker.NumberFaker.Number(100, 10000000),
                Name    = Faker.StringFaker.Alpha(10),
                Power   = Faker.NumberFaker.Number(128, 1024),
                Voltage = Faker.NumberFaker.Number(110, 510),
                Geocode = Faker.LocationFaker.ZipCode()
            };

            _repository.Insert(fider);
        }
Ejemplo n.º 4
0
        public static SubstationsCollection PointToBalanceSubstations(EmcosPoint root)
        {
            var result = new SubstationsCollection(null);

            if (root.Children == null)
            {
                return(null);
            }

            foreach (EmcosPoint departamentPoint in root.Children) // департаменты (РЭС)
            {
                if (departamentPoint.ElementType == ElementTypes.DEPARTAMENT && departamentPoint.TypeCode == "RES" && departamentPoint.Children != null)
                {
                    foreach (EmcosPoint substationPoint in departamentPoint.Children) // подстанции
                    {
                        var substation = new Substation();
                        substation.Departament = departamentPoint.Name;
                        // название имеет формат вида ПС 35кВ Бакшты
                        var nameParts = substationPoint.Name.Split(' ');
                        if (nameParts.Length < 3)
                        {
                            throw new System.ArgumentException(string.Format("Неверный формат названия подстанции - '{0}'", substationPoint.Name));
                        }
                        // выделяем название
                        var name = "";
                        for (int i = 2; i < nameParts.Length; i++)
                        {
                            name += nameParts[i];
                        }
                        substation.Name = name;
                        // выделяем напряжение
                        substation.Voltage = nameParts[1];

                        substation.Code = substationPoint.Code;
                        substation.Id   = substationPoint.Id;

                        if (substationPoint.Children != null)
                        {
                            foreach (EmcosPoint groupPoint in substationPoint.Children) // группы
                            {
                                if (groupPoint.Children == null)
                                {
                                    continue;
                                }

                                IBalanceGroupItem group;
                                IBalanceItem      item;
                                switch (groupPoint.TypeCode)
                                {
                                case "AUXILIARY":
                                    group = new SubstationAuxiliary();
                                    foreach (var subgroupPoint in groupPoint.Children)
                                    {
                                        if (subgroupPoint.ElementType == ElementTypes.UNITTRANSFORMER)
                                        {
                                            item = new UnitTransformer();
                                        }
                                        else
                                        {
                                            item = new UnitTransformerBus();
                                        }
                                        item.Name        = subgroupPoint.Name;
                                        item.Code        = subgroupPoint.Code;
                                        item.Id          = subgroupPoint.Id;
                                        item.Description = subgroupPoint.Description;
                                        item.SetSubstation(substation);
                                        group.Children.Add(item);
                                    }
                                    substation.Children.Add(group);
                                    break;

                                case "POWERTRANSFORMERS":
                                    group = new SubstationPowerTransformers();
                                    foreach (var subgroupPoint in groupPoint.Children)
                                    {
                                        item = new PowerTransformer
                                        {
                                            Name        = subgroupPoint.Name,
                                            Code        = subgroupPoint.Code,
                                            Id          = subgroupPoint.Id,
                                            Description = subgroupPoint.Description
                                        };
                                        item.SetSubstation(substation);
                                        group.Children.Add(item);
                                    }
                                    substation.Children.Add(group);
                                    break;

                                case "SECTIONS":
                                    // проход по ступеням напряжения
                                    foreach (var subgroupPoint in groupPoint.Children)
                                    {
                                        SubstationSection highSection = null;
                                        // по секциям
                                        if (subgroupPoint.Children != null)
                                        {
                                            foreach (var sectionPoint in subgroupPoint.Children)
                                            {
                                                if (sectionPoint.TypeCode == "SECTIONBUS")
                                                {
                                                    var substationSection = new SubstationSection
                                                    {
                                                        Voltage     = subgroupPoint.Name,
                                                        Name        = sectionPoint.Name,
                                                        Code        = sectionPoint.Code,
                                                        Id          = sectionPoint.Id,
                                                        Description = sectionPoint.Description
                                                    };
                                                    substationSection.SetSubstation(substation);

                                                    if (sectionPoint.Children != null)
                                                    {
                                                        foreach (var sectionChildPoint in sectionPoint.Children)
                                                        {
                                                            IBalanceItem childItem = null;
                                                            switch (sectionChildPoint.ElementType)
                                                            {
                                                            case ElementTypes.FIDER:
                                                                childItem = new Fider();
                                                                break;

                                                            case ElementTypes.POWERTRANSFORMER:
                                                                childItem = new PowerTransformer();
                                                                break;

                                                            case ElementTypes.UNITTRANSFORMERBUS:
                                                                childItem = new UnitTransformerBus();
                                                                break;

                                                            case ElementTypes.UNITTRANSFORMER:
                                                                childItem = new UnitTransformer();
                                                                break;
                                                            }
                                                            if (childItem == null)
                                                            {
                                                                System.Diagnostics.Debugger.Break();
                                                            }

                                                            childItem.Name        = sectionChildPoint.Name;
                                                            childItem.Code        = sectionChildPoint.Code;
                                                            childItem.Id          = sectionChildPoint.Id;
                                                            childItem.Description = sectionChildPoint.Description;
                                                            childItem.SetSubstation(substation);

                                                            substationSection.Children.Add(childItem);
                                                        }
                                                    }
                                                    substation.Children.Add(substationSection);
                                                }
                                                else
                                                {
                                                    if (highSection == null)
                                                    {
                                                        highSection = new SubstationSection
                                                        {
                                                            Voltage     = subgroupPoint.Name,
                                                            Name        = subgroupPoint.Name,
                                                            Code        = subgroupPoint.Code,
                                                            Id          = subgroupPoint.Id,
                                                            Description = subgroupPoint.Description
                                                        };
                                                        highSection.SetSubstation(substation);
                                                    }
                                                    switch (sectionPoint.EcpName)
                                                    {
                                                    case "TRANSFORMER":
                                                        var pt = new PowerTransformer
                                                        {
                                                            Id          = sectionPoint.Id,
                                                            Code        = sectionPoint.Code,
                                                            Name        = sectionPoint.Name,
                                                            Description = sectionPoint.Description
                                                        };
                                                        pt.SetSubstation(substation);
                                                        highSection.Children.Add(pt);
                                                        break;

                                                    case "LINE":
                                                        var f = new Fider
                                                        {
                                                            Id          = sectionPoint.Id,
                                                            Code        = sectionPoint.Code,
                                                            Name        = sectionPoint.Name,
                                                            Description = sectionPoint.Description
                                                        };
                                                        f.SetSubstation(substation);
                                                        highSection.Children.Add(f);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        if (highSection != null)
                                        {
                                            substation.Children.Add(highSection);
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        result.Add(substation);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Загрузка файла сессии версии 1.0
        /// </summary>
        /// <remarks>
        /// Файл сессии версии 1.0 представляет собой сериализованный в json объект BalanceSession дополнительно сжатый gzip
        /// </remarks>
        /// <param name="fileName">Имя файла</param>
        /// <param name="balanceSession">Ссылка на сессию</param>
        /// <returns>Сессия</returns>
        private BalanceSession LoadDataFromFileVersion_1_0(string fileName)
        {
            const string UNKNOWN_DEPARTAMENT_NAME = "<неизвестно>";

            BalanceSession balanceSession = new BalanceSession();

            try
            {
                dynamic obj = ParseJsonFromFile(fileName);
                if (obj != null)
                {
                    var fi = new FileInfo(fileName);
                    BalanceSessionInfo balanceSessionInfo = LoadSessionInfoFromOldFileFormat(obj);
                    balanceSessionInfo.FileSize = fi.Length;
                    balanceSession.Info         = balanceSessionInfo;

                    if (obj.Substations != null)
                    {
                        balanceSession.BalancePoints.Clear();

                        // создание списка департаментов (рэс)
                        HashSet <string> departamentsSet = new HashSet <string>();
                        foreach (var item in obj.Substations)
                        {
                            string departament = item?.Departament?.ToString();
                            if (String.IsNullOrWhiteSpace(departament) == false && departamentsSet.Contains(departament) == false)
                            {
                                departamentsSet.Add(departament);
                            }
                        }
                        Dictionary <string, IHierarchicalEmcosPoint> departamentsDictionary = new Dictionary <string, IHierarchicalEmcosPoint>();
                        foreach (var item in departamentsSet)
                        {
                            departamentsDictionary.Add(item, new EmcosPoint()
                            {
                                ElementType = ElementTypes.DEPARTAMENT, TypeCode = "RES", Name = item
                            });
                        }
                        departamentsDictionary.Add("?", new EmcosPoint()
                        {
                            ElementType = ElementTypes.DEPARTAMENT, TypeCode = "RES", Name = UNKNOWN_DEPARTAMENT_NAME
                        });
                        System.Collections.IList parentList = departamentsDictionary["?"].Children;

                        void parseBase(IHierarchicalEmcosPoint source, dynamic data)
                        {
                            try
                            {
                                source.Id          = data.Id ?? 0;
                                source.Code        = data.Code;
                                source.Name        = data.Title;
                                source.Status      = data.Status;
                                source.Description = data.Description;
                                if (String.IsNullOrEmpty(source.Description) == false)
                                {
                                    balanceSession.DescriptionsById.Add(source.Id, source.Description);
                                }
                            }
                            catch (Exception e)
                            {
                                _callBackAction(e);
                            }
                        }

                        object getPropertyValue(string propertyName, dynamic jObject)
                        {
                            if ((jObject is Newtonsoft.Json.Linq.JObject o) && (o.Property(propertyName) != null))
                            {
                                return(o.Property(propertyName).Value);
                            }
                            else
                            {
                                return(null);
                            }
                        }

                        double?getDoubleValue(string propertyName, dynamic jObject)
                        {
                            object value = getPropertyValue(propertyName, jObject);

                            if (value == null)
                            {
                                return(null);
                            }
                            else
                            {
                                return(Convert.ToDouble(value));
                            }
                        }
                        bool getBoolValue(string propertyName, dynamic jObject)
                        {
                            object value = getPropertyValue(propertyName, jObject);

                            if (value == null)
                            {
                                return(false);
                            }
                            else
                            {
                                return(Convert.ToBoolean(value));
                            }
                        }

                        void parseTokens(IEnumerable <Newtonsoft.Json.Linq.JToken> tokens)
                        {
                            foreach (Newtonsoft.Json.Linq.JToken jobj in tokens)
                            {
                                if (jobj.Children().Where(i => (i is Newtonsoft.Json.Linq.JProperty p) && p.Name == "$type").FirstOrDefault() is Newtonsoft.Json.Linq.JProperty typeProperty)
                                {
                                    string tokenType = typeProperty.Value.ToString();
                                    if (String.IsNullOrWhiteSpace(tokenType) == false)
                                    {
                                        string   t     = tokenType.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0];
                                        string[] parts = t.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                                        if (parts.Length > 1 && parts[0] == "TMP")
                                        {
                                            string part = parts[parts.Length - 1];

                                            IBalanceGroupItem group = null;
                                            dynamic           data  = jobj;
                                            switch (part)
                                            {
                                            case "Substation":
                                                group = new Substation
                                                {
                                                    Departament = data.Departament,
                                                    Voltage     = data.Voltage
                                                };
                                                break;

                                            case "SubstationSection":
                                                group = new SubstationSection
                                                {
                                                    Voltage = data.Voltage
                                                };
                                                break;

                                            case "SubstationPowerTransformers":
                                                group = new SubstationPowerTransformers();
                                                break;

                                            case "SubstationAuxiliary":
                                                group = new SubstationAuxiliary();
                                                break;

                                            default:
                                            {
                                                IBalanceItem item = null;
                                                switch (part)
                                                {
                                                case "Fider":
                                                    item = new Fider();
                                                    break;

                                                case "PowerTransformer":
                                                    item = new PowerTransformer();
                                                    break;

                                                case "UnitTransformer":
                                                    item = new UnitTransformer();
                                                    break;

                                                case "UnitTransformerBus":
                                                    item = new UnitTransformerBus();
                                                    break;

                                                default:
                                                    System.Diagnostics.Debugger.Break();
                                                    break;
                                                }
                                                if (item != null)
                                                {
                                                    parseBase(item, data);
                                                }

                                                object value      = getPropertyValue("DailyEplus", data);
                                                string daysValues = value?.ToString();
                                                item.ActiveEnergy.Plus.DaysValues = (daysValues != null && daysValues.StartsWith("<"))
                                                            ? null
                                                            : (value as Newtonsoft.Json.Linq.JToken)?.ToObject <List <double?> >();

                                                value      = getPropertyValue("DailyEminus", data);
                                                daysValues = value?.ToString();
                                                item.ActiveEnergy.Minus.DaysValues = (daysValues != null && daysValues.StartsWith("<"))
                                                            ? null
                                                            : (value as Newtonsoft.Json.Linq.JToken)?.ToObject <List <double?> >();

                                                item.ActiveEnergy.Plus.MonthValue  = getDoubleValue("MonthEplus", data);
                                                item.ActiveEnergy.Minus.MonthValue = getDoubleValue("MonthEminus", data);

                                                item.ActiveEnergy.Plus.CorrectionValue  = getDoubleValue("Eplus", data) - getDoubleValue("DayEplusValue", data);
                                                item.ActiveEnergy.Minus.CorrectionValue = getDoubleValue("Eminus", data) - getDoubleValue("DayEminusValue", data);

                                                item.ActiveEnergy.Plus.UseMonthValue  = getBoolValue("UseMonthValue", data);
                                                item.ActiveEnergy.Minus.UseMonthValue = getBoolValue("UseMonthValue", data);

                                                if (group != null)
                                                {
                                                    group.Children.Add(item);
                                                }
                                                if (item == null && group == null)
                                                {
                                                    System.Diagnostics.Debugger.Break();
                                                }
                                            }
                                            break;
                                            }
                                            if (group != null)
                                            {
                                                parseBase(group, data);

                                                if (group is Substation substation)
                                                {
                                                    departamentsDictionary[substation.Departament].Children.Add(substation);
                                                }
                                                else
                                                {
                                                    parentList.Add(group);
                                                }

                                                var childrenProperty = jobj.Children()
                                                                       .Where(i => (i is Newtonsoft.Json.Linq.JProperty p) && p.Name == "Children")
                                                                       .FirstOrDefault() as Newtonsoft.Json.Linq.JProperty;
                                                if (childrenProperty != null && childrenProperty.Value != null && childrenProperty.Value is Newtonsoft.Json.Linq.JArray childrenArray)
                                                {
                                                    System.Collections.IList oldParentList = parentList;
                                                    parentList = group.Children;
                                                    parseTokens(childrenArray);
                                                    parentList = oldParentList;
                                                }
                                                else
                                                {
                                                    System.Diagnostics.Debugger.Break();
                                                }
                                            }
                                        }
                                    } // значение типа токена пустое
                                    else
                                    {
                                        System.Diagnostics.Debugger.Break();
                                    }
                                } // не найден тип токена
                                else
                                {
                                    System.Diagnostics.Debugger.Break();
                                }
                            }