Skip to content

Commit b0b53cc

Browse files
committed
Added ability to disable devtool data validation. After all, it's devtool lmao.
1 parent fdbbbcd commit b0b53cc

2 files changed

Lines changed: 113 additions & 19 deletions

File tree

DevTool/Form1.Designer.cs

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DevTool/Form1.cs

Lines changed: 96 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ private void overrideTS_CheckedChanged(object sender, EventArgs e)
2222

2323
private async void mqttLogin_Click(object sender, EventArgs e)
2424
{
25+
#if DEBUG
26+
// allow fake login to test UI.
27+
if(string.IsNullOrWhiteSpace(mqttIP.Text))
28+
{
29+
formControl(true, "Connected (Emu)", Color.Green);
30+
MessageBox.Show("You're currently in emulated login mode. Certain functionality will be disable as it requires real login.", "Emulated Login", MessageBoxButtons.OK, MessageBoxIcon.Warning);
31+
return;
32+
}
33+
#endif
2534
// User Input Validation
2635

2736
if (string.IsNullOrWhiteSpace(mqttIP.Text))
@@ -118,6 +127,14 @@ private async Task mqttConnect_Event(MqttClientConnectedEventArgs e)
118127

119128
private async void mqttLogout_Click(object sender, EventArgs e)
120129
{
130+
#if DEBUG
131+
// Check if we're in emulated mode
132+
if (isEmulatedMode())
133+
{
134+
formControl(false);
135+
return;
136+
}
137+
#endif
121138
await client.DisconnectAsync();
122139
}
123140

@@ -140,36 +157,85 @@ public void formControl(bool isAuth, string? customStatText = null, Color? custo
140157
Identifier.Enabled = isAuth;
141158
overrideTS.Enabled = isAuth;
142159
sendData.Enabled = isAuth;
160+
SkipValidation.Enabled = isAuth;
161+
dateTS.Enabled = isAuth && overrideTS.Checked;
162+
timeTS.Enabled = isAuth && overrideTS.Checked;
143163
}));
144164
}
145165

146166
private async void sendData_Click(object sender, EventArgs e)
147167
{
148168
// Input Validation
149-
if (string.IsNullOrWhiteSpace(mqttTopic.Text))
169+
170+
// Start Mandatory Validation
171+
if (!double.TryParse(Temperature.Text, out double temp))
150172
{
151-
MessageBox.Show("Invalid Topic", "Bad Topic", MessageBoxButtons.OK, MessageBoxIcon.Error);
173+
MessageBox.Show("Invalid Temperature Input", "Bad Temperature", MessageBoxButtons.OK, MessageBoxIcon.Error);
152174
return;
153175
}
154-
if (
155-
!int.TryParse(Temperature.Text, out int temp) ||
156-
temp < -42 ||
157-
temp > 125
158-
)
176+
if (!double.TryParse(Humidity.Text, out double hum))
159177
{
160-
MessageBox.Show("Invalid Temperature", "Bad Temperature", MessageBoxButtons.OK, MessageBoxIcon.Error);
178+
MessageBox.Show("Invalid Humidity Input", "Bad Humidity", MessageBoxButtons.OK, MessageBoxIcon.Error);
161179
return;
162180
}
163-
if (
164-
!int.TryParse(Humidity.Text, out int hum) ||
165-
hum < 0 ||
166-
hum > 100
167-
)
181+
182+
if (!SkipValidation.Checked)
168183
{
169-
MessageBox.Show("Invalid Humidity", "Bad Humidity", MessageBoxButtons.OK, MessageBoxIcon.Error);
184+
if (string.IsNullOrWhiteSpace(mqttTopic.Text))
185+
{
186+
MessageBox.Show("Invalid Topic", "Bad Topic", MessageBoxButtons.OK, MessageBoxIcon.Error);
187+
return;
188+
}
189+
if (
190+
temp < -40 ||
191+
temp > 125
192+
)
193+
{
194+
MessageBox.Show("Invalid Temperature Value", "Bad Temperature", MessageBoxButtons.OK, MessageBoxIcon.Error);
195+
return;
196+
}
197+
if (
198+
hum < 0 ||
199+
hum > 100
200+
)
201+
{
202+
MessageBox.Show("Invalid Humidity Value", "Bad Humidity", MessageBoxButtons.OK, MessageBoxIcon.Error);
203+
return;
204+
}
205+
if (
206+
!string.IsNullOrWhiteSpace(Identifier.Text) &&
207+
Identifier.Text.Length > 92
208+
)
209+
{
210+
MessageBox.Show("Identifier too long, maximum 92 characters (though it's actually 100 but devtool reserved the other 8)", "Bad Identifier", MessageBoxButtons.OK, MessageBoxIcon.Error);
211+
return;
212+
}
213+
// Check if the date exceeded the current date
214+
if (overrideTS.Checked)
215+
{
216+
DateTime manualDate = new DateTime(
217+
dateTS.Value.Year,
218+
dateTS.Value.Month,
219+
dateTS.Value.Day,
220+
timeTS.Value.Hour,
221+
timeTS.Value.Minute,
222+
timeTS.Value.Second,
223+
timeTS.Value.Millisecond);
224+
if (manualDate > DateTime.UtcNow)
225+
{
226+
MessageBox.Show("Overridden date cannot exceed your system date", "Bad Date", MessageBoxButtons.OK, MessageBoxIcon.Error);
227+
return;
228+
}
229+
}
230+
}
231+
#if DEBUG
232+
// Check if we're in emulated mode
233+
if (isEmulatedMode())
234+
{
235+
MessageBox.Show("In non-emulated mode, data would be processed to be sent", "Valid Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
170236
return;
171237
}
172-
238+
#endif
173239
// Start Serializing Data
174240
DateTime dateTime = overrideTS.Checked ?
175241
new DateTime(dateTS.Value.Year,
@@ -188,7 +254,7 @@ private async void sendData_Click(object sender, EventArgs e)
188254
identifier = "DevTool_" + Identifier.Text,
189255
timestamp = (ulong)Math.Round(dateTime.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds)
190256
};
191-
using(MemoryStream buff = new MemoryStream())
257+
using (MemoryStream buff = new MemoryStream())
192258
{
193259
Serializer.Serialize<QueueData>(buff, data);
194260
MqttApplicationMessage msg = new MqttApplicationMessageBuilder()
@@ -200,11 +266,24 @@ private async void sendData_Click(object sender, EventArgs e)
200266
{
201267
await client.PublishAsync(msg);
202268
MessageBox.Show("Data Sent", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
203-
} catch (Exception ex)
269+
}
270+
catch (Exception ex)
204271
{
205272
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
206273
}
207274
}
208275
}
276+
277+
private void SkipValidation_CheckedChanged(object sender, EventArgs e)
278+
{
279+
dateTS.MinDate = SkipValidation.Checked ? new DateTime(1753, 1, 1) : new DateTime(1970, 1, 1);
280+
}
281+
282+
#if DEBUG
283+
private bool isEmulatedMode()
284+
{
285+
return mqttStatus.Text == "Connected (Emu)";
286+
}
287+
#endif
209288
}
210289
}

0 commit comments

Comments
 (0)