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()); }
public IActionResult GetSensor([FromRoute] Guid id, [FromQuery] bool includeLight = false) { try { var sensor = _sensorInfoRepository.GetSensor(id, includeLight); if (sensor == null) { _log.Warn($"SensorModel {id} does not exist"); return(NotFound()); } if (includeLight) { var sensorResult = Mapper.Map <SensorDto>(sensor); return(Ok(sensorResult)); } var sensorWithoutLightResult = Mapper.Map <SensorWithoutLightDto>(sensor); return(Ok(sensorWithoutLightResult)); } catch (Exception e) { _log.Error(e, $"Exception while getting sensor {id}"); return(StatusCode(500, "our server did an oopsie")); } }