-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
120 lines (97 loc) · 2.79 KB
/
MainPage.xaml.cs
File metadata and controls
120 lines (97 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Waher.Events;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace ControllerXmpp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private static MainPage instance = null;
private Events events;
public MainPage()
{
this.InitializeComponent();
this.events = new MainPage.Events();
Log.Register(this.events);
if (instance is null)
instance = this;
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
Log.Unregister(this.events);
this.events = null;
if (instance == this)
instance = null;
}
public static MainPage Instance
{
get { return instance; }
}
public async void LightUpdated(double Value, int NrDec, string Unit)
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.Light.Text = Value.ToString("F" + NrDec.ToString()) + " " + Unit);
}
public async void MotionUpdated(bool Motion)
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.Motion.Text = Motion ? "DETECTED" : "-");
}
public async void RelayUpdated(bool Output)
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.Relay.Text = Output ? "ON" : "OFF");
}
public async void AddLogMessage(string Message)
{
DateTime TP = DateTime.Now;
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
this.EventsPanel.Children.Insert(0, new TextBlock() { Text = Message, TextWrapping = TextWrapping.Wrap });
while (this.EventsPanel.Children.Count > 1000)
this.EventsPanel.Children.RemoveAt(1000);
});
}
private class Events : EventSink
{
public Events() : base(string.Empty)
{
}
public override Task Queue(Event Event)
{
StringBuilder sb = new StringBuilder(Event.Message);
if (!string.IsNullOrEmpty(Event.Object))
{
sb.Append(' ');
sb.Append(Event.Object);
}
if (!string.IsNullOrEmpty(Event.Actor))
{
sb.Append(' ');
sb.Append(Event.Actor);
}
foreach (KeyValuePair<string, object> Parameter in Event.Tags)
{
sb.Append(" [");
sb.Append(Parameter.Key);
sb.Append("=");
if (Parameter.Value != null)
sb.Append(Parameter.Value.ToString());
sb.Append("]");
}
if (Event.Type >= EventType.Critical && !string.IsNullOrEmpty(Event.StackTrace))
{
sb.Append("\r\n\r\n");
sb.Append(Event.StackTrace);
}
MainPage.Instance.AddLogMessage(sb.ToString());
return Task.CompletedTask;
}
}
}
}