-
Notifications
You must be signed in to change notification settings - Fork 2
/
Globals.cs
166 lines (145 loc) · 5.99 KB
/
Globals.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace IoTNightLight
{
public static class Globals
{
private static Frame rootFrame;
private static MainPage mainPage;
static Globals()
{
// Get current window and set it to MainPage
rootFrame = Window.Current.Content as Frame;
if (rootFrame != null) mainPage = (MainPage)rootFrame.Content;
}
/// <summary>
/// Interprets commands from console app and relays to the correct function
/// </summary>
/// <param name="msg">Command sent from console app</param>
public static void ParseMsg(string msg)
{
// Grab arguments for functions, but make sure it isn't empty
List<int> listArgs = GetIntValList(msg);
if (listArgs.Any())
{
foreach (var integer in listArgs)
{
Debug.WriteLine(integer);
}
}
// Split it on whitespace sequences.
string[] firstWordInMsg = Regex.Split(msg, @"\s+");
switch (firstWordInMsg[0])
{
// ---------------------------------------- TWEENING
case "tween":
mainPage.AllowTweening();
mainPage.TweenGauge(listArgs[0], listArgs[1], listArgs[2], listArgs[3]);
Debug.WriteLine(firstWordInMsg + ": " + listArgs[0] + " " + listArgs[1] + " " + listArgs[2] + " " + listArgs[3]);
break;
case "stop":
mainPage.PreventTweening();
break;
// ---------------------------------------- TEMPERATURE
case "temp":
mainPage.ChangeTitleText("Temperature");
if (listArgs.Any()){ mainPage.Goto(listArgs[0]);}
Debug.WriteLine(firstWordInMsg + ": " + listArgs[0]);
break;
// ---------------------------------------- LIGHT
case "light":
mainPage.ChangeTitleText("Light");
if (listArgs.Any()) { mainPage.Goto(listArgs[0]); }
Debug.WriteLine(firstWordInMsg + ": " + listArgs[0]);
break;
// ---------------------------------------- Moisture
case "moisture":
mainPage.ChangeTitleText("Moisture");
if (listArgs.Any()) { mainPage.Goto(listArgs[0]); }
Debug.WriteLine(firstWordInMsg + ": " + listArgs[0]);
break;
// ---------------------------------------- NAVIGATION
case "log":
Debug.WriteLine("navigating to log page");
mainPage.ChangeTitleText("Log");
break;
}
}
/* Parsing
* ==========================================================*/
/// <summary>
/// Parses int from msg str which is sent to IoT device for increase / decrease temp, etc.
/// </summary>
/// <param name="msg">What do you want to the IoT device to do?</param>
/// <returns>Integer used to change values in IoT device</returns>
private static int GetIntVal(string msg)
{
int intInMsg = 0;
string[] numbers = Regex.Split(msg, @"\D");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
intInMsg = i;
}
}
return intInMsg;
}
/// <summary>
/// Parses the string from console message and grabs the Ints and stores as a list.
/// </summary>
/// <param name="msg">String sent from console app</param>
/// <returns>List of integers that can be applied as parameters to functions</returns>
private static List<int> GetIntValList(string msg)
{
string[] numbers = Regex.Split(msg, @"\D");
return (from value in numbers where !string.IsNullOrEmpty(value) select int.Parse(value)).ToList();
}
/// <summary>
/// Splits each word in the string receives from the console app.
/// TODO: Not currently used.
/// </summary>
/// <param name="msg">String sent from console app</param>
/// <returns>Each word in the string, separately</returns>
private static string SplitEachWordFromString(string msg)
{
string[] separators = new string[] { ",", ".", "!", "\'", " ", "\'s" };
string text = msg;
string newMsg = "";
foreach (string word in text.Split(separators, StringSplitOptions.RemoveEmptyEntries))
{
Debug.WriteLine(word);
}
return newMsg;
}
/* NAVIGATION
* These change the title of the page and prevent it from tweening the old values from prev page
* ==========================================================*/
public static void Nav_To_Temp(object sender, RoutedEventArgs e)
{
mainPage.PreventTweening();
mainPage.ChangeTitleText("Temperature");
}
public static void Nav_To_Main(object sender, RoutedEventArgs e)
{
mainPage.PreventTweening();
mainPage.ChangeTitleText("Moisture");
}
public static void Nav_To_Light(object sender, RoutedEventArgs e)
{
mainPage.PreventTweening();
mainPage.ChangeTitleText("Light");
}
public static void Nav_To_Log(object sender, RoutedEventArgs e)
{
mainPage.PreventTweening();
mainPage.ChangeTitleText("Log");
}
}
}