private IEnumerable <SleepBlock> ParseSleepBlocks(IEnumerable <string> guardLogStrings)
        {
            var sleepBlocks       = new List <SleepBlock>();
            var currentBlock      = new SleepBlock();
            var orderedLogStrings = guardLogStrings.OrderBy(str => str);

            foreach (var logString in orderedLogStrings)
            {
                if (logString.EndsWith("begins shift"))
                {
                    currentBlock = new SleepBlock()
                    {
                        GuardId = int.Parse(Regex.Match(logString, "(?<=#)[0-9]+").Value)
                    };
                }
                else if (logString.EndsWith("falls asleep"))
                {
                    currentBlock.Start = int.Parse(Regex.Match(logString, "(?<=:)[0-9]+").Value);
                }
                else if (logString.EndsWith("wakes up"))
                {
                    currentBlock.End = int.Parse(Regex.Match(logString, "(?<=:)[0-9]+").Value);
                    sleepBlocks.Add(currentBlock);
                }
            }

            return(sleepBlocks);
        }
        internal override void UpdateValues(Block block)
        {
            SleepBlock sleepBlock = (SleepBlock)block;

            Milliseconds = sleepBlock.Milliseconds;

            currentBlock = block;
        }
예제 #3
0
 public static bool AddSleepBlock(SleepBlock block)
 {
     using (SQLiteConnection connection = new SQLiteConnection(PermDBCS))
     {
         int ret = connection.InsertOrReplace(block);
     }
     return(true);
 }
        public void CreateValidSleepBlockSucceeds()
        {
            var start = new DateTime(2020, 09, 10, 10, 10, 01);
            var end   = new DateTime(2020, 09, 11, 11, 5, 4);
            var block = new SleepBlock(start, end); // should not throw

            Assert.Equal(start, block.Start);
            Assert.Equal(end, block.End);
        }
        public void CreateValidSleepBlockFromStructSucceeds()
        {
            var start = new DateTime(2020, 09, 10, 10, 10, 01).RoundToNearest(TimeSpan.FromMinutes(1));
            var end   = new DateTime(2020, 09, 11, 11, 5, 4).RoundToNearest(TimeSpan.FromMinutes(1));
            var struc = new SleepDataStruct(start, end);

            Assert.Equal(start, struc.Start.AsDateTime());
            Assert.Equal(end, struc.End.AsDateTime());

            var block = new SleepBlock(ref struc);

            Assert.Equal(start, block.Start);
            Assert.Equal(end, block.End);
            Assert.Equal(struc.Start.AsDateTime(), block.Start);
            Assert.Equal(struc.End.AsDateTime(), block.End);
        }
        public void WriteToSleepFile(int[] s, int[] e)
        {
            File.Delete(TestPath);
            var sleepReg = new SleepDataRegistry(TestPath);
            var start    = new DateTime(s[0], s[1], s[2], s[3], s[4], s[5]);
            var end      = new DateTime(e[0], e[1], e[2], e[3], e[4], e[5]);
            var block    = new SleepBlock(start, end);

            bool ret = sleepReg.WriteSleepData(new List <SleepBlock> {
                block
            });

            Assert.True(ret);

            var read = sleepReg.ReadSleepData();

            Assert.Equal(start, read[0].Start);
            Assert.Equal(end, read[0].End);
        }
 public SleepBlockEntity(SleepBlock block)
 {
     Milliseconds = block.Milliseconds;
 }
 public SleepCodeMessage(SleepBlock sleepBlock)
 {
     command = "sleep";
     data    = sleepBlock.Milliseconds.ToString();
 }