Exemplo n.º 1
0
        /// <summary>
        /// The entry point of the program, where the program control starts and ends.
        ///
        /// This program allows the user to add devices into the database. First the
        /// device type is asked, then the particular properties for the given type.
        /// Finally the user is asked if they want to add another device.
        /// </summary>
        public static void Main()
        {
            dbAccess = new DevicePublisher();

            Console.WriteLine("Welcome to the device creator!");
            Console.WriteLine("------------------------------");

            bool doItAgain;

            do
            {
                bool error;
                int  type = 0;
                do
                {
                    error = false;
                    Console.Write("Device type (1: water meter, 2: elec. meter, 3: gateway): ");
                    try
                    {
                        type = int.Parse(Console.ReadLine());
                        if (type < 1 || type > 3)
                        {
                            error = true;
                        }
                    }
                    catch
                    {
                        error = true;
                    }
                    if (error)
                    {
                        Console.WriteLine("Invalid type, try again.");
                    }
                } while (error);

                device = type == 1 ? new WaterMeter() : type == 2 ? new ElectricityMeter() : (IDevice) new Gateway();

                readValue(device, "ID", "ID", true);
                readValue(device, "Serial number", "SerialNumber", true);
                readValue(device, "Firmware version", "FirmwareVersion", false);
                readValue(device, "State", "State", false);
                if (type == 3)
                {
                    readValue(device, "IP", "IP", false);
                    readValue(device, "Port", "Port", false);
                }

                saveData(device);

                Console.WriteLine("\nSave another device (Y/n)? ");
                var oneMore = Console.ReadKey().Key;
                Console.WriteLine();
                doItAgain = (oneMore == ConsoleKey.Enter || oneMore == ConsoleKey.Y);
            } while (doItAgain);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="T:DeviceCreator.Controllers.DeviceController"/> class.
 /// </summary>
 /// <param name="dbAccess">The instance of the interface for accessing the DB.</param>
 /// <param name="deviceFactory">The instance of the device factory.</param>
 public DeviceController(IDevicePublisher dbAccess, IDeviceFactory deviceFactory)
 {
     this.dbAccess      = dbAccess;
     this.deviceFactory = deviceFactory;
 }