public void AddObjectDefinition(ObjectDefinition objectDefinition) { _ObjectDefinitionList.Add(objectDefinition); _ObjectDefinitions.Add(objectDefinition.ObjectDefinitionID, objectDefinition); if (!string.IsNullOrEmpty(objectDefinition.ObjectID)) { Dictionary<string, ObjectDefinition> objectDefinitions; List<ObjectDefinition> objectDefinitionList; if (objectDefinition.OrganisationID.HasValue) { if (!_ObjectDefinitionByOrganisation.TryGetValue(objectDefinition.OrganisationID.Value, out objectDefinitions)) { objectDefinitions = new Dictionary<string, ObjectDefinition>(); _ObjectDefinitionByOrganisation.Add(objectDefinition.OrganisationID.Value, objectDefinitions); } if (!_ObjectDefinitionsByOrganisation.TryGetValue(objectDefinition.OrganisationID.Value, out objectDefinitionList)) { objectDefinitionList = new List<ObjectDefinition>(); _ObjectDefinitionsByOrganisation.Add(objectDefinition.OrganisationID.Value, objectDefinitionList); } } else { objectDefinitions = _DefaultObjectDefinitions; objectDefinitionList = _DefaultObjectDefinitionList; } objectDefinitionList.Add(objectDefinition); objectDefinitions.Add(objectDefinition.ObjectID, objectDefinition); } }
private void LoadObjectDefinition(IMongoDatabase database, ObjectDefinitionLookups lookups) { IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("ObjectDefinition"); IAsyncCursor<BsonDocument> mongoCursor = collection.FindSync(new BsonDocument()); while (mongoCursor.MoveNext()) { foreach (BsonDocument item in mongoCursor.Current) { ObjectDefinition objectDefinition = new ObjectDefinition(); objectDefinition.ObjectDefinitionID = BsonHelper.GetGuid(item, "_id"); objectDefinition.ObjectID = BsonHelper.GetString(item, "ObjectID"); objectDefinition.OrganisationID = BsonHelper.GetInteger(item, "OrganisationID"); if (objectDefinition.OrganisationID.HasValue && (objectDefinition.OrganisationID.Value == 0)) objectDefinition.OrganisationID = null; objectDefinition.Name = BsonHelper.GetString(item, "Name"); objectDefinition.MIMEType = BsonHelper.GetString(item, "MIMEType"); objectDefinition.Description = BsonHelper.GetString(item, "Description"); objectDefinition.SerialisationName = BsonHelper.GetString(item, "SerialisationName"); objectDefinition.Singleton = BsonHelper.GetBoolean(item, "Singleton"); if (item.Contains("Properties")) { BsonArray array = item["Properties"].AsBsonArray; foreach (BsonValue arrayItem in array) { BsonDocument propertyItem = arrayItem.AsBsonDocument; if (propertyItem != null) { if (objectDefinition.Properties == null) objectDefinition.Properties = new List<PropertyDefinition>(); PropertyDefinition property = new PropertyDefinition(); property.PropertyDefinitionID = BsonHelper.GetGuid(propertyItem, "_id"); property.PropertyID = BsonHelper.GetString(propertyItem, "PropertyID"); property.Name = BsonHelper.GetString(propertyItem, "Name"); property.Description = BsonHelper.GetString(propertyItem, "Description"); property.DataType = (TPropertyDataType)propertyItem["DataType"].AsInt32; if (propertyItem.Contains("DataTypeLength")) property.DataTypeLength = propertyItem["DataTypeLength"].AsInt32; property.MIMEType = BsonHelper.GetString(propertyItem, "MIMEType"); property.MinValue = BsonHelper.GetString(propertyItem, "MinValue"); property.MaxValue = BsonHelper.GetString(propertyItem, "MaxValue"); property.Units = BsonHelper.GetString(propertyItem, "Units"); property.IsCollection = BsonHelper.GetBoolean(propertyItem, "IsCollection"); property.IsMandatory = BsonHelper.GetBoolean(propertyItem, "IsMandatory"); property.Access = (TAccessRight)propertyItem["Access"].AsInt32; if (propertyItem.Contains("SortOrder")) property.SortOrder = propertyItem["SortOrder"].AsInt32; property.SerialisationName = BsonHelper.GetString(propertyItem, "SerialisationName"); property.CollectionItemSerialisationName = BsonHelper.GetString(propertyItem, "CollectionItemSerialisationName"); objectDefinition.Properties.Add(property); } } } lookups.AddObjectDefinition(objectDefinition); } } }
public void SaveObjectDefinition(ObjectDefinition objectDefinition, TObjectState state) { List<ObjectDefinition> objectDefinitions = new List<ObjectDefinition>(); objectDefinitions.Add(objectDefinition); SaveObjectDefinitions(objectDefinitions, state); }
private static int SortObjectDefinition(ObjectDefinition x, ObjectDefinition y) { int result = 0; int xValue = 0; int yValue = 0; if (x != null) int.TryParse(x.ObjectID, out xValue); if (y != null) int.TryParse(y.ObjectID, out yValue); result = xValue.CompareTo(yValue); return result; }
private byte[] SerialiseObject(ObjectDefinition objectDefinition, Model.Object item, ushort objectInstanceID) { byte[] result = null; using (MemoryStream steam = new MemoryStream()) { TlvWriter writer = new TlvWriter(steam); byte[] objectTLV = SerialiseObject(objectDefinition, item); int length = objectTLV.Length; writer.WriteType(TTlvTypeIdentifier.ObjectInstance, objectInstanceID, length); steam.Write(objectTLV, 0, length); result = steam.ToArray(); } return result; }
private byte[] SerialiseObject(ObjectDefinition objectDefinition, Model.Object item) { byte[] result = null; TlvWriter arrayWriter = null; MemoryStream arraystream = null; using (MemoryStream steam = new MemoryStream()) { TlvWriter writer = new TlvWriter(steam); foreach (Property property in item.Properties) { PropertyDefinition propertyDefinition = objectDefinition.GetProperty(property.PropertyID); if (propertyDefinition != null) { if (propertyDefinition.IsCollection) { if (property.Values != null) { ushort identifier; if (ushort.TryParse(propertyDefinition.PropertyID, out identifier)) { if (arrayWriter == null) { arraystream = new MemoryStream(); arrayWriter = new TlvWriter(arraystream); } arraystream.SetLength(0); foreach (PropertyValue propertyValue in property.Values) { WriteValue(arrayWriter, TTlvTypeIdentifier.ResourceInstance, propertyDefinition.DataType, propertyValue.PropertyValueID, propertyValue.Value); } byte[] arrayItems = arraystream.ToArray(); writer.Write(TTlvTypeIdentifier.MultipleResources, identifier, arrayItems); } } } else if (property.Value != null) { WriteValue(writer, TTlvTypeIdentifier.ResourceWithValue, propertyDefinition.DataType, propertyDefinition.PropertyID, property.Value.Value); } } } result = steam.ToArray(); } return result; }
private Property ParseProperty(ObjectDefinition objectDefinition, PropertyDefinition propertyDefinition, int requestContentType, Response response) { Property result = null; int contentType; if (response.ContentType == -1) { contentType = requestContentType; } else { contentType = response.ContentType; } if (contentType == TlvConstant.CONTENT_TYPE_TLV) { TlvReader reader = new TlvReader(response.Payload); Model.Object lwm2mObject = ObjectUtils.ParseObject(objectDefinition, reader); if ((lwm2mObject != null) && (lwm2mObject.Properties.Count > 0)) { foreach (Property item in lwm2mObject.Properties) { if (item.PropertyDefinitionID == propertyDefinition.PropertyDefinitionID) { result = item; break; } } } } else if ((contentType == MediaType.TextPlain) || (contentType == TlvConstant.CONTENT_TYPE_PLAIN)) { string text = Encoding.UTF8.GetString(response.Payload); result = new Property(); result.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID; result.PropertyID = propertyDefinition.PropertyID; result.Value = new PropertyValue(ObjectUtils.GetValue(text, propertyDefinition)); } else if ((contentType == MediaType.ApplicationJson) || (contentType == TlvConstant.CONTENT_TYPE_JSON)) { JsonReader reader = new JsonReader(new MemoryStream(response.Payload)); //LWM2MObject lwm2mObject = ObjectUtils.ParseObject(objectDefinition, reader); //if ((lwm2mObject != null) && (lwm2mObject.Properties.Count > 0)) //{ // foreach (LWM2MProperty item in lwm2mObject.Properties) // { // if (item.PropertyDefinitionID == propertyDefinition.PropertyDefinitionID) // { // result = item; // break; // } // } //} result = ObjectUtils.ParseProperty(propertyDefinition, reader); } return result; }
private List<Model.Object> ParseObjects(ObjectDefinition objectDefinition, int requestContentType, Response response) { List<Model.Object> result = new List<Model.Object>(); int contentType; if (response.ContentType == -1) { contentType = requestContentType; } else { contentType = response.ContentType; } if (contentType == TlvConstant.CONTENT_TYPE_TLV) { TlvReader reader = new TlvReader(response.Payload); while (reader.Read()) { if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ObjectInstance) { TlvReader objectReader = new TlvReader(reader.TlvRecord.Value); Model.Object item = ObjectUtils.ParseObject(objectDefinition, objectReader); if (item != null) { item.InstanceID = reader.TlvRecord.Identifier.ToString(); result.Add(item); } } } } return result; }
private Model.Object ParseObject(ObjectDefinition objectDefinition, int requestContentType, Response response) { Model.Object result = null; int contentType; if (response.ContentType == -1) { contentType = requestContentType; } else { contentType = response.ContentType; } if (contentType == TlvConstant.CONTENT_TYPE_TLV) { TlvReader reader = new TlvReader(response.Payload); result = ObjectUtils.ParseObject(objectDefinition, reader); } else if (contentType == MediaType.ApplicationJson) { JsonReader reader = new JsonReader(new MemoryStream(response.Payload)); result = ObjectUtils.ParseObject(objectDefinition, reader); } return result; }
public void Observe( ObjectDefinition objectDefinition, ObjectType objectType, string instanceID, PropertyDefinition propertyDefinition) { Request request; if (propertyDefinition == null) request = NewGetRequest(objectType, instanceID, null); else request = NewGetRequest(objectType, instanceID, propertyDefinition.PropertyID); request.MarkObserve(); if (_ObserveRequests == null) _ObserveRequests = new List<ObserveRequest>(); bool found = false; for (int index = 0; index < _ObserveRequests.Count; index++) { if (_ObserveRequests[index].Request.UriPath == request.UriPath) { found = true; break; } } if (!found) { _ObserveRequests.Add(new ObserveRequest() { Request = request, ObjectDefinition = objectDefinition, PropertyDefinition = propertyDefinition }); request.Respond += new EventHandler<ResponseEventArgs>(ObserveResponse); SendRequest(request); } }
public static Property ParseProperty(ObjectDefinition objectDefinition, JsonReader reader) { Property result = new Property(); string propertyValueID = null; while (reader.Read()) { if (reader.State == TJsonReaderState.Member) { if (string.Compare(reader.Text, "n") == 0) { if (reader.Read() && !string.IsNullOrEmpty(reader.Text)) { string[] fields = reader.Text.Split('/'); PropertyDefinition property = objectDefinition.GetProperty(fields[0]); if (property != null) { result.PropertyDefinitionID = property.PropertyDefinitionID; result.PropertyID = property.PropertyID; if (fields.Length > 1) { propertyValueID = fields[1]; result.Values = new List<PropertyValue>(); } } } } else if ((string.Compare(reader.Text, "v") == 0) || (string.Compare(reader.Text, "sv") == 0)) { if (reader.Read() && !string.IsNullOrEmpty(reader.Text)) { if (string.IsNullOrEmpty(propertyValueID)) { result.Value = new PropertyValue(reader.Text); } else { PropertyValue value = new PropertyValue(); value.PropertyValueID = propertyValueID; value.Value = reader.Text; result.Values.Add(value); } } } else if (string.Compare(reader.Text, "bv") == 0) { if (reader.Read()) { if (string.IsNullOrEmpty(propertyValueID)) { result.Value = new PropertyValue(reader.AsBoolean.ToString()); } else { PropertyValue value = new PropertyValue(); value.PropertyValueID = propertyValueID; value.Value = reader.AsBoolean.ToString(); result.Values.Add(value); } } } } if (reader.State == TJsonReaderState.EndObject) break; } return result; }
public static Model.Object ParseObject(ObjectDefinition objectDefinition, JsonReader reader) { Model.Object result = null; while (reader.Read()) { if ((reader.State == TJsonReaderState.Member) && (string.Compare(reader.Text,"e") == 0)) { if (result == null) { result = new Model.Object(); result.ObjectID = objectDefinition.ObjectID; result.ObjectDefinitionID = objectDefinition.ObjectDefinitionID; } if (reader.Read()) { if (reader.State == TJsonReaderState.Array) { while (reader.Read()) { if (reader.State == TJsonReaderState.Object) { Property property = ParseProperty(objectDefinition, reader); if (property != null) { bool found = false; foreach (Property item in result.Properties) { if (item.PropertyDefinitionID == property.PropertyDefinitionID) { if ((item.Values != null) && (property.Values != null)) { item.Values.Add(property.Values[0]); } found = true; break; } } if (!found) result.Properties.Add(property); } } if (reader.State == TJsonReaderState.EndArray) break; } } } } } return result; }
public static Model.Object ParseObject(ObjectDefinition objectDefinition, TlvReader reader) { Model.Object result = null; while (reader.Read()) { if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ObjectInstance) { TlvReader objectReader = new TlvReader(reader.TlvRecord.Value); result = ParseObject(objectDefinition, objectReader); if (result != null) { result.InstanceID = reader.TlvRecord.Identifier.ToString(); } break; } if ((reader.TlvRecord.TypeIdentifier != TTlvTypeIdentifier.ObjectInstance) && (reader.TlvRecord.TypeIdentifier != TTlvTypeIdentifier.NotSet)) { if (result == null) { result = new Model.Object(); result.ObjectID = objectDefinition.ObjectID; result.ObjectDefinitionID = objectDefinition.ObjectDefinitionID; } if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ResourceWithValue) { string propertyID = reader.TlvRecord.Identifier.ToString(); PropertyDefinition property = objectDefinition.GetProperty(propertyID); if (property != null) { Property lwm2mProperty = new Property(); lwm2mProperty.PropertyDefinitionID = property.PropertyDefinitionID; lwm2mProperty.PropertyID = property.PropertyID; lwm2mProperty.Value = new PropertyValue(GetValue(reader, property)); result.Properties.Add(lwm2mProperty); } } else if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.MultipleResources) { string propertyID = reader.TlvRecord.Identifier.ToString(); PropertyDefinition property = objectDefinition.GetProperty(propertyID); if (property != null) { Property lwm2mProperty = new Property(); lwm2mProperty.PropertyDefinitionID = property.PropertyDefinitionID; lwm2mProperty.PropertyID = property.PropertyID; result.Properties.Add(lwm2mProperty); TlvReader arrayReader = new TlvReader(reader.TlvRecord.Value); while (arrayReader.Read()) { if (arrayReader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ResourceInstance) { string value = GetValue(arrayReader, property); if (value != null) { if (lwm2mProperty.Values == null) lwm2mProperty.Values = new List<PropertyValue>(); PropertyValue propertyValue = new PropertyValue(); propertyValue.PropertyValueID = arrayReader.TlvRecord.Identifier.ToString(); propertyValue.Value = value; lwm2mProperty.Values.Add(propertyValue); } } } } } } } return result; }