private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { string result = ""; result = TrimPacket(this.Read()); if (this.GetPacketType(result) == "d") { WUData wu = new WUData(); wu = this.Populate(result); //Now decide if we need to raise the event long current_time = DateTime.Now.Ticks / 10000000; bool raise = false; // If the time passed is greater than the sampling rate, or if // the difference in watts since the last recorded reading is greater than // the threshold, then we will want to raise an event long time_delta = System.Math.Abs(current_time - last_raised_event_time); if (time_delta >= this.SamplingRate) { raise = true; } else { double watt_delta = System.Math.Abs(wu.Watts - last_reading); if (this.IsUseWattsDelta && watt_delta >= this.WattsThreshold) { raise = true; } } // Raise the event, and set the time and watts reading to the current values if (raise) { this.wu_OnWuReading(wu); last_raised_event_time = current_time; last_reading = wu.Watts; } // Now check to see if the InternalSamplingRate has changed since the last time we // looked. If it has, reset it. if (last_internal_sampling_rate == -1) { last_internal_sampling_rate = this.InternalSamplingRate; } else { double sampling_delta = last_internal_sampling_rate - this.InternalSamplingRate; if (sampling_delta != 0 && sp.IsOpen == true) { this.Write(WUCommands.ExternalModeStartString + this.InternalSamplingRate.ToString() + WUCommands.ExternalModeEndString); } } } }
/// Populate the data structure by parsing the v. private WUData Populate(string packet) { WUData wd = new WUData(); wd.Record = packet; wd.Time = DateTime.Now; wd.Watts = StringToDouble(GetArg(packet, 3)); if (wd.Watts != -1) { wd.Watts = wd.Watts / 10.0; } wd.Volts = DivideByTen(GetArg(packet, 4)); wd.Amps = DivideByTen(GetArg(packet, 5)); wd.WattHours = DivideByTen(GetArg(packet, 6)); wd.Cost = DivideByTen(GetArg(packet, 7)); wd.WattHoursPerMonth = DivideByTen(GetArg(packet, 8)); wd.CostPerMonth = DivideByTen(GetArg(packet, 9)); wd.WattMax = DivideByTen(GetArg(packet, 10)); wd.WattMin = DivideByTen(GetArg(packet, 13)); return(wd); }
protected virtual void wu_OnWuReading(WUData wu) { RaiseEvent(this.OnWuReading, (object)this, new WuReadingEventArgs(wu)); }
public WuReadingEventArgs(WUData wudata) { this.wuData = wudata; }