Example #1
0
        public DeviceDto Create(CreateDeviceDto input)
        {
            var query = _deviceRepository.GetAllIncluding()
                        .Where(d => d.HardwareId == input.HardwareId || d.DeviceName == input.DeviceName);

            if ((query.Any()) && (query.FirstOrDefault().IsDeleted == true))
            {
                var entity = query.FirstOrDefault();
                entity.IsDeleted = false;
                var result_old = _deviceRepository.Update(entity);
                CurrentUnitOfWork.SaveChanges();
                return(ObjectMapper.Map <DeviceDto>(result_old));
            }
            if (query.Any())
            {
                throw new ApplicationException("设备已存在");
            }

            var workshopQuery = _workshopRepository.GetAllIncluding().Where(w => w.WorkshopName == input.WorkshopName)
                                .Where(w => w.Factory.FactoryName == input.FactoryName)
                                .Where(w => w.Factory.City.CityName == input.CityName);
            var workshop = workshopQuery.FirstOrDefault();

            if (workshop == null)
            {
                throw new ApplicationException("Workshop不存在");
            }

            var gatewayQuery = _gatewayRepository.GetAllIncluding().Where(g => g.Workshop.WorkshopName == input.WorkshopName)
                               .Where(g => g.Workshop.Factory.FactoryName == input.FactoryName)
                               .Where(g => g.Workshop.Factory.City.CityName == input.CityName)
                               .Where(g => g.HardwareId == input.HardwareId || g.GatewayName == input.GatewayName);
            var gateway = gatewayQuery.FirstOrDefault();

            if (gateway == null)
            {
                throw new ApplicationException("网关不存在");
            }

            var deviceTypeQuery = _deviceTypeRepository.GetAll().Where(dt => dt.TypeName == input.DeviceTypeName);
            var deviceType      = deviceTypeQuery.FirstOrDefault();

            if (deviceType == null)
            {
                throw new ApplicationException("设备类型不存在");
            }
            var device = ObjectMapper.Map <Device>(input);

            device.Gateway    = gateway;
            device.DeviceType = deviceType;
            var result = _deviceRepository.Insert(device);

            CurrentUnitOfWork.SaveChanges();
            return(ObjectMapper.Map <DeviceDto>(result));
        }
        public async Task <ActionResult> AddDevice([FromBody] CreateDeviceDto device)
        {
            if (await _deviceRepository.ExistByPhoneId(device.PhoneId))
            {
                return(Content("This device is already registered."));
            }

            var result = await _deviceService.CreateAsync(device);

            return(CreatedAtAction(nameof(AddDevice), result));
        }
Example #3
0
        public async Task <DeviceDto> CreateDevice(CreateDeviceDto dto, int gatewayId)
        {
            var entity  = mapper.Map <Device>(dto);
            var gateway = await gatewayRepository.Get(gatewayId, e => e.Devices);

            if (gateway.Devices.Count >= 10)
            {
                throw new InvalidOperationException("Gateway already have the max device count.");
            }

            entity.GatewayId = gatewayId;
            entity.Created   = DateTime.Now;
            return(mapper.Map <DeviceDto>(await deviceRepository.Insert(entity)));
        }
Example #4
0
        public DeviceDto Update(CreateDeviceDto input)
        {
            var entity    = _deviceRepository.Get(input.Id);
            var cityQuery = _cityRepository.GetAll().Where(c => c.CityName == input.CityName);

            if (!cityQuery.Any())
            {
                throw new ApplicationException("City不存在");
            }

            var factoryQuery = _factoryRepository.GetAll().Where(f => f.FactoryName == input.FactoryName);

            if (!factoryQuery.Any())
            {
                throw new ApplicationException("Factory不存在");
            }

            var factory = factoryQuery.FirstOrDefault();

            if (factory != null)
            {
                factory.City = cityQuery.FirstOrDefault();
                var workshopQuery = _workshopRepository.GetAll().Where(w => w.WorkshopName == input.WorkshopName);
                if (!workshopQuery.Any())
                {
                    throw new ApplicationException("Workshop不存在");
                }

                var workshop = workshopQuery.FirstOrDefault();
                if (workshop != null)
                {
                    workshop.Factory = factory;
                    var gatewayQuery = _gatewayRepository.GetAll().Where(g => g.GatewayName == input.GatewayName);

                    if (!gatewayQuery.Any())
                    {
                        throw new ApplicationException("Gateway不存在");
                    }

                    var gateway = gatewayQuery.FirstOrDefault();
                    if (gateway != null)
                    {
                        gateway.Workshop = workshop;
                        ObjectMapper.Map(input, entity);
                        entity.Gateway = gateway;
                        var deviceTypeQuery = _deviceTypeRepository.GetAll().Where(dt => dt.TypeName == input.DeviceTypeName);
                        if (!deviceTypeQuery.Any())
                        {
                            throw new ApplicationException("该设备类型不存在");
                        }

                        var deviceType = deviceTypeQuery.FirstOrDefault();
                        if (deviceType != null)
                        {
                            entity.DeviceType = deviceType;
                        }
                    }
                }
            }
            var result = _deviceRepository.Update(entity);

            CurrentUnitOfWork.SaveChanges();
            return(ObjectMapper.Map <DeviceDto>(result));
        }
