[NF] Schalter auslesen

[BF] C# Guidlines
This commit is contained in:
BlubbFish 2017-08-10 22:15:52 +00:00
parent 90e662458e
commit 39c0ab4e3c
6 changed files with 45 additions and 24 deletions

View File

@ -60,6 +60,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sensor\ASensor.cs" />
<Compile Include="Sensor\Power.cs" />
<Compile Include="Sensor\Switch.cs" />
<Compile Include="Tracings\ATracings.cs" />
<Compile Include="Tracings\Graph.cs" />
<Compile Include="Tracings\Meter.cs" />

View File

@ -35,8 +35,8 @@ namespace Dashboard {
ASensor s;
switch(ini.GetValue(sensor, "type").ToLower()) {
case "power": s = new Power(ini.GetSection(sensor)); break;
default:
throw new ArgumentException("sensor.ini: " + sensor + " section has a missconfiguration in type");
case "switch": s = new Switch(ini.GetSection(sensor)); break;
default: throw new ArgumentException("sensor.ini: " + sensor + " section has a missconfiguration in type");
}
this.sensors.Add(sensor.ToLower().Substring(1, sensor.Length - 2), s);
}

View File

@ -19,6 +19,9 @@ namespace Dashboard.Sensor {
public event UpdatedValue Update;
public ASensor(Dictionary<String, String> settings) {
this.GetBool = true;
this.GetFloat = 0.0f;
this.GetInt = 0;
this.topic = (settings.Keys.Contains("topic")) ? settings["topic"] : "";
this.Polling = (settings.Keys.Contains("polling")) ? Int32.Parse(settings["polling"]) : 60;
this.pollcount = this.Polling;

View File

@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Text;
using uPLibrary.Networking.M2Mqtt.Messages;
namespace Dashboard.Sensor {
class Switch : ASensor {
public Switch(Dictionary<System.String, System.String> settings) : base(settings) {
this.Datatypes = Types.Bool;
}
protected override void UpdateValue(MqttMsgPublishEventArgs e) {
this.GetBool = (Encoding.UTF8.GetString(e.Message).ToLower() == "on") ? true : false;
}
}
}

View File

@ -9,7 +9,7 @@ namespace Dashboard.Tracings {
class Graph : ATracings, IDisposable {
private Series series;
private Chart chart;
private Queue<Tuple<DateTime, float>> hist = new Queue<Tuple<DateTime, float>>();
private Queue<Tuple<DateTime, Single>> hist = new Queue<Tuple<DateTime, Single>>();
private Int32 Chart_Items;
public Graph(ASensor sensor, Dictionary<String, String> settings) : base(sensor, settings) {
@ -58,9 +58,9 @@ namespace Dashboard.Tracings {
}
protected override void SensorUpdate(Object sender, EventArgs e) {
switch(this.sensor.Datatypes) {
case ASensor.Types.Bool: this.hist.Enqueue(new Tuple<DateTime, float>(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0)); break;
case ASensor.Types.Int: this.hist.Enqueue(new Tuple<DateTime, float>(this.sensor.Timestamp, this.sensor.GetInt)); break;
case ASensor.Types.Float: this.hist.Enqueue(new Tuple<DateTime, float>(this.sensor.Timestamp, this.sensor.GetFloat)); break;
case ASensor.Types.Bool: this.hist.Enqueue(new Tuple<DateTime, Single>(this.sensor.Timestamp, (this.sensor.GetBool) ? 1 : 0)); break;
case ASensor.Types.Int: this.hist.Enqueue(new Tuple<DateTime, Single>(this.sensor.Timestamp, this.sensor.GetInt)); break;
case ASensor.Types.Float: this.hist.Enqueue(new Tuple<DateTime, Single>(this.sensor.Timestamp, this.sensor.GetFloat)); break;
}
if(this.hist.Count > this.Chart_Items) {
this.hist.Dequeue();
@ -69,7 +69,7 @@ namespace Dashboard.Tracings {
this.chart.BeginInvoke((MethodInvoker)delegate {
this.series.Points.Clear();
try {
foreach(Tuple<DateTime, float> temp in this.hist) {
foreach(Tuple<DateTime, Single> temp in this.hist) {
this.series.Points.AddXY(temp.Item1, temp.Item2);
}
} catch(Exception) { };

View File

@ -8,7 +8,7 @@ using System.Globalization;
namespace Dashboard.Tracings {
class Meter : ATracings {
private Int32 DisplayItems;
private Queue<float> DisplayAverage = new Queue<float>();
private Queue<Single> DisplayAverage = new Queue<Single>();
private String DisplayUnit;
private Label label1;
private Label label2;
@ -20,22 +20,24 @@ namespace Dashboard.Tracings {
public override Panel GetPanel() {
Panel panel = new Panel();
label1 = new Label();
label1.Font = new System.Drawing.Font("Lucida Sans Typewriter", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label1.Location = new System.Drawing.Point(0, 0);
label1.Size = new System.Drawing.Size(200, 43);
label1.Text = "";
label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.label1 = new Label() {
Font = new System.Drawing.Font("Lucida Sans Typewriter", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((Byte)(0))),
Location = new System.Drawing.Point(0, 0),
Size = new System.Drawing.Size(200, 43),
Text = "",
TextAlign = System.Drawing.ContentAlignment.TopCenter
};
label2 = new Label();
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(0, 50);
label2.Size = new System.Drawing.Size(96, 13);
label2.Text = "";
this.label2 = new Label() {
AutoSize = true,
Location = new System.Drawing.Point(0, 50),
Size = new System.Drawing.Size(96, 13),
Text = ""
};
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Controls.Add(label2);
panel.Controls.Add(label1);
panel.Controls.Add(this.label2);
panel.Controls.Add(this.label1);
panel.Size = new System.Drawing.Size(200, 100);
return panel;
}
@ -49,12 +51,12 @@ namespace Dashboard.Tracings {
if(this.DisplayAverage.Count > this.DisplayItems) {
this.DisplayAverage.Dequeue();
}
float average = this.DisplayAverage.Sum() / this.DisplayAverage.Count();
Single average = this.DisplayAverage.Sum() / this.DisplayAverage.Count();
this.label1.BeginInvoke((MethodInvoker)delegate {
label1.Text = value+" "+this.DisplayUnit;
this.label1.Text = value+" "+this.DisplayUnit;
});
this.label2.BeginInvoke((MethodInvoker)delegate {
label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " " + this.DisplayUnit;
this.label2.Text = "Durchschnitt: " + Math.Round(average, 2) + " " + this.DisplayUnit;
});
}
}