private static void NotifySubscriber(Guid clientID, Subscription subscription, Model.Object changedObject)
 {
     #if DEBUG
     Console.WriteLine("Publishing Subscription.Webhook message for subscription " + subscription.SubscriptionID);
     #endif
     ServiceEventMessage message = new ServiceEventMessage();
     message.AddParameter("AcceptContentType", subscription.AcceptContentType);
     message.AddParameter("SubscriptionID", StringUtils.GuidEncode(subscription.SubscriptionID));
     message.AddParameter("SubscriptionType", subscription.SubscriptionType.ToString());
     message.AddParameter("ClientID", StringUtils.GuidEncode(clientID));
     message.AddParameter("Url", subscription.Url);
     message.AddParameter("TimeTriggered", DateTime.Now);
     if (changedObject != null)
         message.AddParameter("Object", changedObject);
     BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_NOTIFICATION, message, TMessagePublishMode.Confirms);
 }
        public void SaveSubscription(Subscription subscription, TObjectState state)
        {
            IMongoDatabase database = GetDatabase(DATABASE_NAME, true);
            IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>(COLLECTION_NAME);
            EnsureIndexExists<BsonDocument>(collection, "OrganisationID");
            EnsureIndexExists<BsonDocument>(collection, "ClientID");

            FilterDefinition<BsonDocument> query = Builders<BsonDocument>.Filter.Eq("_id", subscription.SubscriptionID.ToByteArray());
            if ((state == TObjectState.Add) || (state == TObjectState.Update))
            {
                BsonDocument doc = new BsonDocument();
                BsonHelper.SetValue(doc, "_id", subscription.SubscriptionID);
                BsonHelper.SetValue(doc, "OrganisationID", subscription.OrganisationID);

                BsonHelper.SetValue(doc, "ClientID", subscription.ClientID);
                BsonHelper.SetValue(doc, "DefinitionID", subscription.ObjectDefinitionID);
                BsonHelper.SetValue(doc, "ObjectID", subscription.ObjectID);

                BsonHelper.SetValue(doc, "SubscriptionType", (int)subscription.SubscriptionType);
                BsonHelper.SetValue(doc, "PropertyDefinitionID", subscription.PropertyDefinitionID);
                BsonHelper.SetValue(doc, "Url", subscription.Url);
                BsonHelper.SetValue(doc, "AcceptContentType", subscription.AcceptContentType);

                if (subscription.NotificationParameters != null)
                {
                    MemoryStream stream = new MemoryStream();
                    subscription.NotificationParameters.Serialise(stream);
                    BsonHelper.SetValue(doc, "NotificationParameters", StringUtils.Encode(stream.ToArray()));
                }

                UpdateOptions options = new UpdateOptions();
                options.IsUpsert = true;
                collection.ReplaceOne(query, doc, options);
            }
            else if (state == TObjectState.Delete)
            {
                collection.DeleteOne(query);
            }
            BroadcastTableChange(COLLECTION_NAME, StringUtils.GuidEncode(subscription.SubscriptionID));
        }
        public void SaveSubscription(Subscription subscription, TObjectState state)
        {
            ServiceEventMessage message = new ServiceEventMessage();
            message.AddParameter("SubscriptionID", StringUtils.GuidEncode(subscription.SubscriptionID));

            switch (state)
            {
                case TObjectState.Add:
                    DataAccessFactory.Subscriptions.SaveSubscription(subscription, state);
                    BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_CREATE, message, TMessagePublishMode.Confirms);
                    break;
                case TObjectState.Update:
                    DataAccessFactory.Subscriptions.SaveSubscription(subscription, state);
                    BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_UPDATE, message, TMessagePublishMode.Confirms);
                    break;
                case TObjectState.Delete:
                    BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_DELETE, message, TMessagePublishMode.Confirms);
                    break;
                default:
                    DataAccessFactory.Subscriptions.SaveSubscription(subscription, state);
                    break;
            }
        }
 private static void CancelObservation(Subscription subscription)
 {
     Client client = DataAccessFactory.Clients.GetClient(subscription.ClientID);
     if (client != null)
     {
         if (subscription.PropertyDefinitionID != Guid.Empty)
         {
             DataAccessFactory.Servers.CancelObserveResource(client, subscription.ObjectDefinitionID, subscription.ObjectID, subscription.PropertyDefinitionID, false);
     #if DEBUG
             ApplicationEventLog.Write(LogLevel.Information, $"Cancelled observing resource /{subscription.ObjectDefinitionID}/{subscription.ObjectID}/{subscription.PropertyDefinitionID}");
     #endif
         }
         else if (subscription.ObjectID != null)
         {
             DataAccessFactory.Servers.CancelObserveObject(client, subscription.ObjectDefinitionID, subscription.ObjectID, false);
     #if DEBUG
             ApplicationEventLog.Write(LogLevel.Information, $"Cancelled observing object /{subscription.ObjectDefinitionID}/{subscription.ObjectID}");
     #endif
         }
         else if (subscription.ObjectDefinitionID != Guid.Empty)
         {
             DataAccessFactory.Servers.CancelObserveObjects(client, subscription.ObjectDefinitionID, false);
     #if DEBUG
             ApplicationEventLog.Write(LogLevel.Information, $"Cancelled observing objects /{subscription.ObjectDefinitionID}");
     #endif
         }
         else
         {
             ApplicationEventLog.Write(LogLevel.Warning, $"Subscription {subscription.SubscriptionID} has no definition ID");
         }
     }
     else
     {
         ApplicationEventLog.Write(LogLevel.Warning, $"No client exists for {subscription.ClientID} in subscription {subscription.SubscriptionID}");
     }
 }
        private Subscription LoadSubscription(BsonDocument item)
        {
            Subscription result = new Subscription();
            result.SubscriptionID = BsonHelper.GetGuid(item, "_id");
            result.OrganisationID = BsonHelper.GetInt32(item, "OrganisationID");
            result.ClientID = BsonHelper.GetGuid(item, "ClientID");
            result.ObjectDefinitionID = BsonHelper.GetGuid(item, "DefinitionID");
            result.ObjectID = BsonHelper.GetString(item, "ObjectID");
            result.SubscriptionType = (TSubscriptionType)BsonHelper.GetInt32(item, "SubscriptionType");
            result.PropertyDefinitionID = BsonHelper.GetGuid(item, "PropertyDefinitionID");
            result.Url = BsonHelper.GetString(item, "Url");
            result.AcceptContentType = BsonHelper.GetString(item, "AcceptContentType");

            byte[] serialisedNotificationParameters = StringUtils.Decode(BsonHelper.GetString(item, "NotificationParameters"));

            if (serialisedNotificationParameters != null)
            {
                result.NotificationParameters = NotificationParameters.Deserialise(new MemoryStream(serialisedNotificationParameters));
            }

            return result;
        }
 private static void Observe(Subscription subscription)
 {
     Client client = DataAccessFactory.Clients.GetClient(subscription.ClientID);
     if (client != null)
     {
         if (subscription.PropertyDefinitionID != Guid.Empty)
         {
             if (subscription.NotificationParameters != null)
             {
                 if (DataAccessFactory.Servers.SetNotificationParameters(client, subscription.ObjectDefinitionID, subscription.ObjectID, subscription.PropertyDefinitionID, subscription.NotificationParameters))
                     ApplicationEventLog.Write(LogLevel.Information, $"Updated notification parameters for resource /{subscription.ObjectDefinitionID}/{subscription.ObjectID}/{subscription.PropertyDefinitionID}");
                 else
                     ApplicationEventLog.Write(LogLevel.Warning, $"Failed to update notification parameters for resource /{subscription.ObjectDefinitionID}/{subscription.ObjectID}/{subscription.PropertyDefinitionID}");
             }
             DataAccessFactory.Servers.ObserveResource(client, subscription.ObjectDefinitionID, subscription.ObjectID, subscription.PropertyDefinitionID);
     #if DEBUG
             ApplicationEventLog.Write(LogLevel.Information, $"Observing resource /{subscription.ObjectDefinitionID}/{subscription.ObjectID}/{subscription.PropertyDefinitionID}");
     #endif
         }
         else if (subscription.ObjectID != null)
         {
             DataAccessFactory.Servers.ObserveObject(client, subscription.ObjectDefinitionID, subscription.ObjectID);
     #if DEBUG
             ApplicationEventLog.Write(LogLevel.Information, $"Observing object /{subscription.ObjectDefinitionID}/{subscription.ObjectID}");
     #endif
         }
         else if (subscription.ObjectDefinitionID != Guid.Empty)
         {
             DataAccessFactory.Servers.ObserveObjects(client, subscription.ObjectDefinitionID);
     #if DEBUG
             ApplicationEventLog.Write(LogLevel.Information, $"Observing objects /{subscription.ObjectDefinitionID}");
     #endif
         }
         else
         {
             ApplicationEventLog.Write(LogLevel.Warning, $"Subscription {subscription.SubscriptionID} has no definition ID");
         }
     }
     else
     {
         ApplicationEventLog.Write(LogLevel.Warning, $"No client exists for {subscription.ClientID} in subscription {subscription.SubscriptionID}");
     }
 }