/// <summary>
        /// Reads data from database and sends the new value to
        /// the Arduino as a string
        /// </summary>
        /// <returns></returns>
        public Room[] ReadDataFromDatabase()
        {
            Room[] room = new Room[6];
            bool[] newHeating = new bool[room.Length];

            for (int i = 0; i < room.Length; i++)
            {
                room[i] = Service.GetRoomByID(i + 1);
                newHeating[i] = room[i].Heating;
            }

            for (int i = 0; i < newHeating.Length; i++)
            {
                if ((newHeating[i] != oldHeating[i]) || startUp)
                {
                    switch (newHeating[i])
                    {
                        case true:
                            Service.SC_SendData(i + 1 + "H#");
                            break;
                        case false:
                            Service.SC_SendData(i + 1 + "L#");
                            break;
                        default:
                            break;
                    }
                    oldHeating[i] = newHeating[i];
                }
            }
            if (startUp)
            {
                startUp = false;
            }
            return room;
        }
 private void UpdateRoom(bool oldHeating, bool newHeating, Room room)
 {
     // See if heating needs to be changed
     if (newHeating != oldHeating)
     {
         // Update heating in DB
         Service.UpdateRoom(room);
         Console.WriteLine("{0} updaterat till {1}", room.RoomDescription, room.Heating);
     }
 }
        public void UpdateRoom(Room room)
        {
            ICollection<ValidationResult> validationResults;
            if (!room.Validate(out validationResults))
            {
                throw new AggregateException("Objektet klarade inte valideringen.",
                    validationResults.Select(vr => new ValidationException(vr.ErrorMessage)).ToList().AsReadOnly());
            }

            RoomDAL.UpdateRoom(room);
        }
        public void UpdateRoom(Room room)
        {
            try
            {
                using (var conn = new SqlConnection(ConnectionString))
                {
                    var cmd = new SqlCommand("app.usp_UpdateRoom", conn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@RoomID", SqlDbType.TinyInt, 1).Value = room.RoomID;
                    cmd.Parameters.Add("@Heating", SqlDbType.Bit, 1).Value = room.Heating;
                    cmd.Parameters.Add("@RoomDescription", SqlDbType.VarChar, 50).Value = room.RoomDescription;
                    cmd.Parameters.Add("@LastTemperature", SqlDbType.Int, 4).Value = room.LastTemperature;
                    cmd.Parameters.Add("@AutomaticControl", SqlDbType.Bit, 1).Value = room.AutomaticControl;

                    conn.Open();

                    cmd.ExecuteNonQuery();
                }
            }
            catch
            {
                throw new ApplicationException("Ett fel inträffade då rummet skulle uppdateras i databasen.");
            }
        }
 private void TurnHeatingOff(Room room)
 {
     var oldHeating = room.Heating;
     room.Heating = false;
     UpdateRoom(oldHeating, room.Heating, room);
 }