-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
285 lines (251 loc) · 10.9 KB
/
Program.cs
File metadata and controls
285 lines (251 loc) · 10.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;
using System.IO;
using System.Collections;
namespace netduino_p1_logging
{
public class Program
{
private static IPEndPoint loggingEndpoint;
private static InterruptPort s0Port;
private const string configFilename = @"\sd\NetduinoP1.config";
private const string valueCacheFilename = @"\sd\ValueCache.txt";
private static string loggingHostName = "genergyloggingfunctions.azurewebsites.net";
private static int loggingPortNumber = 80;
private static string loggingPath = "POST /api/LogEntry?code=code HTTP/1.1";
private static string apiKey = "apikey";
private static int webserverPortnumber = 9080;
private static int s0Counter = 0;
public static void Main()
{
try
{
var timeSet = false;
NTP.UpdateTimeFromNtpServer("pool.ntp.org", 1);
ReadConfiguration();
ReadValueCache(System.DateTime.Today.ToString("ddMMyyyy"));
loggingEndpoint = HttpClient.GetIPEndPoint(loggingHostName, loggingPortNumber);
s0Port = new InterruptPort(Pins.GPIO_PIN_D12, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);
s0Port.OnInterrupt += new NativeEventHandler(S0PulseReceived);
s0Port.EnableInterrupt();
var messageReader = new P1MessageReader();
messageReader.MessageReceived += new P1MessageReader.MessageReceivedDelegate(messageReader_MessageReceived);
messageReader.Start();
while (true)
{
Thread.Sleep(60000);
// Resync time and s0Counter at 3 o'clock at night
if (!timeSet && System.DateTime.Now.Hour == 3)
{
timeSet = NTP.UpdateTimeFromNtpServer("pool.ntp.org", 1);
s0Counter = 0;
}
else if (timeSet && System.DateTime.Now.Hour > 3)
{
timeSet = false;
}
CacheValuesOnSd(System.DateTime.Today.ToString("ddMMyyyy"));
}
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
}
static void S0PulseReceived(uint data1, uint data2, DateTime time)
{
s0Counter++;
s0Port.ClearInterrupt();
}
private static void CacheValuesOnSd(string logdate)
{
new Thread(delegate
{
try
{
var values = new Hashtable();
values.Add("logdate", logdate);
values.Add("s0Counter", s0Counter);
WriteKeyValueFile(valueCacheFilename, values);
}
catch (Exception ex)
{
Debug.Print("ERROR in caching values: " + ex.StackTrace);
}
}).Start();
}
private static void ReadValueCache(string logdate)
{
try
{
var values = ReadKeyValueFile(valueCacheFilename);
if (!values.Contains("logdate") || values["logdate"] as string != logdate)
return;
if (values.Contains("s0Counter"))
s0Counter = int.Parse(values["s0Counter"] as string);
}
catch (Exception ex)
{
Debug.Print("ERROR in ReadValueCache: " + ex.StackTrace);
}
}
private static void ReadConfiguration()
{
try
{
var values = ReadKeyValueFile(configFilename);
if (values.Contains("loggingHostname"))
loggingHostName = values["loggingHostname"] as string;
if (values.Contains("loggingPortNumber"))
loggingPortNumber = int.Parse(values["loggingPortNumber"] as string);
if (values.Contains("apiKey"))
apiKey = values["apiKey"] as string;
if (values.Contains("loggingPath"))
loggingPath = values["loggingPath"] as string;
if (values.Contains("webServerPort"))
webserverPortnumber = int.Parse(values["webServerPort"] as string);
}
catch (Exception ex)
{
Debug.Print("ERROR in ReadConfiguration: " + ex.StackTrace);
}
}
private static Hashtable ReadKeyValueFile(string keyValueFilename)
{
var values = new Hashtable();
try
{
using (var sr = new StreamReader(keyValueFilename))
{
while (true)
{
var line = sr.ReadLine();
if (line == null)
break;
var index = line.IndexOf("=");
var key = line.Substring(0, index);
var value = line.Substring(index + 1);
values.Add(key, value);
}
}
}
catch (Exception)
{
// Do nothing, don't care, program for fallback if empty hashtable received
}
return values;
}
private static void WriteKeyValueFile(string keyValueFilename, Hashtable values)
{
using (var sw = new StreamWriter(keyValueFilename, false))
{
foreach (var key in values.Keys)
{
sw.WriteLine(key + "=" + values[key]);
}
}
}
static void messageReader_MessageReceived(object sender, P1MessageReader.MessageReceivedEventArgs e)
{
new Thread(delegate
{
try
{
StringBuilder content = new StringBuilder(512);
content.AppendLine("{");
content.AppendLine("\"ApiKey\": \"" + apiKey + "\",");
content.AppendLine("\"Timestamp\": \"" + e.Data.LogMoment.ToString("yyyy-MM-ddTHH:mm:ss") + "\",");
content.AppendLine("\"E1\": \"" + e.Data.E1.ToString() + "\",");
content.AppendLine("\"E2\": \"" + e.Data.E2.ToString() + "\",");
content.AppendLine("\"E1Retour\": \"" + e.Data.E1Retour.ToString() + "\",");
content.AppendLine("\"E2Retour\": \"" + e.Data.E2Retour.ToString() + "\",");
content.AppendLine("\"CurrentTariff\": \"" + e.Data.CurrentTariff.ToString() + "\",");
content.AppendLine("\"CurrentUsage\": \"" + e.Data.CurrentUsage.ToString() + "\",");
content.AppendLine("\"CurrentRetour\": \"" + e.Data.CurrentRetour.ToString() + "\",");
var gasValue = "";
if (e.Data.LastGasTransmit != null)
gasValue = e.Data.LastGasTransmit;
content.AppendLine("\"GasMeasurementMoment\": \"" + gasValue + "\",");
content.AppendLine("\"GasMeasurementValue\": \"" + e.Data.Gas.ToString() + "\",");
content.AppendLine("\"PvProductionCounter\": \"" + s0Counter.ToString() + "\"");
content.AppendLine("}");
// produce request
new Thread(delegate()
{
try
{
using (Socket connection = HttpClient.Connect(loggingEndpoint, 2000))
{
if (connection != null)
{
HttpClient.SendRequest(connection, loggingHostName, loggingPath, content.ToString());
}
else
{
Debug.Print("Unable to connect");
}
}
}
catch (Exception ex) {
Debug.Print("Error in sending data: " + ex.StackTrace);
}
}).Start();
}
catch (Exception ex)
{
Debug.Print("ERROR in posting data: " + ex.StackTrace);
}
}).Start();
new Thread(delegate
{
try
{
WriteToSdCard(e.Data);
}
catch (Exception ex)
{
Debug.Print("ERROR in writing to SDCard: " + ex.StackTrace);
}
}).Start();
}
private static void WriteToSdCard(P1Data p1Data)
{
string path = @"\SD\" + DateTime.Now.ToString("yyyyMM");
string logFile = path + @"\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (!File.Exists(logFile))
{
using (var sw = new StreamWriter(logFile, true))
{
sw.WriteLine("Timestamp;E1;E2;E1Retour;E2Retour;CurrentTariff;CurrentUsage;CurrentRetour;GasMeasurementMoment;GasMeasurementValue;PvProductionCounter");
}
}
using (var sw = new StreamWriter(logFile, true))
{
var logLine = new StringBuilder(512);
logLine.Append(p1Data.LogMoment.ToString("yyyy-MM-ddTHH:mm:ss") + ";");
logLine.Append(p1Data.E1.ToString() + ";");
logLine.Append(p1Data.E2.ToString() + ";");
logLine.Append(p1Data.E1Retour.ToString() + ";");
logLine.Append(p1Data.E2Retour.ToString() + ";");
logLine.Append(p1Data.CurrentTariff.ToString() + ";");
logLine.Append(p1Data.CurrentUsage.ToString() + ";");
logLine.Append(p1Data.CurrentRetour.ToString() + ";");
logLine.Append(p1Data.LastGasTransmit + ";");
logLine.Append(p1Data.Gas.ToString() + ";");
logLine.Append(s0Counter.ToString());
sw.WriteLine(logLine.ToString());
}
}
}
}