-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripDetails.cs
149 lines (125 loc) · 5.17 KB
/
StripDetails.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
//--------------------------------------------------------------------------
//
// NightDriverDesktop - (c) 2024 Dave Plummer. All Rights Reserved.
//
// File: StripListView.cs
//
// Description:
//
// A ListViewItem that represents a LightStrip entry in the main strip list
//
// History: Dec-23-2023 Davepl Created
//
//---------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
namespace NightDriver
{
internal partial class StripDetails : Form
{
private LEDServer _server;
private LightStrip _strip;
internal StripDetails()
{
InitializeComponent();
}
internal StripDetails(LEDServer server, LightStrip strip) : this()
{
_server = server;
_strip = strip;
textHostName.Text = strip.HostName;
textName.Text = strip.FriendlyName;
textWidth.Text = strip.Width.ToString();
textHeight.Text = strip.Height.ToString();
textOffset.Text = strip.Offset.ToString();
textBatchSize.Text = strip.BatchSize.ToString();
checkCompress.Checked = strip.CompressData;
checkSwapRedGreen.Checked = strip.RedGreenSwap;
checkReverse.Checked = strip.Reversed;
comboChannel.SelectedIndex = strip.Channel;
if (_strip.StripSite != null)
labelLocation.Text = _strip.StripSite.Name;
}
internal bool StripDetails_Save()
{
// First we validate the inputs
// Hostname must start with a letter and contain only letters and numbers
// Regex pattern for a basic hostname (simplified)
const string hostnamePattern = @"^([a-zA-Z][a-zA-Z0-9\-]*\.)*[a-zA-Z][a-zA-Z0-9\-]*$";
// Regex pattern for IPv4 addresses
const string ipv4Pattern = @"^(\d{1,3}\.){3}\d{1,3}$";
// Regex pattern for IPv6 addresses (very simplified version)
const string ipv6Pattern = @"^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$";
// Check if the text matches any of the patterns
if (!Regex.IsMatch(textHostName.Text, hostnamePattern) && !Regex.IsMatch(textHostName.Text, ipv4Pattern) && !Regex.IsMatch(textHostName.Text, ipv6Pattern))
{
MessageBox.Show("Invalid hostname. Please enter a valid hostname or IP address.");
return false;
}
if (!textHostName.Text.Equals(_strip.HostName, StringComparison.OrdinalIgnoreCase) &&
_server.AllStrips.Any(strip => strip.HostName.Equals(textHostName.Text, StringComparison.OrdinalIgnoreCase)))
{
MessageBox.Show("A strip with that hostname already exists.");
return false;
}
if (!Regex.IsMatch(textName.Text, @"^[A-Za-z][A-Za-z0-9]*$"))
{
MessageBox.Show("Invalid name, must start with a letter and contain only letters and numbers.");
return false;
}
if (!textWidth.Text.IsInRange(1, 10000, out int width))
{
MessageBox.Show("Width must be between 1 and 10000");
return false;
}
if (!textHeight.Text.IsInRange(1, 10000, out int height))
{
MessageBox.Show("Width must be between 1 and 10000");
return false;
}
if (!textOffset.Text.IsInRange(0, 10000, out int offset))
{
MessageBox.Show("Offset must be between 0 and 10000");
return false;
}
if (!textBatchSize.Text.IsInRange(1, 1000, out int batchSize))
{
MessageBox.Show("Batch size must be between 1 and 1000");
return false;
}
// Everything validated so we can save the values, and should be safe to parse them
_strip.HostName = textHostName.Text;
_strip.FriendlyName = textName.Text;
_strip.Width = uint.Parse(textWidth.Text);
_strip.Height = uint.Parse(textHeight.Text);
_strip.Offset = uint.Parse(textOffset.Text);
_strip.BatchSize = uint.Parse(textBatchSize.Text);
_strip.CompressData = checkCompress.Checked;
_strip.RedGreenSwap = checkSwapRedGreen.Checked;
_strip.Reversed = checkReverse.Checked;
_strip.Channel = (byte)comboChannel.SelectedIndex;
return true;
}
// User has clicked the Apply button, so we save the values and close the dialog if
private void buttonApply_Click(object sender, EventArgs e)
{
if (StripDetails_Save())
{
DialogResult = DialogResult.OK;
Close();
}
}
private void StripDetails_Load(object sender, EventArgs e)
{
}
}
}