예제 #1
0
        private void CreateAttributes(DataSourceDto dto)
        {
            this.name = dto.Name;

            Writer.Seek(0, SeekOrigin.End);
            this.fixup.FixupPosition = Writer.BaseStream.Position;
            Writer.Write(dto.ConversionFunction);
            Writer.Write(Convert.ToInt32(dto.PollingInterval.TotalSeconds));
            if (this.lastReadingDao == null)
            {
                this.lastReadingDao = new BinaryFileReadingDao(this);
            }
            ReadingDto lastReadingDto;

            if (dto.LastReading != null)
            {
                lastReadingDto = dto.LastReading;
            }
            else
            {
                lastReadingDto       = new ReadingDto();
                lastReadingDto.Empty = true;
            }
            this.lastReadingDao.Create(lastReadingDto);
            if (this.rangeDao == null)
            {
                this.rangeDao = new BinaryFileRangeDao(this);
            }
            this.rangeDao.Create(dto.Range);
        }
        public void WhenReadingsExist_DeviceDetectedEventSend_ReadingsUpdated()
        {
            var ea       = new EventAggregator();
            var response = new ReadingDto[]
            {
                new ReadingDto {
                    DeviceName = "unit-test", PM25 = "1", PM10 = "2"
                }
            };
            var handler = new HttpMessageHandlerBuilder()
                          .SetupGet <ReadingDto[]>("http://1.2.3.4:8080/v1/readings", response)
                          .Build();

            var target = new MetersViewModel(ea, new HttpClient(handler));

            target.Readings.Add(new Reading()
            {
                ClientId = "existingId", DeviceName = "existing", PM25 = "10", PM10 = "20"
            });

            ea.Publish(new DeviceDetectedEvent("id", IPAddress.Parse("1.2.3.4")));

            var result = target.Readings.Last();

            Assert.AreEqual("id", result.ClientId);
            Assert.AreEqual("unit-test", result.DeviceName);
            Assert.AreEqual("1", result.PM25);
            Assert.AreEqual("2", result.PM10);
        }
        public void WhenReadingsExist_Refresh_ReadingsUpdated()
        {
            var ea = new EventAggregator();
            var initialResponse = new ReadingDto[]
            {
                new ReadingDto {
                    DeviceName = "unit-test", PM25 = "1", PM10 = "2"
                }
            };
            var updateResponse = new ReadingDto[]
            {
                new ReadingDto {
                    DeviceName = "unit-test", PM25 = "10", PM10 = "20"
                }
            };
            var handler = new HttpMessageHandlerBuilder()
                          .SetupGet("http://1.2.3.4:8080/v1/readings", initialResponse, updateResponse)
                          .Build();

            var target = new MetersViewModel(ea, new HttpClient(handler));

            ea.Publish(new DeviceDetectedEvent("id", IPAddress.Parse("1.2.3.4")));

            target.Refresh.Execute(null);

            var result = target.Readings.Single();

            Assert.AreEqual("id", result.ClientId);
            Assert.AreEqual("unit-test", result.DeviceName);
            Assert.AreEqual("10", result.PM25);
            Assert.AreEqual("20", result.PM10);
        }
        public void Create(ReadingCollectionDto dto)
        {
            SeekEnd();

            // The collection must never grow past max size
            this.maxSize = dto.MaxReadings;

            // Write maximum size
            Writer.Write(this.maxSize);

            // Record the position of the current size field
            this.currentSizePosition = Writer.BaseStream.Position;

            // Write current size
            Writer.Write(this.currentSize);

            ReadingDto emptyReadingDto = new ReadingDto();

            // Create an empty reading DTO
            emptyReadingDto.Empty     = true;
            emptyReadingDto.Value     = 0D;
            emptyReadingDto.Timestamp = DateTime.Now;

            // Create empty readings in the database
            for (int i = 0; i < this.maxSize; i++)
            {
                BinaryFileReadingDao emptyReadingDao = new BinaryFileReadingDao(archive);

                emptyReadingDao.Create(emptyReadingDto);
                this.unallocatedReadings.Add(emptyReadingDao);
            }
        }
