Example #1
0
        /// <summary>
        /// Sets up the service with all necessare components. Gets all known storage sites and starts data readers for each of them.
        /// </summary>
        /// <param name="loggerFactory">A factory to create loggers from.</param>
        /// <param name="configuration">The app configuration.</param>
        /// <param name="locationsService">Provides storage sites.</param>
        /// <param name="environmentalDataRepository">Repository of environmental data.</param>
        /// <param name="dataDensityReducer">Data density reducing strategy.</param>
        public EnvironmentService(ILoggerFactory loggerFactory,
                                  IConfiguration configuration,
                                  LocationsService locationsService,
                                  IEnvironmentalDataRepository environmentalDataRepository,
                                  IDataDensityReducer dataDensityReducer)
        {
            LoggerFactory               = loggerFactory;
            Logger                      = LoggerFactory.CreateLogger <EnvironmentService>();
            LocationsService            = locationsService;
            EnvironmentalDataRepository = environmentalDataRepository;
            SnapshotQueue               = new ConcurrentQueue <EnvironmentSnapshot>();
            DataDensityReducer          = dataDensityReducer;

            // Get Kafka endpoint
            KafkaEndpoint = configuration.GetValue("Kafka", "");
            if (string.IsNullOrEmpty(KafkaEndpoint))
            {
                throw new Exception("Invalid Kafka endpoint provided!");
            }
            else
            {
                Logger.LogInformation($"Configured Kafka endpoint `{KafkaEndpoint}`.");
            }

            // Start reading from snapshot queue
            QueueConsumer = new TaskFactory().StartNew(() =>
            {
                while (true)
                {
                    if (SnapshotQueue.TryDequeue(out EnvironmentSnapshot snapshot))
                    {
                        EnvironmentalDataRepository.RecordEnvironmentalSnapshot(snapshot);
                    }
                    else
                    {
                        Task.Delay(1000);
                    }
                }
            });

            // Set up data readers for all existing storage sites
            DataReaders = new Dictionary <Guid, EnvironmentalDataReader>();
            foreach (StorageSite site in LocationsService.GetAllStorageSites())
            {
                CreateAndRegisterDataReader(site);
            }

            // Subscribe to the creation of new storage sites
            LocationsService.Subscribe(this);

            // TODO: check for failed readers (Kafka not available?) and restart them
        }
Example #2
0
 public MockStatsProvider(ILocationsRepository locationsRepository, IMaterialBatchRepository batchRepository, IEnvironmentalDataRepository environmentalDataRepository)
 {
     LocationsRepository         = locationsRepository;
     BatchRepository             = batchRepository;
     EnvironmentalDataRepository = environmentalDataRepository;
 }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StatsService"/> class.
 /// </summary>
 /// <param name="provider">The stats-related data provider..</param>
 /// <param name="environmentalDataRepository">The environmental data repository.</param>
 public StatsService(IStatsProvider provider, IEnvironmentalDataRepository environmentalDataRepository)
 {
     Provider = provider;
     EnvironmentalDataRepository = environmentalDataRepository;
 }