aboutsummaryrefslogtreecommitdiff
path: root/NucLedController/CPUUsageIndicatorControlMode.cs
blob: 5d62339453626c849cf0de833ec9a067ed93808c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace NucLedController
{
    class CPUUsageIndicatorControlMode : IControlMode
    {

        private readonly Timer timer;
        private readonly PerformanceCounter perfCounter;
        private LEDColour currentColour;

        public CPUUsageIndicatorControlMode(int intervalMs)
        {
            timer = new Timer();
            timer.Elapsed += new ElapsedEventHandler(Tick);
            timer.Interval = intervalMs;

            perfCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            currentColour = LEDColour.getLEDColour("WHITE");
        }

        private void Tick(object source, ElapsedEventArgs e)
        {
            float cpuUtilisation = perfCounter.NextValue();

            LEDColour targetColour = mapUsageToColour(cpuUtilisation);
            if(currentColour != targetColour)
            {
                LEDController.SetLEDState(LEDTransition.getLEDTransition("ALWAYS_ON"), targetColour);
                currentColour = targetColour;
            }

        }

        // TODO add option to map usage to brighness
        private LEDColour mapUsageToColour(float utilisation)
        {
            if(utilisation <= 33)
            {
                return LEDColour.getLEDColour("GREEN");
            }
            else if (utilisation > 66)
            {
                return LEDColour.getLEDColour("RED");
            }
            else
            {
                return LEDColour.getLEDColour("YELLOW");
            }
        }

        public void Start()
        {
            timer.Start();
        }

        public void Stop()
        {
            timer.Start();
        }
    }
}