示例#1
0
        private static async Task <List <Site> > GetSampleSites(HttpClient client, Site parentSite, double utcOffset, IEnumerable <string> pointNames)
        {
            Console.Write("GetSampleSites started...");
            List <Site> ret = new List <Site>();

            foreach (var pointName1 in pointNames)
            {
                string pointName = pointName1.Trim();
                if (ret.Exists(x => x.Name.ToUpper() == pointName.ToUpper() && x.ParentId == parentSite.Id))
                {
                    continue;
                }

                List <Site> sites = await SitesAPI.GetByFilterAsync(client, new SiteFilter { NameLike = pointName, ParentId = parentSite.Id });

                sites = sites.Where(x => x.Name.ToUpper() == pointName.ToUpper()).ToList();

                Site site;

                if (sites == null || sites.Count == 0)
                {
                    site = new Site
                    {
                        Name       = pointName,
                        SiteTypeId = (int)EnumSiteType.SamplePoint,
                        Code       = pointName.Substring(0, pointName.Length < 20 ? pointName.Length : 20),

                        AddrRegionId = parentSite.AddrRegionId,
                        Lat          = parentSite.Lat,
                        Lon          = parentSite.Lon,
                        OwnerId      = parentSite.OwnerId,
                        ParentId     = parentSite.Id
                    };
                    site.Id = await SitesAPI.CreateAsync(client, site);

                    SiteAttributesAPI.CreateAsync(client, new SiteAttributeValue
                    {
                        DateS = DATE_S_SITE_ATTR,
                        SiteAttributeTypeId = (int)EnumSiteAttrType.UTCOffset,
                        SiteId = site.Id,
                        Value  = utcOffset.ToString()
                    });
                }
                else if (sites.Count == 1)
                {
                    site = sites[0];
                }
                else
                {
                    throw new Exception($"More than one site with name [{pointName}]");
                }

                ret.Add(site);
            }
            Console.WriteLine($" and endeded. {ret.Count} items generated.");
            return(ret);
        }
