ScheduledEvent.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Database;
  4. using InABox.Scripting;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace PRS.Shared.Events;
  11. public class ScheduledEventDayOfWeek : BaseObject
  12. {
  13. [EditorSequence(1)]
  14. [Editable(Editable.Disabled)]
  15. public DayOfWeek DayOfWeek { get; set; }
  16. [EditorSequence(2)]
  17. [TimeOfDayEditor]
  18. public TimeSpan StartTime { get; set; } = TimeSpan.Zero;
  19. [EditorSequence(3)]
  20. [TimeOfDayEditor]
  21. public TimeSpan EndTime { get; set; } = new TimeSpan(23, 59, 59);
  22. [EditorSequence(4)]
  23. public bool Enabled { get; set; } = true;
  24. public ScheduledEventDayOfWeek()
  25. {
  26. }
  27. public ScheduledEventDayOfWeek(DayOfWeek dayOfWeek)
  28. {
  29. DayOfWeek = dayOfWeek;
  30. }
  31. static ScheduledEventDayOfWeek()
  32. {
  33. DefaultColumns.Add<ScheduledEventDayOfWeek>(x => x.DayOfWeek);
  34. DefaultColumns.Add<ScheduledEventDayOfWeek>(x => x.StartTime);
  35. DefaultColumns.Add<ScheduledEventDayOfWeek>(x => x.EndTime);
  36. }
  37. }
  38. public class ScheduledEventProperties : BaseObject
  39. {
  40. [EditorSequence(1)]
  41. public int Frequency { get; set; } = 1;
  42. [EditorSequence(2)]
  43. public SchedulePeriod Period { get; set; } = SchedulePeriod.Hour;
  44. [EditorSequence(3)]
  45. public DateTime NextSchedule { get; set; }
  46. [EmbeddedListEditor(typeof(ScheduledEventDayOfWeek), AddRows = false)]
  47. public List<ScheduledEventDayOfWeek> DayOfWeekSettings { get; set; } = new()
  48. {
  49. new(DayOfWeek.Monday),
  50. new(DayOfWeek.Tuesday),
  51. new(DayOfWeek.Wednesday),
  52. new(DayOfWeek.Thursday),
  53. new(DayOfWeek.Friday),
  54. new(DayOfWeek.Saturday),
  55. new(DayOfWeek.Sunday),
  56. };
  57. }
  58. public class ScheduledEvent : IEvent<ScheduledEventDataModel>, IPropertiesEvent<ScheduledEventProperties>
  59. {
  60. public ScheduledEventProperties Properties { get; set; } = new();
  61. public IEventDataModelDefinition DataModelDefinition()
  62. {
  63. return new ScheduledEventDataModelDefinition();
  64. }
  65. public Notification GenerateNotification(ScheduledEventDataModel model)
  66. {
  67. var notification = new Notification();
  68. notification.Title = $"Schedule has run.";
  69. notification.Description = $"Schedule has run.";
  70. return notification;
  71. }
  72. public void Init(IStore store, IEventData data, ScheduledEventDataModel model)
  73. {
  74. }
  75. public ScheduledEventProperties GetProperties()
  76. {
  77. return Properties;
  78. }
  79. public void SetProperties(ScheduledEventProperties properties)
  80. {
  81. Properties = properties;
  82. }
  83. }
  84. public class ScheduledEventDataModelDefinition : IEventDataModelDefinition
  85. {
  86. public IEventVariable? GetVariable(string name)
  87. {
  88. return null;
  89. }
  90. public IEnumerable<IEventVariable> GetVariables()
  91. {
  92. return [];
  93. }
  94. }
  95. public class ScheduledEventDataModel : IEventDataModel
  96. {
  97. public bool TryGetVariable(string name, out object? value)
  98. {
  99. value = null;
  100. return false;
  101. }
  102. }
  103. #region Triggers
  104. [Caption("Custom Script")]
  105. public class ScriptScheduledEventTrigger : IEventTrigger<ScheduledEvent, ScheduledEventDataModel>
  106. {
  107. public string Description => "Custom Script";
  108. private ScriptDocument? _scriptDocument;
  109. private ScriptDocument? ScriptDocument
  110. {
  111. get
  112. {
  113. if(_scriptDocument is null && Script is not null)
  114. {
  115. _scriptDocument = new(Script);
  116. _scriptDocument.Compile();
  117. }
  118. return _scriptDocument;
  119. }
  120. }
  121. private string? _script;
  122. public string? Script
  123. {
  124. get => _script;
  125. set
  126. {
  127. if(_script != value)
  128. {
  129. _script = value;
  130. _scriptDocument = null;
  131. }
  132. }
  133. }
  134. public IEnumerable<string> ReferencedVariables => [];
  135. public string DefaultScript()
  136. {
  137. return @"using PRS.Shared.Events;
  138. public class Module
  139. {
  140. public bool Check(ScheduledEventDataModel model)
  141. {
  142. // Return true if the requirements are met for this event trigger.
  143. return true;
  144. }
  145. }";
  146. }
  147. public bool Check(ScheduledEventDataModel dataModel)
  148. {
  149. if (ScriptDocument is null) return false;
  150. return ScriptDocument.Execute(methodname: "Check", parameters: [dataModel]);
  151. }
  152. }
  153. #endregion
  154. #region Actions
  155. [Caption("Custom Script")]
  156. public class ScriptScheduledEventAction : IEventAction<ScheduledEvent>
  157. {
  158. private ScriptDocument? _scriptDocument;
  159. private ScriptDocument? ScriptDocument
  160. {
  161. get
  162. {
  163. if(_scriptDocument is null && Script is not null)
  164. {
  165. _scriptDocument = new(Script);
  166. _scriptDocument.SetValue("Result", null);
  167. _scriptDocument.Compile();
  168. }
  169. return _scriptDocument;
  170. }
  171. }
  172. private string? _script;
  173. public string? Script
  174. {
  175. get => _script;
  176. set
  177. {
  178. if(_script != value)
  179. {
  180. _script = value;
  181. _scriptDocument = null;
  182. }
  183. }
  184. }
  185. public IEnumerable<string> ReferencedVariables => [];
  186. public string Description => "Custom Script";
  187. public string DefaultScript()
  188. {
  189. return @"using PRS.Shared.Events;
  190. public class Module
  191. {
  192. public object? Result { get; set; }
  193. public bool Execute(ScheduledEventDataModel model)
  194. {
  195. // Do anything you want, and then save return-value to 'Result', or leave it as 'null' if no return value is needed.
  196. return true;
  197. }
  198. }";
  199. }
  200. public object? Execute(IEventDataModel dataModel)
  201. {
  202. if (ScriptDocument is null) return null;
  203. var model = dataModel.RootModel<ScheduledEventDataModel>();
  204. if(ScriptDocument.Execute(methodname: "Execute", parameters: [model]))
  205. {
  206. return ScriptDocument.GetValue("Result");
  207. }
  208. else
  209. {
  210. return null;
  211. }
  212. }
  213. }
  214. #endregion