public async Task <IActionResult> Create([Bind("InitValue,Value,Period,Id")] Gap gap) { if (ModelState.IsValid) { _context.Add(gap); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(gap)); }
public async Task <IActionResult> Create([Bind("InitX,X,InitY,Y,Period,Id")] Incl incl) { if (ModelState.IsValid) { _context.Add(incl); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(incl)); }
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)); }