public async Task <IActionResult> AddScaleResult(int id, [FromBody] ScaleResultResource newScaleResultResource) { if (!ModelState.IsValid) { return(BadRequest()); } Person personInDb = await dbContext.Persons.SingleOrDefaultAsync(p => p.Id == id); if (personInDb == null) { return(NotFound()); } ScaleResult newScaleResult = new ScaleResult() { Date = TimeZoneInfo.ConvertTimeFromUtc(newScaleResultResource.Date, TimeZoneInfo.Local),// System.DateTime.Now, Weight = newScaleResultResource.Weight, PercentageMuscles = newScaleResultResource.PercentageMuscles, PercentageFat = newScaleResultResource.PercentageFat, PercentageFatViscal = newScaleResultResource.PercentageFatViscal, Bmi = newScaleResultResource.Bmi, PersonId = id }; personInDb.ScaleResults.Add(newScaleResult); await dbContext.SaveChangesAsync(); ScaleResultResource res = mapper.Map <ScaleResult, ScaleResultResource>(newScaleResult); return(Ok(res)); }
public void GetWeight() { _lastCmd = Command.GetWeight; byte[] buffer; buffer = new byte[2]; buffer[0] = 0x1B; buffer[1] = 0x50; _lastScaledWeight = null; SendMessage(buffer, buffer.Length); }
ScaleResult GetScaleInfo(string data) { ScaleResult sr = new ScaleResult(); sr.DeviceSerial = _deviceSerial; if (data.Length != 14) { return(null); } string strWeight = data.Substring(0, 10); string strUnit = data.Substring(11); double value = 0.0; if (double.TryParse(strWeight.Replace(" ", "").Replace(",", "."), System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, out value)) { sr.WeightValue = value; sr.WeightEvent = DateTime.Now; if (strUnit.Equals("ct ")) { sr.BValueStable = true; sr.StrUnit = ScaleResult.Unit.Carats; } else if (strUnit.Equals("g ")) { sr.BValueStable = true; sr.StrUnit = ScaleResult.Unit.Grams; } else { sr.BValueStable = false; sr.StrUnit = ScaleResult.Unit.None; } return(sr); } return(null); }
/*public string getSerial() * { * lastCmd = Command.GetSerial; * lastSerialRead = null; * byte[] buffer; * buffer = new byte[4]; * buffer[0] = 0x1B; * buffer[1] = 0x78; * buffer[2] = 0x32; * buffer[3] = 0x5F; * sendMessage(buffer, buffer.Length); * System.Threading.Thread.Sleep(250); * return lastSerialRead; * } * public string getModel() * { * lastCmd = Command.GetModel; * lastModelRead = null; * byte[] buffer; * buffer = new byte[4]; * buffer[0] = 0x1B; * buffer[1] = 0x78; * buffer[2] = 0x31; * buffer[3] = 0x5F; * sendMessage(buffer, buffer.Length); * System.Threading.Thread.Sleep(250); * return lastModelRead; * } * * public void setTare() * { * lastCmd = Command.SetTare; * byte[] buffer; * buffer = new byte[2]; * buffer[0] = 0x1B; * buffer[1] = 0x54; * sendMessage(buffer, buffer.Length); * }*/ private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) { while (true) { if (!_serialPort.IsOpen) { return; } _inboundBuffer += _serialPort.ReadExisting(); int frameEnd = _inboundBuffer.IndexOf(CharEndOfFrameHf, 0); if (frameEnd < 0) { break; } // A full message is available. string message = _inboundBuffer.Substring(0, frameEnd - 1); _inboundBuffer = _inboundBuffer.Substring(frameEnd + 1); _dataRead = message; if (_lastCmd == Command.GetWeight) { _lastScaledWeight = GetScaleInfo(_dataRead); } else if (_lastCmd == Command.GetSerial) { _lastSerialRead = _dataRead.TrimStart().Substring(0, 8); } else if (_lastCmd == Command.GetModel) { _lastModelRead = _dataRead; } if (NotifyWeightEvent != null) { NotifyWeightEvent(this, _deviceSerial, _dataRead); } } }