-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
191 lines (157 loc) · 6.9 KB
/
Program.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
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
using System;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using Microsoft.Data.Sqlite;
using System.Linq;
namespace WeatherFetch
{
class Program
{
static void Main(string[] args)
{
string settingsPath = "";
string EndDate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
int Days = 0;
//Obtain number of days past to fetch weather data
for (int i = 0; i < args.Length; i++)
{
if (args[i].ToLower().Contains("-days=") == true)
{
try
{
Days = int.Parse(args[i].Replace("-days=", "").Trim())-1;
if ((Days<0)&&(Days>365))
{
Days = 0;
}
}
catch
{
Days = 0;
}
}
if (args[i].ToLower().Contains("-settingspath=") == true)
{
try
{
settingsPath = args[i].Replace("-settingspath=", "").Trim();
}
catch
{
settingsPath = "";
}
}
}
//Days = 4;
//Read Settings.json file
var settingsJSON = System.IO.File.ReadAllText(settingsPath + "Settings.json");
string DeviceMACAddress = (string)JObject.Parse(settingsJSON)["DeviceMACAddress"];
string ApiKey = (string)JObject.Parse(settingsJSON)["ApiKey"];
string ApplicationKey = (string)JObject.Parse(settingsJSON)["ApplicationKey"];
string dbPath = (string)JObject.Parse(settingsJSON)["dbPath"].ToString().Trim();
//Iterate thru days past and fetch weather data from AmbientWeather Service
for (int i = 0; i<=Days;i++)
{
string NewEndDate = DateTime.Parse(EndDate).AddDays(-1*i).ToString("yyyy-MM-dd");
string status = FetchReadings(DeviceMACAddress, ApiKey, ApplicationKey, NewEndDate, dbPath);
Console.WriteLine(status);
}
System.Environment.Exit(0);
}
public static string FetchReadings(string DeviceMACAddress, string ApiKey, string ApplicationKey, string EndDate, string dbPath)
{
int ReadingsFound = 0;
int ReadingsSaved = 0;
System.Threading.Thread.Sleep(5000); //Ambient Weather 1 second delay for API usage
string response = "";
response = GetReadings(@"https://api.ambientweather.net/v1/devices/" + DeviceMACAddress + "?apiKey=" + ApiKey + "&applicationKey=" + ApplicationKey + "&endDate=" + EndDate);
List<DeviceData> AmbientWeather = JsonConvert.DeserializeObject<List<DeviceData>>(response);
foreach (DeviceData Reading in AmbientWeather)
{
Microsoft.Data.Sqlite.SqliteConnection m_dbConnection = new SqliteConnection("Data Source=" + dbPath + "WeatherStation.sqlite;");
m_dbConnection.Open();
string LocalDateTime = "";
try
{
LocalDateTime = DateTime.Parse(Reading.date).ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz");
}
catch
{
LocalDateTime = "";
}
ReadingsFound++;
string insertReadingsSql = "insert into Readings values "
+ "(" + Reading.dateutc
+ "," + Reading.winddir
+ "," + Reading.windspeedmph
+ "," + Reading.windgustmph
+ "," + Reading.maxdailygust
+ "," + Reading.tempf
+ "," + Reading.hourlyrainin
+ "," + Reading.eventrainin
+ "," + Reading.dailyrainin
+ "," + Reading.weeklyrainin
+ "," + Reading.monthlyrainin
+ "," + Reading.totalrainin
+ "," + Reading.baromrelin
+ "," + Reading.baromabsin
+ "," + Reading.humidity
+ "," + Reading.tempinf
+ "," + Reading.humidityin
+ "," + Reading.uv
+ "," + Reading.solarradiation
+ "," + Reading.feelsLike
+ "," + Reading.dewPoint
+ ",'" + Reading.lastRain + "'"
+ ",'" + Reading.date + "'"
+ ",'" + LocalDateTime + "')";
Microsoft.Data.Sqlite.SqliteCommand insertReadings = new SqliteCommand(insertReadingsSql, m_dbConnection);
try
{
insertReadings.ExecuteNonQuery();
m_dbConnection.Close();
ReadingsSaved++;
}
catch(Exception e)
{
m_dbConnection.Close();
}
}
return DateTime.Parse(EndDate).AddDays(-1).ToString("yyyy-MM-dd") + " - Reading Found=" + ReadingsFound + " Saved=" + ReadingsSaved;
}
public static string GetReadings(string url)
{
var client = new WebClient();
var response = client.DownloadString(url);
return response;
}
public class DeviceData
{
public object dateutc { get; set; }
public int winddir { get; set; }
public double windspeedmph { get; set; }
public double windgustmph { get; set; }
public double maxdailygust { get; set; }
public double tempf { get; set; }
public double hourlyrainin { get; set; }
public double eventrainin { get; set; }
public double dailyrainin { get; set; }
public double weeklyrainin { get; set; }
public double monthlyrainin { get; set; }
public double totalrainin { get; set; }
public double baromrelin { get; set; }
public double baromabsin { get; set; }
public int humidity { get; set; }
public double tempinf { get; set; }
public int humidityin { get; set; }
public int uv { get; set; }
public double solarradiation { get; set; }
public double feelsLike { get; set; }
public double dewPoint { get; set; }
public string lastRain { get; set; }
public string date { get; set; }
}
}
}