Beispiel #1
0
        public async Task <dynamic> GetInstances()
        {
            var now       = DateTime.UtcNow.ToTotalSeconds();
            var instances = await _instanceRepository.GetAllAsync().ConfigureAwait(false);

            var devices = await _deviceRepository.GetAllAsync().ConfigureAwait(false);

            var         list  = new List <dynamic>();
            const ulong delta = 15 * 60;

            foreach (var instance in instances)
            {
                var instanceDevices = devices.Where(device => string.Compare(device.InstanceName, instance.Name, true) == 0);
                var totalCount      = instanceDevices.Count();
                var onlineCount     = instanceDevices.Count(device => device.LastSeen >= now - delta);
                var offlineCount    = instanceDevices.Count(device => device.LastSeen < now - delta);
                list.Add(new
                {
                    name  = instance.Name,
                    type  = FormatInstanceType(instance.Type),
                    level = $"{instance.MinimumLevel}-{instance.MaximumLevel}",
                    //count = totalCount == 0 ? "0" : $"{totalCount} ({onlineCount}/{offlineCount})",
                    count     = totalCount == 0 ? "0" : $"{onlineCount}/{offlineCount}|{totalCount}",
                    geofences = string.Join(", ", instance.Geofences ?? new List <string>()),
                    status    = await InstanceController.Instance.GetInstanceStatus(instance).ConfigureAwait(false),
                    buttons   = $"<a href='/dashboard/instance/edit/{Uri.EscapeDataString(instance.Name)}' role='button' class='btn btn-sm btn-primary'>Edit Instance</a>",
                });
            }
            return(new { data = new { instances = list } });
        }
        public async Task Start()
        {
            var instances = await _instanceRepository.GetAllAsync().ConfigureAwait(false);

            var devices = await _deviceRepository.GetAllAsync().ConfigureAwait(false);

            foreach (var instance in instances)
            {
                if (!ThreadPool.QueueUserWorkItem(async _ =>
                {
                    _logger.LogInformation($"Starting {instance.Name}");
                    await AddInstance(instance).ConfigureAwait(false);
                    _logger.LogInformation($"Started {instance.Name}");
                    foreach (var device in devices.AsEnumerable().Where(d => string.Compare(d.InstanceName, instance.Name, true) == 0))
                    {
                        AddDevice(device);
                    }
                }))
                {
                    _logger.LogError($"Failed to start instance {instance.Name}");
                }
            }
            _logger.LogInformation("Done starting instances");
        }