void Awake() { cam = (Camera)FindObjectOfType(typeof(Camera)); arduinoListener = GetComponent <ArduinoListener>(); yellowStat = new Stat(); greenStat = new Stat(); redStat = new Stat(); blueStat = new Stat(); stats[0] = greenStat; stats[1] = yellowStat; stats[2] = redStat; stats[3] = blueStat; if (instance == null) { instance = this; db = new HapticHarmonyDB("leaderboard.db"); } else if (instance != this) { Destroy(gameObject); } // Do not destroy this object, when we load a new scene. DontDestroyOnLoad(gameObject); }
async Task IHostedService.StartAsync(CancellationToken cancellationToken) { await Task.CompletedTask; Console.WriteLine("Application Started!"); var t = Task.Run(async() => { await Task.CompletedTask; while (true) { if (cancellationToken.IsCancellationRequested) { break; } try { this._counter = 0; this._listener = this._services.GetService <ArduinoListener>(); this._listener.LineReceived += this.SerialPort_LineReceived; this._listener.Open(); Console.WriteLine( $"Application is listening to {this._listener.PortName}:{this._listener.BaudRate}"); while (this._listener.IsOpen) { await Task.Delay(100, cancellationToken); } throw new Exception( $"Application disconnected from {this._listener.PortName}:{this._listener.BaudRate}"); } catch (Exception e) { if (this._listener?.IsOpen ?? false) { this._listener.Close(); } Console.WriteLine($@"Error: {e.Message}"); Console.WriteLine(@"Retry in 30 seconds..."); await Task.Delay(30 * 1000, cancellationToken); } } }, cancellationToken); }