public IHttpActionResult PutSensorTag(string id, SensorTag sensorTag) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != sensorTag.id) { return(BadRequest()); } db.Entry(sensorTag).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!SensorTagExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
private void Add_Click(object sender, RoutedEventArgs e) { using (var database = new SensorDbContext()) { var sensor = new Sensor { SensorId = Guid.NewGuid(), Location = $"Room{DateTime.Now}" }; database.Sensors.Add(sensor); database.SaveChanges(); Sensors.ItemsSource = database.Sensors.ToList(); } }
public async Task <IActionResult> CreateGapAsync([FromForm] CreateGapSensorDto createGapSensorDto) { try { if (!_mqttClient.IsConnected) { await _mqttClient.ConnectAsync(_clientOptions); } var dateNow = DateTime.Now; var dateTimeOffset = new DateTimeOffset(dateNow); var unixDateTime = dateTimeOffset.ToUnixTimeSeconds(); if (createGapSensorDto.v != null) { _logger.LogInformation("D: {0}", createGapSensorDto.v[0]); var currGap = await _sensorDbContext.Gap.FindAsync(createGapSensorDto.ID); if (currGap == null) { await _sensorDbContext.Gap.AddAsync(new Gap() { Id = createGapSensorDto.ID, Value = createGapSensorDto.v[0], InitValue = -createGapSensorDto.v[0], Period = 120 }); await _sensorDbContext.SaveChangesAsync(true); currGap = await _sensorDbContext.Gap.FindAsync(createGapSensorDto.ID); } currGap.Value = createGapSensorDto.v[0]; _sensorDbContext.SaveChanges(true); var info = new SensorInfo(createGapSensorDto.PER, createGapSensorDto.VOLT, createGapSensorDto.CSQ); var meas = createGapSensorDto.v.Select((d, i) => new GapSensorMeas(d, createGapSensorDto.t[i], currGap.InitValue)); var msg = new Message(info, meas); var str = JsonConvert.SerializeObject(msg); var message = new MqttApplicationMessageBuilder() .WithTopic("legacy/gap/" + createGapSensorDto.ID) .WithPayload(str) .WithAtMostOnceQoS() .Build(); await _mqttClient.PublishAsync(message); var sper = currGap.Period > 0 ? currGap.Period : 120; var mper = sper / 20; return(Ok(String.Format("PER={0},TSP={1},ENC={2},", sper, unixDateTime, mper))); } return(Ok(String.Format("PER={0},TSP={1},ENC={2},", 120, unixDateTime, 6))); } catch (Exception ex) { _logger.LogError(ex, ex.Message); } await _mqttClient.DisconnectAsync(); return(StatusCode(StatusCodes.Status500InternalServerError)); }