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")); } }
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()); }