Ejemplo n.º 1
0
        // Construct a message based on an Event obj
        public static string eventMessage(Google.Apis.Calendar.v3.Data.Event eventObj, string calId)
        {
            string version    = "";
            string uuid       = "";
            string event_uuid = "";
            string users      = "";

            // Setup up the Google People service in order to convert attendees to UUIDs
            PeopleResource.ConnectionsResource.ListRequest peopleRequest = GService.peopleService.People.Connections.List("people/me");
            peopleRequest.PersonFields = "names,emailAddresses,userDefined";
            Google.Apis.PeopleService.v1.Data.ListConnectionsResponse connectionsResponse = peopleRequest.Execute();
            IList <Person> connections = connectionsResponse.Connections;

            try
            {
                // Fetch data from the UUID master
                UUID uuidMaster = new UUID();

                // Check if the calendar exists in our system
                try
                {
                    event_uuid = uuidMaster.GetUuidBy(7, calId);
                }
                catch (Exception e)
                {
                    return("");
                }

                // Check if the event exists or not; create or update the UUID
                try
                {
                    uuid = uuidMaster.GetUuidBy(7, eventObj.Id);

                    // uuid exists, so it is an update request -> check time difference with uuid master
                    IdOutput output = JsonConvert.DeserializeObject <IdOutput>(uuidMaster.GetIdBy(uuid, 7));

                    // Add some fake delay
                    output.timestamp.AddSeconds(10);

                    if (eventObj.Updated < output.timestamp)
                    {
                        return("");
                    }

                    // Event was made trough the interface, not us
                    version = (output.version + 1).ToString();

                    uuidMaster.PutUpdateUUID(uuid, eventObj.Id, Convert.ToInt32(version));
                }
                catch (Exception e)
                {
                    uuid    = uuidMaster.PutCreateUUID(eventObj.Id);
                    version = "1";
                }

                if (eventObj.Attendees != null)
                {
                    // Convert all the attendees to UUIDs (employees & speakers)
                    foreach (var user in eventObj.Attendees.ToList())
                    {
                        foreach (var person in connections)
                        {
                            if (person.EmailAddresses.Where(email => email.Value.ToLower().Equals(user.Email.ToLower())).Count() > 0)
                            {
                                users += "<UUID>" + (person.UserDefined.First(ud => ud.Key.ToLower().Equals("uuid")).Value) + "</UUID>";
                            }
                        }
                    }
                }

                if (String.IsNullOrEmpty(users))
                {
                    users = "<UUID/>";
                }
            }
            catch (Exception e)
            {
                // Notify CR when something happens
                ControlRoom.SendErrorMessage("Google Calendar Sync Error", e.Message);
                return(e.StackTrace);
            }

            try
            {
                // Test for overlaying data, notify control room just in case
                ExceptionHandler <Google.Apis.Calendar.v3.Data.Event> .DateOverlay(eventObj.Start.DateTime, eventObj.End.DateTime);

                ExceptionHandler <Google.Apis.Calendar.v3.Data.Event> .LocationOverlay(eventObj.Start.DateTime, eventObj.End.DateTime, eventObj.Location, eventObj.Id);

                ExceptionHandler <Google.Apis.Calendar.v3.Data.Event> .AttendeeOverlay(eventObj.Start.DateTime, eventObj.End.DateTime, eventObj.Attendees, eventObj.Id);
            }
            catch (Exception e)
            {
                ControlRoom.SendErrorMessage("Google Calendar Overlay Error", e.Message);
            }


            // Construct the XML msg
            return("<message>" +
                   "<header><messageType>CreateSession</messageType><description>CRUD of a session</description><sender>planning</sender></header>" +
                   "<datastructure>" +
                   "<event_UUID>" + event_uuid + "</event_UUID>" +
                   "<session_UUID>" + uuid + "</session_UUID>" +
                   "<titel>" + eventObj.Summary + "</titel>" +
                   "<desc>" + eventObj.Description + "</desc>" +
                   "<users>" + users + "</users>" +
                   "<lokaal>" + eventObj.Location + "</lokaal>" +
                   "<start>" + eventObj.Start.DateTime.Value.ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture) + "</start>" +
                   "<end>" + eventObj.End.DateTime.Value.ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture) + "</end>" +
                   "<version>" + version + "</version>" +
                   "<timestamp>" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() + "</timestamp>" +
                   "<isActive>" + (eventObj.Status == "cancelled" ? "0" : "1") + "</isActive>" +
                   "<extraField></extraField>" +
                   "</datastructure>" +
                   "</message>");
        }
Ejemplo n.º 2
0
        // Create a calendar XML message
        public static string calMessage(CalendarListEntry cal)
        {
            string version = "";
            string uuid    = "";

            try
            {
                // Fetch data from the UUID master
                UUID uuidMaster = new UUID();

                // Check if the calendar exists or not; create or update the UUID
                try
                {
                    uuid = uuidMaster.GetUuidBy(7, cal.Id);

                    // uuid exists, so it is an update request -> check time name difference with
                    IdOutput output = JsonConvert.DeserializeObject <IdOutput>(uuidMaster.GetIdBy(uuid, 7));

                    // Delete detected
                    if (cal.Deleted == true)
                    {
                        // Event was made trough the interface, not us
                        version = (output.version + 1).ToString();
                    }
                    else
                    {
                        IdOutput name = JsonConvert.DeserializeObject <IdOutput>(uuidMaster.GetIdBy(String.Format("name-property-{0}", cal.Id), 7));

                        // Name change detected
                        if (!cal.Summary.Trim().Equals(name.id))
                        {
                            // update the name in the uuid master
                            uuidMaster.PutUpdateUUID(String.Format("name-property-{0}", cal.Id), cal.Summary.Trim(), Calendarss.getversion(String.Format("name-property-{0}", cal.Id)) + 1);

                            // Event was made trough the interface, not us
                            version = (output.version + 1).ToString();
                        }
                    }

                    if (version.Equals(output.version))
                    {
                        return("");
                    }

                    uuidMaster.PutUpdateUUID(uuid, cal.Id, Convert.ToInt32(version));
                }
                // UUID did not exist for the calendar, create it and make an entry for the calendar name
                catch (Exception e)
                {
                    // Create listener :)
                    GService.service.Events.Watch(KeySync.newChannel(String.Format("&calId={0}", cal.Id)), cal.Id).Execute();

                    uuid = uuidMaster.PutCreateUUID(cal.Id);
                    uuidMaster.PutUpdateUUID(String.Format("name-property-{0}", cal.Id), cal.Summary.Trim(), 1);

                    version = "1";
                }
            }
            catch (Exception e)
            {
                // Notify CR when something happens
                ControlRoom.SendErrorMessage("Google Calendar Sync Error", e.Message);
                return("");
            }

            // Construct the XML msg
            return("<message>" +
                   "<header><messageType>CreateEvent</messageType><description>CRUD of an event</description><sender>planning</sender></header>" +
                   "<datastructure>" +
                   "<UUID>" + uuid + "</UUID>" +
                   "<eventName>" + cal.Summary + "</eventName>" +
                   "<timestamp>" + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() + "</timestamp>" +
                   "<isActive>" + (cal.Deleted == true ? "0" : "1") + "</isActive>" +
                   "<version>" + version + "</version>" +
                   "<extraField></extraField>" +
                   "</datastructure>" +
                   "</message>");
        }