public async Task <DeviceLogsVm> Handle(GetDeviceLogQuery request, CancellationToken cancellationToken)
        {
            var sensor = _context.Devices.FirstOrDefault(x => x.Id == request.SensorId);

            if (sensor != null)
            {
                string value = null;


                var createDeviceLogCommand = new CreateDeviceLogsCommand
                {
                    Sensor = sensor,
                    Value  = value
                };
                await _mediator.Send(createDeviceLogCommand);
            }


            var data = _context.Devices.FirstOrDefault(x => x.Id == request.SensorId);

            var d = _context.SensorLogs.Where(x => x.Sensor.Id == data.Id).ToList();

            var dt = _mapper.Map <List <SensorLogsDto> >(d);


            return(new DeviceLogsVm
            {
                Device = new DeviceInfoDto {
                    Id = request.SensorId,
                    DeviceName = data?.Name
                },

                Data = dt
            });
        }
Exemple #2
0
        public async Task ShouldCreateSensorLogItemValueRandom()
        {
            var userId = await RunAsDefaultUserAsync();

            var device = new CreateDeviceItemCommand
            {
                Name = "New Devices",
            };

            var deviceId = await SendAsync(device);

            var item = await FindAsync <Sensor>(deviceId);


            var command = new CreateDeviceLogsCommand
            {
                Sensor = item
            };

            var sensorLog = await SendAsync(command);

            var sensorItem = await FindAsync <SensorLog>(sensorLog);

            sensorItem.Should().NotBeNull();
            sensorItem.CreatedBy.Should().Be(userId);
            sensorItem.LastModifiedBy.Should().BeNull();
            sensorItem.LastModified.Should().BeNull();
        }
Exemple #3
0
        public void ShouldRequiredMinimumFields()
        {
            var command = new CreateDeviceLogsCommand();


            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <ValidationException>();
        }
Exemple #4
0
        public async Task ShouldThrowValidationException()
        {
            var userId = await RunAsDefaultUserAsync();

            var device = new CreateDeviceItemCommand
            {
                Name = "New Devices",
            };

            var deviceId = await SendAsync(device);

            var item = await FindAsync <Sensor>(deviceId);



            var command = new CreateDeviceLogsCommand
            {
                Sensor = item
            };


            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <ValidationException>();
        }
Exemple #5
0
        public async Task <DeviceSingleLogsVm> Handle(GetSingleDeviceLogQuery request, CancellationToken cancellationToken)
        {
            //TODO: if first time => init
            var isFirstTime = _context.SensorLogs.Any(x => x.Sensor.Id == request.SensorId);

            if (!isFirstTime)
            {
                //TODO: get type
                var deviceType = _context.Devices.FirstOrDefault(x => x.Id == request.SensorId);

                StringBuilder valueSb = new StringBuilder();
                if (deviceType != null)
                {
                    switch (deviceType.Type)
                    {
                    case Domain.Enums.ESensorType.HueDaySensor:
                        var hueDaylightSensor = new HueDaylightSensor
                        {
                            daylight = false, lastupdated = DateTime.Now.ToString(CultureInfo.InvariantCulture)
                        };
                        valueSb.Append(JsonConvert.SerializeObject(hueDaylightSensor));
                        break;

                    case ESensorType.Default:
                        valueSb.Append(string.Empty);
                        break;

                    case ESensorType.HueWhiteLamp:
                        var hueWhiteLamp = new HueWhiteLamp
                        {
                            @on = false, alert = "none", bri = 100, bri_inc = 100, transisiontime = 10
                        };
                        valueSb.Append(JsonConvert.SerializeObject(hueWhiteLamp));
                        break;

                    default:
                        valueSb.Append(string.Empty);
                        break;
                    }
                }

                var createDeviceLogCommand = new CreateDeviceLogsCommand
                {
                    Sensor = _context.Devices.FirstOrDefault(x => x.Id == request.SensorId),
                    Value  = valueSb.ToString()
                };
                await _mediator.Send(createDeviceLogCommand, cancellationToken);
            }

            var sensor     = _context.Devices.FirstOrDefault(x => x.Id == request.SensorId);
            var sensorLogs = _context.SensorLogs.Where(x => x.Sensor.Id == sensor.Id).ToList();

            return(new DeviceSingleLogsVm
            {
                Device = new DeviceInfoDto
                {
                    Id = request.SensorId,
                    DeviceName = sensor?.Name
                },
                Data = _mapper.Map <SensorLogsDto>(sensorLogs.Last())
            });
        }