Ejemplo n.º 1
0
        private BsonDocument GetDocument(CloudTwin twin)
        {
            var properties = twin.Properties.ToDictionary(p => p.Key, p => p.Value);

            properties["_id"]  = twin.Id;
            properties["name"] = twin.Name;
            return(new BsonDocument(properties));
        }
Ejemplo n.º 2
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            try
            {
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ContinueWith(_ => { }).ConfigureAwait(false);

                if (_mongoDatabaseCollection == null)
                {
                    _messageQueue.Publish(new NotifyUserMessage("Add CloudTwin configuration to config file."));
                    return;
                }

                var cursor = await _mongoDatabaseCollection
                             .FindAsync(new BsonDocument(), cancellationToken : cancellationToken)
                             .ConfigureAwait(false);

                var documents = await cursor.ToListAsync(cancellationToken).ConfigureAwait(false);

                foreach (var document in documents)
                {
                    var twin = new CloudTwin();
                    var dict = document.ToDictionary();

                    foreach (var pair in dict)
                    {
                        if (pair.Key.Equals("_id", StringComparison.Ordinal))
                        {
                            twin.Id = pair.Value as string;
                        }
                        else if (pair.Key.Equals("name", StringComparison.OrdinalIgnoreCase))
                        {
                            twin.Name = pair.Value as string;
                        }
                        else
                        {
                            twin.Properties.Add(pair);
                        }
                    }

                    if (!string.IsNullOrEmpty(twin.Id))
                    {
                        _devices.Add(twin);
                    }
                }

                var lastUpdate = DateTime.MinValue;

                while (!cancellationToken.IsCancellationRequested)
                {
                    try
                    {
                        var devices       = _devices.OfType <CloudTwin>().Where(t => t.LastUpdate > lastUpdate).ToList();
                        var newLastUpdate = lastUpdate;

                        foreach (var twin in devices)
                        {
                            if (lastUpdate < twin.LastUpdate)
                            {
                                newLastUpdate = twin.LastUpdate;
                            }

                            var document = GetDocument(twin);
                            await _mongoDatabaseCollection
                            .ReplaceOneAsync(new BsonDocument("_id", BsonValue.Create(twin.Id)), document, cancellationToken : cancellationToken)
                            .ConfigureAwait(false);
                        }

                        lastUpdate = newLastUpdate;
                    }
                    catch (Exception e)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }

                        _log.Error(e.Message, e);
                    }

                    await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken).ContinueWith(_ => { }).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    _log.Error(e.Message, e);
                }
            }
        }