public void LoadState(bool forcePickFolder = false)
        {
            LogDirectory = IOUtils.RetrieveLogDirectory(forcePickFolder, LogDirectory);
            LogWatcher?.Dispose();
            LogWatcher = new LogWatcher(LogDirectory);

            var allLogs = LogWatcher.RetrieveAllLogs();

            Commanders.Clear();

            var entryDatas =
                JsonConvert.DeserializeObject <List <EntryData> >(IOUtils.GetEntryDatasJson());

            foreach (var commander in allLogs.Keys)
            {
                // some file contains only one line unrelated to anything, could generate Dummy Commander if we don't skip
                if (allLogs[commander].Count <= 1)
                {
                    continue;
                }

                var commanderState = new CommanderViewModel(commander, allLogs[commander], Languages, entryDatas);
                Commanders[commander] = commanderState;
            }

            if (Commanders.Count == 0) // we found absolutely nothing
            {
                Commanders[LogWatcher.DEFAULT_COMMANDER_NAME] = new CommanderViewModel(LogWatcher.DEFAULT_COMMANDER_NAME, new List <string>(), Languages, entryDatas);
            }

            if (Commanders.Any(k => k.Key == SettingsManager.SelectedCommander))
            {
                CurrentCommander = Commanders.First(k => k.Key == SettingsManager.SelectedCommander);
            }
            else
            {
                CurrentCommander = Commanders.First();
            }

            LogWatcher.InitiateWatch(logs =>
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    if (logs.Item2.Count == 0)
                    {
                        return;
                    }

                    if (Commanders.ContainsKey(logs.Item1))
                    {
                        Commanders[logs.Item1].ApplyEventsToSate(logs.Item2);
                    }
                    else if (logs.Item1 != LogWatcher.DEFAULT_COMMANDER_NAME)
                    {
                        var commanderState     = new CommanderViewModel(logs.Item1, logs.Item2, Languages, entryDatas);
                        Commanders[logs.Item1] = commanderState;
                    }
                });
            });
        }
 public static EDCommander GetCommander(int id)      // null if not valid - cope with it. Hidden gets returned.
 {
     if (Commanders.ContainsKey(id))
     {
         return(Commanders[id]);
     }
     else
     {
         return(null);
     }
 }
        private DeviceCommander FindOrCreateCommander(Type type)
        {
            if (Commanders.TryGetValue(type, out DeviceCommander value))
            {
                return(value);
            }
            object          instance     = Activator.CreateInstance(type, device);
            DeviceCommander devCommander = (DeviceCommander)instance;

            devCommander.CmdStation = commandStation;
            Commanders.Add(type, devCommander);
            return(FindOrCreateCommander(type));
        }
        public static void Delete(EDCommander cmdr)
        {
            Commanders.Remove(cmdr.Id);

            UserDatabase.Instance.ExecuteWithDatabase(cn =>
            {
                using (DbCommand cmd = cn.Connection.CreateCommand("UPDATE Commanders SET Deleted = 1 WHERE Id = @Id"))
                {
                    cmd.AddParameterWithValue("@Id", cmdr.Id);
                    cmd.ExecuteNonQuery();
                }
            });
        }