Exemple #1
0
        public int CreateDevice(DeviceDto model)
        {
            Device tblDevice = Mapper.Map <DeviceDto, Device>(model);

            _deviceRepository.CreateDevice(tblDevice, _dbContext);
            return(_dbContext.SaveChanges());
        }
Exemple #2
0
        public async Task CreateMeasurement(string deviceUuid, float temperature, float humidity)
        {
            var device = await _deviceRepository.GetDevice(deviceUuid);

            if (device == null)
            {
                await _deviceRepository.CreateDevice(deviceUuid, "unknown device", true);
            }

            if (!_memoryCache.TryGetValue <List <Measurement> >(deviceUuid, out var measurements))
            {
                measurements = new List <Measurement>();
            }

            var measurement = new Measurement
            {
                DeviceUuid = deviceUuid, Humidity = humidity, Temperature = temperature, TimeStamp = DateTime.UtcNow
            };

            measurements.Add(measurement);
            if (measurements.Count > Threshold)
            {
                await _measurementRepository.CreateMeasurements(measurements);

                measurements.Clear();
            }

            _memoryCache.Set(deviceUuid, measurements);
        }
        public async Task <IActionResult> CreateOrUpdateDevice([FromBody] CreateOrUpdateDeviceDTO device)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState)); //400
                }

                var createDev = _mapper.Map <Device>(device);

                if (device.Id != null)
                {
                    await _repository.UpdateDevice(createDev);
                }
                else
                {
                    await _repository.CreateDevice(createDev);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
Exemple #4
0
        /// <summary>
        /// Create device
        /// </summary>
        /// <param name="device"></param>
        /// <param name="user">This parameter is provided so that we can know which user created device.</param>
        /// <returns></returns>
        public int CreateDevice(CreateDeviceDomain device, UserDomain user)
        {
            if (device == null || user == null)
            {
                throw new NsiArgumentException(DeviceMessages.DeviceInvalidArgument);
            }

            return(_deviceRepository.CreateDevice(device, user));
        }
        // public async Task<IActionResult> GetAllTrkDos(){
        //     return Ok(await this.repo.GetAllTrkDos());
        // }
        public async Task <IActionResult> device(m_device device)
        {
            if (await repo.GetDevice(device.DeviceID) != null)
            {
                return(BadRequest("device already exists"));
            }
            var created = await repo.CreateDevice(device);

            return(Ok(created));
        }
Exemple #6
0
        public async Task <IActionResult> Create([Bind("Name,MACAddress,location,Description")] DeviceViewModel deviceViewModel)
        {
            if (ModelState.IsValid)
            {
                var device = _mapper.Map <Device>(deviceViewModel);
                device.UserID = _userManager.GetUserId(User);
                await _repo.CreateDevice(device);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(deviceViewModel));
        }
        public IActionResult CreateDevice(CreateDeviceRequest device)
        {
            Device created_device = _deviceRepository.CreateDevice(device.name,
                                                                   Guid.NewGuid().ToString(),
                                                                   HttpContext.User.Identity.Name);

            if (created_device == null)
            {
                return(BadRequest(new { statusCode = 400, message = "bad_request" }));
            }
            return(StatusCode(201, new { statusCode = 201, message = "created" }));
        }
Exemple #8
0
        public async Task <IActionResult> CreateOrUpdateDevice(DeviceForCreationOrUpdate deviceDto)
        {
            if (deviceDto == null)
            {
                return(BadRequest());
            }

            if (deviceDto.Id == null)
            {
                //ovaj blok se izvrsava ukoliko je u pitanju kreiranje uredjaja
                var device = _mapper.Map <Device>(deviceDto);

                _repo.CreateDevice(device);

                if (await _repo.SaveAll())
                {
                    var deviceFromRepo = await _repo.GetDevice(device.Id);

                    var deviceToReturn = _mapper.Map <DeviceToReturnDto>(deviceFromRepo);
                    return(CreatedAtRoute("GetDevice", new { Id = deviceFromRepo.Id }, deviceToReturn));
                }
                throw new Exception("Creating device failed on save");
            }
            else
            {
                //ovaj blok se izvrsava ukoliko je u pitanju update uredjaja
                var deviceToUpdate = _mapper.Map <Device>(deviceDto);

                _repo.UpdateDevice(deviceToUpdate);

                if (await _repo.SaveAll())
                {
                    return(NoContent());
                }

                throw new Exception("Updating device failed on save");
            }
        }