private static void ProcessRelay(Heater p_Heater, TimedEvent p_Event, Boolean p_StaySame)
        {
            // if p_StaySame the relay should stay off if already off or on if already on
            bool bTooHot = false;

            for (int a = 0; a < p_Heater.Sensors.Count; a++)
            {
                if (p_Heater.Sensors[a].Reading >= p_Heater.TempMax)
                {
                    bTooHot = true;
                    break;
                }
            }

            if (!bTooHot)
            {
                bool bFound = false;
                bool bState = (p_Event.Type == (int)TimedEvent.EventType.Heat);

                for (int k = 0; k < m_oProcessedRelays.Count; k++)
                {
                    if (p_Heater.RelayAddress == m_oProcessedRelays[k][0])
                    {
                        if (p_Event.Id >= int.Parse(m_oProcessedRelays[k][1]))
                        {// highest event id has priority
                            RelayControl.SetupRelay(p_Heater.RelayAddress, (p_StaySame) ? RelayControl.GetRelayState(p_Heater.RelayAddress) : bState);
                            m_oProcessedRelays[k][1] = p_Event.Id.ToString();
                        }
                        bFound = true;
                    }
                }

                if (!bFound)
                {
                    RelayControl.SetupRelay(p_Heater.RelayAddress, (p_StaySame) ? RelayControl.GetRelayState(p_Heater.RelayAddress) : bState);
                    string[] saUsedRelay = new string[3];
                    saUsedRelay[0] = p_Heater.RelayAddress;
                    saUsedRelay[1] = p_Event.Id.ToString();
                    m_oProcessedRelays.Add(saUsedRelay);
                }

            }
        }
        public static void LoadFromFile()
        {
            Groups.Clear();
            Rooms.Clear();
            Events.Clear();
            FloorHeats.Clear();
            TowelRads.Clear();
            RoomSensors.Clear();
            FloorSensors.Clear();

            EventGroup group;
            Room room;
            Heater heater;
            Sensor sensor;
            TimedEvent timedEvent;

            XmlTextReader reader = new XmlTextReader("initdata.xml");

            while ((reader.Read()))
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        //Display beginning of element.
                        switch (reader.Name)
                        {
                            case "group":
                                group = new EventGroup(Convert.ToInt32(reader.GetAttribute("id")), reader.GetAttribute("name"));
                                Groups.Add(group);
                                break;
                            case "room":
                                room = new Room(Convert.ToInt32(reader.GetAttribute("id")), reader.GetAttribute("name"), Convert.ToInt32(reader.GetAttribute("group")),
                                    Convert.ToInt32(reader.GetAttribute("tempmin")), Convert.ToInt32(reader.GetAttribute("tempmax")), Convert.ToInt32(reader.GetAttribute("temptarget")));
                                Rooms.Add(room);
                                break;
                            case "floorheat":
                                heater = new Heater(Convert.ToInt32(reader.GetAttribute("id")), Heater.HEATER_TYPE_FLOOR, reader.GetAttribute("name"), Convert.ToInt32(reader.GetAttribute("group")),
                                    Convert.ToInt32(reader.GetAttribute("room")), reader.GetAttribute("relay"), Convert.ToInt32(reader.GetAttribute("tempmax")));
                                FloorHeats.Add(heater);
                                break;
                            case "towelrad":
                                heater = new Heater(Convert.ToInt32(reader.GetAttribute("id")), Heater.HEATER_TYPE_TOWEL, reader.GetAttribute("name"), Convert.ToInt32(reader.GetAttribute("group")),
                                    Convert.ToInt32(reader.GetAttribute("room")), reader.GetAttribute("relay"), Convert.ToInt32(reader.GetAttribute("tempmax")));
                                TowelRads.Add(heater);
                                break;
                            case "timergroup":
                                break;
                            case "roomsensor":
                                sensor = new Sensor(Convert.ToInt32(reader.GetAttribute("id")), Sensor.SENSOR_TYPE_ROOM, reader.GetAttribute("name"), Convert.ToInt32(reader.GetAttribute("room")),
                                    reader.GetAttribute("owid"), "x");
                                RoomSensors.Add(sensor);
                                break;
                            case "floorsensor":
                                sensor = new Sensor(Convert.ToInt32(reader.GetAttribute("id")), Sensor.SENSOR_TYPE_FLOOR, reader.GetAttribute("name"), Convert.ToInt32(reader.GetAttribute("room")),
                                    reader.GetAttribute("owid"), "x");
                                FloorSensors.Add(sensor);
                                break;
                            case "event":
                                timedEvent = new TimedEvent(Convert.ToInt32(reader.GetAttribute("id")), 1, Convert.ToInt32(reader.GetAttribute("subject")),
                                    reader.GetAttribute("timestart"), reader.GetAttribute("timeend"), reader.GetAttribute("comment"));
                                if (timedEvent.Id > highestEventId) highestEventId = timedEvent.Id;
                                Events.Add(timedEvent);
                                break;
                        }
                        break;
                }
            }

            // Add heaters to rooms
            for (int i = 0; i < FloorHeats.Count; i++)
            {
                for (int j = 0; j < Rooms.Count; j++)
                {
                    if (Rooms[j].Id == FloorHeats[i].RoomId)
                    {
                        Rooms[j].Heaters.Add(FloorHeats[i]);
                        break;
                    }
                }
            }
            for (int i = 0; i < TowelRads.Count; i++)
            {
                for (int j = 0; j < Rooms.Count; j++)
                {
                    if (Rooms[j].Id == TowelRads[i].RoomId)
                    {
                        Rooms[j].Heaters.Add(TowelRads[i]);
                        break;
                    }
                }
            }

            // Add sensors to rooms and heaters
            for (int i = 0; i < RoomSensors.Count; i++)
            {
                for (int j = 0; j < Rooms.Count; j++)
                {
                    if (Rooms[j].Id == RoomSensors[i].RoomId)
                    {
                        Rooms[j].Sensors.Add(RoomSensors[i]);
                        for (int k = 0; k < Rooms[j].Heaters.Count; k++)
                        {
                            Rooms[j].Heaters[k].Sensors.Add(RoomSensors[i]);
                        }
                        break;
                    }
                }
            }
            for (int i = 0; i < FloorSensors.Count; i++)
            {
                for (int j = 0; j < Rooms.Count; j++)
                {
                    if (Rooms[j].Id == FloorSensors[i].RoomId)
                    {
                        Rooms[j].Sensors.Add(FloorSensors[i]);
                        for (int k = 0; k < Rooms[j].Heaters.Count; k++)
                        {
                            Rooms[j].Heaters[k].Sensors.Add(FloorSensors[i]);
                        }
                        break;
                    }
                }
            }

            reader.Close();
        }