Esempio n. 1
0
        public async override Task TestFindByIdAsync()
        {
            int     StationId = 1;
            Station station   = await stationDao.FindByIdAsync(StationId);

            Assert.IsTrue(station.StationId == StationId);
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> GetStation(int id)
        {
            IStationDao   stationDao  = AdoFactory.Instance.GetStationDao("wetr");
            IAddressDao   addressDao  = AdoFactory.Instance.GetAddressDao("wetr");
            ICommunityDao communitDao = AdoFactory.Instance.GetCommunityDao("wetr");
            IDistrictDao  districtDao = AdoFactory.Instance.GetDistrictDao("wetr");
            IProvinceDao  provinceDao = AdoFactory.Instance.GetProvinceDao("wetr");
            ICountryDao   countryDao  = AdoFactory.Instance.GetCountryDao("wetr");

            Station myStations = await stationDao.FindByIdAsync(id);

            if (myStations == null)
            {
                return(Content(HttpStatusCode.BadRequest, new object()));
            }

            StationDTO station = new StationDTO(myStations);

            station.CommunityId = (await addressDao.FindByIdAsync(station.AddressId)).CommunityId;
            station.DistrictId  = (await communitDao.FindByIdAsync(station.CommunityId)).DistrictId;
            station.ProvinceId  = (await districtDao.FindByIdAsync(station.DistrictId)).ProvinceId;
            station.CountryId   = (await provinceDao.FindByIdAsync(station.ProvinceId)).CountryId;
            station.Location    = (await addressDao.FindByIdAsync(station.AddressId)).Location;

            return(Content(HttpStatusCode.OK, station));
        }
Esempio n. 3
0
        public async Task <IHttpActionResult> EditStationAsync(StationDTO station)
        {
            /* Check if model is valid */
            if (!ModelState.IsValid)
            {
                var errors = ModelState.ToDictionary(
                    kvp => kvp.Key,
                    kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                    );
                return(Content(HttpStatusCode.BadRequest, errors));
            }
            string token  = Request.Headers.GetValues("Authorization").FirstOrDefault();
            int    userId = JwtHelper.Instance.GetUserId(token);

            IStationDao stationDao    = AdoFactory.Instance.GetStationDao("wetr");
            Station     stationToEdit = await stationDao.FindByIdAsync(station.StationId);

            /* If the station doesn't belong to the user */
            if (stationToEdit.UserId != userId)
            {
                return(Content(HttpStatusCode.Forbidden, new object()));
            }

            /* Create new address */
            IAddressDao addressDao = AdoFactory.Instance.GetAddressDao("wetr");

            await addressDao.UpdateAsync(new Address()
            {
                AddressId   = stationToEdit.AddressId,
                CommunityId = station.CommunityId,
                Location    = station.Location,
                Zip         = "NO_ZIP"
            });

            int newAddressId = Convert.ToInt32((await addressDao.GetNextId()) - 1);

            station.AddressId = newAddressId;


            /* Edit Station */
            await stationDao.UpdateAsync(station.ToStation());

            return(Content(HttpStatusCode.OK, new object()));
        }
        public async Task <IHttpActionResult> QueryMeasurements(QueryRequest query)
        {
            /* Check if model is valid */
            if (!ModelState.IsValid)
            {
                var errors = ModelState.ToDictionary(
                    kvp => kvp.Key,
                    kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
                    );
                return(Content(HttpStatusCode.BadRequest, errors));
            }

            IMeasurementDao dao        = AdoFactory.Instance.GetMeasurementDao("wetr");
            IStationDao     stationDao = AdoFactory.Instance.GetStationDao("wetr");
            Station         s          = await stationDao.FindByIdAsync(query.StationId);

            double[] result = await dao.GetQueryResult(query.Start, query.End, query.MeasurementTypeId, query.ReductionTypeId, query.GroupingTypeId,
                                                       new List <Station>() { s });

            return(Content(HttpStatusCode.OK, result));
        }
Esempio n. 5
0
        private async Task CheckForWarning(Measurement m)
        {
            Warning warning = null;

            // Temperature
            if (m.MeasurementTypeId == 1)
            {
                if (m.Value < -10)
                {
                    warning = new Warning()
                    {
                        Type      = WarningType.LowTemperature,
                        Timestamp = m.TimesStamp,
                        StationId = m.StationId,
                        TypeId    = m.MeasurementTypeId
                    };
                }
                else if (m.Value > 38)
                {
                    warning = new Warning()
                    {
                        Type      = WarningType.HighTemperature,
                        Timestamp = m.TimesStamp,
                        StationId = m.StationId,
                        TypeId    = m.MeasurementTypeId
                    };
                }
            }
            else if (m.MeasurementTypeId == 3)
            {
                if (m.Value >= 35)
                {
                    warning = new Warning()
                    {
                        Type      = WarningType.HeavyRain,
                        Timestamp = m.TimesStamp,
                        StationId = m.StationId,
                        TypeId    = m.MeasurementTypeId
                    };
                }
            }
            else if (m.MeasurementTypeId == 5)
            {
                if (m.Value >= 65)
                {
                    warning = new Warning()
                    {
                        Type      = WarningType.Storm,
                        Timestamp = m.TimesStamp,
                        StationId = m.StationId,
                        TypeId    = m.MeasurementTypeId
                    };
                }
            }
            else if (m.MeasurementTypeId == 4)
            {
                if (m.Value >= 45)
                {
                    warning = new Warning()
                    {
                        Type      = WarningType.DryAir,
                        Timestamp = m.TimesStamp,
                        StationId = m.StationId,
                        TypeId    = m.MeasurementTypeId
                    };
                }
            }

            /* Check warning interval */
            if (warning != null)
            {
                foreach (Warning w in PastWarnings)
                {
                    /* Remove old warnings */
                    if (w.Timestamp < DateTime.Now.AddHours(-1))
                    {
                        PastWarnings.Remove(w);
                    }
                }

                /* If there was alredy a warning of this type for this station in the last hour*/
                if (PastWarnings.Where(w => w.StationId == m.StationId && w.TypeId == m.MeasurementTypeId).Any())
                {
                    return;
                }

                PastWarnings.Add(warning);

                Station station = await stationDao.FindByIdAsync(m.StationId);

                string prov = (await addressManager.GetProvinceForAddressId(station.AddressId)).Name;
                this.Tweet("Warning: " + warning.Type.ToString() + " for province '" + prov + "'");
            }
        }