-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
300 lines (250 loc) · 6.81 KB
/
MainPage.xaml.cs
File metadata and controls
300 lines (250 loc) · 6.81 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Maker.RemoteWiring;
using Waher.Events;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace SensorCoap
{
/// <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 StreamWriter d8Output = null;
private StreamWriter a0Output = null;
private StreamWriter lightOutput = null;
private StreamWriter eventOutput = null;
private DateTime prevD8TP = DateTime.MinValue;
private DateTime prevA0TP = DateTime.MinValue;
private DateTime prevLightTP = DateTime.MinValue;
private bool d8First;
private bool a0First;
private bool lightFirst;
private bool eventFirst;
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;
this.CloseFiles();
}
public static MainPage Instance
{
get { return instance; }
}
public async void AnalogPinUpdated(string Pin, ushort Value)
{
if (Pin == "A0")
{
DateTime TP = DateTime.Now;
StreamWriter w;
if ((w = this.a0Output) != null)
{
lock (w)
{
if (!this.WriteNewRecord(w, TP, ref this.prevA0TP, ref this.a0First))
return;
w.Write(Value.ToString());
w.Write(']');
}
}
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.A0.Text = Value.ToString());
}
}
public async void DigitalPinUpdated(byte Pin, PinState Value)
{
if (Pin == 8)
{
DateTime TP = DateTime.Now;
StreamWriter w;
if ((w = this.d8Output) != null)
{
lock (w)
{
if (!this.WriteNewRecord(w, TP, ref this.prevD8TP, ref this.d8First))
return;
w.Write("\"");
w.Write(Value.ToString());
w.Write("\"]");
}
}
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.D8.Text = Value.ToString());
}
}
public async void LightUpdated(double Value, int NrDec, string Unit)
{
DateTime TP = DateTime.Now;
StreamWriter w;
string ValueStr = Value.ToString("F" + NrDec.ToString());
if ((w = this.lightOutput) != null)
{
lock (w)
{
if (!this.WriteNewRecord(w, TP, ref this.prevLightTP, ref this.lightFirst))
return;
w.Write(ValueStr.Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, "."));
w.Write(",\"");
w.Write(Unit);
w.Write("\"]");
}
}
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.Light.Text = ValueStr + " " + Unit);
}
public async void AddLogMessage(string Message)
{
DateTime TP = DateTime.Now;
StreamWriter w;
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);
});
if ((w = this.eventOutput) != null)
{
lock (w)
{
DateTime PrevLog = DateTime.MinValue; // Makes sure all events are logged.
if (!this.WriteNewRecord(w, TP, ref PrevLog, ref this.eventFirst))
return;
w.Write("\"");
w.Write(Message);
w.Write("\"]");
}
}
}
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;
}
}
private bool WriteNewRecord(StreamWriter w, DateTime TP, ref DateTime Prev, ref bool First)
{
if (First)
{
First = false;
w.Write("[[");
}
else
{
if (TP.Year == Prev.Year &&
TP.Month == Prev.Month &&
TP.Day == Prev.Day &&
TP.Hour == Prev.Hour &&
TP.Minute == Prev.Minute &&
TP.Second == Prev.Second &&
TP.Millisecond == Prev.Millisecond)
{
return false;
}
w.WriteLine(",");
w.Write(" [");
}
Prev = TP;
w.Write("DateTime(");
w.Write(TP.Year.ToString("D4"));
w.Write(",");
w.Write(TP.Month.ToString("D2"));
w.Write(",");
w.Write(TP.Day.ToString("D2"));
w.Write(",");
w.Write(TP.Hour.ToString("D2"));
w.Write(",");
w.Write(TP.Minute.ToString("D2"));
w.Write(",");
w.Write(TP.Second.ToString("D2"));
w.Write(",");
w.Write(TP.Millisecond.ToString("D3"));
w.Write("),");
return true;
}
private void OutputToFile_Click(object sender, RoutedEventArgs e)
{
this.CloseFiles();
if (this.OutputToFile.IsChecked.HasValue && this.OutputToFile.IsChecked.Value)
{
this.d8First = true;
this.d8Output = File.CreateText(Windows.Storage.ApplicationData.Current.LocalFolder.Path +
Path.DirectorySeparatorChar + "D8.script");
this.a0First = true;
this.a0Output = File.CreateText(Windows.Storage.ApplicationData.Current.LocalFolder.Path +
Path.DirectorySeparatorChar + "A0.script");
this.lightFirst = true;
this.lightOutput = File.CreateText(Windows.Storage.ApplicationData.Current.LocalFolder.Path +
Path.DirectorySeparatorChar + "Light.script");
this.eventFirst = true;
this.eventOutput = File.CreateText(Windows.Storage.ApplicationData.Current.LocalFolder.Path +
Path.DirectorySeparatorChar + "Events.script");
}
}
private void CloseFiles()
{
this.CloseFile(ref this.d8Output);
this.CloseFile(ref this.a0Output);
this.CloseFile(ref this.lightOutput);
this.CloseFile(ref this.eventOutput);
}
private void CloseFile(ref StreamWriter File)
{
StreamWriter w;
if ((w = File) != null)
{
File = null;
lock (w)
{
w.WriteLine("];");
w.Flush();
w.Dispose();
}
}
}
}
}