Example #1
0
        //===================================================================== INITIALIZE
        public BandwidthTracker(int monthLimit, int monthStart, int offPeakStart, int offPeakEnd)
        {
            _monthLimit = monthLimit * (long)Math.Pow(1024, 3);
            _monthStart = monthStart;
            _offPeakStart = offPeakStart;
            _offPeakEnd = offPeakEnd;
            _currentDay = DateTime.Now.Date;
            _currentMonthStart = GetLastMonthStart();

            // initialize nic
            _nic = GetNIC();
            _bwignored = TotalBytes;
            _bwCounted = new BandwidthUsage();

            // start timer
            _timer.Elapsed += timer_Elapsed;
            _timer.Start();
        }
Example #2
0
        public BandwidthTracker(int monthLimit, int monthStart, int offPeakStart, int offPeakEnd, string save)
            : this(monthLimit, monthStart, offPeakStart, offPeakEnd)
        {
            string[] split = save.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            _currentDay = new DateTime(int.Parse(split[2]), int.Parse(split[1]), int.Parse(split[0]));
            _currentMonthStart = new DateTime(int.Parse(split[5]), int.Parse(split[4]), int.Parse(split[3]));

            // load daily usage only if it is not a new day
            if (!IsNewDay(_currentDay))
            {
                _bwTodayOnPeak = new BandwidthUsage(long.Parse(split[6]), long.Parse(split[7]));
                _bwTodayOffPeak = new BandwidthUsage(long.Parse(split[8]), long.Parse(split[9]));
            }
            else _currentDay = DateTime.Now.Date;

            // load monthly usage log only if it is not a new month
            if (!IsNewMonth(_currentMonthStart))
            {
                _bwMonthOnPeak = new BandwidthUsage(long.Parse(split[10]), long.Parse(split[11]));
                _bwMonthOffPeak = new BandwidthUsage(long.Parse(split[12]), long.Parse(split[13]));
            }
            else _currentMonthStart = GetLastMonthStart();
        }
Example #3
0
 private void UpdateUsage(BandwidthUsage bytes)
 {
     if (IsOffPeakTime())
     {
         _bwTodayOffPeak += bytes;
         _bwMonthOffPeak += bytes;
     }
     else
     {
         _bwTodayOnPeak += bytes;
         _bwMonthOnPeak += bytes;
     }
 }
Example #4
0
        private void UpdateNewDay()
        {
            if (IsNewDay(_currentDay))
            {
                _currentDay = DateTime.Today.Date;
                _bwTodayOnPeak = new BandwidthUsage();
                _bwTodayOffPeak = new BandwidthUsage();

                if (IsNewMonth(_currentMonthStart))
                {
                    _currentMonthStart = DateTime.Today.Date;
                    _bwMonthOnPeak = new BandwidthUsage();
                    _bwMonthOffPeak = new BandwidthUsage();
                }
            }
        }
Example #5
0
 private void UpdateDownloadSpeed(BandwidthUsage bytes)
 {
     _downloadSpeed = (int)(bytes.Download / (_timer.Interval / 1000) / 1024);
     _uploadSpeed = (int)(bytes.Upload / (_timer.Interval / 1000) / 1024);
 }
Example #6
0
        //===================================================================== EVENTS
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (_nic == null)
            {
                _nic = GetNIC();
                if (_nic == null) return;
                _bwignored = TotalBytes;
                _bwCounted = new BandwidthUsage();
            }
            else CheckInterfaceReset();

            // calculate downloaded data in last tick and add to counted
            BandwidthUsage added = Bytes - _bwCounted;
            _bwCounted = Bytes;

            /* hack fix: when waking up from sleep mode, TotalBytes momentarily returns 0 which results in CheckInterfaceReset setting ignored to 0
               and therefore Bytes becomes TotalBytes when TotalBytes loads. To prevent this, ignore any download over 10 MB. Find a better fix later */
            if (added.Download < 1024 * 1024 * 10)
            {
                UpdateUsage(added);
                UpdateDownloadSpeed(added);
            }
            else Console.WriteLine("DON'T ADD");

            UpdateNewDay();
            if (Tick != null) Tick(this, new EventArgs());
        }
Example #7
0
 private void CheckInterfaceReset()
 {
     if (TotalBytes < _bwignored)
     {
         _bwignored = TotalBytes;
         _bwCounted = new BandwidthUsage();
     }
 }