예제 #1
0
 public ProcessingThread(SerialThread serialThread, WebSocketThread webSocketThread, string databaseHost, string webServerHost)
 {
     flurlClient          = new FlurlClient($"http://{webServerHost}:3001");
     client               = new MongoClient($"mongodb://{databaseHost}:27017/?connect=direct;replicaSet=rs0;readPreference=primaryPreferred");
     database             = client.GetDatabase("beademing");
     this.serialThread    = serialThread;
     this.webSocketThread = webSocketThread;
 }
예제 #2
0
        static async Task Main(string[] args)
        {
            var mongoHost     = Environment.GetEnvironmentVariable("MONGO_HOST") ?? "localhost";
            var interfaceHost = Environment.GetEnvironmentVariable("INTERFACE_HOST") ?? "localhost";

            //wait for mongo to be available
            await CheckMongoAvailibility(mongoHost);
            await CheckWebServerAvailibility(interfaceHost);

            FlurlHttp.Configure(settings => {
                var jsonSettings = new JsonSerializerSettings
                {
                    NullValueHandling      = NullValueHandling.Ignore,
                    ObjectCreationHandling = ObjectCreationHandling.Replace
                };
                settings.JsonSerializer = new NewtonsoftJsonSerializer(jsonSettings);
            });

            Thread.CurrentThread.CurrentCulture   = new System.Globalization.CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
            Console.WriteLine("Starting daemon");
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = cancellationTokenSource.Token;

            SerialThread serialThread = new SerialThread(mongoHost, interfaceHost);

            serialThread.PlayBeep();

            WebSocketThread  webSocketThread  = new WebSocketThread($"ws://{interfaceHost}:3001", serialThread);
            ProcessingThread processingThread = new ProcessingThread(serialThread, webSocketThread, mongoHost, interfaceHost);

            var serialPort = Environment.GetEnvironmentVariable("SERIAL_PORT");

            if (string.IsNullOrEmpty(serialPort))
            {
                serialThread.SetPortName();
            }
            else
            {
                serialThread.SetPortName(serialPort);
            }

            var webSocketTask  = webSocketThread.Start(cancellationToken);
            var serialTask     = serialThread.Start(cancellationToken);
            var processingTask = processingThread.Start(cancellationToken);

            Task.WaitAll(webSocketTask, serialTask, processingTask);

            Console.WriteLine("Daemon finished");
        }
        public ProcessingThread(SerialThread serialThread,
                                WebSocketThread webSocketThread,
                                AlarmThread alarmThread,
                                IApiService apiService,
                                IDbService dbService,
                                ILoggerFactory loggerFactory)
        {
            this.serialThread    = serialThread;
            this.webSocketThread = webSocketThread;
            this.alarmThread     = alarmThread;
            this.apiService      = apiService;
            this.dbService       = dbService;

            this.logger = loggerFactory.CreateLogger <ProcessingThread>();
        }
예제 #4
0
        static async Task Main(string[] args)
        {
            // get environment variables
            var mongoHost     = Environment.GetEnvironmentVariable("MONGO_HOST") ?? "localhost";
            var interfaceHost = Environment.GetEnvironmentVariable("INTERFACE_HOST") ?? "localhost";
            var serialPort    = Environment.GetEnvironmentVariable("SERIAL_PORT");

            var serviceProvider = new ServiceCollection()
                                  .AddLogging(builder => builder.AddConsole().AddFilter(level => level >= LogLevel.Debug))
                                  .AddSingleton <ProgramSettings>(new ProgramSettings()
            {
                DatabaseHost  = mongoHost,
                SerialPort    = serialPort,
                WebServerHost = interfaceHost,
            })
                                  .AddTransient <IApiService, ApiService>()
                                  .AddTransient <IDbService, DbService>()
                                  .AddSingleton <AlarmThread>()
                                  .AddSingleton <SerialThread>()
                                  .AddSingleton <WebSocketThread>()
                                  .AddSingleton <ProcessingThread>()
                                  .BuildServiceProvider();

            logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger <Program>();


            //wait for mongo to be available
            await CheckMongoAvailibility(mongoHost);
            await CheckWebServerAvailibility(interfaceHost);

            GeneralConfiguration();

            logger.LogInformation("Starting daemon");
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = cancellationTokenSource.Token;


            AlarmThread      alarmThread      = serviceProvider.GetService <AlarmThread>();
            SerialThread     serialThread     = serviceProvider.GetService <SerialThread>();
            WebSocketThread  webSocketThread  = serviceProvider.GetService <WebSocketThread>();
            ProcessingThread processingThread = serviceProvider.GetService <ProcessingThread>();

            if (string.IsNullOrEmpty(serialPort))
            {
                serialThread.SetPortName();
            }
            else
            {
                serialThread.SetPortName(serialPort);
            }

            var alarmTask      = alarmThread.Start(cancellationToken);
            var webSocketTask  = webSocketThread.Start(cancellationToken);
            var serialTask     = serialThread.Start(cancellationToken);
            var processingTask = processingThread.Start(cancellationToken);

            Task.WaitAll(webSocketTask, serialTask, processingTask, alarmTask);

            // as there is currently no way to cancel the tokens, we should never get here
            logger.LogInformation("Daemon finished");
        }
