Пример #1
0
        public void LoadDB(int load_version_id, int version_number)
        {
            GetActualVersion();
            using (Network_SchemeEntities db = new Network_SchemeEntities(DBUtils.getConnString()))
            {
                List <Network_Hardware>    db_devices;
                List <Network_Settings>    db_settings;
                List <Network_Ports>       db_ports;
                List <Network_Connections> db_connections;
                try
                {
                    db_devices     = db.Network_Hardware.Where(b => b.C_Version == load_version_id).ToList <Network_Hardware>();
                    db_settings    = db.Network_Settings.Where(b => b.C_Version == load_version_id).ToList <Network_Settings>();
                    db_ports       = db.Network_Ports.Where(b => b.C_Version == load_version_id).ToList <Network_Ports>();
                    db_connections = db.Network_Connections.Where(b => b.C_Version == load_version_id).ToList <Network_Connections>();
                }
                catch
                {
                    MessageBox.Show("Ошибка загрузки актуальной версии");
                    return;
                }
                panel.Controls.Clear();
                devices.Clear();

                NetDevice.id    = 0;
                NetSettings.id  = 0;
                NetPort.id      = 0;
                NetDevice.pc_id = 0;
                NetDevice.pr_id = 0;
                NetDevice.sw_id = 0;
                NetDevice.ro_id = 0;

                int max_real_id = 0;
                int id_shift    = (load_version_id) * (-1000);

                foreach (Network_Hardware db_device in db_devices)
                {
                    int id = id_shift + db_device.ID;
                    if (id > max_real_id)
                    {
                        max_real_id = id;
                    }
                    NetDevice device = new NetDevice(this, db_device.C_Type, false);
                    device.x          = db_device.C_Interface_X;
                    device.y          = db_device.C_Interface_Y;
                    device.name       = db_device.C_Name;
                    device.title.Text = device.name;
                    device.id_real    = id;

                    devices.Add(device);
                }
                NetDevice.id = max_real_id + 1;

                max_real_id = 0;
                foreach (Network_Ports db_port in db_ports)
                {
                    int id = id_shift + db_port.ID;
                    if (id > max_real_id)
                    {
                        max_real_id = id;
                    }
                    NetPort port = new NetPort(null);
                    port.id_real     = id;
                    port.enabled     = db_port.C_Status;
                    port.settings_id = id_shift + db_port.C_Settings_ID;

                    // set device
                    int device_id = id_shift + db_port.C_Hardware_ID;
                    foreach (NetDevice device in devices)
                    {
                        if (device.id_real == device_id)
                        {
                            port.device = device;
                            for (int i = 0; i < device.ports.Count; i++)
                            {
                                if (device.ports[i] == null)
                                {
                                    device.ports[i] = port;
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                NetPort.id = max_real_id + 1;

                max_real_id = 0;
                foreach (Network_Settings db_setting in db_settings)
                {
                    int id = id_shift + db_setting.ID;
                    if (id > max_real_id)
                    {
                        max_real_id = id;
                    }
                    NetSettings setting = new NetSettings();
                    setting.id_real         = id;
                    setting.IP              = db_setting.C_IP;
                    setting.default_gateway = db_setting.C_Default_Gateway;
                    setting.subnet_mask     = db_setting.C_Subnet_Mask;

                    // set device
                    bool stop = false;
                    foreach (NetDevice device in devices)
                    {
                        if (stop)
                        {
                            break;
                        }
                        foreach (NetPort port in device.ports)
                        {
                            if (setting.id_real == port.settings_id)
                            {
                                port.settings = setting;
                                stop          = true;
                                break;
                            }
                        }
                    }
                }
                NetSettings.id = max_real_id + 1;

                foreach (Network_Connections db_connection in db_connections)
                {
                    int port_start_id = id_shift + db_connection.C_From_Port_ID;
                    int port_end_id   = id_shift + db_connection.C_To_Port_ID;

                    NetPort start_port = null;
                    NetPort end_port   = null;

                    bool stop = false;
                    foreach (NetDevice device in devices)
                    {
                        if (stop)
                        {
                            break;
                        }
                        foreach (NetPort port in device.ports)
                        {
                            if (port.id_real == port_start_id)
                            {
                                start_port = port;
                                stop       = true;
                                break;
                            }
                        }
                    }

                    stop = false;
                    foreach (NetDevice device in devices)
                    {
                        if (stop)
                        {
                            break;
                        }
                        foreach (NetPort port in device.ports)
                        {
                            if (port.id_real == port_end_id)
                            {
                                end_port = port;
                                stop     = true;
                                break;
                            }
                        }
                    }

                    start_port.connected_port = end_port;
                    end_port.connected_port   = start_port;
                }
            }
            RefreshDraw();
            panel.Refresh();
            MessageBox.Show("Успешно загружена версия : " + version_number);
        }