Example #1
0
        public IActionResult CreateSensorData([FromBody] TheThingsNetworkUplinkBodyDto ttnBody)
        {
            Sensor sensor = _sensorInfoRepository.GetSensor(ttnBody.dev_id, false);

            if (sensor == null)
            {
                sensor = new Sensor {
                    Description = ttnBody.dev_id
                };
                _sensorInfoRepository.AddSensor(sensor);

                if (!_sensorInfoRepository.Save())
                {
                    return(StatusCode(500, "our server did an oopsie"));
                }
            }

            dynamic payload_fields = JsonConvert.DeserializeObject(ttnBody.payload_fields.ToString());

            var lightValue = payload_fields.light;

            if (lightValue != null)
            {
                LightForCreationDto lightFromTtn = new LightForCreationDto()
                {
                    Value = lightValue
                };

                var finalLight = Mapper.Map <Entities.Light>(lightFromTtn);

                finalLight = _sensorActionDetermination.AutofillLight(finalLight);

                sensor = _sensorInfoRepository.GetSensor(ttnBody.dev_id, false);

                _sensorInfoRepository.AddLightForSensor(sensor.Id, finalLight);

                if (!_sensorInfoRepository.Save())
                {
                    return(StatusCode(500, "our server did an oopsie"));
                }
            }

            TheThingsNetworkDownlinkBodyDto downlinkBody = new TheThingsNetworkDownlinkBodyDto()
            {
                dev_id      = ttnBody.dev_id,
                confirmed   = false,
                payload_raw = "01",
                port        = 1
            };

            _uplinkService.QueueDownlink(ttnBody.downlink_url, downlinkBody);

            return(Ok());
        }
Example #2
0
        public IActionResult CreateSensor([FromBody] SensorForCreationDto sensorModelFromRequest)
        {
            try
            {
                if (sensorModelFromRequest == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                var finalSensor = Mapper.Map <Entities.Sensor>(sensorModelFromRequest);

                _sensorInfoRepository.AddSensor(finalSensor);

                if (!_sensorInfoRepository.Save())
                {
                    return(StatusCode(500, "our server did an oopsie"));
                }

                var sensorModelForStore = Mapper.Map <Models.SensorDto>(finalSensor);

                return(CreatedAtAction("GetSensor", new { id = sensorModelForStore.Id }, sensorModelForStore));
            }
            catch (Exception e) {
                _log.Error(e, $"Exception while posting new sensor");
                return(StatusCode(500, "our server did an oopsie"));
            }
        }
Example #3
0
        public IActionResult CreateLight([FromRoute] Guid sensorId,
                                         [FromBody] LightForCreationDto lightModelFromRequest)
        {
            try
            {
                if (lightModelFromRequest == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                if (!_sensorInfoRepository.SensorExists(sensorId))
                {
                    _log.Warn($"SensorModel {sensorId} does not exist");
                    return(NotFound());
                }

                var finalLight = Mapper.Map <Entities.Light>(lightModelFromRequest);

                finalLight = _sensorActionDetermination.AutofillLight(finalLight);

                _sensorInfoRepository.AddLightForSensor(sensorId, finalLight);

                if (!_sensorInfoRepository.Save())
                {
                    return(StatusCode(500, "our server did an oopsie"));
                }

                var lightModelForStore = Mapper.Map <Models.LightDto>(finalLight);

                return(CreatedAtAction("GetLight", new
                {
                    sensorId, id = lightModelForStore.Id
                }, lightModelForStore));
            }
            catch (Exception e)
            {
                _log.Error(e, $"Exception while posting light for {sensorId}");
                return(StatusCode(500, "our server did an oopsie"));
            }
        }