-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaMetric.cs
More file actions
152 lines (135 loc) · 5.21 KB
/
LaMetric.cs
File metadata and controls
152 lines (135 loc) · 5.21 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using log4net;
using Newtonsoft.Json;
using SmartPesa.Objects;
using SmartPesa.WorkflowLibrary;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace SmartPesa.Workflow
{
public class LaMetric : DestinationBase
{
private static readonly ILog _log = LogManager.GetLogger("LogFile");
private string _url = "";
private string _accessToken = "";
private string _icon = null;
private int _index = 0;
private int _interval = 0;
private Int64 _txnCounter = 0;
private DateTime _lastRequest;
/*
spWorkflow.spring
=================
<objects>
<object name="lametric" type="SmartPesa.Workflow.LaMetric,LaMetric"></object>
</objects>
spWorkflow.config
=================
<Workflow>
<Destinations>
<add key="lametric" type="LaMetric" active="true" subscriber="true" />
</Destinations>
</Workflow>
<LaMetric>
<requestSettings>
<add key="url" value="https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.[unique-uri]" />
<add key="accessToken" value="[secret]" />
<add key="icon" value="i59" />
<add key="index" value="0" />
<add key="interval" value="60" /> <!-- seconds -->
</requestSettings>
</LaMetric>
*/
public LaMetric()
{
_log.Info(" -> Loading LaMetric settings");
NameValueCollection requestSettings = ConfigurationManager.GetSection("LaMetric/requestSettings") as NameValueCollection;
if (requestSettings != null)
{
_url = requestSettings["url"];
_accessToken = requestSettings["accessToken"];
_icon = requestSettings["icon"];
_index = Convert.ToInt32(requestSettings["index"]);
_interval = int.Parse(requestSettings["interval"]);
_lastRequest = DateTime.UtcNow.AddSeconds(-_interval);
}
else
{
_log.Error("Check configuration LaMetric/requestSettings");
}
}
public override string Name()
{
return "LaMetric";
}
public override string Version()
{
return "1.0";
}
public override object ProcessMessage(object payload)
{
Payment newPayment = JsonConvert.DeserializeObject<Payment>((string)payload);
_log.InfoFormat(" {0}{1}{2}", newPayment.TransactionRef, newPayment.Amount.ToString().PadLeft(20).PadRight(40), newPayment.ResponseCode);
if (newPayment.ResponseCode == ResponseCodes.Approved)
{
if (_txnCounter >= 9999999 || DateTime.UtcNow.Day != _lastRequest.Day)
{
_lastRequest = DateTime.UtcNow;
_txnCounter = 0;
}
_txnCounter++;
if ((DateTime.UtcNow - _lastRequest).TotalSeconds > _interval)
{
_lastRequest = DateTime.UtcNow;
SendRequest();
}
}
return null;
}
private async void SendRequest()
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
request.Accept = "application/json";
request.Headers.Add("X-Access-Token", _accessToken);
request.Headers.Add("Cache-Control", "no-cache");
request.Method = "POST";
request.ContentType = "application/json; encoding='utf-8'";
request.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
Frames frames = new Frames(_txnCounter, _icon, _index);
string requestBody = JsonConvert.SerializeObject(frames);
streamWriter.Write(requestBody);
streamWriter.Close();
streamWriter.Dispose();
HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
_log.InfoFormat("Response: {0} - {1}", response.StatusCode, response.StatusDescription);
}
catch (Exception ex)
{
_log.Warn("Response: " + ex.Message);
}
}
public override string Shutdown()
{
return null;
}
public class Frames
{
public List<dynamic> frames { get; set; }
public Frames(Int64 txnCounter, string icon, int index)
{
frames = new List<dynamic>
{
new { text = txnCounter.ToString(), icon = icon, index = index}
};
}
}
}
}