using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TelegramBotBase.Base; using TelegramBotBase.Form; using TelegramBotBase.Sessions; namespace TelegramBotBase { /// /// Base class for managing all active sessions /// public class SessionBase { public MessageClient Client { get; set; } public Dictionary SessionList { get; set; } public SessionBase() { this.SessionList = new Dictionary(); } /// /// Get device session from Device/ChatId /// /// /// public DeviceSession this[long key] { get { return this.SessionList[key]; } set { this.SessionList[key] = value; } } /// /// Get device session from Device/ChatId /// /// /// public DeviceSession GetSession(long deviceId) { DeviceSession ds = this.SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null; return ds; } /// /// Start a new session /// /// /// /// public async Task StartSession(long deviceId) where T : FormBase { T start = typeof(T).GetConstructor(new Type[] { }).Invoke(new object[] { }) as T; start.Client = this.Client; DeviceSession ds = new Sessions.DeviceSession(deviceId, start); start.Device = ds; await start.Init(); await start.Opened(); this[deviceId] = ds; return ds; } /// /// End session /// /// public void EndSession(long deviceId) { var d = this[deviceId]; if (d != null) { this.SessionList.Remove(deviceId); } } } }