示例#2
0
        /// <summary>
        /// Чтение данных из файла и запись в БД Амур через REST сервис.
        /// </summary>
        /// <param name="filePath">Путь к файлу.</param>
        /// <param name="client">REST</param>
        /// <returns></returns>
        public static async Task <int> Import(HttpClient client, string filePath, FileType dataFileType)
        {
            // PARSE

            List <Data> datas = Parse(filePath, dataFileType);

            // GET UTC OFFSET

            SiteAttributeValue sav = await SiteAttributesAPI.GetByFilterAsync(client, new SiteAttribute
                                                                              { DateS = DateTime.Today, SiteAttributeTypeId = (int)EnumSiteAttrType.UTCOffset, SiteId = SITE_ID_BPISTAC });

            if (sav == null)
            {
                throw new Exception($"Отсутствует UTCOffset для сайта id={SITE_ID_BPISTAC}.");
            }
            int utcOffset = int.Parse(sav.Value);

            // GET Sample attributes
            Site parentSite = await SitesAPI.GetByIdAsync(client, SITE_ID_BPISTAC);

            List <Site> sampleSites = await GetSampleSites(client, parentSite, utcOffset, datas.Select(x => x.PointName).Distinct());

            List <DeviceItem> deviceItems = await GetDeviceItems(client, _deviceNames);

            List <Sample> samples = await GetSamples(client, sampleSites, datas);

            List <SampleDeviceItem> sampleDeviceItems = await GetSampleDeviceItems(client, deviceItems, samples);

            // PREPARE CATALOG BUFFER

            List <Catalog> catalogs = await CatalogsAPI.GetByFilterAsync(client, new CatalogFilter { Sites = sampleSites.Select(x => x.Id).ToList() });

            // INSERT DATA
            int itemsCount     = 0;
            int bufItemsLength = 50000;

            for (int i = 0; i < datas.Count; i++)
            {
                Data data = datas[i];

                if (_amurVars.Count != datas[i].Values.Count)
                {
                    throw new Exception("(_amurVars.Count != datas[i].Values.Count)");
                }

                // GET SAMPLE_DEVICE_ITEM

                int siteId;
                {
                    IEnumerable <Site> sites1 = sampleSites.Where(x => x.Name.ToUpper() == data.PointName.Trim().ToUpper());
                    if (sites1.Count() != 1)
                    {
                        throw new Exception("(sites.Count() != 1)");
                    }
                    siteId = sites1.ElementAt(0).Id;
                }
                int sampleId;
                {
                    IEnumerable <Sample> samples1 = samples.Where(x => x.SiteId == siteId && x.SampleNum == data.SampleNum);
                    if (samples1.Count() != 1)
                    {
                        throw new Exception("(samples1.Count() != 1)");
                    }
                    sampleId = samples1.ElementAt(0).Id;
                }

                for (int iVar = 0; iVar < _amurVars.Count;)
                {
                    // GET SAMPLE DEVICE ITEM
                    int sampleDeviceItemId;
                    {
                        IEnumerable <SampleDeviceItem> sampleDeviceItems1 = sampleDeviceItems.Where(x =>
                                                                                                    x.SampleId == sampleId &&
                                                                                                    x.DeviceItemId == deviceItems[_varXdev[iVar]].Id
                                                                                                    );
                        if (sampleDeviceItems1.Count() != 1)
                        {
                            throw new Exception("(sampleDeviceItems1.Count() != 1)");
                        }
                        sampleDeviceItemId = sampleDeviceItems1.ElementAt(0).Id;
                    }

                    // GET or CREATE CATALOGS FOR VARIABLE

                    Catalog catalog = new Catalog
                    {
                        MethodId     = (int)EnumMethod.ObservationInSitu,
                        ParentId     = null,
                        SiteId       = siteId,
                        SourceId     = SOURCE_ID_PGI,
                        VariableId   = _amurVars[iVar][0],
                        ValueTypeId  = _amurVars[iVar][1],
                        OffsetTypeId = (int)EnumOffsetType.Sample,
                        OffsetValue  = sampleDeviceItemId
                    };

                    List <Catalog> catalogs1 = catalogs.FindAll(x =>
                                                                x.MethodId == catalog.MethodId &&
                                                                !x.ParentId.HasValue &&
                                                                x.SiteId == catalog.SiteId &&
                                                                x.SourceId == catalog.SourceId &&
                                                                x.VariableId == catalog.VariableId &&
                                                                x.ValueTypeId == catalog.ValueTypeId &&
                                                                x.OffsetTypeId == catalog.OffsetTypeId &&
                                                                x.OffsetValue == catalog.OffsetValue
                                                                );

                    if (catalogs1.Count == 0)
                    {
                        catalog.Id = await CatalogsAPI.CreateAsync(client, catalog);

                        catalogs.Add(catalog);
                    }
                    else if (catalogs1.Count == 1)
                    {
                        catalog = catalogs1[0];
                    }
                    else
                    {
                        throw new Exception($"(catalogs1.Count = {catalogs1.Count })");
                    }


                    // CONVERT DATA 2 AMUR DATAVALUES
                    List <DataValue> bufDatas = new List <DataValue>(bufItemsLength);
                    for (int k = 0; k < bufItemsLength; k++)
                    {
                        if (iVar < datas[i].Values.Count)
                        {
                            bufDatas.Add(new DataValue()
                            {
                                CatalogId  = catalog.Id,
                                Date       = data.Date,
                                DateTypeId = (int)EnumDateType.LOC,
                                UTCOffset  = utcOffset,
                                Value      = data.Values[iVar],
                                FlagAQC    = (byte)EnumFlagAQC.NoAQC
                            });
                            iVar++;
                            itemsCount++;
                        }
                    }

                    // INSERT DATAVALUES BUF
                    Console.WriteLine($"Insert data buf ({bufDatas.Count} elements)...");
                    //////await DataValuesAPI.Insert(client, bufDatas);
                }
            }
            return(itemsCount);
        }
示例#3
0
        public static int EMERCIT_ORG_ID         = 1297; // EMERCIT

        /// <summary>
        /// Чтение данных из файла и запись в БД Амур через REST сервис.
        /// </summary>
        /// <param name="filePath">Путь к файлу.</param>
        /// <param name="client">REST</param>
        /// <returns></returns>
        public static async Task <int> Import(HttpClient client, string filePath)
        {
            // Дата актуальности, которая фигурирует при записи атрибутов пункта.
            DateTime importSiteAttrDateActual = new DateTime(2000, 1, 1);

            // PARSE FILE

            List <FileSite.SiteEMERCIT> sites = FileSite.Parse(filePath);

            Console.WriteLine($"{sites.Count} readed from file [{filePath}]");

            // INSERT DATA
            List <Site> allSites = await SitesAPI.GetByFilterAsync(client, new Amur.Data.Filters.SiteFilter {
                OwnerId = EMERCIT_ORG_ID, SiteTypeId = EMERCIT_SITE_TYPE_ID
            });

            int i = 1;

            foreach (var siteE in sites)
            {
                Console.WriteLine($"Insert site #{i++}");

                // GET EXISTING or CREATE SITE

                Site existSite = allSites.FirstOrDefault(x => x.Code == siteE.Site.Code && x.Name == siteE.Site.Name);
                int  siteId    = (existSite == null) ? await SitesAPI.CreateAsync(client, siteE.Site) : existSite.Id;

                // GET EXISTING or CREATE GEOOBJECTS

                int?goFallIntoId = await GetGeoObjectId(client, siteE.FallIntoName);

                int?goId = await GetGeoObjectId(client, siteE.RiverName, (int)EnumGeoObject.River, goFallIntoId);

                if (goId.HasValue)
                {
                    List <SiteXGeoObject> sgos = await SiteXGeoObjectAPI.GetBySiteIdAsync(client, siteId);

                    if (sgos == null || sgos.Count == 0)
                    {
                        SiteXGeoObjectAPI.CreateAsync(client, new SiteXGeoObject {
                            SiteId = siteId, GeoObjectId = (int)goId, OrderBy = -1
                        });
                    }
                }
                // CREATE SITE ATTRIBUTES

                await SiteAttributesAPI.CreateAsync(client, siteId, (int)EnumSiteAttrType.CatchmentArea, siteE.CatchmentArea, importSiteAttrDateActual);

                await SiteAttributesAPI.CreateAsync(client, siteId, (int)EnumSiteAttrType.NYa, siteE.NYa, importSiteAttrDateActual);

                await SiteAttributesAPI.CreateAsync(client, siteId, (int)EnumSiteAttrType.OYa, siteE.OYa, importSiteAttrDateActual);

                await SiteAttributesAPI.CreateAsync(client, siteId, (int)EnumSiteAttrType.DistFromMouth, siteE.DistFromMouth, importSiteAttrDateActual);

                await SiteAttributesAPI.CreateAsync(client, siteId, (int)EnumSiteAttrType.MarkSiteZeroBS77, siteE.DeviceLevel, importSiteAttrDateActual);

                await SiteAttributesAPI.CreateAsync(client, siteId, (int)EnumSiteAttrType.UTCOffset, siteE.UTCOffset, importSiteAttrDateActual);
            }
            Console.WriteLine($"Site import ended...");
            return(sites.Count);
        }
