private async Task Populate() { UseWaitCursor = true; foreach (Control ctl in Controls) { ctl.Enabled = false; } AccessProperties accessprops = await GetAccessPropertiesFromDatabase(); if (accessprops != null) { string description = await DatabaseManager.GetDescription(dbconnprop, table_name, uid); textBox1.Text = description; checkBox1.Checked = accessprops.ForceEnable; checkBox2.Checked = accessprops.ForceDisable; dateTimePicker1.Value = accessprops.EnabledFrom; dateTimePicker2.Value = accessprops.EnabledFrom; dateTimePicker3.Value = accessprops.EnabledTo; dateTimePicker4.Value = accessprops.EnabledTo; listBox1.DataSource = null; listBox1.DataSource = accessprops.ActivationProperties; } foreach (Control ctl in Controls) { ctl.Enabled = true; } UseWaitCursor = false; }
private async Task <AccessProperties> GetAccessPropertiesFromDatabase() { AccessProperties accessprops = await DatabaseManager.GetAccessProperties(dbconnprop, table_name, uid); if (accessprops == null) { accessprops = new AccessProperties(DateTime.Now, DateTime.Now, new List <ActivationProperties>(), false, false); await WriteAccessPropertiesToDatabase(accessprops); } return(await DatabaseManager.GetAccessProperties(dbconnprop, table_name, uid)); }
private async void button3_Click(object sender, EventArgs e) { //delete activation property if (listBox1.SelectedIndex > -1) { AccessProperties accessprop = await GetAccessPropertiesFromDatabase(); accessprop.ActivationProperties.RemoveAt(listBox1.SelectedIndex); await WriteAccessPropertiesToDatabase(accessprop); await Populate(); } }
private async void button1_Click(object sender, EventArgs e) { //add activation property ActivationPropertiesEditor actiproped = new ActivationPropertiesEditor(); actiproped.ShowDialog(); if (actiproped.DialogResult == DialogResult.OK) { AccessProperties accessprop = await GetAccessPropertiesFromDatabase(); accessprop.ActivationProperties.Add(actiproped.ActivationProperties); await WriteAccessPropertiesToDatabase(accessprop); await Populate(); } actiproped.Dispose(); }
private async Task <bool> AddCard(UInt64 uid, string table_name, int default_option) { AccessProperties accessprop = GenerateDefaults(default_option); if (!await DatabaseManager.ContainsUID(dbconnprop, table_name, uid)) { await DatabaseManager.AddRow(dbconnprop, table_name, uid, accessprop); MessageBox.Show(this, "Successfully added card to database."); return(true); } else if (MessageBox.Show(this, "UID exists in database. Overwrite?", "Error", MessageBoxButtons.OKCancel) == DialogResult.OK) { await DatabaseManager.SetAccessProperties(dbconnprop, table_name, uid, accessprop); MessageBox.Show(this, "Successfully added card to database."); return(true); } return(false); }
private async void button4_Click(object sender, EventArgs e) { //save UseWaitCursor = true; foreach (Control ctl in Controls) { ctl.Enabled = false; } await DatabaseManager.SetDescription(dbconnprop, table_name, uid, textBox1.Text.Trim() == ""?null : textBox1.Text.Trim()); AccessProperties accessprop = await GetAccessPropertiesFromDatabase(); accessprop.ForceEnable = checkBox1.Checked; accessprop.ForceDisable = checkBox2.Checked; accessprop.EnabledFrom = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day, dateTimePicker2.Value.Hour, dateTimePicker2.Value.Minute, dateTimePicker2.Value.Second); accessprop.EnabledTo = new DateTime(dateTimePicker3.Value.Year, dateTimePicker3.Value.Month, dateTimePicker3.Value.Day, dateTimePicker4.Value.Hour, dateTimePicker4.Value.Minute, dateTimePicker4.Value.Second); await WriteAccessPropertiesToDatabase(accessprop); DialogResult = DialogResult.OK; }
private async void button2_Click(object sender, EventArgs e) { //edit activation property int selected_index = listBox1.SelectedIndex; if (selected_index > -1) { ActivationPropertiesEditor actiproped = new ActivationPropertiesEditor((ActivationProperties)listBox1.SelectedItem); actiproped.ShowDialog(); if (actiproped.DialogResult == DialogResult.OK) { AccessProperties accessprop = await GetAccessPropertiesFromDatabase(); accessprop.ActivationProperties[selected_index] = actiproped.ActivationProperties; WriteAccessPropertiesToDatabase(accessprop); await Populate(); } actiproped.Dispose(); } }
public static Task SetAccessProperties(DatabaseConnectionProperties dbconnprop, string table_name, UInt64 uid, AccessProperties accessprops) { return(Task.Run(() => { using (MySqlConnection sqlconn = new MySqlConnection(dbconnprop.ConnectionString)) { sqlconn.Open(); using (MySqlCommand cmdName = new MySqlCommand("update `" + table_name + "` set data = @data where uid=" + uid, sqlconn)) { cmdName.Parameters.AddWithValue("@data", accessprops == null ? null : Utilities.ConvertObjectToByteArray <AccessProperties>(accessprops)); cmdName.ExecuteNonQuery(); } } })); }
public static Task AddRow(DatabaseConnectionProperties dbconnprop, string table_name, UInt64 uid, string description, string[] notes, AccessProperties accessprop) { return(Task.Run(() => { using (MySqlConnection sqlconn = new MySqlConnection(dbconnprop.ConnectionString)) { sqlconn.Open(); using (MySqlCommand sqlcmd = new MySqlCommand("insert into `" + table_name + "` (uid, description, notes, data) values (@uid, @description, @notes, @data)", sqlconn)) { sqlcmd.Parameters.AddWithValue("@uid", uid); sqlcmd.Parameters.AddWithValue("@description", description); sqlcmd.Parameters.AddWithValue("@notes", notes == null ? null : Utilities.ConvertObjectToByteArray <string[]>(notes)); sqlcmd.Parameters.AddWithValue("@data", accessprop == null ? null : Utilities.ConvertObjectToByteArray <AccessProperties>(accessprop)); sqlcmd.ExecuteNonQuery(); } } })); }
public static Task AddRow(DatabaseConnectionProperties dbconnprop, string table_name, UInt64 uid, AccessProperties accessprop) { return(AddRow(dbconnprop, table_name, uid, null, null, accessprop)); }
private Task WriteAccessPropertiesToDatabase(AccessProperties accessprop) { return(DatabaseManager.SetAccessProperties(dbconnprop, table_name, uid, accessprop)); }
private void ProcessCSV(string filename, string tablename) { using (StreamReader csv_reader = new StreamReader(filename)) using (MySqlConnection mysqlconnection = new MySqlConnection()) { mysqlconnection.ConnectionString = dbconnprop.ConnectionString; mysqlconnection.Open(); while (true) { string line_to_parse = csv_reader.ReadLine(); if (line_to_parse != null) { UInt64 card_id; string entry_times, entry_days, available_floors; //get the values card_id = UInt64.Parse(line_to_parse.Substring(0, line_to_parse.IndexOf(',')).Trim()); line_to_parse = line_to_parse.Substring(line_to_parse.IndexOf(',') + 1); entry_times = line_to_parse.Substring(0, line_to_parse.IndexOf(',')).Trim(); line_to_parse = line_to_parse.Substring(line_to_parse.IndexOf(',') + 1); entry_days = line_to_parse.Substring(0, line_to_parse.IndexOf(',')).Trim(); line_to_parse = line_to_parse.Substring(line_to_parse.IndexOf(',') + 1); available_floors = line_to_parse.Trim(); //// //parse entry times uint start_hour, start_minute, end_hour, end_minute; start_hour = uint.Parse(entry_times.Substring(0, entry_times.IndexOf(':')).Trim()); entry_times = entry_times.Substring(entry_times.IndexOf(':') + 1); start_minute = uint.Parse(entry_times.Substring(0, entry_times.IndexOf('-')).Trim()); entry_times = entry_times.Substring(entry_times.IndexOf('-') + 1); end_hour = uint.Parse(entry_times.Substring(0, entry_times.IndexOf(':')).Trim()); entry_times = entry_times.Substring(entry_times.IndexOf(':') + 1); end_minute = uint.Parse(entry_times.Trim()); //// //parse entry days byte converted_entry_days = 0; while (true) { if (entry_days.IndexOf('-') == -1 && entry_days.IndexOf(':') == -1) { converted_entry_days |= dayOfWeekToBytePos(entry_days); break; //done :) } else { if (entry_days.IndexOf('-') > -1 && entry_days.IndexOf(':') > -1) { if (entry_days.IndexOf('-') > entry_days.IndexOf(':')) { //so... looks like the colon is closer converted_entry_days |= dayOfWeekToBytePos(entry_days.Substring(0, entry_days.IndexOf(':'))); entry_days = entry_days.Substring(entry_days.IndexOf(':') + 1); } else { //it seems like the dash is closer int first_day, second_day; first_day = getDayOfWeekPos(entry_days.Substring(0, entry_days.IndexOf('-')).Trim()); entry_days = entry_days.Substring(entry_days.IndexOf('-') + 1); //there must be a colon coming up so we can use that second_day = getDayOfWeekPos(entry_days.Substring(0, entry_days.IndexOf(':')).Trim()); entry_days = entry_days.Substring(entry_days.IndexOf(':') + 1); for (; first_day <= second_day; first_day++) { converted_entry_days |= dayOfWeekToBytePos(days_of_the_week[first_day]); } } } else if (entry_days.IndexOf('-') > -1) { //there's a dash but no colon int first_day, second_day; first_day = getDayOfWeekPos(entry_days.Substring(0, entry_days.IndexOf('-')).Trim()); entry_days = entry_days.Substring(entry_days.IndexOf('-') + 1); //you shouldn't have another dash after this one and there is no colon second_day = getDayOfWeekPos(entry_days.Trim()); for (; first_day <= second_day; first_day++) { converted_entry_days |= dayOfWeekToBytePos(days_of_the_week[first_day]); } } else { //so if we're here there's a colon but no dash while (entry_days.IndexOf(':') > -1) { converted_entry_days |= dayOfWeekToBytePos(entry_days.Substring(0, entry_days.IndexOf(':'))); entry_days = entry_days.Substring(entry_days.IndexOf(':') + 1); } } } } //// //parse floors uint converted_available_floors = 0; while (true) { if (available_floors.IndexOf('-') == -1 && available_floors.IndexOf(':') == -1) { converted_available_floors |= floorToUintPos(uint.Parse(available_floors.Trim())); break; } else if (available_floors.IndexOf('-') > -1 && available_floors.IndexOf(':') > -1) { if (available_floors.IndexOf('-') > available_floors.IndexOf(':')) { //so... looks like the colon is closer converted_available_floors |= floorToUintPos(uint.Parse(available_floors.Substring(0, available_floors.IndexOf(':')))); available_floors = available_floors.Substring(available_floors.IndexOf(':') + 1); } else { //the dash is closer... uint first_floor, second_floor; first_floor = uint.Parse(available_floors.Substring(0, available_floors.IndexOf('-')).Trim()); available_floors = available_floors.Substring(available_floors.IndexOf('-') + 1); //there must be a colon coming up so we can use that second_floor = uint.Parse(available_floors.Substring(0, available_floors.IndexOf(':')).Trim()); for (; first_floor <= second_floor; first_floor++) { converted_available_floors |= floorToUintPos(first_floor); } } } else if (available_floors.IndexOf('-') > -1) { //there's a dash but no colon uint first_floor, second_floor; first_floor = uint.Parse(available_floors.Substring(0, available_floors.IndexOf('-')).Trim()); available_floors = available_floors.Substring(available_floors.IndexOf('-') + 1); //you shouldn't have another dash after this one and there is no colon second_floor = uint.Parse(available_floors.Trim()); for (; first_floor <= second_floor; first_floor++) { converted_available_floors |= floorToUintPos(first_floor); } } else { //got it. colon. ;) converted_available_floors |= floorToUintPos(uint.Parse(available_floors.Substring(0, available_floors.IndexOf(':')))); available_floors = available_floors.Substring(available_floors.IndexOf(':') + 1); } } //// //done with the conversions for this line. add it to the database now //generate the accessproperties object DateTime begin_enable = DateTime.Now; DateTime end_enable = DateTime.Now.AddYears(1); List <ActivationProperties> actiprops = new List <ActivationProperties>(); //only compare the day of week bool[] comparison_flags = { false, false, false, true, false, false, false }; int activation_duration = 6; bool[] exp0mask = new bool[16]; bool[] exp0val = new bool[16]; bool[] exp1mask = new bool[16]; bool[] exp1val = new bool[16]; uint floor_mask = 8192; for (int bit_counter = 0; bit_counter < 14; bit_counter++) { if ((converted_available_floors & (floor_mask >> bit_counter)) > 0) { exp0mask[13 - bit_counter] = true; exp0val[13 - bit_counter] = true; exp1mask[13 - bit_counter] = true; exp1val[13 - bit_counter] = true; } } exp0mask[0] = false; exp1mask[0] = false; List <DateTime> activation_days = new List <DateTime>(); byte mask = 64; for (int bit_counter = 0; bit_counter < 7; bit_counter++) { if ((converted_entry_days & (mask >> bit_counter)) > 0) { activation_days.Add(GetNextWeekday((DayOfWeek)bit_counter)); } } foreach (DateTime dt in activation_days) { actiprops.Add(new ActivationProperties(dt, dt, exp0mask, exp1mask, exp0val, exp1val, comparison_flags, activation_duration)); } AccessProperties accessprop = new AccessProperties(begin_enable, end_enable, actiprops, false, false); //add the entry byte[] data; using (var ms = new MemoryStream()) { BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(ms, accessprop); data = ms.ToArray(); using (MySqlCommand sqlcmd = new MySqlCommand("insert into `" + tablename + "` (uid, data) values (@uid, @data)", mysqlconnection)) { sqlcmd.Parameters.AddWithValue("@uid", card_id); sqlcmd.Parameters.AddWithValue("@data", data); sqlcmd.ExecuteNonQuery(); } } } else { break; } } } }