public TEntity Insert <TEntity>(string collectionName, TEntity obj) where TEntity : class, IArgonEntity { if (obj.Id == null) { obj.Id = EntitiesUtils.GenerateId(); } _mongoDatabase.GetCollection <TEntity>(collectionName).InsertOne(obj); return(obj); }
public Task PersistEntity <T>(T entity) where T : class, INeonIoTEntity { T obj = default(T); entity.EventDateTime = DateTime.Now; entity.EntityType = entity.GetType().FullName; if (string.IsNullOrEmpty(entity.Id)) { entity.Id = EntitiesUtils.GenerateId(); } if (string.IsNullOrEmpty(entity.Name)) { obj = _entitiesConnector.Query <T>(EntitiesCollectionName).FirstOrDefault(e => e.EntityType == typeof(T).FullName && e.GroupName == entity.GroupName); } else { obj = _entitiesConnector.Query <T>(EntitiesCollectionName).FirstOrDefault(e => e.Name == entity.Name && e.GroupName == entity.GroupName); } if (obj == null) { _logger.LogDebug($"Inserting new entity type {entity.GetType().Namespace} - {entity.Name} {entity.GroupName}"); entity = _entitiesConnector.Insert(EntitiesCollectionName, entity); } else { entity.Id = obj.Id; _entitiesConnector.Update(EntitiesCollectionName, entity); } if (!EntityHaveChanges(obj, entity)) { return(Task.CompletedTask); } PersistEvent(entity); Publish(entity); return(Task.CompletedTask); }
private void PersistEvent <T>(T entity) where T : class, INeonIoTEntity { entity.Id = EntitiesUtils.GenerateId(); _eventsConnector.Insert(GetCollectionName(entity.GetType()), entity); }
public TelegramNotifierEntity() { Id = EntitiesUtils.GenerateId(); IsEnabled = true; }
private async Task ConnectToServer() { var clientOptions = new MqttClientOptionsBuilder().WithClientId($"Neon-Server-{EntitiesUtils.GenerateId()}"); if (_config.UseEmbeddedServer) { clientOptions = clientOptions.WithTcpServer("127.0.0.1", _config.EmbeddedServerPort); } else { clientOptions = clientOptions.WithTcpServer(_config.Client.Hostname, _config.Client.Port); } _mqttClient = new MqttFactory().CreateMqttClient(); var options = clientOptions.Build(); _logger.LogInformation($"Connecting to server "); _mqttClient.UseDisconnectedHandler(async args => { _logger.LogInformation($"Disconnected from server, will retry in 5 seconds"); await Task.Delay(5000); if (_clientConnectionRetryNum <= 3) { if (_mqttClient != null) { await _mqttClient.ConnectAsync(options); _logger.LogInformation($"Reconnected to server..."); _clientConnectionRetryNum++; } } }); _mqttClient.UseApplicationMessageReceivedHandler(args => { if (args.ApplicationMessage.Payload == null) { args.ApplicationMessage.Payload = Encoding.UTF8.GetBytes(""); } OnMessageReceived(args.ApplicationMessage.Topic, Encoding.UTF8.GetString(args.ApplicationMessage.Payload)); }); await _mqttClient.ConnectAsync(options); await SubscribeTopic("/neon"); _logger.LogInformation($"MQTT Client Connected"); }