예제 #1
0
        public async Task <ActionResult <DeviceViewModel> > CreateDevice(DeviceViewModel deviceViewModel)
        {
            try
            {
                if (deviceViewModel == null)
                {
                    return(BadRequest());
                }

                // Add custom model validation error
                Device dep = await deviceRepository.GetDeviceByname(deviceViewModel.Device);

                if (dep != null)
                {
                    ModelState.AddModelError("Name", $"Device name: {deviceViewModel.Device.Name} already in use");
                    return(BadRequest(ModelState));
                }

                deviceViewModel = await deviceRepository.AddDevice(deviceViewModel);

                return(CreatedAtAction(nameof(GetDevice),
                                       new { id = deviceViewModel.Device.Id }, deviceViewModel));
            }
            catch (DbUpdateException Ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  Ex.InnerException.Message));
            }
        }
        public async Task <string> AddNewDevice(DeviceDTO device)
        {
            device.DeviceTagName = UniqueIdGenerator.GetUniqueId();
            await _devicerepo.AddDevice(device);

            return(device.DeviceTagName);
        }
예제 #3
0
 public async Task <IActionResult> Create(Device model)
 {
     if (ModelState.IsValid)
     {
         await deviceRepo.AddDevice(model);
     }
     return(RedirectToAction("Index"));
 }
        public ActionResult <DevicesDto> CreateDeviceForTerminal(Guid terminalId, DevicesForCreationDto device)
        {
            var deviceEntity = _mapper.Map <Entities.Device>(device);

            deviceEntity.TerminalId = terminalId;
            //deviceEntity.Id = Guid.NewGuid();

            _deviceRepository.AddDevice(deviceEntity);
            _deviceRepository.Save();

            var deviceToReturn = _mapper.Map <DevicesDto>(deviceEntity);

            var links = CreateLinksForDevice(terminalId.ToString(), deviceToReturn.Id, null);

            var linkedResourceToReturn = deviceToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetDevices",
                                  new { terminalId, deviceId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
예제 #5
0
        public async Task <IActionResult> Device()
        {
            var newDevice = new Device
            {
                Name         = "Device 1",
                Description  = "ABC",
                LastMaintain = DateTime.Now,
            };

            deviceRepository1.AddDevice(newDevice);
            await unitOfWork1.SaveAsync();

            return(Ok());
        }
        public async Task <string> AddNewDevice(DeviceDTO[] devices)
        {
            string deviceTagName = string.Empty;

            for (int i = 0; i < devices.Length; i++)
            {
                devices[i].DeviceTagName = UniqueIdGenerator.GetUniqueId();
                deviceTagName            = deviceTagName + devices[i].DeviceTagName + ",";
            }
            deviceTagName = deviceTagName.TrimEnd(',');
            await _devicerepo.AddDevice(devices);

            return(deviceTagName);
        }
예제 #7
0
        public IActionResult CreateDevice([FromBody] DeviceForCreationDto device)
        {
            try
            {
                // Check the input has been passed in
                if (device == null)
                {
                    return(BadRequest());
                }

                // Validate using the fluid validation rules
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                if (_deviceRepository.DeviceExists(device.DeviceId))
                {
                    return(StatusCode(409, "Device already registered with server"));
                }

                // Map the dto to a domain entity
                var finalDevice = _mapper.Map <Device>(device);

                // Create the school record in the repository
                _deviceRepository.AddDevice(finalDevice);


                if (!_deviceRepository.Save())
                {
                    return(StatusCode(500, "A problem happened while handling your request"));
                }

                var createdDeviceToReturn = _mapper.Map <DeviceDto>(finalDevice);

                return(CreatedAtRoute("GetDevice", new { id = createdDeviceToReturn.Id },
                                      createdDeviceToReturn));
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Exception while creating device.", ex);
                return(StatusCode(500, "A problem happened while handling your request"));
            }
        }
예제 #8
0
        public DeviceMutation(IDeviceRepository deviceRepository)
        {
            Name = "CRUD_Device";

            Description = "This API provides methods to create,edit,update and delete devices";

            Field <DeviceType>(
                "AddDevice",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <CreateDeviceType> > {
                Name = "CreateDeviceType"
            }
                    ),
                resolve: context =>
            {
                var device      = context.GetArgument <Device>("CreateDeviceType");
                device.Category = Category.Internal;
                return(deviceRepository.AddDevice(device));
            });
        }
        public async Task <ActionResult <Device> > AddDevice([FromBody] Device newDevice)
        {
            try
            {
                var result = await _deviceRepository.AddDevice(newDevice);

                if (result != null)
                {
                    return(Ok(result));
                }

                _logger.Log(LogLevel.Warning, $"Couldn't add device {newDevice.DeviceName}");
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, $"Error while call AddDevice - {ex}");
                return(BadRequest(ex.Message));
            }
        }
예제 #10
0
        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"));
            }
        }
 public void AddDevice([FromBody] Device inst)
 {
     idivrep.AddDevice(inst);
 }
예제 #12
0
 public void AddDevice(Device nd)
 {
     repoDevice.AddDevice(nd);
 }