private void ProcessRequests() { while (!_Terminate) { _TriggerProcessRequests.Reset(); while (_ClientsToValidate.Count > 0) { LWM2MClient client = null; lock (_ClientsToValidate) { if (_ClientsToValidate.Count > 0) { client = _ClientsToValidate.Dequeue(); } } if (client != null) { if ((client.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) || (client.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)) { ValidateClient(client); } } } if (!_Terminate) { _TriggerProcessRequests.WaitOne(); } } }
public void AddClient(LWM2MClient client) { lock (_NewClients) { _NewClients.Enqueue(client); } _TriggerProcessRequests.Set(); }
public void ClientConnectionExpired(LWM2MClient client) { if (client.OrganisationID != 0) { ServiceEventMessage message = new ServiceEventMessage(); FillDeviceParameters(client, message); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.CLIENT_CONNECTION_EXPIRED, message, TMessagePublishMode.Confirms); } }
public void ClientUpdate(LWM2MClient client) { if (client.OrganisationID != 0) { ServiceEventMessage message = new ServiceEventMessage(); FillDeviceParameters(client, message); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.CLIENT_UPDATE, message, TMessagePublishMode.Confirms); } }
public void AddClient(LWM2MClient client) { _Clients.TryAdd(client.ClientID, client); lock (_ClientsToValidate) { _ClientsToValidate.Enqueue(client); } _TriggerProcessRequests.Set(); }
public void ClientChangedSupportedTypes(LWM2MClient client) { BusinessLogicFactory.Events.ClientUpdate(client); lock (_ClientsToValidate) { _ClientsToValidate.Enqueue(client); } _TriggerProcessRequests.Set(); }
public void DeleteClient(Guid clientID) { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client != null) { BusinessLogicFactory.Clients.DeleteClient(clientID); DataAccessFactory.Clients.SaveClient(client, TObjectState.Delete); } }
public void ObservationNotify(LWM2MClient client, Model.Object lwm2mObject) { if (client.OrganisationID != 0) { ServiceEventMessage message = new ServiceEventMessage(); FillDeviceParameters(client, message); message.Parameters.Add("Object", lwm2mObject); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.OBSERVATION_NOTIFICATION, message, TMessagePublishMode.Confirms); } }
private void ProcessUpdateRequest(Exchange exchange) { Request request = exchange.Request; Guid clientID; Response response; if (StringUtils.GuidTryDecode(request.UriPath.Substring(4), out clientID)) { LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client == null) { response = Response.CreateResponse(request, StatusCode.NotFound); } else { client.Parse(request.UriQueries); BusinessLogicFactory.Clients.UpdateClientActivity(client); client.Address = request.Source; client.EndPoint = exchange.EndPoint; bool updatedLifeTime = false; if ((request.ContentType == (int)MediaType.ApplicationLinkFormat) || (request.ContentType == -1)) { if (request.PayloadSize > 0) { ObjectTypes objectTypes = new ObjectTypes(); objectTypes.Parse(request.PayloadString); if (ObjectTypes.Compare(client.SupportedTypes, objectTypes) != 0) { client.SupportedTypes = objectTypes; if (client.ClientID != Guid.Empty) { DataAccessFactory.Clients.SaveClient(client, TObjectState.Add); updatedLifeTime = true; } BusinessLogicFactory.Clients.ClientChangedSupportedTypes(client); } } } if (!updatedLifeTime) { BusinessLogicFactory.Clients.UpdateClientLifetime(client.ClientID, client.Lifetime); } response = Response.CreateResponse(request, StatusCode.Changed); ApplicationEventLog.Write(LogLevel.Information, string.Concat("Client update ", client.Name, " address ", client.Address.ToString())); } } else { ApplicationEventLog.WriteEntry(string.Concat("Invalid update location", request.UriPath)); response = Response.CreateResponse(request, StatusCode.BadRequest); } exchange.SendResponse(response); }
public void MetricsUpdate(LWM2MClient client) { if (client.OrganisationID != 0) { ServiceEventMessage message = new ServiceEventMessage(); FillDeviceParameters(client, message); client.Metrics.FillParameters(message); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.CLIENT_METRICS, message, TMessagePublishMode.Confirms); client.Metrics.ResetIncrementalMetrics(); } }
private void ProcessRequestBootstrap(Exchange exchange) { Request request = exchange.Request; LWM2MClient client = new LWM2MClient(); // TODO: fix warning client.Address = request.Source; client.EndPoint = exchange.EndPoint; client.Parse(request.UriQueries); BusinessLogicFactory.Clients.AddClient(client); Response response = Response.CreateResponse(request, StatusCode.Changed); exchange.SendResponse(response); }
public DeviceConnectedStatus GetDeviceConnectedStatus(Guid clientID) { DeviceConnectedStatus result = null; LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); if (client != null) { result = new DeviceConnectedStatus(); result.Online = (client.Lifetime > DateTime.UtcNow); if (client.LastActivityTime > DateTime.MinValue) { result.LastActivityTime = client.LastActivityTime; } } return(result); }
public bool ExecuteResource(Guid clientID, Guid objectDefinitionID, string instanceID, Guid propertyDefinitionID) { 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.NewPostRequest(objectType, instanceID, propertyDefinition.PropertyID, -1, null); 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); }
private void ValidateClient(LWM2MClient client) { UpdateClientActivity(client); if (client.ClientID != Guid.Empty) { // TODO: for now simply add the client to the DB. In the future we will want to validate the client first. DataAccessFactory.Clients.SaveClient(client, TObjectState.Add); BusinessLogicFactory.Events.ClientConnected(client); LWM2MClient existingClient; if (_ClientByDeviceID.TryRemove(client.ClientID, out existingClient)) { if (existingClient != client) { existingClient.Cancel(); } } _ClientByDeviceID.TryAdd(client.ClientID, client); } }
public Property GetObjectProperty(Guid clientID, Guid objectDefinitionID, string instanceID, Guid propertyDefinitionID) { Property 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) { PropertyDefinition propertyDefinition = objectDefinition.GetProperty(propertyDefinitionID); if (propertyDefinition != null) { Request request = client.NewGetRequest(objectType, instanceID, propertyDefinition.PropertyID); Response response = client.SendRequest(request).WaitForResponse(LWM2MClient.REQUEST_TIMEOUT); if (response != null && response.StatusCode == StatusCode.Content) { BusinessLogicFactory.Clients.UpdateClientActivity(client); result = ParseProperty(objectDefinition, propertyDefinition, request.Accept, response); } } } } } } } 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); } }
private void ProcessDeregisterRequest(Exchange exchange) { Request request = exchange.Request; Guid clientID = StringUtils.GuidDecode(request.UriPath.Substring(4)); LWM2MClient client = BusinessLogicFactory.Clients.GetClient(clientID); Response response; if (client == null) { response = Response.CreateResponse(request, StatusCode.NotFound); } else { client.Lifetime = DateTime.UtcNow; BusinessLogicFactory.Clients.UpdateClientLifetime(client.ClientID, DateTime.UtcNow); BusinessLogicFactory.Clients.UpdateClientActivity(client.ClientID, DateTime.UtcNow); BusinessLogicFactory.Clients.DeleteClient(clientID); response = Response.CreateResponse(request, StatusCode.Deleted); ApplicationEventLog.Write(LogLevel.Information, string.Concat("Client deregister ", client.Name, " address ", client.Address.ToString())); } exchange.SendResponse(response); }
private void ProcessRequests() { while (!_Terminate) { _TriggerProcessRequests.Reset(); while (_NewClients.Count > 0) { LWM2MClient client = null; try { lock (_NewClients) { if (_NewClients.Count > 0) { client = _NewClients.Dequeue(); } } if (client != null) { if ((client.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) || (client.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)) { Server server = BusinessLogicFactory.Servers.GetServer(); System.Net.IPEndPoint ipEndPoint = client.Address as System.Net.IPEndPoint; CoapClient coapClient = new CoapClient(); coapClient.EndPoint = client.EndPoint; coapClient.Timeout = REQUEST_TIMEOUT; ushort objectInstanceID = 1; foreach (Model.Security item in server.EndPoints) { Request request = new Request(Method.PUT); request.ContentType = TlvConstant.CONTENT_TYPE_TLV;// (int)MediaType.ApplicationOctetStream; request.Destination = client.Address; request.UriPath = "/0"; request.Payload = SerialiseObject(item, objectInstanceID); objectInstanceID++; coapClient.SendAsync(request, (response) => { if (response != null && response.StatusCode == StatusCode.Changed) { request = new Request(Method.PUT); request.ContentType = TlvConstant.CONTENT_TYPE_TLV; //(int)MediaType.ApplicationOctetStream; request.Destination = client.Address; request.UriPath = "/1"; request.Payload = SerialiseObject(server, 1); coapClient.SendAsync(request, (response2) => { if (response2 != null && response2.StatusCode == StatusCode.Changed) { request = new Request(Method.POST); request.Destination = client.Address; request.UriPath = "/bs"; coapClient.SendAsync(request); } }); } } ); } } } } catch (Exception ex) { ApplicationEventLog.WriteEntry("Flow", ex.ToString(), System.Diagnostics.EventLogEntryType.Error); } } if (!_Terminate) { _TriggerProcessRequests.WaitOne(); } } }
private void ProcessRegisterRequest(Exchange exchange) { Request request = exchange.Request; LWM2MClient client = new LWM2MClient(); client.Server = _ServerEndPoint; client.Address = request.Source; client.Parse(request.UriQueries); ObjectTypes objectTypes = new ObjectTypes(); objectTypes.Parse(request.PayloadString); client.SupportedTypes = objectTypes; client.EndPoint = exchange.EndPoint; if (_SecureChannel != null) { CertificateInfo certificateInfo = _SecureChannel.GetClientCertificateInfo(client.Address); if (certificateInfo == null) { string pskIdentity = _SecureChannel.GetClientPSKIdentity(client.Address); if (!string.IsNullOrEmpty(pskIdentity)) { Guid clientID; PSKIdentity identity = DataAccessFactory.Identities.GetPSKIdentity(pskIdentity); if (identity != null) { if (StringUtils.GuidTryDecode(pskIdentity, out clientID)) { client.ClientID = clientID; } client.OrganisationID = identity.OrganisationID; } } } else { Console.WriteLine(certificateInfo.Subject.CommonName); Console.WriteLine(certificateInfo.Subject.Organistion); Guid clientID; if (Guid.TryParse(certificateInfo.Subject.CommonName, out clientID)) { client.ClientID = clientID; } int organisationID; if (int.TryParse(certificateInfo.Subject.Organistion, out organisationID)) { client.OrganisationID = organisationID; } } } if (client.ClientID != Guid.Empty && (client.OrganisationID > 0 || !SecureOnly) && !DataAccessFactory.Clients.IsBlacklisted(client.ClientID)) { BusinessLogicFactory.Clients.AddClient(client); } Response response = Response.CreateResponse(request, StatusCode.Created); //response.AddOption(Option.Create(OptionType.LocationPath, string.Concat("rd/",StringUtils.GuidEncode(client.ClientID)))); response.AddOption(Option.Create(OptionType.LocationPath, "rd")); response.AddOption(Option.Create(OptionType.LocationPath, StringUtils.GuidEncode(client.ClientID))); exchange.SendResponse(response); ApplicationEventLog.Write(LogLevel.Information, string.Concat("Client registered ", client.Name, " address ", client.Address.ToString())); }
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 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 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); }