コード例 #1
0
        private ConnectionBase CreateConnection(FloorSet allFloorSet, SettingsBase connectionSetting)
        {
            // Parser the floor info from the settings
            FloorSet newConnectionFloorSet;

            string[] floorInfo;
            if (connectionSetting["FloorSet"].ToString().Contains("L"))
            {
                // Use old parser method
                floorInfo = ((string)connectionSetting["FloorSet"]).Split(new char[] { 'F', 'L' });

                // First Detect the FloorNumber has exist and the value is correct
                for (int i = 0, FloorNumber, FloorLength; i <= (floorInfo.Length / 2) - 1; i++)
                {
                    FloorNumber = Convert.ToInt32(floorInfo[i * 2 + 1]);
                    FloorLength = Convert.ToInt32(floorInfo[i * 2 + 2]);
                    if ((allFloorSet[FloorNumber] != null) || (FloorLength <= 0) || (FloorLength >= 100))
                    {
                        throw new ApplicationException("Settings中的楼层值有错误!");
                    }
                }

                // Start to create FloorSet
                newConnectionFloorSet = new FloorSet();
                for (int i = 0; i <= (floorInfo.Length / 2) - 1; i++)
                {
                    Floor floor = new Floor(Convert.ToInt32(floorInfo[i * 2 + 1]), Convert.ToInt32(floorInfo[i * 2 + 2]));
                    newConnectionFloorSet.Add(floor);
                    allFloorSet.Add(floor);
                }
            }
            else
            {
                // Use new parser method
                floorInfo = connectionSetting["FloorSet"].ToString().Split(new char[] { 'F' }, StringSplitOptions.RemoveEmptyEntries);

                // Parser each floor's room information
                Dictionary <int, int[]> roomNumberInfo = new Dictionary <int, int[]>();
                for (int floorIndex = 0; floorIndex < floorInfo.Length; floorIndex++)
                {
                    string[] roomInfo = floorInfo[floorIndex].Split('R');

                    // Obtain floor number
                    int floorNumber = int.Parse(roomInfo[0]);
                    if (allFloorSet[floorNumber] != null)
                    {
                        Trace.WriteLine(string.Format(
                                            "Settings:{0} contains duplicated floor {1}",
                                            connectionSetting["Name"],
                                            floorNumber),
                                        "Connection");
                        throw new ApplicationException("Settings中的楼层值有错误!");
                    }

                    // Obtain room numbers
                    List <int> rooms = new List <int>();
                    for (int roomIndex = 1; roomIndex < roomInfo.Length; roomIndex++)
                    {
                        int newRoomNumber = int.Parse(roomInfo[roomIndex]);
                        if ((newRoomNumber <= 0) || (newRoomNumber >= 100) || rooms.Contains(newRoomNumber))
                        {
                            Trace.WriteLine(string.Format(
                                                "Settings:{0} contains illegal room {1}",
                                                connectionSetting["Name"],
                                                floorNumber),
                                            "Connection");
                            throw new ApplicationException("Settings中的楼层值有错误!");
                        }

                        rooms.Add(newRoomNumber);
                    }

                    roomNumberInfo.Add(floorNumber, rooms.ToArray());
                }

                // Create new floor set for the new connection
                newConnectionFloorSet = new FloorSet();
                foreach (KeyValuePair <int, int[]> floorRoomInfo in roomNumberInfo)
                {
                    Floor floor = new Floor(floorRoomInfo.Key, floorRoomInfo.Value);
                    newConnectionFloorSet.Add(floor);
                    allFloorSet.Add(floor);
                }
            }



            // Create new Connection
            ConnectionBase newConnection         = null;
            string         newConnectionTypeName = connectionSetting["Type"].ToString();

            if (_ConnectionTypes[newConnectionTypeName] != null)
            {
                newConnection = ((ConnectionType)_ConnectionTypes[newConnectionTypeName]).CreateConnection(newConnectionFloorSet, connectionSetting);
            }
            else
            {
                throw new Exception("Settings中含有未知的协议");
            }
            return(newConnection);
        }