예제 #5
0
        public async Task <IReadOnlyList <Reading> > GetAllFilteredAsync(ReadingDto readingDto)
        {
            var sql = $@"EXEC FilteredReading {readingDto.BuildingId}, {readingDto.ObjectId}, 
                      {readingDto.DataFieldId}, '{readingDto.TimestampFrom}', '{readingDto.TimestampTo}'";

            using var connection = new SqlConnection(_connectionString);

            var result = await connection.QueryAsync <Reading>(sql);

            return(result.ToList());
        }
        public void WhenNoReadingsExist_MultipleDeviceDetectedEventSend_ReadingsUpdated()
        {
            var ea          = new EventAggregator();
            var firstDevice = new ReadingDto[]
            {
                new ReadingDto {
                    DeviceName = "unit-test-1", PM25 = "1", PM10 = "2"
                }
            };
            var secondDevice = new ReadingDto[]
            {
                new ReadingDto {
                    DeviceName = "unit-test-2", PM25 = "10", PM10 = "20"
                }
            };
            var handler = new HttpMessageHandlerBuilder()
                          .SetupGet <ReadingDto[]>("http://1.2.3.4:8080/v1/readings", firstDevice)
                          .SetupGet <ReadingDto[]>("http://1.2.3.5:8080/v1/readings", secondDevice)
                          .Build();

            var target = new MetersViewModel(ea, new HttpClient(handler));

            ea.Publish(new DeviceDetectedEvent("id1", IPAddress.Parse("1.2.3.4")));
            ea.Publish(new DeviceDetectedEvent("id2", IPAddress.Parse("1.2.3.5")));

            var firstResult = target.Readings.First();
            var lastResult  = target.Readings.Last();

            Assert.AreEqual(2, target.Readings.Count);

            //TODO add object comparison
            Assert.AreEqual("id1", firstResult.ClientId);
            Assert.AreEqual("unit-test-1", firstResult.DeviceName);
            Assert.AreEqual("1", firstResult.PM25);
            Assert.AreEqual("2", firstResult.PM10);

            Assert.AreEqual("id2", lastResult.ClientId);
            Assert.AreEqual("unit-test-2", lastResult.DeviceName);
            Assert.AreEqual("10", lastResult.PM25);
            Assert.AreEqual("20", lastResult.PM10);
        }
        public ReadingCollectionDto Read()
        {
            ReadingCollectionDto dto = new ReadingCollectionDto();

            dto.Dao = this;

            SeekPosition();

            // Maximum size
            dto.MaxReadings = this.maxSize = Reader.ReadInt32();

            // Record the position of the current size field
            this.currentSizePosition = Reader.BaseStream.Position;

            // Current size
            this.currentSize = Reader.ReadInt32();

            int counter = 0;

            // Read all of the non-empty readings
            for (; counter < this.currentSize; counter++)
            {
                BinaryFileReadingDao newReadingDao = new BinaryFileReadingDao(archive);
                ReadingDto           newReadingDto = newReadingDao.Read();
                newReadingDto.Dao = (IReadingDao)newReadingDao;
                this.readings.Add(newReadingDao);
                dto.Add(newReadingDto);
            }

            // Read all of the empty readings
            for (; counter < this.maxSize; counter++)
            {
                BinaryFileReadingDao newReadingDao = new BinaryFileReadingDao(archive);
                ReadingDto           newReadingDto = newReadingDao.Read();
                Debug.Assert(newReadingDto.Empty);
                newReadingDto.Dao = (IReadingDao)newReadingDao;
                this.unallocatedReadings.Add(newReadingDao);
            }

            return(dto);
        }
예제 #8
0
        private void Write(ReadingDto dto)
        {
            if (Position == BinaryFileDao.UnknownOffset)
            {
                // Record where the reading has been read from
                Position = Writer.BaseStream.Position;
            }
            else
            {
                // Go to the location of the reading in the file
                Writer.BaseStream.Seek((int)Position, SeekOrigin.Begin);
            }

            Writer.Write(dto.Empty);
            if (this.timestampDao == null)
            {
                this.timestampDao = new BinaryFileDateTimeDao(this);
            }
            this.timestampDao.Create(dto.Timestamp);
            Writer.Write(dto.Value);
        }
예제 #9
0
        public IHttpActionResult getReadings(int id)
        {
            var readingsByDay                 = _context.HeaterReadings.Where(r => r.HeaterId == id && r.ReadingTimestamp.Month == DateTime.Now.Month).GroupBy(r => r.ReadingTimestamp.Day).ToList();
            var temperatureReadings           = _context.HeaterReadings.Where(r => r.HeaterId == id).ToList();
            List <ReadingsDto> listOfReadings = new List <ReadingsDto>();
            AllReadingsDto     allReadings    = new AllReadingsDto();

            if (readingsByDay.Count == 0)
            {
                return(BadRequest("No readings for this heater"));
            }
            foreach (var reading in readingsByDay)
            {
                ReadingsDto readingDto = new ReadingsDto
                {
                    Date        = "",
                    Readings    = new List <ReadingDto>(),
                    AverageTemp = 0
                };
                foreach (var item in reading)
                {
                    readingDto.Date = item.ReadingTimestamp.ToString("d MMMM yyyy", CultureInfo.CreateSpecificCulture("en-US"));

                    var time = new ReadingDto {
                        Temperature = item.Temperature,
                        Time        = item.ReadingTimestamp.ToString("hh:mm:ss")
                    };
                    readingDto.Readings.Add(time);
                    readingDto.AverageTemp += item.Temperature;
                }
                readingDto.AverageTemp = readingDto.AverageTemp / readingDto.Readings.Count();
                listOfReadings.Add(readingDto);
            }
            allReadings.AllReadings   = temperatureReadings;
            allReadings.ReadingsByDay = listOfReadings;

            return(Ok(allReadings));
        }
예제 #10
0
        public ReadingDto Read()
        {
            if (Position != BinaryFileDao.UnknownOffset)
            {
                Reader.BaseStream.Seek(Position, SeekOrigin.Begin);
            }
            else
            {
                // Save the position of the state information
                Position = Reader.BaseStream.Position;
            }

            ReadingDto dto = new ReadingDto();

            dto.Empty = Reader.ReadBoolean();
            if (this.timestampDao == null)
            {
                this.timestampDao = new BinaryFileDateTimeDao(this);
            }
            dto.Timestamp = this.timestampDao.Read();
            dto.Value     = Reader.ReadDouble();

            return(dto);
        }
예제 #11
0
 public void UpdateLastReading(ReadingDto readingDto)
 {
     Debug.Assert(this.lastReadingDao != null);
     this.lastReadingDao.Update(readingDto);
 }
예제 #12
0
        public async Task <IActionResult> GetAll([FromQuery] ReadingDto readingDto)
        {
            var data = await _readingService.GetAllFilteredAsync(readingDto);

            return(Ok(data));
        }
예제 #13
0
 public Task <IReadOnlyList <Reading> > GetAllFilteredAsync(ReadingDto readingDto)
 {
     return(_unitOfWork.ReadingRepository.GetAllFilteredAsync(readingDto));
 }
예제 #14
0
 public void Update(ReadingDto dto)
 {
     Write(dto);
 }
예제 #15
0
 public void Create(ReadingDto dto)
 {
     Write(dto);
 }