Example #5
0
 public Task <DeviceDto> CreateDevice(int gatewayId, CreateDeviceDto dto)
 {
     return(deviceService.CreateDevice(dto, gatewayId));
 }
Example #6
0
        // ---------------------------- CREATE OR UPDATE DEVICE -----------------------------//
        // --------------------------------- STEP 3 (last)---------------------------------//
        public void CreateOrUpdateDevice(CreateDeviceDto device)
        {
            if (device.Id == 0)
            {
                var newDevice = new Device
                {
                    Name         = device.DeviceName,
                    Description  = device.Description,
                    DeviceTypeId = device.DeviceTypeId
                };

                _deviceRepository.Insert(newDevice);

                foreach (var deviceType in device.DeviceTypes)
                {
                    var propValues = deviceType.PropValues;

                    foreach (var propValue in propValues)
                    {
                        _valueRepository.Insert(new DevicePropertyValue
                        {
                            Value = propValue.Value,
                            DeviceTypePropertyId = _propertyRepository.FirstOrDefault(x =>
                                                                                      x.DeviceTypeId == deviceType.Id && x.Name == propValue.PropName).Id,
                            DeviceId = newDevice.Id
                        });
                    }
                }

                return;
            }

            var targetDevice = _deviceRepository.GetAll().Include(x => x.DevicePropertyValues)
                               .First(x => x.Id == device.Id);

            targetDevice.Name        = device.DeviceName;
            targetDevice.Description = device.Description;

            if (targetDevice.DeviceTypeId != device.DeviceTypeId)
            {
                var oldTypes = _typeService.GetDeviceTypeWithParents(targetDevice.DeviceTypeId).ToList();

                var newTypes = _typeService.GetDeviceTypeWithParents(device.DeviceTypeId).ToList();

                foreach (var oldType in oldTypes)
                {
                    var type = newTypes.FirstOrDefault(x => x.Id == oldType.Id);

                    if (type == null)
                    {
                        foreach (var prop in oldType.DeviceTypeProperties)
                        {
                            var valueToDelete =
                                targetDevice.DevicePropertyValues.FirstOrDefault(x =>
                                                                                 x.DeviceTypePropertyId == prop.Id);

                            if (valueToDelete == null)
                            {
                                continue;
                            }

                            _valueRepository.Delete(valueToDelete);
                        }
                    }
                }
            }

            foreach (var deviceType in device.DeviceTypes)
            {
                foreach (var propValue in deviceType.PropValues)
                {
                    var property = _propertyRepository.GetAll().Include(x => x.DeviceType).First(x =>
                                                                                                 x.DeviceTypeId == deviceType.Id && x.Name == propValue.PropName);

                    var values = _valueRepository.GetAll().Include(x => x.Device)
                                 .Include(x => x.DeviceTypeProperty);

                    var value = values.FirstOrDefault(x =>
                                                      x.DeviceId == targetDevice.Id && x.DeviceTypePropertyId == property.Id);

                    if (propValue.Value != null)
                    {
                        if (value == null)
                        {
                            var newValue = new DevicePropertyValue
                            {
                                DeviceId             = targetDevice.Id,
                                DeviceTypePropertyId = property.Id,
                                Value = propValue.Value
                            };

                            targetDevice.DevicePropertyValues.Add(newValue);

                            continue;
                        }

                        value.Value = propValue.Value;
                    }
                }
            }

            targetDevice.DeviceTypeId = device.DeviceTypeId;
        }