public async Task <IActionResult> UpdateDevice(string address)
        {
            var addressId = await GetAddressId(address);

            Database.Entities.Device device = await _repo.GetDeviceByAddresId(addressId);

            if (device == null)
            {
                return(StatusCode(304));
            }

            if (device.IsAutoUpdate.HasValue && device.IsAutoUpdate.Value == true)
            {
                if (!device.KindId.HasValue || !device.ComponentId.HasValue)
                {
                    return(StatusCode(304));
                }

                var versionId = await _repo.GetLatestVersionId(device.KindId, device.ComponentId) ?? 0;

                if (versionId > 0 && versionId != device.VersionId)
                {
                    device.VersionId = versionId;
                    device.IsUpdated = false;
                    device.Updated   = null;

                    await _repo.SaveAllChangesAsync();
                }
            }

            if (device.IsUpdated.HasValue && device.IsUpdated.Value == true)
            {
                return(StatusCode(304));
            }

            Database.Entities.Version version = null;

            if (device.VersionId.HasValue)
            {
                version = await _repo.GetVersionById(device.VersionId.Value);
            }

            if (version == null)
            {
                return(StatusCode(304));
            }

            var file = version.FileData;

            var content     = new MemoryStream(file.Content);
            var contentType = "APPLICATION/octet-stream";
            var fileName    = $"{file.Name}{file.Extension}";

            return(File(content, contentType, fileName));
        }
        public async Task <IActionResult> ConfirmUpdate(string address)
        {
            var addressId = await GetAddressId(address);

            Database.Entities.Device device = await _repo.GetDeviceByAddresId(addressId);

            if (device == null)
            {
                return(BadRequest());
            }

            device.IsUpdated = true;
            device.Updated   = DateTime.Now;

            await _repo.SaveAllChangesAsync();

            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task<IActionResult> AddDevice(DeviceDto deviceDto)
        {
            try
            {
                if (string.IsNullOrEmpty(deviceDto.AddressLabel))
                    return StatusCode((int)HttpStatusCode.BadRequest, "The Address field is required.");

                var address = await _repo.GetAddress(deviceDto.AddressLabel);

                if (address == null)
                    address = await _repo.AddAddress(deviceDto.AddressLabel);
                else if (address.IsConfirmed == false)
                    address.IsConfirmed = true;
                else
                    return StatusCode((int)HttpStatusCode.BadRequest, "Address exists!");

                var newDevice = new Database.Entities.Device()
                {
                    Name = deviceDto.Name,
                    AddressId = address.Id,
                    Created = DateTime.Now,
                    KindId = deviceDto.KindId,
                    ComponentId = deviceDto.ComponentId,
                    CategoryId = deviceDto.CategoryId,
                    Icon = deviceDto.Icon,
                    IsAutoUpdate = deviceDto.IsAutoUpdate,
                    VersionId = deviceDto.VersionId,
                };

                await _repo.Add(newDevice);

                return Ok(newDevice);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);

                return StatusCode((int)HttpStatusCode.InternalServerError, "Error!");
            }
        }