namespace Hik_Config
{
public partial class Form1 : Form
{
private NvrSettings _nvrSettings;
public Form1(NvrSettings nvrSettings)
{
var data = JsonConvert.DeserializeObject<Model>(@".\nvrModels.json");
foreach (var model in data.Models) {
NVRModel.Items.Add(model.ModelNumber);
}
InitializeComponent();
_nvrSettings = nvrSettings;
JSONLOAD();
}
private void Loud_Btn_Click(object sender, EventArgs e)
{
JSONLOAD();
}
public void JSONLOAD()
{
IpAddressTextBox.Text = _nvrSettings.IpAddress;
UserNameTextBox.Text = _nvrSettings.UserName;
PasswordTextBox.Text = _nvrSettings.Password;
NVRModel.Text= _nvrSettings.Model;
}
private void Save_Btn_Click(object sender, EventArgs e)
{
//string json_txt = File.ReadAllText(@".\Config.json");
//NvrSettings NVR = JsonConvert.DeserializeObject<NvrSettings>(json_txt);
if (IsAddressValid(IpAddressTextBox.Text) == true)
{
_nvrSettings.IpAddress = IpAddressTextBox.Text;
_nvrSettings.UserName = UserNameTextBox.Text;
_nvrSettings.Password = PasswordTextBox.Text;
_nvrSettings.Model = NVRModel.Text;
// Wrap NvrSettings in a RootObject
RootObject rootObject = new RootObject
{
NvrSettings = _nvrSettings
};
File.WriteAllText(@".\Config.json", JsonConvert.SerializeObject(rootObject, Formatting.Indented));
MessageBox.Show("NVR Loader Config Saved Successfully", "SAVED");
Close();
Application.Exit();
}
else
{
MessageBox.Show("INVALID IP ADDRESS", "INVALID IP ADDRESS");
}
}
public bool IsAddressValid(string addrString)
// Check IP is Valid
{
string pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
Regex regex = new Regex(pattern);
return regex.IsMatch(addrString);
//IPAddress address;
//return IPAddress.TryParse(addrString, out address) && address.ToString() == addrString;
}
}
}