public async Task <bool> UpdateAsync <TEntity>(TEntity item, bool AutoSave = SAVE) where TEntity : class
        {
            var result = repository.Update(item);

            if (AutoSave)
            {
                await SaveChangesAsync().ConfigureAwait(false);
            }
            ;
            return(result);
        }
Ejemplo n.º 2
0
        public async Task <int> AddPeripheral(int id, Peripheral ph)
        {
            var gw = await _repo.Get(id);

            if (gw.Peripheral.Count == 10)
            {
                throw new Exception("Gateway is on full capacity(10)");
            }
            gw.Peripheral.Add(this.ConvetToDomain(ph));
            return(await _repo.Update(gw));
        }
Ejemplo n.º 3
0
        public async Task <GatewayResponse> UpdateAsync(int id, Gateway gateway)
        {
            var serialExist = _gatewayRepository.FindBySerialAsync(gateway.SerialNumber);

            if (serialExist != null)
            {
                return(new GatewayResponse($"A gateway with Serial: {gateway.SerialNumber} Number already exists"));
            }

            var existing = await _gatewayRepository.FindByIdAsync(id);

            if (existing == null)
            {
                return(new GatewayResponse("Gateway not found"));
            }

            existing.Address      = gateway.Address ?? existing.Address;
            existing.SerialNumber = gateway.SerialNumber ?? existing.SerialNumber;
            existing.Name         = gateway.Name ?? existing.Name;
            try
            {
                _gatewayRepository.Update(existing);
                await _unitOfWork.CompleteAsync();

                return(new GatewayResponse(existing));
            }
            catch (Exception e)
            {
                return(new GatewayResponse($"Error updating Gateway: {e.Message}"));
            }
        }
Ejemplo n.º 4
0
        public GatewayDto Create(CreateGatewayDto input)
        {
            var query       = _gatewayRepository.GetAllIncluding().Where(g => g.HardwareId == input.HardwareId || g.GatewayName == input.GatewayName);
            var gateway_old = query.FirstOrDefault();

            if ((query.Any()) && (query.FirstOrDefault().IsDeleted == true))
            {
                gateway_old.IsDeleted = false;
                var result_old = _gatewayRepository.Update(gateway_old);
                CurrentUnitOfWork.SaveChanges();
                return(ObjectMapper.Map <GatewayDto>(result_old));
            }

            if (query.Any() && gateway_old.IsDeleted == false)
            {
                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 gatewayTypeQuery = _gatewayTypeRepository.GetAll().Where(gt => gt.TypeName == input.GatewayTypeName);
            var gatewayType      = gatewayTypeQuery.FirstOrDefault();

            if (gatewayType == null)
            {
                throw new ApplicationException("网关类型不存在");
            }
            var gateway = ObjectMapper.Map <Gateway>(input);

            gateway.Workshop    = workshop;
            gateway.GatewayType = gatewayType;
            var result = _gatewayRepository.Insert(gateway);

            CurrentUnitOfWork.SaveChanges();
            return(ObjectMapper.Map <GatewayDto>(result));
        }
Ejemplo n.º 5
0
        public IHttpActionResult GetConnections(string address)
        {
            var gateway = _gatewayRep.GetAll()
                          .SingleOrDefault(g => g.Address == address);

            if (gateway == null)
            {
                var errorMessage = _messages.GetMessage(Custom.NotFound, "Gateway");
                return(NotFound(errorMessage));
            }

            gateway.Active         = true;
            gateway.LastSignalDate = _dateTime.GetDateTime();
            _gatewayRep.Update(gateway);

            var network      = _networkRep.Get(gateway.Network_Id);
            var networkModel = _mapper.Map <NetworkWithSensorsModel>(network);

            return(Ok(networkModel));
        }
Ejemplo n.º 6
0
 public Gateway Update(Gateway entity)
 {
     return(_gatewayRepositories.Update(entity));
 }
Ejemplo n.º 7
0
        public IHttpActionResult Add(SensorReadingModelPost sensorReadingModel)
        {
            if (sensorReadingModel == null)
            {
                var errorMessage = _messages.GetMessage(Generic.NullObject);
                return(BadRequest(errorMessage));
            }

            _throttler.ThrottlerSetup(sensorReadingModel.SensorAddress, 1, 3);
            if (_throttler.RequestShouldBeThrottled())
            {
                return(TooManyRequests(_throttler));
            }

            var sensor = _sensorRep.GetAll()
                         .Where(s => s.Address == sensorReadingModel.SensorAddress)
                         .SingleOrDefault();


            if (sensor != null)
            {
                var gateway = _gatewayRep.GetAll()
                              .SingleOrDefault(g => g.Address == sensorReadingModel.GatewayAddress);

                if (gateway == null)
                {
                    var errorMessage = _messages.GetMessage(Custom.NotFound, "Gateway", "Address");
                    return(NotFound(errorMessage));
                }

                var sensorReading = _mapper.Map <SensorReading>(sensorReadingModel);
                sensorReading.Sensor_Id  = sensor.Id;
                sensorReading.InsertDate = _dateTime.GetDateOffSet();
                _readingRep.Add(sensorReading);

                var pending =
                    TheSensorIntervalPending
                    .GetPendingMember(sensor.Id);

                //Check if the pending exists
                if (pending != null)
                {
                    _mapper.Map(pending, sensor);
                    TheSensorIntervalPending.ClearPending(pending);
                }

                sensor.Active          = true;
                sensor.LastReadingDate = sensorReading.ReadingDate;
                sensor.LastInsertDate  = sensorReading.InsertDate;
                _sensorRep.Update(sensor);

                var address = sensorReadingModel.SensorAddress;
                Hub.Clients.Group(address).refreshReadings();



                //add the gateway connections
                if (!_connectionRep.GetAll()
                    .Any(
                        c => c.Gateway_Id == gateway.Id &&
                        c.Sensor_Id == sensor.Id)
                    )
                {
                    var connection = _connectionService.Create(gateway.Id, sensor.Id);
                    _connectionRep.Add(connection);
                }

                gateway.LastSensorDate = sensorReading.ReadingDate;
                _gatewayRep.Update(gateway);

                var createdReading = _mapper.Map <SensorReadingModelGet>(sensorReading);
                return(Created($"api/networks/{sensor.Network_Id}/sensors/{sensor.Id}/readings", createdReading));
            }
            else
            {
                var errorMessage = _messages.GetMessage(Custom.NotFound, "Sensor", "Address");
                return(NotFound(errorMessage));
            }
        }