public void SaveObjectDefinition(ObjectDefinition objectDefinition, TObjectState state) { List <ObjectDefinition> objectDefinitions = new List <ObjectDefinition>(); objectDefinitions.Add(objectDefinition); SaveObjectDefinitions(objectDefinitions, state); }
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; } }
public void SaveObjectDefinitions(List <ObjectDefinition> objectDefinitions, TObjectState state) { if (state == TObjectState.Add) { ObjectDefinitionLookups lookups = GetLookups(); foreach (ObjectDefinition item in objectDefinitions) { int organisationID = 0; if (item.OrganisationID.HasValue) { organisationID = item.OrganisationID.Value; } ObjectDefinition existingObjectDefinition = lookups.GetObjectDefinition(organisationID, item.ObjectID); if (existingObjectDefinition != null) { int existingOrganisationID = 0; if (existingObjectDefinition.OrganisationID.HasValue) { existingOrganisationID = existingObjectDefinition.OrganisationID.Value; } if (organisationID == existingOrganisationID) { throw new ConflictException(); } } } } DataAccessFactory.ObjectDefinitions.SaveObjectDefinitions(objectDefinitions, state); }
public void SaveMetric(ClientMetric metric, TObjectState state) { Dictionary <string, BsonValue> clientMetricValues = new Dictionary <string, BsonValue>(); clientMetricValues.Add("Incremental", metric.Incremental); SaveMetric(metric, state, metric.ClientID.ToByteArray(), CLIENT_METRICS_COLLECTION, clientMetricValues); }
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); } }
// 시간과 관련된 정보를 등록한다. void RegisterTimeState() { // 상속을 사용해서 해결해본다? TimeState timeState = new TimeState(); // 이동 관련 정보 Vector2 vec2Pos = new Vector2(transform.position.x, transform.position.y); for (int i = 0; i < components.Properties.Length; i++) { switch (components.Properties[i]) { case Components.EnumProperty.OBJECT: TObjectState tos = new TObjectState { Position = vec2Pos, DeltaTime = Time.deltaTime, RealPlayTime = Time.realtimeSinceStartup }; timeState.TObjectSt = tos; tos.AttachEvent(timeState); break; case Components.EnumProperty.ACTOR: TActorState tas = new TActorState { JumpHeight = rb2D.velocity.y, IsMoveAnimator = components.GetAnimator().GetBool("isMove"), RotateYFloat = transform.rotation.eulerAngles.y }; timeState.TActorSt = tas; tas.AttachEvent(timeState); break; } } if (OtherRegisterEvent != null) { OtherRegisterEvent(); } TimeStates.Add(timeState); }
public void SavePSKIdentity(PSKIdentity pskIdentity, TObjectState state) { if (pskIdentity.OrganisationID == 0) { throw new NotSupportedException(); } if (state == TObjectState.Add) { pskIdentity.Identity = StringUtils.GuidEncode(Guid.NewGuid()); byte[] secretkey = new Byte[32]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(secretkey); pskIdentity.Secret = StringUtils.HexString(secretkey); } DataAccessFactory.Identities.SavePSKIdentity(pskIdentity, state); }
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 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 SaveAccessKey(AccessKey accessKey, TObjectState state) { if (accessKey.OrganisationID == 0) { accessKey.OrganisationID = DataAccessFactory.AccessKeys.GenerateOrganisationID(); } if ((state == TObjectState.Add) && string.IsNullOrEmpty(accessKey.Key)) { byte[] secretkey = new Byte[64]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); Model.AccessKey existingTenantAccessKey; do { rng.GetBytes(secretkey); accessKey.Key = StringUtils.Encode(secretkey); existingTenantAccessKey = DataAccessFactory.AccessKeys.GetAccessKey(accessKey.Key); } while (existingTenantAccessKey != null); rng.GetBytes(secretkey); accessKey.Secret = StringUtils.Encode(secretkey); } DataAccessFactory.AccessKeys.SaveAccessKey(accessKey, state); }
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 SaveObjectDefinitions(List<ObjectDefinition> objectDefinitions, TObjectState state) { if (state == TObjectState.Add) { ObjectDefinitionLookups lookups = GetLookups(); foreach (ObjectDefinition item in objectDefinitions) { int organisationID = 0; if (item.OrganisationID.HasValue) organisationID = item.OrganisationID.Value; ObjectDefinition existingObjectDefinition = lookups.GetObjectDefinition(organisationID, item.ObjectID); if (existingObjectDefinition != null) { int existingOrganisationID = 0; if (existingObjectDefinition.OrganisationID.HasValue) existingOrganisationID = existingObjectDefinition.OrganisationID.Value; if (organisationID == existingOrganisationID) throw new ConflictException(); } } } DataAccessFactory.ObjectDefinitions.SaveObjectDefinitions(objectDefinitions, state); }
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 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 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 SaveObjectDefinition(ObjectDefinition objectDefinition, TObjectState state) { List<ObjectDefinition> objectDefinitions = new List<ObjectDefinition>(); objectDefinitions.Add(objectDefinition); SaveObjectDefinitions(objectDefinitions, state); }
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); }
public void SaveObjectProperty(Guid clientID, Guid objectDefinitionID, string instanceID, Property property, TObjectState state) { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client == null) { ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Client not found ", clientID.ToString())); throw new NoLongerAvailableException("Device not connected"); } else { ObjectDefinitionLookups lookups = BusinessLogicFactory.Clients.GetLookups(); ObjectDefinition objectDefinition = lookups.GetObjectDefinition(objectDefinitionID); if (objectDefinition != null) { int objectID; if (int.TryParse(objectDefinition.ObjectID, out objectID)) { Model.ObjectType objectType = client.SupportedTypes.GetObjectType(objectID); if (objectType != null) { PropertyDefinition propertyDefinition = objectDefinition.GetProperty(property.PropertyDefinitionID); if (propertyDefinition != null) { byte[] payload = null; int contentType = TlvConstant.CONTENT_TYPE_TLV; if (state != TObjectState.Delete) { if ((property.Value != null) || (property.Values != null)) { if ((property.Value != null) && (LWM2MClient.DataFormat == MediaType.TextPlain)) { contentType = LWM2MClient.DataFormat; //contentType = TlvConstant.CONTENT_TYPE_PLAIN; string text = SerialiseProperty(propertyDefinition, property); if (text != null) payload = Encoding.UTF8.GetBytes(text); } else { Model.Object lwm2mObject = new Model.Object(); lwm2mObject.Properties.Add(property); payload = SerialiseObject(objectDefinition, lwm2mObject); } } } Request request = null; switch (state) { case TObjectState.NotChanged: break; case TObjectState.Add: request = client.NewPostRequest(objectType, instanceID, propertyDefinition.PropertyID, contentType, payload); break; case TObjectState.Update: request = client.NewPutRequest(objectType, instanceID, propertyDefinition.PropertyID, contentType, payload); break; case TObjectState.Delete: request = client.NewDeleteRequest(objectType, instanceID, propertyDefinition.PropertyID); break; default: break; } Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response == null) { throw new TimeoutException(); } else { BusinessLogicFactory.Clients.UpdateClientActivity(client); if (response.StatusCode != StatusCode.Changed) { throw new BadRequestException(); } } } } } } } }
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); }
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 string SaveObject(Guid clientID, Model.Object item, TObjectState state) { string result = null; LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client == null) { ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Client not found ", clientID.ToString())); throw new NoLongerAvailableException("Device not connected"); } else { ObjectDefinitionLookups lookups = BusinessLogicFactory.Clients.GetLookups(); ObjectDefinition objectDefinition = lookups.GetObjectDefinition(item.ObjectDefinitionID); if (objectDefinition == null) { ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Metadata not found ", item.ObjectDefinitionID.ToString(), " client ", client.Address.ToString())); } else { int objectID; if (int.TryParse(objectDefinition.ObjectID, out objectID)) { Model.ObjectType objectType = client.SupportedTypes.GetObjectType(objectID); if (objectType != null) { Request request = null; switch (state) { case TObjectState.NotChanged: break; case TObjectState.Add: ushort instanceID; if (ushort.TryParse(item.InstanceID, out instanceID)) { request = client.NewPostRequest(objectType, null, null, TlvConstant.CONTENT_TYPE_TLV, SerialiseObject(objectDefinition, item, instanceID)); } else { request = client.NewPostRequest(objectType, null, null, TlvConstant.CONTENT_TYPE_TLV, SerialiseObject(objectDefinition, item)); } break; case TObjectState.Update: request = client.NewPostRequest(objectType, item.InstanceID, null, TlvConstant.CONTENT_TYPE_TLV, SerialiseObject(objectDefinition, item)); break; case TObjectState.Delete: request = client.NewDeleteRequest(objectType, item.InstanceID, null); break; default: break; } ApplicationEventLog.Write(LogLevel.Information, string.Concat("SaveObject - Send request ", string.Concat(objectType.Path, "/", item.InstanceID), " client ", client.Address.ToString())); Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response == null) { throw new TimeoutException(); } else { BusinessLogicFactory.Clients.UpdateClientActivity(client); if (response.StatusCode == StatusCode.Created) { if (!string.IsNullOrEmpty(response.LocationPath) && (response.LocationPath.StartsWith(request.UriPath))) { int startIndex = request.UriPath.Length + 1; if (startIndex < response.LocationPath.Length) result = response.LocationPath.Substring(startIndex); } } else if (response.StatusCode == StatusCode.Changed) { } else if ((response.StatusCode == StatusCode.NotFound) && (state == TObjectState.Delete)) { } else { throw new BadRequestException(); } } } } } } return result; }
public void SaveMetric(ClientMetric metric, TObjectState state) { Dictionary<string, BsonValue> clientMetricValues = new Dictionary<string, BsonValue>(); clientMetricValues.Add("Incremental", metric.Incremental); SaveMetric(metric, state, metric.ClientID.ToByteArray(), CLIENT_METRICS_COLLECTION, clientMetricValues); }
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 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); }
/*private void OnNotification(object sender, NotificationEventArgs e) { if (!string.IsNullOrEmpty(e.ID)) _Metrics.Remove(StringUtils.GuidDecode(e.ID)); }*/ public void SaveMetric(OrganisationMetric metric, TObjectState state) { Dictionary<string, BsonValue> organisationMetricValues = new Dictionary<string, BsonValue>(); SaveMetric(metric, state, metric.OrganisationID, ORGANISATION_METRICS_COLLECTION, organisationMetricValues); }
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); }
/*private void OnNotification(object sender, NotificationEventArgs e) * { * if (!string.IsNullOrEmpty(e.ID)) * _Metrics.Remove(StringUtils.GuidDecode(e.ID)); * }*/ public void SaveMetric(OrganisationMetric metric, TObjectState state) { Dictionary <string, BsonValue> organisationMetricValues = new Dictionary <string, BsonValue>(); SaveMetric(metric, state, metric.OrganisationID, ORGANISATION_METRICS_COLLECTION, organisationMetricValues); }
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); }
public void SaveObjectProperty(Guid clientID, Guid objectDefinitionID, string instanceID, Property property, TObjectState state) { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client == null) { ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Client not found ", clientID.ToString())); throw new NoLongerAvailableException("Device not connected"); } else { ObjectDefinitionLookups lookups = BusinessLogicFactory.Clients.GetLookups(); ObjectDefinition objectDefinition = lookups.GetObjectDefinition(objectDefinitionID); if (objectDefinition != null) { int objectID; if (int.TryParse(objectDefinition.ObjectID, out objectID)) { Model.ObjectType objectType = client.SupportedTypes.GetObjectType(objectID); if (objectType != null) { PropertyDefinition propertyDefinition = objectDefinition.GetProperty(property.PropertyDefinitionID); if (propertyDefinition != null) { byte[] payload = null; int contentType = TlvConstant.CONTENT_TYPE_TLV; if (state != TObjectState.Delete) { if ((property.Value != null) || (property.Values != null)) { if ((property.Value != null) && (LWM2MClient.DataFormat == MediaType.TextPlain)) { contentType = LWM2MClient.DataFormat; //contentType = TlvConstant.CONTENT_TYPE_PLAIN; string text = SerialiseProperty(propertyDefinition, property); if (text != null) { payload = Encoding.UTF8.GetBytes(text); } } else { Model.Object lwm2mObject = new Model.Object(); lwm2mObject.Properties.Add(property); payload = SerialiseObject(objectDefinition, lwm2mObject); } } } Request request = null; switch (state) { case TObjectState.NotChanged: break; case TObjectState.Add: request = client.NewPostRequest(objectType, instanceID, propertyDefinition.PropertyID, contentType, payload); break; case TObjectState.Update: request = client.NewPutRequest(objectType, instanceID, propertyDefinition.PropertyID, contentType, payload); break; case TObjectState.Delete: request = client.NewDeleteRequest(objectType, instanceID, propertyDefinition.PropertyID); break; default: break; } Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response == null) { throw new TimeoutException(); } else { BusinessLogicFactory.Clients.UpdateClientActivity(client); if (response.StatusCode != StatusCode.Changed) { throw new BadRequestException(); } } } } } } } }
private void ProcessRequest(Socket client, IPCRequest request) { byte[] response = new byte[4]; try { if (string.Compare(request.Method, "CancelObserveObject", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); bool useReset = request.ReadBoolean(); _ServerAPI.CancelObserveObject(clientID, objectDefinitionID, instanceID, useReset); } else if (string.Compare(request.Method, "CancelObserveObjectProperty", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Guid propertyDefinitionID = request.ReadGuid(); bool useReset = request.ReadBoolean(); _ServerAPI.CancelObserveObjectProperty(clientID, objectDefinitionID, instanceID, propertyDefinitionID, useReset); } else if (string.Compare(request.Method, "CancelObserveObjects", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); bool useReset = request.ReadBoolean(); _ServerAPI.CancelObserveObjects(clientID, objectDefinitionID, useReset); } else if (string.Compare(request.Method, "DeleteClient", true) == 0) { Guid clientID = request.ReadGuid(); _ServerAPI.DeleteClient(clientID); } else if (string.Compare(request.Method, "ExecuteResource", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Guid propertyDefinitionID = request.ReadGuid(); bool success = _ServerAPI.ExecuteResource(clientID, objectDefinitionID, instanceID, propertyDefinitionID); MemoryStream data = new MemoryStream(4096); data.Position = 4; IPCHelper.Write(data, success); data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } if (string.Compare(request.Method, "GetClients", true) == 0) { List <Client> clients = _ServerAPI.GetClients(); if (clients != null) { MemoryStream data = new MemoryStream(4096); data.Position = 4; IPCHelper.Write(data, clients.Count); foreach (Client item in clients) { item.Serialise(data); } data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } } else if (string.Compare(request.Method, "GetDeviceConnectedStatus", true) == 0) { Guid clientID = request.ReadGuid(); DeviceConnectedStatus responseObject = _ServerAPI.GetDeviceConnectedStatus(clientID); if (responseObject != null) { MemoryStream data = new MemoryStream(4096); data.Position = 4; responseObject.Serialise(data); data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } } else if (string.Compare(request.Method, "GetObject", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Model.Object responseObject = _ServerAPI.GetObject(clientID, objectDefinitionID, instanceID); if (responseObject != null) { MemoryStream data = new MemoryStream(4096); data.Position = 4; responseObject.Serialise(data); data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } } else if (string.Compare(request.Method, "GetObjectProperty", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Guid propertyDefinitionID = request.ReadGuid(); Property responseProperty = _ServerAPI.GetObjectProperty(clientID, objectDefinitionID, instanceID, propertyDefinitionID); if (responseProperty != null) { MemoryStream data = new MemoryStream(4096); data.Position = 4; responseProperty.Serialise(data); data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } } else if (string.Compare(request.Method, "GetObjects", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); List <Model.Object> responseObjects = _ServerAPI.GetObjects(clientID, objectDefinitionID); if (responseObjects != null) { MemoryStream data = new MemoryStream(4096); data.Position = 4; foreach (Model.Object responseObject in responseObjects) { responseObject.Serialise(data); } data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } } else if (string.Compare(request.Method, "ObserveObject", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); _ServerAPI.ObserveObject(clientID, objectDefinitionID, instanceID); } else if (string.Compare(request.Method, "ObserveObjectProperty", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Guid propertyDefinitionID = request.ReadGuid(); _ServerAPI.ObserveObjectProperty(clientID, objectDefinitionID, instanceID, propertyDefinitionID); } else if (string.Compare(request.Method, "ObserveObjects", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); _ServerAPI.ObserveObjects(clientID, objectDefinitionID); } else if (string.Compare(request.Method, "SaveObject", true) == 0) { Guid clientID = request.ReadGuid(); Model.Object item = Model.Object.Deserialise(request.Payload); TObjectState state = (TObjectState)request.ReadInt32(); string responseID = _ServerAPI.SaveObject(clientID, item, state); if (responseID != null) { MemoryStream data = new MemoryStream(4096); byte[] buffer = Encoding.UTF8.GetBytes(responseID); NetworkByteOrderConverter.WriteInt32(data, (int)(buffer.Length + 4)); NetworkByteOrderConverter.WriteInt32(data, buffer.Length); data.Write(buffer, 0, buffer.Length); response = data.ToArray(); } } else if (string.Compare(request.Method, "SaveObjectProperty", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Property property = Property.Deserialise(request.Payload); TObjectState state = (TObjectState)request.ReadInt32(); _ServerAPI.SaveObjectProperty(clientID, objectDefinitionID, instanceID, property, state); } else if (string.Compare(request.Method, "SetDataFormat", true) == 0) { TDataFormat dataFormat = (TDataFormat)request.ReadInt32(); _ServerAPI.SetDataFormat(dataFormat); } else if (string.Compare(request.Method, "SetNotificationParameters", true) == 0) { Guid clientID = request.ReadGuid(); Guid objectDefinitionID = request.ReadGuid(); string instanceID = request.ReadString(); Guid propertyDefinitionID = request.ReadGuid(); NotificationParameters notificationParameters = NotificationParameters.Deserialise(request.Payload); bool success = _ServerAPI.SetNotificationParameters(clientID, objectDefinitionID, instanceID, propertyDefinitionID, notificationParameters); MemoryStream data = new MemoryStream(4096); data.Position = 4; IPCHelper.Write(data, success); data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, (int)data.Length - 4); response = data.ToArray(); } } catch (Exception ex) { MemoryStream data = new MemoryStream(4096); data.Position = 4; IPCHelper.Write(data, ex.GetType().AssemblyQualifiedName); IPCHelper.Write(data, ex.Message); data.Position = 0; NetworkByteOrderConverter.WriteInt32(data, -((int)data.Length - 4)); response = data.ToArray(); } client.Send(response, SocketFlags.None); }
public string SaveObject(Guid clientID, Model.Object item, TObjectState state) { string result = null; LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client == null) { ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Client not found ", clientID.ToString())); throw new NoLongerAvailableException("Device not connected"); } else { ObjectDefinitionLookups lookups = BusinessLogicFactory.Clients.GetLookups(); ObjectDefinition objectDefinition = lookups.GetObjectDefinition(item.ObjectDefinitionID); if (objectDefinition == null) { ApplicationEventLog.Write(LogLevel.Warning, string.Concat("SaveObject - Metadata not found ", item.ObjectDefinitionID.ToString(), " client ", client.Address.ToString())); } else { int objectID; if (int.TryParse(objectDefinition.ObjectID, out objectID)) { Model.ObjectType objectType = client.SupportedTypes.GetObjectType(objectID); if (objectType != null) { Request request = null; switch (state) { case TObjectState.NotChanged: break; case TObjectState.Add: ushort instanceID; if (ushort.TryParse(item.InstanceID, out instanceID)) { request = client.NewPostRequest(objectType, null, null, TlvConstant.CONTENT_TYPE_TLV, SerialiseObject(objectDefinition, item, instanceID)); } else { request = client.NewPostRequest(objectType, null, null, TlvConstant.CONTENT_TYPE_TLV, SerialiseObject(objectDefinition, item)); } break; case TObjectState.Update: request = client.NewPostRequest(objectType, item.InstanceID, null, TlvConstant.CONTENT_TYPE_TLV, SerialiseObject(objectDefinition, item)); break; case TObjectState.Delete: request = client.NewDeleteRequest(objectType, item.InstanceID, null); break; default: break; } ApplicationEventLog.Write(LogLevel.Information, string.Concat("SaveObject - Send request ", string.Concat(objectType.Path, "/", item.InstanceID), " client ", client.Address.ToString())); Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response == null) { throw new TimeoutException(); } else { BusinessLogicFactory.Clients.UpdateClientActivity(client); if (response.StatusCode == StatusCode.Created) { if (!string.IsNullOrEmpty(response.LocationPath) && (response.LocationPath.StartsWith(request.UriPath))) { int startIndex = request.UriPath.Length + 1; if (startIndex < response.LocationPath.Length) { result = response.LocationPath.Substring(startIndex); } } } else if (response.StatusCode == StatusCode.Changed) { } else if ((response.StatusCode == StatusCode.NotFound) && (state == TObjectState.Delete)) { } else { throw new BadRequestException(); } } } } } } return(result); }