|
@@ -0,0 +1,216 @@
|
|
|
+using Comal.Classes;
|
|
|
+using InABox.Core;
|
|
|
+using InABox.Database;
|
|
|
+using InABox.Scripting;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace PRS.Shared.Events;
|
|
|
+
|
|
|
+public class ScheduledEventProperties : BaseObject
|
|
|
+{
|
|
|
+ [EditorSequence(1)]
|
|
|
+ public int Frequency { get; set; } = 1;
|
|
|
+
|
|
|
+ [EditorSequence(2)]
|
|
|
+ public SchedulePeriod Period { get; set; } = SchedulePeriod.Hour;
|
|
|
+}
|
|
|
+
|
|
|
+public class ScheduledEvent : IEvent<ScheduledEventDataModel>, IPropertiesEvent<ScheduledEventProperties>
|
|
|
+{
|
|
|
+ public int Frequency { get; set; } = 1;
|
|
|
+
|
|
|
+ public SchedulePeriod Period { get; set; } = SchedulePeriod.Hour;
|
|
|
+
|
|
|
+ public DateTime LastExecution { get; set; }
|
|
|
+
|
|
|
+ public IEventDataModelDefinition DataModelDefinition()
|
|
|
+ {
|
|
|
+ return new ScheduledEventDataModelDefinition();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Notification GenerateNotification(ScheduledEventDataModel model)
|
|
|
+ {
|
|
|
+ var notification = new Notification();
|
|
|
+ notification.Title = $"Schedule has run.";
|
|
|
+ notification.Description = $"Schedule has run.";
|
|
|
+ return notification;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Init(IStore store, IEventData data, ScheduledEventDataModel model)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScheduledEventProperties GetProperties()
|
|
|
+ {
|
|
|
+ return new()
|
|
|
+ {
|
|
|
+ Frequency = Frequency,
|
|
|
+ Period = Period,
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetProperties(ScheduledEventProperties properties)
|
|
|
+ {
|
|
|
+ Frequency = properties.Frequency;
|
|
|
+ Period = properties.Period;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+public class ScheduledEventDataModelDefinition : IEventDataModelDefinition
|
|
|
+{
|
|
|
+ public IEventVariable? GetVariable(string name)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerable<IEventVariable> GetVariables()
|
|
|
+ {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+public class ScheduledEventDataModel : IEventDataModel
|
|
|
+{
|
|
|
+ public bool TryGetVariable(string name, out object? value)
|
|
|
+ {
|
|
|
+ value = null;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#region Triggers
|
|
|
+
|
|
|
+[Caption("Custom Script")]
|
|
|
+public class ScriptScheduledEventTrigger : IEventTrigger<ScheduledEvent, ScheduledEventDataModel>
|
|
|
+{
|
|
|
+ public string Description => "Custom Script";
|
|
|
+
|
|
|
+ private ScriptDocument? _scriptDocument;
|
|
|
+ private ScriptDocument? ScriptDocument
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if(_scriptDocument is null && Script is not null)
|
|
|
+ {
|
|
|
+ _scriptDocument = new(Script);
|
|
|
+ _scriptDocument.Compile();
|
|
|
+ }
|
|
|
+ return _scriptDocument;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private string? _script;
|
|
|
+ public string? Script
|
|
|
+ {
|
|
|
+ get => _script;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if(_script != value)
|
|
|
+ {
|
|
|
+ _script = value;
|
|
|
+ _scriptDocument = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerable<string> ReferencedVariables => [];
|
|
|
+
|
|
|
+ public string DefaultScript()
|
|
|
+ {
|
|
|
+ return @"using PRS.Shared.Events;
|
|
|
+
|
|
|
+public class Module
|
|
|
+{
|
|
|
+ public bool Check(ScheduledEventDataModel model)
|
|
|
+ {
|
|
|
+ // Return true if the requirements are met for this event trigger.
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}";
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool Check(ScheduledEventDataModel dataModel)
|
|
|
+ {
|
|
|
+ if (ScriptDocument is null) return false;
|
|
|
+
|
|
|
+ return ScriptDocument.Execute(methodname: "Check", parameters: [dataModel]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#endregion
|
|
|
+
|
|
|
+#region Actions
|
|
|
+
|
|
|
+[Caption("Custom Script")]
|
|
|
+public class ScriptScheduledEventAction : IEventAction<ScheduledEvent>
|
|
|
+{
|
|
|
+ private ScriptDocument? _scriptDocument;
|
|
|
+ private ScriptDocument? ScriptDocument
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if(_scriptDocument is null && Script is not null)
|
|
|
+ {
|
|
|
+ _scriptDocument = new(Script);
|
|
|
+ _scriptDocument.SetValue("Result", null);
|
|
|
+ _scriptDocument.Compile();
|
|
|
+ }
|
|
|
+ return _scriptDocument;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private string? _script;
|
|
|
+ public string? Script
|
|
|
+ {
|
|
|
+ get => _script;
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if(_script != value)
|
|
|
+ {
|
|
|
+ _script = value;
|
|
|
+ _scriptDocument = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public IEnumerable<string> ReferencedVariables => [];
|
|
|
+
|
|
|
+ public string Description => "Custom Script";
|
|
|
+
|
|
|
+ public string DefaultScript()
|
|
|
+ {
|
|
|
+ return @"using PRS.Shared.Events;
|
|
|
+
|
|
|
+public class Module
|
|
|
+{
|
|
|
+ public object? Result { get; set; }
|
|
|
+
|
|
|
+ public bool Execute(ScheduledEventDataModel model)
|
|
|
+ {
|
|
|
+ // Do anything you want, and then save return-value to 'Result', or leave it as 'null' if no return value is needed.
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}";
|
|
|
+ }
|
|
|
+
|
|
|
+ public object? Execute(IEventDataModel dataModel)
|
|
|
+ {
|
|
|
+ if (ScriptDocument is null) return null;
|
|
|
+
|
|
|
+ var model = dataModel.RootModel<ScheduledEventDataModel>();
|
|
|
+ if(ScriptDocument.Execute(methodname: "Execute", parameters: [model]))
|
|
|
+ {
|
|
|
+ return ScriptDocument.GetValue("Result");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#endregion
|