From 3c09fb8f15291243de9ff630a559704beb26ee27 Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Wed, 8 Jun 2022 02:59:39 +0100 Subject: [PATCH] Bot auto recovery --- SecondBotEvents/Services/BotClientService.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/SecondBotEvents/Services/BotClientService.cs b/SecondBotEvents/Services/BotClientService.cs index 817d07c..0bd3eef 100644 --- a/SecondBotEvents/Services/BotClientService.cs +++ b/SecondBotEvents/Services/BotClientService.cs @@ -4,6 +4,7 @@ using SecondBotEvents.Config; using System; using System.Collections.Generic; using System.Text; +using System.Timers; namespace SecondBotEvents.Services { @@ -14,12 +15,18 @@ namespace SecondBotEvents.Services protected bool ExitBot = false; - + protected Timer AutoRestartLoginTimer; public BotClientService(EventsSecondBot setMaster) : base(setMaster) { basicCfg = new BasicConfig(master.fromEnv, master.fromFolder); } + protected void RestartTimer(object o, ElapsedEventArgs e) + { + AutoRestartLoginTimer.Stop(); + Restart(); + } + public bool IsLoaded() { return basicCfg.IsLoaded(); @@ -28,10 +35,15 @@ namespace SecondBotEvents.Services public override void Start() { Console.WriteLine("Client service [Starting]"); + AutoRestartLoginTimer = new Timer(); + AutoRestartLoginTimer.Interval = 30 * 1000; + AutoRestartLoginTimer.AutoReset = false; + AutoRestartLoginTimer.Elapsed += RestartTimer; Login(); } public override void Stop() { + AutoRestartLoginTimer.Stop(); Console.WriteLine("Client service [Stopping]"); ResetClient(); } @@ -43,6 +55,7 @@ namespace SecondBotEvents.Services protected void BotSimConnected(object o, SimConnectedEventArgs e) { + AutoRestartLoginTimer.Stop(); Console.WriteLine("Client service ~ Connected to sim: "+e.Simulator.Name); } @@ -163,6 +176,7 @@ namespace SecondBotEvents.Services { loginParams.URI = basicCfg.GetLoginURI(); } + AutoRestartLoginTimer.Start(); client.Network.BeginLogin(loginParams); }