-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMessageHandler.cs
More file actions
77 lines (68 loc) · 3.06 KB
/
MessageHandler.cs
File metadata and controls
77 lines (68 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using TSLab.Script.Handlers.Options;
namespace TSLab.Script.Handlers
{
[HandlerCategory(HandlerCategories.ServiceElements)]
[HelperName("Message", Language = Constants.En)]
[HelperName("Сообщение", Language = Constants.Ru)]
[InputsCount(1)]
[Input(0, TemplateTypes.BOOL)]
[OutputsCount(0)]
[Description("При появлении на входе блока значения 'Истина' выводит в лог программы пользовательское сообщение.")]
[HelperDescription("When input becomes TRUE a handler sends user message to a TSLab log.", Constants.En)]
public sealed class MessageHandler : IOneSourceHandler, IBooleanReturns, IStreamHandler, IValuesHandlerWithNumber, IBooleanInputs, IContextUses
{
private const string UserMessageTag = "$UserMessageTag";
public IContext Context { get; set; }
/// <summary>
/// \~english Message
/// \~russian Текст
/// </summary>
[HelperName("Message", Constants.En)]
[HelperName("Текст", Constants.Ru)]
[Description("Текст")]
[HelperDescription("Message", Constants.En)]
[HandlerParameter(true, "", NotOptimized = true)]
public string Message { get; set; }
/// <summary>
/// \~english Additional user tag
/// \~russian Дополнительная пользовательская метка
/// </summary>
[HelperName("Tag", Constants.En)]
[HelperName("Метка", Constants.Ru)]
[Description("Дополнительная пользовательская метка")]
[HelperDescription("Additional user tag", Constants.En)]
[HandlerParameter(true, "Tag", NotOptimized = true)]
public string Tag { get; set; }
/// <summary>
/// \~english Message importance (Info, Warning, Error)
/// \~russian Важность сообщения (Info, Warning, Error)
/// </summary>
[HelperName("Importance", Constants.En)]
[HelperName("Важность", Constants.Ru)]
[Description("Важность сообщения (Info, Warning, Error)")]
[HelperDescription("Message importance (Info, Warning, Error)", Constants.En)]
[HandlerParameter(true, "Info", NotOptimized = true)]
public MessageType Type { get; set; }
public void Execute(IList<bool> values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.LastOrDefault())
Log();
}
public void Execute(bool value, int number)
{
if (value && number == Context.BarsCount - (Context.IsLastBarUsed ? 1 : 2))
Log();
}
private void Log()
{
var args = new Dictionary<string, object> { { UserMessageTag, Tag ?? string.Empty } };
Context.Log(Message, Type, true, args);
}
}
}