public ActionResult AddAllDeviceAttrToCollectList(long id /*physical device id*/) { try { // the method adds all atrributes of all logical devices for given physical device to collect list _deviceLogicalDeviceRepository.AddAllDeviceAttrToCollectList(id, GetUserId()); return(RedirectToAction("Index/" + id)); } catch (Exception e) { ExceptionMessageToViewBag(e); return(View("Error")); } }
public ActionResult AddMetersForIPs(ViewGroup viewgroup) { try { if (viewgroup.DeviceNamePrefix == null) { AddForIPsResult = AddForIPsResults.DeviceNamePrefixError; return(AddMetersForIPs()); } if (string.IsNullOrEmpty(viewgroup.Name)) { AddForIPsResult = AddForIPsResults.NameError; return(AddMetersForIPs()); } UInt32 startingIp1 = 0, startingIp2 = 0, startingIp3 = 0, startingIp4 = 0, startingIpPort = 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))) { AddForIPsResult = AddForIPsResults.StartingIpError; return(AddMetersForIPs()); } if (!IsValidIpClass(startingIp1, startingIp2, startingIp3, startingIp4)) { AddForIPsResult = AddForIPsResults.StartingIpError; return(AddMetersForIPs()); } if (String.IsNullOrWhiteSpace(viewgroup.StartingIPPort) || (!UInt32.TryParse(viewgroup.StartingIPPort, out startingIpPort)) || !(startingIpPort <= 65535)) { AddForIPsResult = AddForIPsResults.StartingIpPortError; return(AddMetersForIPs()); } } UInt32 endingIp1 = 0, endingIp2 = 0, endingIp3 = 0, endingIp4 = 0, endingIpPort = 0; if (String.IsNullOrWhiteSpace(viewgroup.EndingIP1) && String.IsNullOrWhiteSpace(viewgroup.EndingIP2) && String.IsNullOrWhiteSpace(viewgroup.EndingIP3) && String.IsNullOrWhiteSpace(viewgroup.EndingIP4)) { } else { if (String.IsNullOrWhiteSpace(viewgroup.EndingIP1) || (!UInt32.TryParse(viewgroup.EndingIP1, out endingIp1)) || String.IsNullOrWhiteSpace(viewgroup.EndingIP2) || (!UInt32.TryParse(viewgroup.EndingIP2, out endingIp2)) || String.IsNullOrWhiteSpace(viewgroup.EndingIP3) || (!UInt32.TryParse(viewgroup.EndingIP3, out endingIp3)) || String.IsNullOrWhiteSpace(viewgroup.EndingIP4) || (!UInt32.TryParse(viewgroup.EndingIP4, out endingIp4))) { AddForIPsResult = AddForIPsResults.EndingIpError; return(AddMetersForIPs()); } if (!IsValidIpClass(endingIp1, endingIp2, endingIp3, endingIp4)) { AddForIPsResult = AddForIPsResults.EndingIpError; return(AddMetersForIPs()); } if (String.IsNullOrWhiteSpace(viewgroup.EndingIPPort) || (!UInt32.TryParse(viewgroup.EndingIPPort, out endingIpPort)) || !(endingIpPort <= 65535)) { AddForIPsResult = AddForIPsResults.EndingIpPortError; return(AddMetersForIPs()); } } UInt32 secondaryStartingIp1 = 0, secondaryStartingIp2 = 0, secondaryStartingIp3 = 0, secondaryStartingIp4 = 0, secondaryStartingIpPort = 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))) { AddForIPsResult = AddForIPsResults.SecondaryStartingIpError; return(AddMetersForIPs()); } if (!IsValidIpClass(secondaryStartingIp1, secondaryStartingIp2, secondaryStartingIp3, secondaryStartingIp4)) { AddForIPsResult = AddForIPsResults.SecondaryStartingIpError; return(AddMetersForIPs()); } if (String.IsNullOrWhiteSpace(viewgroup.SecondaryStartingIPPort) || (!UInt32.TryParse(viewgroup.SecondaryStartingIPPort, out secondaryStartingIpPort)) || !(secondaryStartingIpPort <= 65535)) { AddForIPsResult = AddForIPsResults.SecondaryStartingPortError; return(AddMetersForIPs()); } } if (ModelState.IsValid) { UInt32 netShift = 0, netSecShift = 0; if (!startingIpUndefined && GetIPClass(startingIp1) == IpClass.A) { if (GetIPClass(endingIp1) != IpClass.A) { AddForIPsResult = AddForIPsResults.EndingIpError; return(AddMetersForIPs()); } netShift = 24; // class-A } else if (!startingIpUndefined && GetIPClass(startingIp1) == IpClass.B) { if (GetIPClass(endingIp1) != IpClass.B) { AddForIPsResult = AddForIPsResults.EndingIpError; return(AddMetersForIPs()); } netShift = 16; // class-B } else if (!startingIpUndefined && GetIPClass(startingIp1) == IpClass.C) { if (GetIPClass(endingIp1) != IpClass.C) { AddForIPsResult = AddForIPsResults.EndingIpError; return(AddMetersForIPs()); } 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 endingIpAddr = endingIp1 << 24 | endingIp2 << 16 | endingIp3 << 8 | endingIp4; 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; UInt32 sIp, eIp, sPort, ePort; if (startingIpAddr <= endingIpAddr) { sIp = startingIpAddr; eIp = endingIpAddr; } else { sIp = endingIpAddr; eIp = startingIpAddr; } if (startingIpPort <= endingIpPort) { sPort = startingIpPort; ePort = endingIpPort; } else { ePort = startingIpPort; sPort = endingIpPort; } UInt32 netPart = (sIp >> (Int32)netShift); UInt32 ipPart = sIp & ipMask; uint sSecIp = secondaryStartingIpAddr; uint sSecPort = secondaryStartingIpPort; 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 }; if (0 != viewgroup.DeviceNamePrefix.Trim().Length) { td.Name = viewgroup.DeviceNamePrefix.Trim() + (serNum).ToString(CultureInfo.InvariantCulture); } long?deviceId = _deviceRepository.AddDevice(td, userId); _deviceLogicalDeviceRepository.AddAllDeviceAttrToCollectList((long)deviceId, userId); } else { for (UInt32 ipAddr = sIp, ipSecAddr = sSecIp; ipAddr <= eIp;) { for (UInt32 ipPort = sPort, ipSecPort = sSecPort; ipPort <= ePort;) { 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 = ipPort.ToString(CultureInfo.InvariantCulture); 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 = ipSecPort.ToString(CultureInfo.InvariantCulture); } _deviceRepository.AddDevice(td, userId); ipPort++; ipSecPort++; if (ipPort > ePort) { ipPart++; if (ipPart == ipMask) { ipPart = 1; netPart++; } ipAddr = (netPart << (Int32)netShift) | ipPart; ipSecPart++; if (ipSecPart == ipSecMask) { ipSecPart = 1; netSecPart++; } ipSecAddr = (netSecPart << (Int32)netSecShift) | ipSecPart; break; } } } } transaction.Complete(); } AddForIPsResult = AddForIPsResults.MetersAddedSuccessfully; return(RedirectToAction("Index")); } AddForIPsResult = AddForIPsResults.ModelStateIsNotValid; return(AddMetersForIPs()); } catch (Exception e) { ExceptionMessageToViewBag(e); AddForIPsResult = AddForIPsResults.ExceptionRaised; return(View("Error")); } }