public void UpdateDbWithCalendarEvents(CalendarEvent[] CalendarEvents, int startHeatingInAdvancedHours, int stopHeatingMinutes)
        {
            DateTime startHeating = DateTime.Now.AddHours(startHeatingInAdvancedHours);
            DateTime stopHeating = DateTime.Now.AddMinutes(stopHeatingMinutes);

            IEnumerable<Room> Rooms = Service.GetRooms();
            Room[] RoomArray = Rooms.ToArray();

            foreach (var item in RoomArray)
            {
                bool roomInUse = false;
                if (CalendarEvents.Length > 0)
                {
                    for (int i = 0; i < CalendarEvents.Length; i++)
                    {
                        // Make location and room description lower case
                        string tempEventLocation = CalendarEvents[i].Location.ToLower();
                        string tempRoomDescription = item.RoomDescription.ToLower();

                        if (tempEventLocation.Contains(tempRoomDescription) && item.AutomaticControl)
                        {
                            bool oldHeating = item.Heating;

                            // See if heating should be on or off
                            if (CalendarEvents[i].End < stopHeating)
                            {
                                item.Heating = false;
                            }
                            else if (CalendarEvents[i].Start < startHeating)
                            {
                                item.Heating = true;
                            }
                            else if (!roomInUse)
                            {
                                item.Heating = false;
                            }
                            // Make sure we only set the heating according to the first and thereby the earliest event
                            roomInUse = true;

                            UpdateRoom(oldHeating, item.Heating, item);
                        }
                        else if (!roomInUse && item.AutomaticControl)
                        {
                            TurnHeatingOff(item);
                        }
                    } 
                }
                else if (item.AutomaticControl)
                {
                    // If no events and in auto, turn room off
                    TurnHeatingOff(item);
                }
            }
        }
        private static CalendarEvent[] GetData(UserCredential credential)
        {
            // Create Calendar Service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // Define parameters of request.
            //var calendarID = "*****@*****.**";
            //var calendarID = "primary";
            var calendarID = "*****@*****.**";
            EventsResource.ListRequest request = service.Events.List(calendarID);
            request.TimeMin = DateTime.Now;
            request.ShowDeleted = false;
            request.SingleEvents = true;
            request.MaxResults = 10;
            request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

            // New list for events
            List<CalendarEvent> EventsList = new List<CalendarEvent>(10);

            // Process the events
            //Console.WriteLine("Upcoming events:");
            Events events = request.Execute();
            if (events.Items.Count > 0)
            {
                foreach (var eventItem in events.Items)
                {
                    string summary = eventItem.Summary;
                    string when = eventItem.Start.DateTime.ToString();
                    string location = eventItem.Location;
                    DateTime start;
                    DateTime end;
                    if (String.IsNullOrEmpty(when))
                    {
                        DateTime.TryParse(eventItem.Start.Date.ToString(), out start);
                        DateTime.TryParse(eventItem.Start.Date.ToString(), out end);
                        end = end.AddHours(23);
                        end = end.AddMinutes(59);
                        end = end.AddSeconds(59);
                    }
                    else
                    {
                        DateTime.TryParse(eventItem.Start.DateTime.ToString(), out start);
                        DateTime.TryParse(eventItem.End.DateTime.ToString(), out end);
                    }
                    //Console.WriteLine("{0}, {1} ({2})", eventItem.Summary, where, when);

                    CalendarEvent ev = new CalendarEvent(summary, location, start, end);
                    EventsList.Add(ev);
                    //Console.WriteLine("{0}, {1} ({2} - {3})", ev.Summary, ev.Location, ev.Start, ev.End);
                }
            }
            else
            {
                Console.WriteLine("No upcoming events found.");
            }

            EventsList.TrimExcess();

            // Array to return
            CalendarEvent[] EventsArray = EventsList.ToArray();

            return EventsArray;
        }