Пример #1
0
        /// <summary>
        /// Get all usb keys in the usbs table
        /// </summary>
        /// <returns></returns>
        private List <UsbKey> GetUsbKeys()
        {
            List <UsbKey> usbKeys = new List <UsbKey>();

            try
            {
                MySqlCommand command = this.connection.CreateCommand(); // init a new request to DB

                command.CommandText = "SELECT * FROM usbs";             //request

                MySqlDataReader reader = command.ExecuteReader();       // get all usb in usbs table

                while (reader.Read())
                {
                    UsbKey usbKey = new UsbKey(reader.GetString("name"), reader.GetString("uuid"), reader.GetUInt64("freeSpaceInBytes"), reader.GetString("created_at"), reader.GetInt32("port_number"), reader.GetInt32("rack_number"));

                    usbKeys.Add(usbKey);
                }

                reader.Close();
            }
            catch (Exception exc)
            {
                Console.WriteLine("L'exception suivante est apparue : " + exc);
            }

            return(usbKeys);
        }
Пример #2
0
        /// <summary>
        /// Add a new usb Key if the key already exist update it
        /// </summary>
        /// <param name="newUsbKey"></param>
        public void AddUsbKey(UsbKey newUsbKey)
        {
            List <UsbKey> usbKeys     = new List <UsbKey>();
            bool          usbKeyExist = false;

            usbKeys = GetUsbKeys();

            foreach (UsbKey usbKey in usbKeys)
            {
                //if the key already exist update his rack and is port
                if (usbKey.Name == newUsbKey.Name)
                {
                    MySqlCommand command = this.connection.CreateCommand();

                    command.CommandText = "UPDATE usbs set rack_number = @rack_number, port_number = @port_number, freeSpaceInBytes = @freeSpaceInBytes WHERE name = @name";

                    command.Parameters.AddWithValue("@name", newUsbKey.Name);
                    command.Parameters.AddWithValue("@rack_number", newUsbKey.Rack_number);
                    command.Parameters.AddWithValue("@port_number", newUsbKey.Port_number);
                    command.Parameters.AddWithValue("@freeSpaceInBytes", newUsbKey.FreeSpaceInBytes);

                    command.ExecuteNonQuery();

                    usbKeyExist = true;
                }
            }

            //if the usb key dosen't exist we try to add it in the DB
            if (!usbKeyExist)
            {
                try
                {
                    MySqlCommand command = this.connection.CreateCommand(); //create a new request for DB

                    command.CommandText = "INSERT INTO usbs (name, uuid, freeSpaceInBytes, status_id, rack_number, port_number, created_at) VALUES (@name, @uuid, @freeSpaceInBytes, @status_id, @rack_number, @port_number, @created_at)";

                    command.Parameters.AddWithValue("@name", newUsbKey.Name);
                    command.Parameters.AddWithValue("@uuid", newUsbKey.Uuid);
                    command.Parameters.AddWithValue("@freeSpaceInBytes", newUsbKey.FreeSpaceInBytes);
                    command.Parameters.AddWithValue("@status_id", newUsbKey.Status_id);
                    command.Parameters.AddWithValue("@rack_number", newUsbKey.Rack_number);
                    command.Parameters.AddWithValue("@port_number", newUsbKey.Port_number);
                    command.Parameters.AddWithValue("@created_at", newUsbKey.Created_at);

                    command.ExecuteNonQuery();
                }
                catch (Exception exc)
                {
                    Console.WriteLine("L'exception suivante est apparue : " + exc);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Get the usb devices and add it to the list
        /// </summary>
        private void GetDevices()
        {
            List <string> devices = new List <string>();

            string[] usbLocations;
            int      i;
            int      usbPort;
            int      usbRack;

            while (true)
            {
                devices.Clear();
                i            = 0;
                usbLocations = GetUsbLocation();

                try
                {
                    foreach (DriveInfo drive in DriveInfo.GetDrives())
                    {
                        // if the device is an removable device (USB, external drive), add it to the list
                        if (drive.DriveType == DriveType.Removable)
                        {
                            usbPort = System.Convert.ToInt32(usbLocations[i].Substring(6, 4));
                            usbRack = System.Convert.ToInt32(usbLocations[i].Substring(16, 4));

                            devices.Add(string.Format("({0}) {1} - port : {2}, rack : {3}", drive.Name.Replace("\\", ""), drive.VolumeLabel, usbPort, usbRack));

                            UsbKey usbKey = new UsbKey(drive.VolumeLabel, "null", (UInt64)drive.AvailableFreeSpace, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), usbRack, usbPort);

                            db.AddUsbKey(usbKey); //try to add a usb in DB

                            i++;
                        }
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine("L'exception suivante est apparue : " + exc.Message);
                }


                try
                {
                    Invoke((GetResponse)DisplayResponse, devices);
                }
                catch (Exception exc)
                {
                    Console.WriteLine("L'exception suivante est apparue : " + exc.Message);
                }
            }
        }