示例#1
0
        private void RemoveSchedule()
        {
            if (lvwTasks.SelectedItems.Count == 0)
            {
                return;
            }

            //"Vill du ta bort markerad schemaläggning?"
            DialogResult result = MessageBox.Show(Localization.GetString("msg2"),
                                                  Localization.GetString("telldusscheduler"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                foreach (ListViewItem item in lvwTasks.SelectedItems)
                {
                    Task task = m_Tasks.OpenTask(item.SubItems[0].Text);
                    int  id   = Convert.ToInt32(task.Parameters);
                    task.Close();

                    //First remove the task from windows scheduling
                    bool result2 = m_Tasks.DeleteTask(item.SubItems[0].Text);

                    //Second remove the entry from the Job table in the datastorage.
                    DataStorage.JobRow job = m_Data.Job.FindByID(id);

                    //Third remove all JobActions
                    DataStorage.JobActionsRow[] actions = m_Data.JobActions.Select("JobID=" + job.ID.ToString()) as DataStorage.JobActionsRow[];

                    foreach (DataStorage.JobActionsRow action in actions)
                    {
                        action.Delete();
                    }

                    //Forth remove the job
                    if (job != null)
                    {
                        job.Delete();
                    }

                    //Fifth remove the entry from the listview.
                    lvwTasks.Items.Remove(item);
                }

                m_Data.AcceptChanges();
            }
        }
示例#2
0
        private bool SetInformation()
        {
            try
            {
                if (lvwDevices.Items.Count == 0)
                {
                    MessageBox.Show(Localization.GetString("FailedToSelectUnits"), Localization.GetString("telldusscheduler"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }

                if (txtName.Text.Length == 0)
                {
                    MessageBox.Show(Localization.GetString("FailedToNameSchedule"), Localization.GetString("telldusscheduler"),
                                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return(false);
                }

                if (m_NewJob)
                {
                    frmAskUsername frm    = new frmAskUsername();
                    DialogResult   result = frm.ShowDialog(this);

                    if (result == DialogResult.Cancel)
                    {
                        return(false);
                    }

                    m_Job = m_Data.Job.NewJobRow();


                    string encrypted = EncryptDecrypt.Encrypt(frm.Password, "ThisIsNotParadise");

                    m_Job.Username = frm.Username;
                    m_Job.Password = encrypted;

                    frm.Close();
                }

                string password = EncryptDecrypt.Decrypt(m_Job.Password, "ThisIsNotParadise");

                //Set general information.
                m_Job.JobName     = txtName.Text;
                m_Job.Name        = txtName.Text;
                m_Job.Description = txtDescription.Text;

                if (!m_NewJob) //Clear previous actions and add new ones.
                {
                    foreach (DataStorage.JobActionsRow action in m_Job.GetJobActionsRows())
                    {
                        action.Delete();
                    }
                }
                m_Data.AcceptChanges();

                //Add actions.
                foreach (ListViewItem item in lvwDevices.Items)
                {
                    DataStorage.JobActionsRow action = m_Data.JobActions.NewJobActionsRow();
                    DeviceHandler.Device      device = (DeviceHandler.Device)item.Tag;

                    action.DeviceID = device.ID;
                    action.JobID    = m_Job.ID;

                    if (item.SubItems[1].Text == Localization.GetString("uniton"))
                    {
                        action.TurnOn = true;
                    }
                    else
                    {
                        action.TurnOn = false;
                    }

                    m_Data.JobActions.AddJobActionsRow(action);
                }

                TaskScheduler.Task task = null;

                //Create a new schedule in windows.
                if (m_Task != null)
                {
                    task = m_Task;
                }
                else
                {
                    task = m_Tasks.CreateTask(txtName.Text);
                }

                string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
                path = System.IO.Path.Combine(path, "DeviceSchedulerAgent.exe");

                //Clear trigger.
                if (task.Triggers != null)
                {
                    task.Triggers.Clear();
                }

                //Set general information.
                task.ApplicationName = path;
                task.Parameters      = m_Job.ID.ToString();
                task.Comment         = txtDescription.Text;

                //Set user account information.
                try
                {
                    if (password.Length == 0)
                    {
                        task.SetAccountInformation(m_Job.Username, null);
                    }
                    else
                    {
                        task.SetAccountInformation(m_Job.Username, password);
                    }
                }
                catch
                {
                    MessageBox.Show(Localization.GetString("FailedToSetPassword"),
                                    Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                //Set Telldus as creator so we can recognize the task next time.
                task.Creator = "Telldus";

                m_Job.StartTime = dtpTime.Value.Hour.ToString() + ":" + dtpTime.Value.Minute.ToString();

                dtpTime.Value = DateTime.Parse(dtpDate.Value.ToShortDateString() + " " + dtpTime.Value.ToShortTimeString());

                DateTime time   = dtpTime.Value;
                short    hour   = short.Parse(dtpTime.Value.Hour.ToString());
                short    minute = short.Parse(dtpTime.Value.Minute.ToString());

                short interval = 1;

                switch (cboSchedule.SelectedIndex)
                {
                case 0:     //Dailytrigger
                    interval = short.Parse(nupDay.Value.ToString());
                    TaskScheduler.DailyTrigger dayTrigger = new TaskScheduler.DailyTrigger(hour, minute, interval);
                    dayTrigger.BeginDate = dtpDate.Value;
                    //trigger.BeginDate.AddHours(Convert.ToDouble(dtpTime.Value.Hour));
                    //trigger.BeginDate.AddMinutes(Convert.ToDouble(dtpTime.Value.Minute));
                    dayTrigger.Disabled = false;
                    task.Triggers.Add(dayTrigger);
                    break;

                case 1:     //Weeklytrigger
                    TaskScheduler.DaysOfTheWeek weekDays = 0;
                    interval = short.Parse(nupWeek.Value.ToString());

                    if (cboWeekMonday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Monday;
                    }

                    if (cboWeekTuesday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Tuesday;
                    }

                    if (cboWeekWednesday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Wednesday;
                    }

                    if (cboWeekThursday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Thursday;
                    }

                    if (cboWeekFriday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Friday;
                    }

                    if (cboWeekSaturday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Saturday;
                    }

                    if (cboWeekSunday.Checked)
                    {
                        weekDays = weekDays | TaskScheduler.DaysOfTheWeek.Sunday;
                    }

                    TaskScheduler.WeeklyTrigger weeklyTrigger = new TaskScheduler.WeeklyTrigger(hour, minute, weekDays, interval);
                    time = DateTime.Parse(time.ToShortDateString() + " " + dtpTime.Value.Hour + ":" + dtpTime.Value.Minute + ":00");
                    //weeklyTrigger.BeginDate = time;
                    //weeklyTrigger.BeginDate=weeklyTrigger.BeginDate.AddHours(Convert.ToDouble(dtpTime.Value.Hour));
                    //weeklyTrigger.BeginDate=weeklyTrigger.BeginDate.AddMinutes(Convert.ToDouble(dtpTime.Value.Minute));
                    task.Triggers.Add(weeklyTrigger);
                    break;

                case 2:     //Monthlytrigger
                    //interval = int.Parse(nupmo

                    TaskScheduler.MonthsOfTheYear months = 0;

                    if (cboMonthJan.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.January;
                    }

                    if (cboMonthFeb.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.February;
                    }

                    if (cboMonthMar.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.March;
                    }

                    if (cboMonthApr.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.April;
                    }

                    if (cboMonthMay.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.May;
                    }

                    if (cboMonthJune.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.June;
                    }

                    if (cboMonthJuly.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.July;
                    }

                    if (cboMonthAug.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.August;
                    }

                    if (cboMontSept.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.September;
                    }

                    if (cboMontOct.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.October;
                    }

                    if (cboMonthNov.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.November;
                    }

                    if (cboMontDec.Checked)
                    {
                        months = months | TaskScheduler.MonthsOfTheYear.December;
                    }

                    int[] days = new int[1] {
                        int.Parse(nupMonth.Value.ToString())
                    };
                    TaskScheduler.MonthlyTrigger monthlyTrigger = new TaskScheduler.MonthlyTrigger(hour, minute, days, months);
                    monthlyTrigger.BeginDate = dtpDate.Value;
                    //trigger.BeginDate.AddMinutes(Convert.ToDouble(dtpTime.Value.Minute));
                    task.Triggers.Add(monthlyTrigger);
                    break;

                case 3:
                    TaskScheduler.RunOnceTrigger onceTrigger = new TaskScheduler.RunOnceTrigger(dtpDate.Value);
                    onceTrigger.StartHour   = hour;
                    onceTrigger.StartMinute = minute;
                    task.Triggers.Add(onceTrigger);
                    break;
                }


                task.Triggers[0].BeginDate = time;

                task.Save();
                task.Close();

                if (m_NewJob)
                {
                    m_Data.Job.AddJobRow(m_Job);
                }

                m_Data.AcceptChanges();

                return(true);
            }
            catch (Exception ex)
            {
                //ett ohanterat fel har uppstått i programmet.
                MessageBox.Show(Localization.GetString("msg1") + Environment.NewLine + ex.Message,
                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return(false);
            }
        }