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 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);
            }
        }
 private void ReadAttributes()
 {
     this.name = this.dto.Name = this.fixup.Name;
     this.dto.ConversionFunction = Reader.ReadInt32();
     this.dto.PollingInterval    = new TimeSpan(0, 0, Reader.ReadInt32());
     if (this.lastReadingDao == null)
     {
         this.lastReadingDao = new BinaryFileReadingDao(this);
     }
     this.dto.LastReading = this.lastReadingDao.Read();
     if (this.rangeDao == null)
     {
         this.rangeDao = new BinaryFileRangeDao(this);
     }
     this.dto.Range = this.rangeDao.Read();
 }
        private IReadingDao AllocateReading()
        {
            Debug.Assert(this.currentSize < this.maxSize);

            BinaryFileReadingDao readingDao = this.unallocatedReadings[0];

            // Remove an unallocated reading from the unallocated list
            this.unallocatedReadings.RemoveAt(0);

            // Add the reading DAO to the allocated list
            this.readings.Add(readingDao);

            this.currentSize++;

            return(readingDao);
        }
        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);
        }