-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmonCmsProxy.cs
80 lines (69 loc) · 2.34 KB
/
EmonCmsProxy.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
using System;
using Microsoft.SPOT;
using System.Collections;
using System.Threading;
using System.Net;
namespace PowerReader
{
/// <summary>
/// Class is static only for better performance
/// </summary>
public static class EmonCmsProxy
{
public static string ApiKey = "d5662f82cbbfc7e4a9950de818d1afe7";
public static string Node = "1";
public static string ServerUrl = "http://212.20.105.58/emoncms";
public static int WriteIntervalMiliseconds = 10000;
private static readonly Thread _worker = new Thread(Work);
private static string _requestUri;
private static double _voltageReadSum, _currentReadSum;
private static int _readingCount;
/// <summary>
/// Start processing data from queue
/// </summary>
public static void Start()
{
_requestUri = ServerUrl + "/input/post.json?node=" + Node + "&apikey=" + ApiKey + "&json={current:"; //{2}, voltage:{3}}}""
_worker.Start();
}
public static void Push(double current, double voltage)
{
lock (_worker)
{
_voltageReadSum += voltage;
_currentReadSum += current;
_readingCount++;
}
}
private static void Work()
{
while (true)
{
if (_readingCount != 0)
{
double voltage, current;
lock (_worker)
{
voltage = _voltageReadSum / _readingCount;
current = _currentReadSum / _readingCount;
_voltageReadSum = 0;
_currentReadSum = 0;
_readingCount = 0;
}
string url = _requestUri + current + ", voltage:" + voltage + "}";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
Debug.Print(url);
try
{
request.GetResponse();
}
catch (Exception e)
{
Debug.Print(e.Message);
}
}
Thread.Sleep(WriteIntervalMiliseconds);
}
}
}
}