예제 #1
0
 private static void AddAggregatedData(SensorInput input, ICollection <AggregatedSensorData> aggregatedSensorData, Sensor sensor, SensorData sensorData)
 {
     aggregatedSensorData.Add(new AggregatedSensorData
     {
         SensorBoxId     = sensor.BoxId,
         SensorType      = sensor.Type,
         AggregationType = AggregationType.Mean,
         CreatedAt       = input.Timestamp,
         Value           = sensorData.Values.Average()
     });
     aggregatedSensorData.Add(new AggregatedSensorData
     {
         SensorBoxId     = sensor.BoxId,
         SensorType      = sensor.Type,
         AggregationType = AggregationType.Min,
         CreatedAt       = input.Timestamp,
         Value           = sensorData.Values.Min()
     });
     aggregatedSensorData.Add(new AggregatedSensorData
     {
         SensorBoxId     = sensor.BoxId,
         SensorType      = sensor.Type,
         AggregationType = AggregationType.Max,
         CreatedAt       = input.Timestamp,
         Value           = sensorData.Values.Max()
     });
     aggregatedSensorData.Add(new AggregatedSensorData
     {
         SensorBoxId     = sensor.BoxId,
         SensorType      = sensor.Type,
         AggregationType = AggregationType.StandardDeviation,
         CreatedAt       = input.Timestamp,
         Value           = sensorData.Values.Aggregate(0d, (a, x) => a + Math.Pow(x - sensorData.Values.Average(), 2)) / (sensorData.Values.Count() - 1)
     });
 }
예제 #2
0
        public async Task <Sensor> Add(SensorInput sensor)
        {
            var entity = mapper.Map <SensorEntity>(sensor);

            this.dbContext.Add(entity);
            await SaveChanges();

            return(mapper.Map <Sensor>(entity));
        }
예제 #3
0
        public Sensor AddSensor([FromBody] SensorInput input)
        {
            var returning = _sensorRepository.CreateSensor(new Sensor()
            {
                Name         = input.Name,
                Description  = input.Description,
                ControllerId = input.ControllerId,
                Type         = input.Type,
                UserGroupId  = input.UserGroupId
            });

            _sensorRepository.SaveChanges();
            return(returning);
        }
예제 #4
0
        public async Task <Sensor> Update(Guid sensorId, SensorInput sensor)
        {
            var entity = await this.Entity(sensorId);

            if (entity != null)
            {
                var oldName = entity.Name;
                mapper.Map(sensor, entity);
                entity.Name = oldName;
                await SaveChanges();

                return(mapper.Map <Sensor>(entity));
            }
            throw new KeyNotFoundException($"Cannot find sensor {sensorId.ToString()}");
        }
예제 #5
0
        public async Task <IEnumerable <AggregatedSensorData> > ProcessInputAsync(SensorInput input)
        {
            _logger.LogDebug($"Incoming raw sensor data: sensor id {input.SensorBoxId}");

            if (!input.Data.SelectMany(x => x.Values).Any())
            {
                return(Enumerable.Empty <AggregatedSensorData>());
            }

            var aggregatedSensorData = new List <AggregatedSensorData>();

            foreach (var sensorData in input.Data)
            {
                var sensor = await _sensorRepository.GetByBoxIdAndTypeAsync(input.SensorBoxId, sensorData.Type);

                AddAggregatedData(input, aggregatedSensorData, sensor, sensorData);
            }

            return(aggregatedSensorData);
        }
예제 #6
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] SensorInput input,
            [Principal] ClaimsPrincipal principal,
            [Queue("aggregated-sensor-data")] IAsyncCollector <AggregatedSensorData> output,
            CancellationToken cancellationToken)
        {
            if (!(principal?.Identity?.IsAuthenticated).GetValueOrDefault(false))
            {
                return(new UnauthorizedResult());
            }
            var aggregatedSensorData = await _inputService.ProcessInputAsync(input);

            foreach (var sensorData in aggregatedSensorData)
            {
                await output.AddAsync(sensorData, cancellationToken);
            }
            await output.FlushAsync(cancellationToken);

            return(new OkResult());
        }
예제 #7
0
        public void OnReceive(OscPort.Capsule c)
        {
            if (c.message.path != OSC_PATH)
            {
                return;
            }

            var     p = SensorInput.Parse(c.message);
            Vector3 worldPos;

            if (Contact(p.center, out worldPos))
            {
                if (p.center.z > 0.5f)
                {
                    planter.AddCreationMarker(worldPos);
                }
                else
                {
                    planter.AddDestructionMarker(worldPos);
                }
            }
        }
    public async Task <IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] SensorInput input,
        [Queue("aggregated-sensor-data")] IAsyncCollector <AggregatedSensorData> output,
        CancellationToken cancellationToken)
    {
        try
        {
            var aggregatedSensorData = await _inputService.ProcessInputAsync(input);

            foreach (var sensorData in aggregatedSensorData)
            {
                await output.AddAsync(sensorData, cancellationToken);
            }
            await output.FlushAsync(cancellationToken);

            return(new OkResult());
        }
        catch (Exception e)
        {
            _logger.LogError(e, "Error while executing function 'Sensorinput'. Stack trace: {StackTrace}", e.StackTrace);
            throw;
        }
    }
예제 #9
0
    public async Task <IEnumerable <AggregatedSensorData> > ProcessInputAsync(SensorInput input)
    {
        _logger.LogDebug($"Incoming raw sensor data: sensor id {input.SensorBoxId}");

        if (!input.Data.SelectMany(x => x.Values).Any())
        {
            return(Enumerable.Empty <AggregatedSensorData>());
        }

        var aggregatedSensorData = new List <AggregatedSensorData>();

        foreach (var sensorData in input.Data)
        {
            var sensor = await _sensorRepository.GetByBoxIdAndTypeAsync(input.SensorBoxId, sensorData.Type);

            if (sensor == null)
            {
                throw new InvalidOperationException($"Sensor with box id {input.SensorBoxId} and type {sensorData.Type} not found.");
            }
            AddAggregatedData(input, aggregatedSensorData, sensor, sensorData);
        }

        return(aggregatedSensorData);
    }
예제 #10
0
 public async Task <Sensor> Update(Guid sensorId, [FromBody] SensorInput sensor)
 {
     return(await repo.Update(sensorId, sensor));
 }
예제 #11
0
파일: Protocol.cs 프로젝트: smo-key/NXTLib
        /// <summary>
        /// [Native] Get a sensor's current state.
        /// </summary>
        /// <param name="port">The port of the attached sensor.</param>
        /// <returns>Returns a SensorInput package.</returns>
        public SensorInput GetSensorValues(SensorPort port)
        {
            byte[] request = new byte[] {
            0x00,
            (byte) DirectCommand.GetInputValues,
            (byte) port
            };

            byte[] reply = CompleteRequest(request);
            if (reply[3] != (byte)port)
            {
                throw new NXTReplyIncorrect();
            }
            SensorInput result = new SensorInput();
            result.valid = (reply[4] != 0x00);
            result.calibrated = (reply[5] != 0x00);
            result.type = (SensorType)reply[6];
            result.mode = (SensorMode)reply[7];
            result.value_raw = GetUInt16(reply, 8);
            result.value_normalized = GetUInt16(reply, 10);
            result.value_scaled = GetInt16(reply, 12);
            result.value_calibrated = GetInt16(reply, 14);
            return result;
        }