예제 #5
0
        static async Task <int> Main(string[] args)
        {
            // get environment variables
            var mongoHost          = Environment.GetEnvironmentVariable("MONGO_HOST") ?? "localhost";
            var interfaceHost      = Environment.GetEnvironmentVariable("INTERFACE_HOST") ?? "localhost";
            var serialPort         = Environment.GetEnvironmentVariable("SERIAL_PORT");
            var logDirectory       = Environment.GetEnvironmentVariable("LOG_DIRECTORY");
            var dbCollectionPrefix = Environment.GetEnvironmentVariable("DB_COLLECTION_PREFIX") ?? "";
            var interfacePort      = Environment.GetEnvironmentVariable("INTERFACE_PORT") ?? "3001";

            var showHelp = false;

            // get console line arguments
            var options = new OptionSet {
                { "p|port=", "The name of the serialport", p => serialPort = p ?? serialPort },
                { "i|interface=", "The host running the interface server.", i => interfaceHost = i ?? interfaceHost },
                { "m|mongo=", "The hostname for connecting to the mongo servers", m => mongoHost = m ?? mongoHost },
                { "d|logdirectory=", "The directory to where the daemon writes the log files, if provided", l => logDirectory = l ?? logDirectory },
                { "c|collectionPrefix=", "The prefix to be used in front of the mongo collection name", l => dbCollectionPrefix = l ?? dbCollectionPrefix },
                { "ip|interfacePort=", "The port on which the interface listens", l => interfacePort = l ?? interfacePort },
                { "h|help", "show this message and exit", v => showHelp = (v != null) },
            };

            if (showHelp)
            {
                options.WriteOptionDescriptions(Console.Out);
                return(0);
            }

            try
            {
                // parse the command line
                options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.WriteLine("Error while trying to parse the options: " + e.Message);
                return(-1);
            }

            int interfacePortInt = 3001;

            int.TryParse(interfacePort, out interfacePortInt);

            var programSettings = new ProgramSettings()
            {
                DatabaseHost       = mongoHost,
                SerialPort         = serialPort,
                WebServerHost      = interfaceHost,
                LogDirectory       = logDirectory,
                WebServerPort      = interfacePortInt,
                DbCollectionPrefix = dbCollectionPrefix,
            };

            var serviceProvider = new ServiceCollection()
                                  .AddLogging(builder => builder.AddConsole().AddFilter(level => level >= LogLevel.Debug))
                                  .AddSingleton(programSettings)
                                  .AddTransient <IApiService, ApiService>()
                                  .AddTransient <IDbService, DbService>()
                                  .AddSingleton <AlarmThread>()
                                  .AddSingleton <SerialThread>()
                                  .AddSingleton <WebSocketThread>()
                                  .AddSingleton <ProcessingThread>()
                                  .BuildServiceProvider();

            logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger <Program>();

            logger.LogInformation("Starting daemon {0} with version: {1}",
                                  Assembly.GetEntryAssembly().GetName().Name,
                                  Assembly.GetEntryAssembly().GetName().Version);

            //wait for mongo to be available
            await CheckMongoAvailibility(programSettings);
            await CheckWebServerAvailibility(programSettings);

            GeneralConfiguration();

            logger.LogInformation("Starting daemon");
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = cancellationTokenSource.Token;

            Console.CancelKeyPress += delegate {
                // call methods to clean up
                cancellationTokenSource.Cancel();
            };

            AlarmThread      alarmThread      = serviceProvider.GetService <AlarmThread>();
            SerialThread     serialThread     = serviceProvider.GetService <SerialThread>();
            WebSocketThread  webSocketThread  = serviceProvider.GetService <WebSocketThread>();
            ProcessingThread processingThread = serviceProvider.GetService <ProcessingThread>();

            if (string.IsNullOrEmpty(serialPort))
            {
                serialThread.SetPortName(cancellationToken);
            }
            else
            {
                serialThread.SetPortName(serialPort);
            }

            if (!cancellationToken.IsCancellationRequested)
            {
                var alarmTask      = alarmThread.Start(cancellationToken);
                var webSocketTask  = webSocketThread.Start(cancellationToken);
                var serialTask     = serialThread.Start(cancellationToken);
                var processingTask = processingThread.Start(cancellationToken);

                Task.WaitAll(webSocketTask, serialTask, processingTask, alarmTask);
            }

            // as there is currently no way to cancel the tokens, we should never get here
            logger.LogInformation("Daemon finished");
            return(0);
        }