private void SaveEnergyValues(SmlMessageFinder smlCutter, SmlMessageParser parser) { var box = new MeasureValueBox(); box.MeasuredUtcTime = DateTime.UtcNow; box.MeasuredValues.Add("EHZ.MeasureTime", DateTime.Now); box.MeasuredValues.Add("EHZ.ConsumedEnergy1", parser.ConsumedEnergy1_Wh); box.MeasuredValues.Add("EHZ.ConsumedEnergy2", parser.ConsumedEnergy2_Wh); box.MeasuredValues.Add("EHZ.ProducedEnergy1", parser.ProducedEnergy1_Wh); box.MeasuredValues.Add("EHZ.ProducedEnergy2", parser.ProducedEnergy2_Wh); box.MeasuredValues.Add("EHZ.Device", parser.Device); box.MeasuredValues.Add("EHZ.CompanyName", parser.CompanyName); _Queue.Send(box); }
private void ParseSmlMessage(SmlMessageFinder smlCutter) { SmlMessageParser parser = new SmlMessageParser(smlCutter.Message); if (parser.IsValid) { _Logger.DebugFormat("Consumed: {0} Produced: {1}", parser.ConsumedEnergy1_Wh, parser.ProducedEnergy1_Wh); if (_TimeOfLastValidValue.HasValue) { TimeSpan lastReadDuration = DateTime.Now - _TimeOfLastValidValue.Value; if (lastReadDuration.TotalMinutes > 1) { _Logger.InfoFormat("last valid read from serial port is {0} minutes old", lastReadDuration.TotalMinutes); _ReadNumberOfResultBeforeWattCalculation = 5; } } _TimeOfLastValidValue = DateTime.Now; SaveEnergyValues(smlCutter, parser); if (_ReadNumberOfResultBeforeWattCalculation <= 0) { CalculateConsumedPower(smlCutter, parser); CalculateProducedPower(smlCutter, parser); } else { _ReadNumberOfResultBeforeWattCalculation--; } } else { string fileName = string.Format("Data\\{0:yyMMdd-HHmmss}.sml", DateTime.Now); using (FileStream fs = new FileStream(fileName, FileMode.Create)) { fs.Write(smlCutter.Message, 0, smlCutter.Message.Length); fs.Close(); } } }
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { var sp = (System.IO.Ports.SerialPort)sender; if (sp.BytesToRead < 392 * 2 - 1) { return; } try { IStreamReader stream = new SerialPortStreamReader(sp); SmlMessageFinder smlCutter = new SmlMessageFinder(stream); if (smlCutter.Found) { ParseSmlMessage(smlCutter); } } catch (Exception ex) { _Logger.Error(ex.Message); sp.BaseStream.Flush(); } }
private void CalculateConsumedPower(SmlMessageFinder smlCutter, SmlMessageParser parser) { if (false == _LastComsumedEnergy_Wh.MeasuredTime.HasValue) { // first point _LastComsumedEnergy_Wh.MeasuredTime = smlCutter.MeasuredTime; _LastComsumedEnergy_Wh.Value = parser.ConsumedEnergy1_Wh; } else { double?watt = CalcWattDifference(parser.ConsumedEnergy1_Wh, smlCutter.MeasuredTime, _LastComsumedEnergy_Wh.Value, _LastComsumedEnergy_Wh.MeasuredTime.Value); if (watt.HasValue) { var box = new MeasureValueBox(); box.MeasuredUtcTime = _LastComsumedEnergy_Wh.MeasuredTime.Value.ToUniversalTime(); box.MeasuredValues.Add("EHZ.MeasureTime", _LastComsumedEnergy_Wh.MeasuredTime.Value); box.MeasuredValues.Add("EHZ.ComsumedPower", watt.Value); _Queue.Send(box); _LastComsumedEnergy_Wh.MeasuredTime = smlCutter.MeasuredTime; _LastComsumedEnergy_Wh.Value = parser.ConsumedEnergy1_Wh; } } }