Esempio n. 1
0
        static void Main(string[] args)
        {
            var root = new RootEquipment();

            root.Initialize();

            root.RandomActions(1);

            GetAllEquipment(root);

            Console.WriteLine("You're in user edit mode. Use command 'help' to get more useful info");

            bool showMenu = true;

            while (showMenu)
            {
                try
                {
                    showMenu = MainMenu(root);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Esempio n. 2
0
 private static void GetAllEquipment(RootEquipment root)
 {
     foreach (var group in root.Groups)
     {
         if (group != null)
         {
             GetGroupInfo(group);
         }
     }
 }
Esempio n. 3
0
        private static GroupEquipment ChooseGroup(RootEquipment root)
        {
            Console.WriteLine($"Groups: ");
            foreach (var group in root.Groups)
            {
                Console.WriteLine($"\t{group.Name}");
            }

            while (true)
            {
                Console.Write("Choose group name:");

                var groupName = Console.ReadLine();
                var group     = root.GetGroupByName(groupName);
                if (group != null)
                {
                    return(group);
                }
                else
                {
                    Console.WriteLine("Group not found");
                }
            }
        }
Esempio n. 4
0
        private static bool MainMenu(RootEquipment root)
        {
            string inputString = Console.ReadLine();

            string[] splittedString = inputString.Split(' ');

            var command    = splittedString[0].Trim();
            var parameters = splittedString.Skip(1).ToArray();

            //TODO: Баг. Убрать этот метод, т.к. при добавлении новой группы, его имя с маленькой буквы...
            LowerParameters(parameters);

            //TODO: Обработать отсутствие параметров
            switch (command)
            {
            case "help":
                Console.WriteLine(GetInstrustion());
                return(true);

            case "show":
                if (parameters.Length == 1)
                {
                    //show all
                    if (parameters[0] == "all")
                    {
                        GetAllEquipment(root);
                    }
                    else if (parameters[0] == "group")
                    {
                        var group = ChooseGroup(root);
                        GetGroupInfo(group);
                    }
                }
                else if (parameters.Length == 2)
                {
                    if (parameters[0] == "device")
                    {
                        //show device <id>
                        var equipmentId = parameters[1];
                        var equipment   = root.GetEquipmentById(equipmentId);

                        Console.WriteLine(equipment.GetCurrentState());
                    }
                    else
                    {
                        throw new Exception($"For 'show' second parameter expected: 'group' or 'device'");
                    }
                }
                else if (parameters.Length > 2)
                {
                    Console.WriteLine(ShowFormatError());
                }
                else
                {
                    GetAllEquipment(root);
                }
                return(true);

            case "add":
                if (parameters.Length >= 1)
                {
                    if (parameters[0] == "group")
                    {
                        if (parameters.Length > 1)
                        {
                            //add group [groupName]
                            var newGroupName = parameters[1];
                            if (!string.IsNullOrEmpty(newGroupName))
                            {
                                var newGroup = new GroupEquipment(newGroupName);
                                root.AddGroup(newGroup);
                                GetAllEquipment(root);
                            }
                            else
                            {
                                throw new Exception($"Cant find parameter 'group name'");
                            }
                        }
                        else
                        {
                            throw new Exception($"Cant find parameter 'group name'");
                        }
                    }
                    else if (parameters[0] == "device")
                    {
                        //add device
                        Equipment      newDevice = ChooseType();
                        GroupEquipment group     = ChooseGroup(root);

                        group.AddEquipment(newDevice);

                        GetAllEquipment(root);
                    }
                    else
                    {
                        throw new Exception($"Expected first command parameter 'device' or 'group'.");
                    }
                }
                else
                {
                    throw new Exception($"Expected one command parameter. You can add 'group' or 'device'.");
                }
                return(true);

            case "edit":
                if (parameters.Length == 2)
                {
                    if (parameters[0] == "device")
                    {
                        //edit device [deviceId]
                        var equipmentId = parameters[1];
                        var equipment   = root.GetEquipmentById(equipmentId);

                        EditDeviceProperty(equipment);
                    }
                    else
                    {
                        throw new Exception($"Expected two command parameter 'device' and '<deviceId>'.");
                    }
                }
                else
                {
                    throw new Exception($"Expected two command parameter 'device' and '<deviceId>.");
                }
                return(true);

            case "delete":
                if (parameters[0] == "group")
                {
                    //delete group
                    GroupEquipment group = ChooseGroup(root);
                    root.Groups.Remove(group);
                    GetAllEquipment(root);
                }
                else if (parameters[0] == "device")
                {
                    //delete device [deviceId]
                    if (parameters.Length == 2)
                    {
                        var equipmentId = parameters[1];
                        var equipment   = root.GetEquipmentById(equipmentId);

                        root.RemoveEquipment(equipment);

                        GetAllEquipment(root);
                    }
                    else
                    {
                        throw new Exception($"Expected two command parameter 'groupNumber' and 'equipmentNumber'.");
                    }
                }
                return(true);

            case "cls":
                Console.Clear();
                return(true);

            case "exit":
                return(false);

            default:
                return(true);
            }
        }