예제 #1
0
        public ActionResult AddMetersForBuildings(ViewGroup viewgroup)
        {
            try
            {
                try
                {
                    // parameters checking
                    if (viewgroup.DeviceNamePrefix == null)
                    {
                        AddForBuildingsResult = AddForBuildingsResults.DeviceNamePrefixError;
                        return(AddMetersForBuildings());
                    }
                    if (string.IsNullOrEmpty(viewgroup.Name))
                    {
                        AddForBuildingsResult = AddForBuildingsResults.NameError;
                        return(AddMetersForBuildings());
                    }

                    UInt32  startingIp1 = 0, startingIp2 = 0, startingIp3 = 0, startingIp4 = 0;
                    Boolean startingIpUndefined = false;

                    if (String.IsNullOrWhiteSpace(viewgroup.StartingIP1) &&
                        String.IsNullOrWhiteSpace(viewgroup.StartingIP2) &&
                        String.IsNullOrWhiteSpace(viewgroup.StartingIP3) &&
                        String.IsNullOrWhiteSpace(viewgroup.StartingIP4))
                    {
                        startingIpUndefined = true;
                    }
                    else
                    {
                        if (String.IsNullOrWhiteSpace(viewgroup.StartingIP1) ||
                            (!UInt32.TryParse(viewgroup.StartingIP1, out startingIp1)) ||
                            String.IsNullOrWhiteSpace(viewgroup.StartingIP2) ||
                            (!UInt32.TryParse(viewgroup.StartingIP2, out startingIp2)) ||
                            String.IsNullOrWhiteSpace(viewgroup.StartingIP3) ||
                            (!UInt32.TryParse(viewgroup.StartingIP3, out startingIp3)) ||
                            String.IsNullOrWhiteSpace(viewgroup.StartingIP4) ||
                            (!UInt32.TryParse(viewgroup.StartingIP4, out startingIp4)))
                        {
                            AddForBuildingsResult = AddForBuildingsResults.StartingIpError;
                            return(AddMetersForBuildings());
                        }
                        if (!IsValidIpClass(startingIp1, startingIp2, startingIp3, startingIp4))
                        {
                            AddForBuildingsResult = AddForBuildingsResults.StartingIpError;
                            return(AddMetersForBuildings());
                        }
                        UInt32 startingIpPort;
                        if (String.IsNullOrWhiteSpace(viewgroup.StartingIPPort) ||
                            (!UInt32.TryParse(viewgroup.StartingIPPort, out startingIpPort)) ||
                            !(startingIpPort <= 65535))
                        {
                            AddForBuildingsResult = AddForBuildingsResults.StartingIpPortError;
                            return(AddMetersForBuildings());
                        }
                    }

                    UInt32 secondaryStartingIp1          = 0,
                           secondaryStartingIp2          = 0,
                           secondaryStartingIp3          = 0,
                           secondaryStartingIp4          = 0;
                    Boolean secondaryStartingIpUndefined = false;

                    if (String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP1) &&
                        String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP2) &&
                        String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP3) &&
                        String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP4))
                    {
                        secondaryStartingIpUndefined = true;
                    }
                    else
                    {
                        if (String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP1) ||
                            (!UInt32.TryParse(viewgroup.SecondaryStartingIP1, out secondaryStartingIp1)) ||
                            String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP2) ||
                            (!UInt32.TryParse(viewgroup.SecondaryStartingIP2, out secondaryStartingIp2)) ||
                            String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP3) ||
                            (!UInt32.TryParse(viewgroup.SecondaryStartingIP3, out secondaryStartingIp3)) ||
                            String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIP4) ||
                            (!UInt32.TryParse(viewgroup.SecondaryStartingIP4, out secondaryStartingIp4)))
                        {
                            AddForBuildingsResult = AddForBuildingsResults.SecondaryStartingIpError;
                            return(AddMetersForBuildings());
                        }
                        if (!IsValidIpClass(secondaryStartingIp1, secondaryStartingIp2, secondaryStartingIp3, secondaryStartingIp4))
                        {
                            AddForBuildingsResult = AddForBuildingsResults.SecondaryStartingIpError;
                            return(AddMetersForBuildings());
                        }
                        UInt32 secondaryStartingIpPort;
                        if (String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIPPort) ||
                            (!UInt32.TryParse(viewgroup.SecondaryStartingIPPort, out secondaryStartingIpPort)) ||
                            !(secondaryStartingIpPort <= 65535))
                        {
                            AddForBuildingsResult = AddForBuildingsResults.SecondaryStartingPortError;
                            return(AddMetersForBuildings());
                        }
                    }

                    uint zipCode, startingBuilding, endingBuilding, startingApartament, endingApartament;
                    if (!UInt32.TryParse(viewgroup.ZipCode, out zipCode))
                    {
                        AddForBuildingsResult = AddForBuildingsResults.ZipCodeError;
                        return(AddMetersForBuildings());
                    }
                    if (!UInt32.TryParse(viewgroup.StartingBuilding, out startingBuilding))
                    {
                        AddForBuildingsResult = AddForBuildingsResults.StartingBuildingError;
                        return(AddMetersForBuildings());
                    }
                    if (!UInt32.TryParse(viewgroup.EndingBuilding, out endingBuilding))
                    {
                        AddForBuildingsResult = AddForBuildingsResults.EndingBuildingError;
                        return(AddMetersForBuildings());
                    }
                    if (!UInt32.TryParse(viewgroup.StartingApartment, out startingApartament))
                    {
                        AddForBuildingsResult = AddForBuildingsResults.StartingApartamentError;
                        return(AddMetersForBuildings());
                    }
                    if (!UInt32.TryParse(viewgroup.EndingApartment, out endingApartament))
                    {
                        AddForBuildingsResult = AddForBuildingsResults.EndingApartamentError;
                        return(AddMetersForBuildings());
                    }
                    if (viewgroup.Street == "")
                    {
                        AddForBuildingsResult = AddForBuildingsResults.StreetError;
                        return(AddMetersForBuildings());
                    }
                    if (viewgroup.City == "")
                    {
                        AddForBuildingsResult = AddForBuildingsResults.CityError;
                        return(AddMetersForBuildings());
                    }
                    if (viewgroup.State == "")
                    {
                        AddForBuildingsResult = AddForBuildingsResults.StateError;
                        return(AddMetersForBuildings());
                    }
                    if (viewgroup.Country == "")
                    {
                        AddForBuildingsResult = AddForBuildingsResults.CountryError;
                        return(AddMetersForBuildings());
                    }

                    //////////////////////////////////////////
                    //  devices creating
                    if (ModelState.IsValid)
                    {
                        UInt32 netShift = 0, netSecShift = 0;

                        if (!startingIpUndefined && GetIPClass(startingIp1) == IpClass.A)
                        {
                            netShift = 24; //  class-A
                        }
                        else if (!startingIpUndefined && GetIPClass(startingIp1) == IpClass.B)
                        {
                            netShift = 16; //   class-B
                        }
                        else if (!startingIpUndefined && GetIPClass(startingIp1) == IpClass.C)
                        {
                            netShift = 8; //    class-C
                        }

                        if (!secondaryStartingIpUndefined && GetIPClass(secondaryStartingIp1) == IpClass.A)
                        {
                            netSecShift = 24; //  class-A
                        }
                        else if (!secondaryStartingIpUndefined && GetIPClass(secondaryStartingIp1) == IpClass.B)
                        {
                            netSecShift = 16; //   class-B
                        }
                        else if (!secondaryStartingIpUndefined && GetIPClass(secondaryStartingIp1) == IpClass.C)
                        {
                            netSecShift = 8; //    class-C
                        }

                        UInt32 startingIpAddr          = startingIp1 << 24 | startingIp2 << 16 | startingIp3 << 8 | startingIp4;
                        UInt32 secondaryStartingIpAddr = secondaryStartingIp1 << 24 | secondaryStartingIp2 << 16 |
                                                         secondaryStartingIp3 << 8 | secondaryStartingIp4;
                        var ipMask    = (UInt32)((1 << (Int32)netShift) - 1);
                        var ipSecMask = (UInt32)((1 << (Int32)netSecShift) - 1);

                        Guid userId = GetUserId();
                        using (var transaction = new TransactionScope())
                        {
                            //  сначала нужно добавить группу, чтобы получить идентификатор
                            long? groupId = _groupRepository.AddGroup(viewgroup, userId);
                            Int32 serNum  = 1;
                            uint  sIp     = startingIpAddr;
                            uint  sSecIp  = secondaryStartingIpAddr;

                            UInt32 sBuilding, eBuilding, sApartament, eApartament;
                            if (startingBuilding <= endingBuilding)
                            {
                                sBuilding = startingBuilding;
                                eBuilding = endingBuilding;
                            }
                            else
                            {
                                sBuilding = endingBuilding;
                                eBuilding = startingBuilding;
                            }

                            if (startingApartament <= endingApartament)
                            {
                                sApartament = startingApartament;
                                eApartament = endingApartament;
                            }
                            else
                            {
                                sApartament = endingApartament;
                                eApartament = startingApartament;
                            }

                            UInt32 netPart = (sIp >> (Int32)netShift);
                            UInt32 ipPart  = sIp & ipMask;

                            UInt32 netSecPart = (sSecIp >> (Int32)netSecShift);
                            UInt32 ipSecPart  = sSecIp & ipSecMask;

                            if (startingIpUndefined)
                            {
                                var td = new ViewDevice
                                {
                                    InternalAddress = "1",
                                    AddressType     = "IP",
                                    GroupID         = groupId.Value,
                                    TypeID          = viewgroup.DeviceTypeID
                                };

                                UInt32 building   = sBuilding;
                                UInt32 apartament = sApartament;

                                if (0 != viewgroup.DeviceNamePrefix.Trim().Length)
                                {
                                    td.Name = viewgroup.DeviceNamePrefix.Trim() + (serNum).ToString(CultureInfo.InvariantCulture);
                                }

                                var a = new ViewAddress
                                {
                                    Apartament = apartament.ToString(CultureInfo.InvariantCulture),
                                    Building   = building.ToString(CultureInfo.InvariantCulture),
                                    Street     = viewgroup.Street,
                                    City       = viewgroup.City,
                                    State      = viewgroup.State,
                                    Country    = viewgroup.Country,
                                    ZipCode    = viewgroup.ZipCode
                                };

                                _deviceRepository.AddDeviceWithAddress(td, a, userId);
                            }
                            else
                            {
                                for (UInt32 building = sBuilding, ipAddr = sIp, ipSecAddr = sSecIp;
                                     building <= eBuilding;
                                     building++)
                                {
                                    for (UInt32 apartament = sApartament; apartament <= eApartament; apartament++)
                                    {
                                        var td = new ViewDevice
                                        {
                                            InternalAddress = "1",
                                            AddressType     = "IP",
                                            GroupID         = groupId.Value,
                                            TypeID          = viewgroup.DeviceTypeID
                                        };

                                        if (0 != viewgroup.DeviceNamePrefix.Trim().Length)
                                        {
                                            td.Name = viewgroup.DeviceNamePrefix.Trim() + (serNum++).ToString(CultureInfo.InvariantCulture);
                                        }

                                        td.IP1  = (ipAddr >> 24).ToString(CultureInfo.InvariantCulture);
                                        td.IP2  = ((ipAddr >> 16) & 0xff).ToString(CultureInfo.InvariantCulture);
                                        td.IP3  = ((ipAddr >> 8) & 0xff).ToString(CultureInfo.InvariantCulture);
                                        td.IP4  = (ipAddr & 0xff).ToString(CultureInfo.InvariantCulture);
                                        td.Port = viewgroup.StartingIPPort;

                                        if (!secondaryStartingIpUndefined)
                                        {
                                            td.SecondaryIP1  = (ipSecAddr >> 24).ToString(CultureInfo.InvariantCulture);
                                            td.SecondaryIP2  = ((ipSecAddr >> 16) & 0xff).ToString(CultureInfo.InvariantCulture);
                                            td.SecondaryIP3  = ((ipSecAddr >> 8) & 0xff).ToString(CultureInfo.InvariantCulture);
                                            td.SecondaryIP4  = (ipSecAddr & 0xff).ToString(CultureInfo.InvariantCulture);
                                            td.SecondaryPort = viewgroup.SecondaryStartingIPPort;
                                        }

                                        var a = new ViewAddress
                                        {
                                            Apartament = apartament.ToString(CultureInfo.InvariantCulture),
                                            Building   = building.ToString(CultureInfo.InvariantCulture),
                                            Street     = viewgroup.Street,
                                            City       = viewgroup.City,
                                            State      = viewgroup.State,
                                            Country    = viewgroup.Country,
                                            ZipCode    = viewgroup.ZipCode
                                        };

                                        _deviceRepository.AddDeviceWithAddress(td, a, userId);

                                        ipPart++;
                                        if (ipPart == ipMask)
                                        {
                                            ipPart = 1;
                                            netPart++;
                                        }
                                        ipAddr = (netPart << (Int32)netShift) | ipPart;

                                        ipSecPart++;
                                        if (ipSecPart == ipSecMask)
                                        {
                                            ipSecPart = 1;
                                            netSecPart++;
                                        }
                                        ipSecAddr = (netSecPart << (Int32)netSecShift) | ipSecPart;
                                    }
                                }
                            }
                            transaction.Complete();
                        }
                        AddForBuildingsResult = AddForBuildingsResults.MetersAddedSuccessfully;
                        return(RedirectToAction("Index"));
                    }

                    AddForBuildingsResult = AddForBuildingsResults.ModelStateIsNotValid;
                    return(AddMetersForBuildings());
                }
                catch (Exception e)
                {
                    ExceptionMessageToViewBag(e);
                    AddForBuildingsResult = AddForBuildingsResults.ExceptionRaised;
                    return(View("Error"));
                }
            }
            catch (Exception e)
            {
                ExceptionMessageToViewBag(e);
                return(View("Error"));
            }
        }