-
Notifications
You must be signed in to change notification settings - Fork 3
/
NotificationNode.cs
93 lines (75 loc) · 3.18 KB
/
NotificationNode.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Net.Security;
using LogicModule.Nodes.Helpers;
using LogicModule.ObjectModel;
using LogicModule.ObjectModel.TypeSystem;
namespace PushoverNode
{
public class NotificationNode : LogicNodeBase
{
public NotificationNode(INodeContext context)
: base(context)
{
context.ThrowIfNull("context");
this.SetupIgnoreSSLTrust();
var typeService = context.GetService<ITypeService>();
this.Trigger = typeService.CreateBool(PortTypes.Binary, "Trigger");
this.Token = typeService.CreateString(PortTypes.String, "Token");
this.UserKey = typeService.CreateString(PortTypes.String, "UserKey");
this.Message = typeService.CreateString(PortTypes.String, "Message");
this.Variables = new List<StringValueObject>();
this.VariableCount = typeService.CreateInt(PortTypes.Integer, "VariableCount", 0);
this.VariableCount.MinValue = 0;
this.VariableCount.MaxValue = 99;
ListHelpers.ConnectListToCounter(this.Variables, this.VariableCount, typeService.GetValueObjectCreator(PortTypes.String, "Variable"), null, null);
}
[Input(DisplayOrder = 1)]
public BoolValueObject Trigger { get; private set; }
[Parameter(DisplayOrder = 2, IsRequired = true)]
public StringValueObject Token { get; private set; }
[Parameter(DisplayOrder = 3, IsRequired = true)]
public StringValueObject UserKey { get; private set; }
[Parameter(DisplayOrder = 4, IsRequired = true)]
public StringValueObject Message { get; private set; }
[Input(DisplayOrder = 5, InitOrder = 2)]
public IList<StringValueObject> Variables { get; private set; }
[Input(DisplayOrder = 6, InitOrder = 1, IsDefaultShown = false)]
public IntValueObject VariableCount { get; private set; }
public override void Execute()
{
if (!this.Trigger.HasValue || !this.Trigger.WasSet || !this.Trigger.Value) return;
var parameters = new NameValueCollection {
{ "token", this.Token },
{ "user", this.UserKey },
{ "message", this.ComposeMessage() }
};
using (var client = new WebClient())
{
client.UploadValues("https://api.pushover.net/1/messages.json", parameters);
}
}
private string ComposeMessage()
{
try
{
return string.Format(this.Message, this.Variables.Select(v => v.Value).ToArray());
}
catch (FormatException)
{
return this.Message;
}
}
private void SetupIgnoreSSLTrust()
{
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(
delegate
{ return true; }
);
}
}
}