public static string GetValue(TlvReader reader, PropertyDefinition property) { string result = null; switch (property.DataType) { case TPropertyDataType.NotSet: break; case TPropertyDataType.String: result = reader.TlvRecord.ValueAsString(); break; case TPropertyDataType.Boolean: result = reader.TlvRecord.ValueAsBoolean().ToString(); break; case TPropertyDataType.Integer: result = reader.TlvRecord.ValueAsInt64().ToString(); break; case TPropertyDataType.Float: result = reader.TlvRecord.ValueAsDouble().ToString(); break; case TPropertyDataType.DateTime: result = reader.TlvRecord.ValueAsDateTime().ToString(XmlHelper.XMLDATEFORMAT); break; case TPropertyDataType.Opaque: result = Convert.ToBase64String(reader.TlvRecord.Value); break; case TPropertyDataType.Object: break; default: break; } return result; }
public async void SetStringResource() { // Arrange string expectedValue = "Imagination Technologies 123"; string objectTypeID = "3"; string objectInstanceID = "0"; string resourceID = "0"; await _HttpClientFixture.Login(); ObjectInstance matchedObjectInstance = await _HttpClientFixture.GetObjectInstanceModel(_TestClient.ClientID, objectTypeID, objectInstanceID); Assert.NotNull(matchedObjectInstance); Imagination.Model.PropertyDefinition propertyDefinition = _HttpClientFixture.GetResourceDefinition(matchedObjectInstance.ObjectDefinition, resourceID); Assert.NotNull(propertyDefinition); Imagination.Model.Object objectModel = new Imagination.Model.Object(); Imagination.Model.Property propertyToSet = new Imagination.Model.Property(); propertyToSet.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID; propertyToSet.PropertyID = propertyDefinition.PropertyID; propertyToSet.Value = new Imagination.Model.PropertyValue(expectedValue); objectModel.Properties.Add(propertyToSet); ObjectInstance objectInstanceToSet = new ObjectInstance(matchedObjectInstance.ObjectDefinition, objectModel); Link selfLink = matchedObjectInstance.GetLink("self"); Assert.NotNull(selfLink); HttpResponseMessage response = await _HttpClientFixture.SetClientObject(selfLink.href, objectInstanceToSet); Assert.True(response.IsSuccessStatusCode); // Act IResource resource = _TestClient.GetResource($"{objectTypeID}/{objectInstanceID}/{resourceID}"); Assert.NotNull(resource); string actualValue = (resource as LWM2MResource).ToString(); // Assert Assert.Equal(expectedValue, actualValue); }
public static string GetValue(string text, PropertyDefinition property) { string result = null; switch (property.DataType) { case TPropertyDataType.NotSet: break; case TPropertyDataType.String: result = text; break; case TPropertyDataType.Boolean: if (string.Compare(text, "1") == 0) result = true.ToString(); else result = false.ToString(); break; case TPropertyDataType.Integer: case TPropertyDataType.Float: result = text; break; case TPropertyDataType.DateTime: long seconds = 0; if (long.TryParse(text, out seconds)) { result = _Epoch.AddSeconds(seconds).ToString(XmlHelper.XMLDATEFORMAT); } break; case TPropertyDataType.Opaque: result = text; break; case TPropertyDataType.Object: break; default: break; } return result; }
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); } } }
private string SerialiseProperty(PropertyDefinition propertyDefinition, Property property) { string result = null; switch (propertyDefinition.DataType) { case TPropertyDataType.NotSet: break; case TPropertyDataType.String: result = property.Value.Value; break; case TPropertyDataType.Boolean: bool boolValue; if (bool.TryParse(property.Value.Value, out boolValue)) { if (boolValue) result = "1"; else result = "0"; } break; case TPropertyDataType.Integer: case TPropertyDataType.Float: result = property.Value.Value; break; case TPropertyDataType.DateTime: try { DateTime dateTimeValue = System.Xml.XmlConvert.ToDateTime(property.Value.Value, System.Xml.XmlDateTimeSerializationMode.Utc); TimeSpan diff = dateTimeValue.Subtract(_Epoch); long seconds = (long)diff.TotalSeconds; result = seconds.ToString(); } catch { } break; case TPropertyDataType.Opaque: result = property.Value.Value; break; case TPropertyDataType.Object: break; default: break; } 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; }
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(PropertyDefinition propertyDefinition, JsonReader reader) { Property result = null; while (reader.Read()) { if ((reader.State == TJsonReaderState.Member) && (string.Compare(reader.Text, "e") == 0)) { if (result == null) { result = new Property(); result.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID; result.PropertyID = propertyDefinition.PropertyID; } if (reader.Read()) { if (reader.State == TJsonReaderState.Array) { while (reader.Read()) { if (reader.State == TJsonReaderState.Object) { string propertyValueID = null; string value = 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('/'); 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)) { value = reader.Text; } } else if (string.Compare(reader.Text, "bv") == 0) { if (reader.Read()) { value = reader.AsBoolean.ToString(); } } } if (reader.State == TJsonReaderState.EndObject) { if (propertyDefinition.IsCollection) { PropertyValue propertyValue = new PropertyValue(); propertyValue.PropertyValueID = propertyValueID; propertyValue.Value = value; result.Values.Add(propertyValue); } else { result.Value = new PropertyValue(value); } break; } } } if (reader.State == TJsonReaderState.EndArray) break; } } } } } return result; }