public Model.Object GetObject(Guid clientID, Guid objectDefinitionID, string instanceID) { Model.Object result = null; try { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client != null) { 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) { Request request = client.NewGetRequest(objectType, instanceID); Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response != null && response.StatusCode == StatusCode.Content) { BusinessLogicFactory.Clients.UpdateClientActivity(client); result = ParseObject(objectDefinition, request.Accept, response); if (result != null) { result.InstanceID = instanceID; } } } } } } } catch (Exception ex) { ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error); } return(result); }
public void CancelObserveObjectProperty(Guid clientID, Guid objectDefinitionID, string instanceID, Guid propertyDefinitionID, bool useReset) { try { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client != null) { 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) { if (propertyDefinitionID != Guid.Empty) { PropertyDefinition propertyDefinition = objectDefinition.GetProperty(propertyDefinitionID); if (propertyDefinition != null) { client.CancelObserve(objectType, instanceID, propertyDefinition.PropertyID, useReset); } } else { client.CancelObserve(objectType, instanceID, null, useReset); } } } } } } catch (Exception ex) { ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error); } }
public bool SetNotificationParameters(Guid clientID, Guid objectDefinitionID, string instanceID, Guid propertyDefinitionID, NotificationParameters notificationParameters) { bool result = false; try { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client != null) { 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(propertyDefinitionID); if (propertyDefinition != null) { Request request = client.NewPutRequest(objectType, instanceID, propertyDefinition.PropertyID, -1, null); if (notificationParameters.MinimumPeriod.HasValue) { request.AddUriQuery(string.Concat("pmin=", notificationParameters.MinimumPeriod.Value.ToString())); } if (notificationParameters.MaximumPeriod.HasValue) { request.AddUriQuery(string.Concat("pmax=", notificationParameters.MaximumPeriod.Value.ToString())); } if (notificationParameters.GreaterThan.HasValue) { request.AddUriQuery(string.Concat("gt=", notificationParameters.GreaterThan.Value.ToString("0.0"))); } if (notificationParameters.LessThan.HasValue) { request.AddUriQuery(string.Concat("lt=", notificationParameters.LessThan.Value.ToString("0.0"))); } if (notificationParameters.Step.HasValue) { request.AddUriQuery(string.Concat("stp=", notificationParameters.Step.Value.ToString("0.0"))); } Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response == null) { throw new TimeoutException(); } else { if (response.StatusCode == StatusCode.Changed) { result = true; } } } } } } } } catch (Exception ex) { ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error); } return(result); }
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 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); }