// Update Plug base information by PlugID public static bool UpdatePlugBasicInfoByPlugID(PlugDevice plugdev, string dbPath) { try { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath + ";")) { conn.Open(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = @"UPDATE PlugList set PlugName=@PlugName1, PlugMac=@PlugMac1, UserCode=@UserCode1, Voltage=@Voltage1, Power=@Power1, DayBudget=@DayBudget1, Description=@Description1 where PlugID=@PlugID1;"; cmd.Parameters.Add(new SQLiteParameter("PlugID1", plugdev.PlugID)); cmd.Parameters.Add(new SQLiteParameter("PlugName1", plugdev.PlugName)); cmd.Parameters.Add(new SQLiteParameter("PlugMac1", plugdev.PlugMac)); cmd.Parameters.Add(new SQLiteParameter("UserCode1", plugdev.UserCode)); cmd.Parameters.Add(new SQLiteParameter("Voltage1", plugdev.Voltage)); cmd.Parameters.Add(new SQLiteParameter("Power1", plugdev.Power)); cmd.Parameters.Add(new SQLiteParameter("DayBudget1", plugdev.DayBudget)); cmd.Parameters.Add(new SQLiteParameter("Description1", plugdev.Description)); int i = cmd.ExecuteNonQuery(); return(i == 1); } } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace); return(false); } catch (Exception ex) { //Do any logging operation here if necessary MessageBox.Show(ex.Message); return(false); } }
private void btnUpdateSche_Click(object sender, RoutedEventArgs e) { try { if (NightBudgetTextBox.Text == "" || MorningBudgetTextBox.Text == "" || AfternoonBudgetTextBox.Text == "" || EveningBudgetTextBox.Text == "") { MessageBox.Show("请输入预算使用的电能量!", "提示", MessageBoxButton.OK); return; } int selectindex = DeviceScheList.SelectedIndex; if (selectindex != -1) { PlugDevice plugdev = new PlugDevice(); plugdev.PlugID = Convert.ToInt64(PlugDeviceTable.Rows[selectindex]["PlugID"]); plugdev.NightBudget = Convert.ToSingle(NightBudgetTextBox.Text); plugdev.MorningBudget = Convert.ToSingle(MorningBudgetTextBox.Text); plugdev.AfternoonBudget = Convert.ToSingle(AfternoonBudgetTextBox.Text); plugdev.EveningBudget = Convert.ToSingle(AfternoonBudgetTextBox.Text); plugdev.NightSaving = NightSavingSwitch.IsChecked; plugdev.MorningSaving = MorningSavingSwitch.IsChecked; plugdev.AfternoonSaving = AfternoonSavingSwitch.IsChecked; plugdev.EveningSaving = EveningSavingSwitch.IsChecked; bool success = SmartPlugDAL.UpdatePlugScheduleInfoByPlugID(plugdev, SqliteDBPath); if (success == true) { PlugDeviceTable = SmartPlugDAL.GetAllPlugDevice(SqliteDBPath, ref success); BudgettedEnergy[0] = Convert.ToSingle(NightBudgetTextBox.Text); BudgettedEnergy[1] = Convert.ToSingle(MorningBudgetTextBox.Text); BudgettedEnergy[2] = Convert.ToSingle(AfternoonBudgetTextBox.Text); BudgettedEnergy[3] = Convert.ToSingle(EveningBudgetTextBox.Text); UpdateBudgettedUsedChart(); MessageBox.Show("更新成功!", "提示", MessageBoxButton.OK); } else { MessageBox.Show("无法更新,请检查设置!", "提示", MessageBoxButton.OK); } } } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace); } catch (Exception ex) { //Do any logging operation here if necessary throw new Exception(ex.Message); } }
// Get Plug by PlugID public static PlugDevice GetPlugDeviceByPlugID(Int64 plugid, string dbPath) { try { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath + ";")) { conn.Open(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from PlugList where PlugID=@PlugID;"; cmd.Parameters.Add(new SQLiteParameter("PlugID", plugid)); SQLiteDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { PlugDevice plugdev = new PlugDevice(); plugdev.PlugID = dr.GetInt64(0); plugdev.PlugName = dr.GetString(1); plugdev.PlugMac = dr.GetString(2); plugdev.UserCode = (UInt16)dr.GetInt32(3); plugdev.Voltage = dr.GetFloat(4); plugdev.Power = dr.GetFloat(5); plugdev.DayBudget = dr.GetFloat(6); plugdev.NightBudget = dr.GetFloat(7); plugdev.MorningBudget = dr.GetFloat(8); plugdev.AfternoonBudget = dr.GetFloat(9); plugdev.EveningBudget = dr.GetFloat(10); plugdev.NightSaving = dr.GetBoolean(11); plugdev.MorningSaving = dr.GetBoolean(12); plugdev.AfternoonSaving = dr.GetBoolean(13); plugdev.EveningSaving = dr.GetBoolean(14); plugdev.Description = dr.GetString(15); plugdev.CreateDateTime = dr.GetDateTime(16); return(plugdev); } else { return(null); } } } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace); return(null); } catch (Exception ex) { //Do any logging operation here if necessary throw new Exception(ex.Message); } }
// Add Plug to DataBase public static bool AddPlugDevice(PlugDevice plugdev, string dbPath) { try { SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath + ";"); conn.Open(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = @"INSERT INTO PlugList(PlugID, PlugName, PlugMac, UserCode, Voltage, Power, DayBudget, NightBudget, MorningBudget, AfternoonBudget, EveningBudget, NightSaving, MorningSaving, AfternoonSaving, EveningSaving, Description, CreateDateTime) VALUES(@PlugID1, @PlugName1, @PlugMac1, @UserCode1, @Voltage1, @Power1, @DayBudget1, @NightBudget1, @MorningBudget1, @AfternoonBudget1, @EveningBudget1, @NightSaving1, @MorningSaving1, @AfternoonSaving1, @EveningSaving1, @Description1, @CreateDateTime1)"; cmd.Parameters.Add(new SQLiteParameter("PlugID1", plugdev.PlugID)); cmd.Parameters.Add(new SQLiteParameter("PlugName1", plugdev.PlugName)); cmd.Parameters.Add(new SQLiteParameter("PlugMac1", plugdev.PlugMac)); cmd.Parameters.Add(new SQLiteParameter("UserCode1", plugdev.UserCode)); cmd.Parameters.Add(new SQLiteParameter("Voltage1", plugdev.Voltage)); cmd.Parameters.Add(new SQLiteParameter("Power1", plugdev.Power)); cmd.Parameters.Add(new SQLiteParameter("DayBudget1", plugdev.DayBudget)); cmd.Parameters.Add(new SQLiteParameter("NightBudget1", plugdev.NightBudget)); cmd.Parameters.Add(new SQLiteParameter("MorningBudget1", plugdev.MorningBudget)); cmd.Parameters.Add(new SQLiteParameter("AfternoonBudget1", plugdev.AfternoonBudget)); cmd.Parameters.Add(new SQLiteParameter("EveningBudget1", plugdev.EveningBudget)); cmd.Parameters.Add(new SQLiteParameter("NightSaving1", plugdev.NightSaving)); cmd.Parameters.Add(new SQLiteParameter("MorningSaving1", plugdev.MorningSaving)); cmd.Parameters.Add(new SQLiteParameter("AfternoonSaving1", plugdev.AfternoonSaving)); cmd.Parameters.Add(new SQLiteParameter("EveningSaving1", plugdev.EveningSaving)); cmd.Parameters.Add(new SQLiteParameter("Description1", plugdev.Description)); cmd.Parameters.Add(new SQLiteParameter("CreateDateTime1", plugdev.CreateDateTime)); int i = cmd.ExecuteNonQuery(); return(i == 1); } catch (SQLiteException se) { MessageBox.Show(se.Message + " \n\n" + se.Source + "\n\n" + se.StackTrace + "\n\n" + se.Data); return(false); } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace + "\n\n" + ae.Data); return(false); } catch (Exception ex) { //Do any logging operation here if necessary MessageBox.Show(ex.Message + "\n\n" + ex.Source + "\n\n" + ex.StackTrace + "\n\n" + ex.Data); return(false); } }
//更新插座表 private void btnUpdateDev_Click(object sender, RoutedEventArgs e) { try { if (Add_IDTextBox.Text != "") { PlugDevice plugdev = new PlugDevice(); plugdev.PlugID = Convert.ToInt64(Add_IDTextBox.Text); plugdev.PlugName = Add_NameTextBox.Text; plugdev.PlugMac = Add_MACTextBox.Text; plugdev.UserCode = (UInt16)(256 * Convert.ToUInt16(Add_UserCodeTextBox1.Text) + Convert.ToUInt16(Add_UserCodeTextBox2.Text)); plugdev.Voltage = Convert.ToSingle(Add_VoltageTextBox.Text); plugdev.Power = Convert.ToSingle(Add_PowerTextBox.Text); plugdev.DayBudget = Convert.ToSingle(Add_BudgettedEnergyTextBox.Text); plugdev.Description = Add_DescriptionTextBox.Text; bool success = SmartPlugDAL.UpdatePlugBasicInfoByPlugID(plugdev, SqliteDBPath); if (success == true) { RefreshPlugInfo(); MessageBox.Show("更新成功!", "提示", MessageBoxButton.OK); } else { MessageBox.Show("更新失败,请检查设置!", "提示", MessageBoxButton.OK); } } } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace); } catch (Exception ex) { //Do any logging operation here if necessary throw new Exception(ex.Message); } }
// Update Plug Schedule infor by PlugID public static bool UpdatePlugScheduleInfoByPlugID(PlugDevice plugdev, string dbPath) { try { using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + dbPath + ";")) { conn.Open(); SQLiteCommand cmd = conn.CreateCommand(); cmd.CommandText = @"UPDATE PlugList set NightBudget=@NightBudget1, MorningBudget=@MorningBudget1, AfternoonBudget=@AfternoonBudget1, EveningBudget=@EveningBudget1, NightSaving=@NightSaving1, MorningSaving=@MorningSaving1, AfternoonSaving=@AfternoonSaving1, EveningSaving=@EveningSaving1 where PlugID=@PlugID1;"; cmd.Parameters.Add(new SQLiteParameter("PlugID1", plugdev.PlugID)); cmd.Parameters.Add(new SQLiteParameter("NightBudget1", plugdev.NightBudget)); cmd.Parameters.Add(new SQLiteParameter("MorningBudget1", plugdev.MorningBudget)); cmd.Parameters.Add(new SQLiteParameter("AfternoonBudget1", plugdev.AfternoonBudget)); cmd.Parameters.Add(new SQLiteParameter("EveningBudget1", plugdev.EveningBudget)); cmd.Parameters.Add(new SQLiteParameter("NightSaving1", plugdev.NightSaving)); cmd.Parameters.Add(new SQLiteParameter("MorningSaving1", plugdev.MorningSaving)); cmd.Parameters.Add(new SQLiteParameter("AfternoonSaving1", plugdev.AfternoonSaving)); cmd.Parameters.Add(new SQLiteParameter("EveningSaving1", plugdev.EveningSaving)); int i = cmd.ExecuteNonQuery(); return(i == 1); } } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace); return(false); } catch (Exception ex) { //Do any logging operation here if necessary MessageBox.Show(ex.Message); return(false); } }
//添加插座 private void btnAddDev_Click(object sender, RoutedEventArgs e) { try { if (Add_NameTextBox.Text == "") { MessageBox.Show("建议不要将名称设置为空!", "提示", MessageBoxButton.OK); } if (Add_IDTextBox.Text == "") { MessageBox.Show("ID不能为空!", "提示", MessageBoxButton.OK); return; } if (Add_MACTextBox.Text == "") { MessageBox.Show("建议不要将MAC地址设置为空!", "提示", MessageBoxButton.OK); } if (Add_UserCodeTextBox1.Text == "" || Add_UserCodeTextBox2.Text == "") { MessageBox.Show("建议不要将用户码设置为空!", "提示", MessageBoxButton.OK); } if (Add_VoltageTextBox.Text == "") { MessageBox.Show("建议不要将额定电压设置为空!", "提示", MessageBoxButton.OK); } if (Add_PowerTextBox.Text == "") { MessageBox.Show("建议不要将额定功率设置为空!", "提示", MessageBoxButton.OK); } PlugDevice plugdev = new PlugDevice(); plugdev.PlugID = Convert.ToInt64(Add_IDTextBox.Text); plugdev.PlugName = Add_NameTextBox.Text; plugdev.PlugMac = Add_MACTextBox.Text; plugdev.UserCode = (UInt16)(256 * Convert.ToUInt16(Add_UserCodeTextBox1.Text) + Convert.ToUInt16(Add_UserCodeTextBox2.Text)); plugdev.Voltage = Convert.ToSingle(Add_VoltageTextBox.Text); plugdev.Power = Convert.ToSingle(Add_PowerTextBox.Text); plugdev.DayBudget = Convert.ToSingle(Add_BudgettedEnergyTextBox.Text); plugdev.Description = Add_DescriptionTextBox.Text; plugdev.CreateDateTime = DateTime.Now; bool success = SmartPlugDAL.AddPlugDevice(plugdev, SqliteDBPath); if (success == true) { RefreshPlugInfo(); //更新列表 AddedDeviceList.SelectedIndex = PlugDeviceTable.Rows.Count - 1; MessageBox.Show("成功添加该插座!", "提示", MessageBoxButton.OK); } else { MessageBox.Show("无法添加该插座,请检查设置!", "提示", MessageBoxButton.OK); } } catch (ArgumentException ae) { MessageBox.Show(ae.Message + " \n\n" + ae.Source + "\n\n" + ae.StackTrace); } catch (Exception ex) { //Do any logging operation here if necessary throw new Exception(ex.Message); } }