/// <summary> /// Set the sequence to the specified sequence and type to Fast. /// </summary> /// <param name="seq">The sequence to be set.</param> internal static void Set(SequenceList seq) { Log.Write("Trying to change sequence."); if (currentSequence == seq) { OnErrorEvent(seq == SequenceList.NoShutdown ? "Already unset any sequence." : "There is a sequence already set."); return; } if (currentSequence > SequenceList.NoShutdown && seq > SequenceList.NoShutdown) { OnErrorEvent("There is a sequence already set."); return; } if (seq < SequenceList.NoShutdown || seq > SequenceList.LockUser) { OnErrorEvent("Setting to an invalid sequence."); return; } Stop(); if (seq == SequenceList.NoShutdown) { currentType = SequenceType.None; Log.Write("Successfully unset current sequence."); } else { alarm = new Alarm(2000); alarm.Expire += () => { ExecuteCommand(); Reset(); }; alarm.Start(); currentType = SequenceType.Fast; if (Config.Equals("alert", "1")) { AlertBox.Show("Sequence Changed!", "Will " + seq + " in a moment.", 1800); } Log.Write("Set sequence to " + currentType + " " + seq + "."); } currentSequence = seq; OnSequenceChangeEvent(new SequenceChangeEventArgs(CurrentSequence, CurrentType)); }
/// <summary> /// Invokes the message event with the specified error message. /// </summary> /// <param name="message">The error message of the event.</param> private static void OnErrorEvent(string message) { Log.Write(message); if (Config.Equals("alert", "1")) { AlertBox.Show("Sequence Change Error!", message); } if (Requested) { Server.Send(new Data("sequence", "-1").Add(message)); Requested = false; } Error?.Invoke(message); }
/// <summary> /// Set the sequence to the specified sequence and its type to Scheduled. /// Will execute on the date provided. /// </summary> /// <param name="seq">The sequence to be set.</param> /// <param name="date">DateTime instance of the date when will execute.</param> internal static void Set(SequenceList seq, MyDate date) { Log.Write("Trying to change sequence."); if (currentSequence > SequenceList.NoShutdown && seq > SequenceList.NoShutdown) { OnErrorEvent("There is a sequence already set."); return; } if (seq < SequenceList.Shutdown || seq > SequenceList.LockUser) { OnErrorEvent("Setting to an invalid timed sequence."); return; } if (date.Millis < new MyDate().Millis + 5000) { OnErrorEvent("Setting scheduled sequence time five seconds or less in the future."); return; } Stop(); alarm = new Alarm(date); alarm.Expire += () => { ExecuteCommand(); Reset(); }; alarm.AddTimeoutThreshold(60000); alarm.AddTimeoutThreshold(600000); alarm.AddTimeoutThreshold(3600000); alarm.TimeoutRemaining += (int remain) => { if (Config.Equals("alert", "1")) { string alert = "Will " + CurrentSequence + " in less than "; switch (remain) { case 60000: alert += "a minute."; break; case 600000: alert += "ten minutes."; break; case 3600000: alert += "an hour."; break; } AlertBox.Show("Sequence Reminder!", alert, 15000); } }; Console.WriteLine(alarm.Timeout); alarm.Start(); currentType = SequenceType.Scheduled; currentSequence = seq; if (Config.Equals("alert", "1")) { AlertBox.Show("Sequence Changed!", "Will " + seq + " on " + date.GetString() + ".", 15000); } Log.Write("Set sequence to " + currentType + " " + seq + " that will execute on " + date.GetString() + "."); OnSequenceChangeEvent(new SequenceChangeEventArgs(CurrentSequence, date)); }
/// <summary> /// Set the sequence to the specified sequence and its type to Timed. /// Will execute after the specified timeout in milliseconds. /// </summary> /// <param name="seq">The sequence to be set.</param> /// <param name="timeout">The number of milliseconds before it executes.</param> internal static void Set(SequenceList seq, int timeout) { Log.Write("Trying to change sequence."); if (currentSequence > SequenceList.NoShutdown && seq > SequenceList.NoShutdown) { OnErrorEvent("There is a sequence already set."); return; } if (seq < SequenceList.Shutdown || seq > SequenceList.LockUser) { OnErrorEvent("Setting to an invalid timed sequence."); return; } if (timeout < 5000) { OnErrorEvent("Setting timed sequence with timeout less than 5 seconds."); return; } Stop(); alarm = new Alarm(timeout); alarm.Expire += () => { ExecuteCommand(); Reset(); }; alarm.AddTimeoutThreshold(60000); alarm.AddTimeoutThreshold(600000); alarm.AddTimeoutThreshold(3600000); alarm.TimeoutRemaining += (int remain) => { if (Config.Equals("alert", "1")) { string alert = "Will " + CurrentSequence + " in less than "; switch (remain) { case 60000: alert += "a minute."; break; case 600000: alert += "ten minutes."; break; case 3600000: alert += "an hour."; break; } AlertBox.Show("Sequence Reminder!", alert, 15000); } }; alarm.Start(); currentType = SequenceType.Timed; currentSequence = seq; int hr = timeout / 3600000; int min = (timeout % 3600000) / 60000; int sec = ((timeout % 3600000) % 60000) / 1000; string show = ""; if (hr > 0) { show += hr + " hour" + (hr > 0 ? "s" : "") + (min > 0 || sec > 0 ? ", " : ""); } if (min > 0) { show += min + " minute" + (min > 0 ? "s" : "") + (sec > 0 ? ", " : ""); } if (sec > 0) { show += sec + " second" + (sec > 0 ? "s" : "") + ""; } if (Config.Equals("alert", "1")) { AlertBox.Show("Sequence Changed!", "Will " + seq + " in " + show + ".", 15000); } Log.Write("Set sequence to " + currentType + " " + seq + " that will execute after " + show + "."); OnSequenceChangeEvent(new SequenceChangeEventArgs(CurrentSequence, timeout)); }