private void LoadTasks() { //Clear listview of tasks lvwTasks.Items.Clear(); foreach (string item in m_Tasks.GetTaskNames()) { Task task = m_Tasks.OpenTask(item); if (task != null) { if (task.Creator.ToLower().Contains("telldus")) { DataStorage.JobRow job = m_Data.Job.FindByID(Convert.ToInt32(task.Parameters)); if (job != null) { ListViewItem lvwItem = lvwTasks.Items.Add(task.Name, 3); lvwItem.SubItems.Add(Convert.ToDateTime(job.StartTime).ToShortTimeString()); lvwItem.SubItems.Add(task.Triggers[0].BeginDate.ToShortDateString()); lvwItem.SubItems.Add(task.Comment); lvwItem.Tag = task; } else { //Den schemalagda uppgiften / kunde inte hittas i systemet och har förmodligen tagits bort manuellt. MessageBox.Show(Localization.GetString("thescheduledtask") + " " + task.Name + " " + Localization.GetString("couldnotbefoundprobablygone"), Localization.GetString("telldusscheduler"), MessageBoxButtons.OK, MessageBoxIcon.Error); m_Tasks.DeleteTask(task.Name); } } task.Close(); } } }
static void Main(string[] args) { //If no arguments are supplied then exit. if (args.Length == 0) { MessageBox.Show("Inga argument har angivits!"); return; } try { //Makesure that we have all data loaded. DataStorage data = LoadData(); //Get the JobID. int jobID = Convert.ToInt32(args[0]); DataStorage.JobRow job = data.Job.FindByID(jobID); if (job != null) { DeviceHandler.Handler handler = new Handler(); //Load devices. handler.Load(); //Go through each action and execute it. foreach (DataStorage.JobActionsRow action in job.GetJobActionsRows()) { Device item = handler.Find(action.DeviceID); if (item != null) { if (action.TurnOn) { handler.TurnOnWithDelay(item); } else { handler.TurnOffWithDelay(item); } } else { MessageBox.Show("Device could not be found. Please make sure that TelldusService is running and no device scheduled for turn on/off has been removed.", "Telldus scheduler"); } } } else { MessageBox.Show("No scheduled jobs could be found.", "Telldus scheduler"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public frmSchedule(DataStorage data, TaskScheduler.ScheduledTasks tasks, DataStorage.JobRow job, bool newJob) { Localization.ChangeLanguage(); InitializeComponent(); m_Data = data; m_NewJob = newJob; m_Job = job; m_Tasks = tasks; }
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(); } }
private void toolEdit_Click(object sender, EventArgs e) { if (lvwTasks.SelectedItems.Count == 0) { return; } Task task = m_Tasks.OpenTask(lvwTasks.SelectedItems[0].Text); DataStorage.JobRow job = m_Data.Job.FindByID(Convert.ToInt32(task.Parameters)); task.Close(); frmSchedule frm = new frmSchedule(m_Data, m_Tasks, job, false); DialogResult result = frm.ShowDialog(this); if (result == DialogResult.OK) { //Reload all tasks LoadTasks(); } frm.Close(); }
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); } }
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; } }