-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFetchOpenWeatherMapMeasures.csx
121 lines (106 loc) · 4.04 KB
/
FetchOpenWeatherMapMeasures.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
116
117
118
119
120
121
#r "nuget: Newtonsoft.Json, 12.0.3"
#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 = "WeatherSensorMeasurements";
private string _weatherServiceAPIKey => Environment.GetEnvironmentVariable("appId");
private string _type = "m";
private int _limit = 2048;
private int _defaultNumberOfHours = 2;
private List<string> _stations = new List<string>
{
"5bf05033199f0300011f2b9f"
};
private string _influxDbConnectionString = @"http://database:8086";
if(string.IsNullOrWhiteSpace(_weatherServiceAPIKey))
{
Console.WriteLine("'appId' is empty. export it as env variable.");
return 1;
}
var measurementsJson = await GetMeasurementsJson(GetFromDate(), DateTimeOffset.UtcNow.ToUnixTimeSeconds(), _stations.FirstOrDefault());
var sensorMeasurements = JsonConvert.DeserializeObject<IEnumerable<WeatherSensorMeasurement>>(measurementsJson);
Console.WriteLine(measurementsJson);
await WriteMeasurementsToTimeSeriesDb(_influxDbConnectionString, sensorMeasurements);
private long GetFromDate()
{
if (Args.Any())
{
_defaultNumberOfHours = int.Parse(Args[0]);
}
return DateTimeOffset.UtcNow.AddHours(- _defaultNumberOfHours).ToUnixTimeSeconds();
}
private async Task<string> GetMeasurementsJson(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 WriteMeasurementsToTimeSeriesDb(string dbConnectionString, IEnumerable<WeatherSensorMeasurement> measurements)
{
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 measurement in measurements)
{
var point = new Point();
point.Tags.Add("stationId", measurement.StationId);
point.Fields.Add("averageTemperature", measurement.Temp.Average);
point.Fields.Add("averageHumidity", measurement.Humidity.Average);
point.Measurement = "WeatherSensorMeasurement";
point.Timestamp = measurement.TimeStamp;
InfluxDbApiResponse writeResponse = await client.WriteAsync(_databaseName, point);
}
}
public class WeatherSensorMeasurement
{
[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; }
}