-
Notifications
You must be signed in to change notification settings - Fork 2
/
Globals.cs
168 lines (143 loc) · 5.8 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
167
168
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using Windows.Globalization.Collation;
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()
{
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)
{
List<int> listArgs = GetIntValList(msg);
foreach (var integer in listArgs)
{
Debug.WriteLine(integer);
}
Debug.WriteLine("First Word: " + FirstWordFromMsg(msg));
var firstWord = FirstWordFromMsg(msg);
//switch (msg)
switch (firstWord)
{
// ---------------------------------------- TWEENING
case "tween":
mainPage.TweenGauge(listArgs[0], listArgs[1], listArgs[2], listArgs[3]);
Debug.WriteLine(firstWord + ": " + listArgs[0] + " " + listArgs[1] + " " + listArgs[2] + " " + listArgs[3]);
break;
// ---------------------------------------- TEMPERATURE
case "temp":
mainPage.ChangeTitleText("Temperature");
mainPage.Goto(listArgs[0]);
Debug.WriteLine(firstWord + ": " + listArgs[0]);
break;
// ---------------------------------------- LIGHT
case "light":
mainPage.ChangeTitleText("Light");
mainPage.Goto(listArgs[0]);
Debug.WriteLine(firstWord + ": " + listArgs[0]);
break;
// ---------------------------------------- Moisture
case "moisture":
mainPage.ChangeTitleText("Moisture");
mainPage.Goto(listArgs[0]);
Debug.WriteLine(firstWord + ": " + listArgs[0]);
break;
// ---------------------------------------- NAVIGATION
case "nav to 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>
/// Used to parse functions from console message. Use in tandem w/ GetIntValList, which will
/// place parameters within function
/// </summary>
/// <param name="msg"></param>
/// <returns>First word from the console message</returns>
private static string FirstWordFromMsg(string msg)
{
var firstWord = msg.Substring(0, msg.IndexOf(" ", StringComparison.Ordinal));
return firstWord;
}
/// <summary>
/// Parse message sent from console app to IoT device
/// </summary>
/// <param name="msg">String sent from console app</param>
/// <returns>Each word in the string, separately</returns>
private static string stringValFromMsg(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
* ==========================================================*/
public static void Nav_To_Temp(object sender, RoutedEventArgs e)
{
mainPage.ChangeTitleText("Temperature");
}
public static void Nav_To_Main(object sender, RoutedEventArgs e)
{
mainPage.ChangeTitleText("Moisture");
}
public static void Nav_To_Light(object sender, RoutedEventArgs e)
{
mainPage.ChangeTitleText("Light");
}
public static void Nav_To_Log(object sender, RoutedEventArgs e)
{
mainPage.ChangeTitleText("Log");
}
}
}