示例#4
0
        static int InsertData(HttpClient client, List <Data> datas)
        {
            int bufItemsLength = 1000;
            int itemsCount     = 0;

            for (int i = 0; i < datas.Count; i++)
            {
                if (datas[i].Varoff == null)
                {
                    continue;
                }

                Site site = datas[i].Site;

                // GET UTC OFFSET

                SiteAttributeValue sav = SiteAttributesAPI.GetByFilterAsync(client, new SiteAttribute
                {
                    DateS = DateTime.Today, SiteAttributeTypeId = (int)EnumSiteAttrType.UTCOffset, SiteId = site.Id
                })
                                         .Result;
                if (sav == null)
                {
                    throw new Exception($"Отсутствует UTCOffset для сайта [{site.Name}] ({site.Id}).");
                }
                int siteUTCOffset = int.Parse(sav.Value);

                // GET or CREATE CATALOG

                List <Catalog> catalogs = CatalogsAPI.GetByFilterAsync(client, new CatalogFilter
                {
                    Sites = new List <int> {
                        site.Id
                    },
                    Variables = new List <int> {
                        datas[i].Varoff.VariableId
                    }
                }).Result;
                if (catalogs.Count > 1)
                {
                    throw new Exception("(catalogs.Count > 1)");
                }

                int catalogId;
                if (catalogs.Count == 0)
                {
                    Catalog catalog = new Catalog
                    {
                        SiteId      = site.Id,
                        VariableId  = datas[i].Varoff.VariableId * (datas[i].Varoff.VariableId < 0 ? -1 : 1),
                        ValueTypeId = (int)EnumValueType.FieldObservation,
                        MethodId    = (int)EnumMethod.ObservationInSitu,
                        SourceId    = FileSite.EMERCIT_ORG_ID,

                        OffsetTypeId = datas[i].Varoff.OffsetTypeId,
                        OffsetValue  = datas[i].Varoff.OffsetValue,

                        ParentId = 0
                    };
                    catalogId = CatalogsAPI.CreateAsync(client, catalog).Result;
                }
                else
                {
                    catalogId = catalogs[0].Id;
                }

                for (int j = 0; j < datas[i].DateValues.Count;)
                {
                    // CONVERT DATA 2 AMUR DATAVALUES
                    List <DataValue> bufDatas = new List <DataValue>(bufItemsLength);
                    for (int k = 0; k < bufItemsLength; k++)
                    {
                        if (j < datas[i].DateValues.Count)
                        {
                            bufDatas.Add(new DataValue()
                            {
                                CatalogId  = catalogId,
                                Date       = datas[i].DateValues[j].Date,
                                DateTypeId = (int)EnumDateType.LOC,
                                UTCOffset  = siteUTCOffset,
                                Value      = datas[i].DateValues[j].Value,
                                FlagAQC    = (byte)EnumFlagAQC.NoAQC
                            });
                            j++;
                            itemsCount++;
                        }
                    }

                    // INSERT DATAVALUES BUF

                    Console.WriteLine($"Insert {bufDatas.Count} elements from data buf. Total inserted {itemsCount} of {datas[i].DateValues.Count}.");

                    DataValuesAPI.InsertAsync(client, bufDatas);
                }
            }

            return(itemsCount);
        }