Forked from
Wetterstation / Frontend
97 commits behind the upstream repository.
-
Dennis Eisold authored3c300407
Form1.cs 5.44 KiB
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using Newtonsoft.Json;
namespace Wetterstation
{
public partial class Hintergrund : Form
{
private MqttClient myClient;
SettingsModel WeatherstationSettings = new SettingsModel();
public Hintergrund()
{
InitializeComponent();
WeatherstationSettings = SqliteDataAccess.LoadWeatherstationSettings();
lbCity.Text = WeatherstationSettings.city;
// Start a timer for date and time
Timer t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(this.t_DateTime);
t.Start();
mqttStart();
}
private void t_DateTime(object sender, EventArgs e)
{
lbUhr.Text = DateTime.Now.ToLongTimeString();
lbDatum.Text = DateTime.Now.ToShortDateString();
}
private void LED_Click(object sender, EventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(818, 238, 12, 12));
myBrush.Dispose();
formGraphics.Dispose();
}
private void Label1_Click(object sender, EventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(872, 290, 12, 12));
myBrush.Dispose();
formGraphics.Dispose();
}
private void LED_S_Click(object sender, EventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(818,350, 12, 12));
myBrush.Dispose();
formGraphics.Dispose();
}
private void LED_W_Click(object sender, EventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(764, 290, 12, 12));
myBrush.Dispose();
formGraphics.Dispose();
}
//MQTT connect
public void mqttStart()
{
String topic_publish = "/wetterstation/backend/";
String topic_subscribe = "/wetterstation/frontend/#";
System.Console.WriteLine("Hello, World!");
// Create Client instance
this.myClient = new MqttClient("mqtt.itstall.de");
// Register to message received
myClient.MqttMsgPublishReceived += client_recievedMessage;
string clientId = Guid.NewGuid().ToString();
this.myClient.Connect("", "wetterstation", "wetterstation");
// Subscribe to topic
this.myClient.Subscribe(new String[] { topic_subscribe }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
this.myClient.Publish(topic_publish, Encoding.UTF8.GetBytes("Request_frontend"));
}
public void msgPublish(String topic, String message)
{
Console.Write("Sending via MQTT: ");
Console.Write("Topic: " + topic);
Console.WriteLine("Message: " + message);
this.myClient.Publish(topic, Encoding.UTF8.GetBytes(message));
}
private void client_recievedMessage(object sender, MqttMsgPublishEventArgs e)
{
// Handle message received
var message = System.Text.Encoding.Default.GetString(e.Message);
System.Console.WriteLine("Message received: " + message);
this.updateFormWeather(message);
}
private void BtSystemKnopf_Click(object sender, EventArgs e)
{
Form settings = new Settings();
// abhängig von Form1
settings.ShowDialog();
updateFormComplete();
this.updateSettings2Mqtt();
}
public void updateFormWeather(String message)
{
try
{
dynamic weatherData = JsonConvert.DeserializeObject(message.ToString());
lbAussenTemp.Invoke(new Action(() => { lbAussenTemp.Text = weatherData.sensors[1].temperature + " °C"; }));
}
catch (Exception ignored) { }
}
public void updateFormComplete()
{
WeatherstationSettings = SqliteDataAccess.LoadWeatherstationSettings();
lbCity.Text = WeatherstationSettings.city;
}
public void updateSettings2Mqtt()
{
Console.WriteLine(JsonConvert.SerializeObject(WeatherstationSettings).ToString());
this.msgPublish(WeatherstationSettings.mqtt_topic_backend + WeatherstationSettings.frontendId + "/settings", JsonConvert.SerializeObject(WeatherstationSettings).ToString());
}
private void Hintergrund_Load(object sender, EventArgs e)
{
}
}
}