public void SaveClient(Client client, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("Client"); EnsureIndexExists <BsonDocument>(collection, "OrganisationID"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", client.ClientID.ToByteArray()); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", client.ClientID); BsonHelper.SetValue(doc, "Name", client.Name); BsonHelper.SetValue(doc, "OrganisationID", client.OrganisationID); BsonHelper.SetValue(doc, "Lifetime", client.Lifetime); BsonHelper.SetValue(doc, "Version", client.Version.ToString()); BsonHelper.SetValue(doc, "BindingMode", (int)client.BindingMode); BsonHelper.SetValue(doc, "SMSNumber", client.SMSNumber); BsonHelper.SetValue(doc, "Server", client.Server); BsonHelper.SetValue(doc, "LastActivityTime", client.LastActivityTime); if (client.SupportedTypes.Count > 0) { BsonArray array = new BsonArray(); foreach (ObjectType supportedType in client.SupportedTypes) { BsonDocument supportedTypeDoc = new BsonDocument(); BsonHelper.SetValue(supportedTypeDoc, "_id", supportedType.ObjectTypeID); BsonHelper.SetValue(supportedTypeDoc, "Path", supportedType.Path); if (supportedType.Instances.Count > 0) { BsonArray instances = new BsonArray(); foreach (int instance in supportedType.Instances) { instances.Add(instance); } supportedTypeDoc.Add("Instances", instances); } array.Add(supportedTypeDoc); } doc.Add("SupportedTypes", array); } UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } }
public void AllocateBootstrapServer(int organisationID, BootstrapServer bootstrapServer) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("OrganisationBootstrapServer"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", organisationID); BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", organisationID); BsonHelper.SetValue(doc, "Url", bootstrapServer.Url); UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); }
public void SaveLWM2MServer(LWM2MServer lwm2mServer, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("Server"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", lwm2mServer.Url); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", lwm2mServer.Url); BsonHelper.SetValue(doc, "Lifetime", lwm2mServer.Lifetime); BsonHelper.SetValue(doc, "DefaultMinimumPeriod", lwm2mServer.DefaultMinimumPeriod); BsonHelper.SetValue(doc, "DefaultMaximumPeriod", lwm2mServer.DefaultMaximumPeriod); BsonHelper.SetValue(doc, "DisableTimeout", lwm2mServer.DisableTimeout); BsonHelper.SetValue(doc, "NotificationStoringWhenOffline", lwm2mServer.NotificationStoringWhenOffline); BsonHelper.SetValue(doc, "Binding", (int)lwm2mServer.Binding); if (lwm2mServer.ServerIdentities != null && lwm2mServer.ServerIdentities.Count > 0) { BsonArray array = new BsonArray(); foreach (PSKIdentity pskIdentity in lwm2mServer.ServerIdentities) { BsonDocument pskIdentityDoc = new BsonDocument(); BsonHelper.SetValue(pskIdentityDoc, "_id", pskIdentity.Identity); BsonHelper.SetValue(pskIdentityDoc, "Secret", pskIdentity.Secret); array.Add(pskIdentityDoc); } doc.Add("ServerIdentities", array); } if (lwm2mServer.ServerCertificate != null) { BsonDocument serverCertificateDoc = new BsonDocument(); BsonHelper.SetValue(serverCertificateDoc, "_id", (int)lwm2mServer.ServerCertificate.CertificateFormat); BsonHelper.SetValue(serverCertificateDoc, "RawCertificate", lwm2mServer.ServerCertificate.RawCertificate); doc.Add("ServerCertificate", serverCertificateDoc); } UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } BroadcastTableChange("LWM2MServer", string.Empty); }
private void SaveMetric(MetricBase metric, TObjectState state, BsonValue id, string collectionName, Dictionary <string, BsonValue> values) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>(collectionName); if (state == TObjectState.Add) { BsonDocument newItem = new BsonDocument(); BsonHelper.SetValue(newItem, "Name", metric.Name); BsonHelper.SetValue(newItem, "Value", metric.Value); foreach (KeyValuePair <string, BsonValue> pair in values) { newItem[pair.Key] = pair.Value; } FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.Eq("_id", id); UpdateDefinition <BsonDocument> update = Builders <BsonDocument> .Update.AddToSet("Metrics", newItem); UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.UpdateOne(filter, update, options); } else if (state == TObjectState.Update) { FilterDefinition <BsonDocument> clientOrOrganisationIDFilter = Builders <BsonDocument> .Filter.Eq("_id", id); FilterDefinition <BsonDocument> metricNameFilter = Builders <BsonDocument> .Filter.Eq("Metrics.Name", metric.Name); FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.And(clientOrOrganisationIDFilter, metricNameFilter); UpdateDefinition <BsonDocument> update = Builders <BsonDocument> .Update.Set("Metrics.$.Value", metric.Value); collection.UpdateOne(filter, update); } else if (state == TObjectState.Delete) { FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.Eq("_id", id); UpdateDefinition <BsonDocument> update = Builders <BsonDocument> .Update.PullFilter("Metrics", Builders <BsonDocument> .Filter.Eq("Name", metric.Name)); collection.UpdateOne(filter, update); } }
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 SaveBootstrapServer(BootstrapServer bootstrapServer, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("BootstrapServer"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", bootstrapServer.Url); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", bootstrapServer.Url); if (bootstrapServer.ServerIdentities != null && bootstrapServer.ServerIdentities.Count > 0) { BsonArray array = new BsonArray(); foreach (PSKIdentity pskIdentity in bootstrapServer.ServerIdentities) { BsonDocument pskIdentityDoc = new BsonDocument(); BsonHelper.SetValue(pskIdentityDoc, "_id", pskIdentity.Identity); BsonHelper.SetValue(pskIdentityDoc, "Secret", pskIdentity.Secret); array.Add(pskIdentityDoc); } doc.Add("ServerIdentities", array); } if (bootstrapServer.ServerCertificate != null) { BsonDocument serverCertificateDoc = new BsonDocument(); BsonHelper.SetValue(serverCertificateDoc, "_id", (int)bootstrapServer.ServerCertificate.CertificateFormat); BsonHelper.SetValue(serverCertificateDoc, "RawCertificate", bootstrapServer.ServerCertificate.RawCertificate); doc.Add("ServerCertificate", serverCertificateDoc); } UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } BroadcastTableChange("BootstrapServer", string.Empty); }
public void SaveBlacklistedClient(Client client, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("BlacklistedClient"); EnsureIndexExists <BsonDocument>(collection, "OrganisationID"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", client.ClientID.ToByteArray()); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", client.ClientID); BsonHelper.SetValue(doc, "OrganisationID", client.OrganisationID); UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } BroadcastTableChange("BlacklistedClient", StringUtils.GuidEncode(client.ClientID)); }
public void SaveAccessKey(AccessKey accessKey, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("AccessKey"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", accessKey.Key); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", accessKey.Key); BsonHelper.SetValue(doc, "OrganisationID", accessKey.OrganisationID); BsonHelper.SetValue(doc, "Name", accessKey.Name); BsonHelper.SetValue(doc, "Secret", accessKey.Secret); UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } BroadcastTableChange("AccessKey", accessKey.Key); }
public void SavePSKIdentity(PSKIdentity pskIdentity, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>(COLLECTION_NAME); EnsureIndexExists <BsonDocument>(collection, "OrganisationID"); FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", pskIdentity.Identity); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", pskIdentity.Identity); BsonHelper.SetValue(doc, "Secret", pskIdentity.Secret); BsonHelper.SetValue(doc, "OrganisationID", pskIdentity.OrganisationID); UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } BroadcastTableChange(COLLECTION_NAME, pskIdentity.Identity); }
public void SaveObjectDefinitions(List <ObjectDefinition> items, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("ObjectDefinition"); foreach (ObjectDefinition item in items) { if (item.ObjectDefinitionID == Guid.Empty) { item.ObjectDefinitionID = Guid.NewGuid(); } FilterDefinition <BsonDocument> query = Builders <BsonDocument> .Filter.Eq("_id", item.ObjectDefinitionID.ToByteArray()); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", item.ObjectDefinitionID); BsonHelper.SetValue(doc, "ObjectID", item.ObjectID); if (item.OrganisationID.HasValue && (item.OrganisationID.Value == 0)) { item.OrganisationID = null; } BsonHelper.SetValue(doc, "OrganisationID", item.OrganisationID); BsonHelper.SetValue(doc, "Name", item.Name); BsonHelper.SetValue(doc, "MIMEType", item.MIMEType); BsonHelper.SetValue(doc, "SerialisationName", item.SerialisationName); BsonHelper.SetValue(doc, "Description", item.Description); BsonHelper.SetValue(doc, "Singleton", item.Singleton); if ((item.Properties != null) && item.Properties.Count > 0) { BsonArray array = new BsonArray(); foreach (PropertyDefinition property in item.Properties) { if (property.PropertyDefinitionID == Guid.Empty) { property.PropertyDefinitionID = Guid.NewGuid(); } BsonDocument propertyDoc = new BsonDocument(); BsonHelper.SetValue(propertyDoc, "_id", property.PropertyDefinitionID); BsonHelper.SetValue(propertyDoc, "PropertyID", property.PropertyID); BsonHelper.SetValue(propertyDoc, "Name", property.Name); BsonHelper.SetValue(propertyDoc, "Description", property.Description); BsonHelper.SetValue(propertyDoc, "DataType", (int)property.DataType); BsonHelper.SetValue(propertyDoc, "DataTypeLength", property.DataTypeLength); BsonHelper.SetValue(propertyDoc, "MIMEType", property.MIMEType); BsonHelper.SetValue(propertyDoc, "MinValue", property.MinValue); BsonHelper.SetValue(propertyDoc, "MaxValue", property.MaxValue); BsonHelper.SetValue(propertyDoc, "Units", property.Units); BsonHelper.SetValue(propertyDoc, "IsCollection", property.IsCollection); BsonHelper.SetValue(propertyDoc, "IsMandatory", property.IsMandatory); BsonHelper.SetValue(propertyDoc, "Access", (int)property.Access); BsonHelper.SetValue(propertyDoc, "SortOrder", property.SortOrder); BsonHelper.SetValue(propertyDoc, "SerialisationName", property.SerialisationName); BsonHelper.SetValue(propertyDoc, "CollectionItemSerialisationName", property.CollectionItemSerialisationName); array.Add(propertyDoc); } doc.Add("Properties", array); } UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } } BroadcastTableChange("ObjectDefinition", string.Empty); }