public static void DoEvents()
        {
            try
            {
                RelayControl.ResetAllSetupRelays();
                foreach (TimedEvent timedEvent in HeatingControl.GetAllEvents())
                {
                    if (timedEvent.IsActive(DateTime.MinValue))
                    {
                        switch (timedEvent.subjectType)
                        {
                        case "heater":
                            Heater heater = HeatingControl.GetHeater(timedEvent.subjectId);
                            if (heater == null)
                            {
                                break;
                            }
                            Logger.Log(Logger.LOGLEVEL_INFO, "Active timed event: heater: " + heater.name + ", relay: " + heater.relayAddress +
                                       ", event: " + timedEvent.description);

                            ProcessHeaterEvent(heater, timedEvent);
                            break;
                        }
                    }
                }
                RelayControl.UpdateRelays();
            }
            catch (Exception ex)
            {
                var testVal = ex;
            }
        }
        private static void ProcessHeaterEvent(Heater heater, TimedEvent timedEvent)
        {
            bool newState       = false;
            bool foundProcessed = false;
            Room room           = HeatingControl.GetRoom(heater.roomId);

            int sensorTotal   = 0;
            int sensorAverage = 0;
            int sensorCount   = 0;

            foreach (Sensor sensor in heater.sensors)
            {
                if (!sensor.ignore)
                {
                    sensorTotal += sensor.reading;
                    sensorCount++;
                }
            }

            if (sensorCount > 0)
            {
                sensorAverage = sensorTotal / sensorCount;
                if (sensorAverage >= room.tempMax)
                {
                    return;
                }
            }

            if (timedEvent.action == "timed")
            {
                newState = true;
            }
            else if (timedEvent.action == "target")
            {
                if (sensorCount == 0)
                {
                    return;
                }
                if (sensorAverage < room.tempTarget + m_iTempDelta)
                {
                    newState = true;
                }
                else if (sensorAverage == room.tempTarget + m_iTempDelta)
                { // Keep relay in same state to avoid flip-flopping
                    newState = RelayControl.GetRelayState(heater.relayAddress);
                }
            }

            foundProcessed = false;
            foreach (ProcessedRelay processedRelay in m_oProcessedRelays)
            {
                if (heater.relayAddress == processedRelay.relayAddress)
                {
                    if (timedEvent.id >= processedRelay.eventId)
                    {// highest event id has priority
                        processedRelay.eventId = timedEvent.id;
                    }
                    foundProcessed = true;
                    break;
                }
            }

            if (!foundProcessed)
            {
                m_oProcessedRelays.Add(new ProcessedRelay(heater.relayAddress, timedEvent.id));
            }

            RelayControl.SetupRelay(heater.relayAddress, newState);
        }