forked from andmos/RPIWeatherStation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchOpenWeatherMapMessures.csx
115 lines (98 loc) · 3.77 KB
/
FetchOpenWeatherMapMessures.csx
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
#! "netcoreapp2.1"
#r "nuget: Newtonsoft.Json, 11.0.2"
#r "nuget: System.Net.Http, 4.3.3"
#r "nuget: InfluxDB.Net.Core, 1.1.22-beta"
using System.Net.Http;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using InfluxDB.Net;
using InfluxDB.Net.Models;
using InfluxDB.Net.Helpers;
using InfluxDB.Net.Infrastructure.Influx;
using InfluxDB.Net.Infrastructure.Configuration;
private const string _apiBaseAddress = @"http://api.openweathermap.org/data/3.0/measurements";
private const string _databaseName = "WeatherSensorMessurements";
private string _weatherServiceAPIKey => Environment.GetEnvironmentVariable("appId");
private string _type = "m";
private int _limit = 400;
private List<string> _stations = new List<string>
{
"5bf05033199f0300011f2b9f"
};
private string _influxDbConnectionString = @"http://127.0.0.1:8086";
if (Args.Any())
{
long fromDate = long.Parse(Args[0]);
long toDate = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var mesurementsJson = await GetMesurementsJson(fromDate, toDate, _stations.FirstOrDefault());
var sensorMessurements = JsonConvert.DeserializeObject<IEnumerable<WeatherSensorMessurement>>(mesurementsJson);
Console.WriteLine(mesurementsJson);
await WriteMesurementsToTimeSeriesDb(_influxDbConnectionString, sensorMessurements);
}
private async Task<string> GetMesurementsJson(long fromDate, long toDate, string station)
{
List<string> jsonRespons = new List<string>();
using (var client = new HttpClient())
{
var response = await client.GetAsync($"{_apiBaseAddress}?" +
$"type={_type}" +
$"&station_id={station}" +
$"&APPID={_weatherServiceAPIKey}" +
$"&limit={_limit}" +
$"&from={fromDate}" +
$"&to={toDate}");
if (!response.IsSuccessStatusCode)
{
throw new NotImplementedException($"Could not find any weather data, {_apiBaseAddress} returned status code {response.StatusCode}");
}
return await response.Content.ReadAsStringAsync();
}
}
private async Task WriteMesurementsToTimeSeriesDb(string dbConnectionString, IEnumerable<WeatherSensorMessurement> messurements)
{
var client = new InfluxDb(dbConnectionString, "root", "root");
Pong pong = await client.PingAsync();
if(!pong.Success)
{
Console.WriteLine($"could not connect to database at {dbConnectionString}");
return;
}
var response = await client.CreateDatabaseAsync(_databaseName);
foreach(var messurement in messurements)
{
var poin = new Point();
poin.Tags.Add("stationId", messurement.StationId);
poin.Fields.Add("averageTemperature", messurement.Temp.Average);
poin.Fields.Add("averageHumidity", messurement.Humidity.Average);
poin.Measurement = "WeatherSensorMessurement";
poin.Timestamp = messurement.TimeStamp;
InfluxDbApiResponse writeResponse =await client.WriteAsync(_databaseName, poin);
}
}
public class WeatherSensorMessurement
{
[JsonProperty("station_id")]
public string StationId { get; set; }
[JsonProperty("date"), JsonConverter(typeof(UnixDateTimeConverter))]
public DateTime TimeStamp { get; set; }
[JsonProperty("temp")]
public Temperature Temp { get; set; }
[JsonProperty("humidity")]
public Humidity Humidity { get; set; }
}
public class Temperature
{
[JsonProperty("min")]
public double Min { get; set; }
[JsonProperty("max")]
public double Max { get; set; }
[JsonProperty("average")]
public double Average { get; set; }
}
public class Humidity
{
[JsonProperty("average")]
public double Average { get; set; }
}