private void OnUnboundClientEventSent(NetClient client, AbstractEntityEvent evt) { if (evt is EntityDefinitionEvent definitionEvent) { //TODO: manage to build something to handle disconnect / reconnect of devices (might be useful for energy saving) //TODO: see if the unique device ID is already bound to an entity and reuse it AbstractEntityModule[] modules = new AbstractEntityModule[definitionEvent.Modules.Length]; for (int i = 0; i < modules.Length; i++) { modules[i] = EntityModuleFactory.BuildModule(definitionEvent.Modules[i]); } var newEntity = new Entity(client, _nextEntityId++, definitionEvent.DeviceId, modules); client.EntityEventSent -= OnUnboundClientEventSent; lock (_unboundClients) _unboundClients.Remove(client); newEntity.SendCommand(new EntityConfigCommand(newEntity.Id)); //TODO: see if modules need to add something to the config ? lock (_entities) _entities.Add(newEntity); if (EntityAdded != null) { lock (EntityAdded) EntityAdded(newEntity); } } else { //this entity sent an event before if was declared as an entity. This is not good. //TODO: handle this exception somehow gracefully } }
public static void send(AbstractEntityEvent incomingEvent) { incomingEvent.Invoke(incomingEvent.entity); }