-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
156 lines (137 loc) · 6.58 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OSIsoft.Data.Http;
using OSIsoft.Identity;
using OSIsoft.Omf;
using OSIsoft.Omf.Converters;
using OSIsoft.OmfIngress;
namespace OpenWeather
{
public static class Program
{
private static readonly HttpClient _client = new();
private static IOmfIngressService _omfIngressService;
private static ILogger _log;
public static AppSettings Settings { get; set; }
[FunctionName("CurrentWeather")]
#pragma warning disable CA1801 // Review unused parameters; this method declaration follows Azure Function examples
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
#pragma warning restore CA1801 // Review unused parameters
{
_log = log;
LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
LoadConfiguration();
if (string.IsNullOrEmpty(Settings.OpenWeatherKey))
{
LogInformation("No OpenWeather API Key provided, function will generate random data");
}
// Set up OMF Ingress Service
_omfIngressService = ConfigureAdhOmf(Settings.CdsUri, Settings.CdsTenantId, Settings.CdsNamespaceId, Settings.CdsClientId, Settings.CdsClientSecret);
// Send OMF Type message
SendOmfMessage(_omfIngressService, OmfMessageCreator.CreateTypeMessage(typeof(CurrentWeather)));
// Prepare OMF containers
string typeId = ClrToOmfTypeConverter.Convert(typeof(CurrentWeather)).Id;
List<OmfContainer> containers = new();
Dictionary<string, IEnumerable<CurrentWeather>> data = new();
string[] queries = Settings.OpenWeatherQueries.Split('|');
foreach (string query in queries)
{
if (!string.IsNullOrEmpty(Settings.OpenWeatherKey))
{
// Get Current Weather Data
JObject response = JsonConvert.DeserializeObject<JObject>(HttpGet($"{Settings.OpenWeatherUri}?q={query}&appid={Settings.OpenWeatherKey}"));
// Parse data into OMF messages
CurrentWeather value = new(response);
string streamId = $"OpenWeather_Current_{value.Name}";
containers.Add(new OmfContainer(streamId, typeId));
data.Add(streamId, new CurrentWeather[] { value });
}
else
{
// No key provided, generate random data
containers.Add(new OmfContainer(query, typeId));
CurrentWeather value = new(query);
data.Add(query, new CurrentWeather[] { value });
}
}
SendOmfMessage(_omfIngressService, new OmfContainerMessage(containers));
LogInformation($"Sent {containers.Count} containers");
SendOmfMessage(_omfIngressService, OmfMessageCreator.CreateDataMessage(data));
LogInformation($"Sent {data.Count} data messages");
}
private static void LogInformation(string message)
{
if (_log != null)
{
_log.LogInformation(message);
}
}
private static void LoadConfiguration()
{
if (File.Exists(Directory.GetCurrentDirectory() + "/appsettings.json"))
{
// Running locally, read configuration from file
Settings = JsonConvert.DeserializeObject<AppSettings>(File.ReadAllText(Directory.GetCurrentDirectory() + "/appsettings.json"));
}
else
{
// Running in Azure Function, read configuration from Environment
Settings = new AppSettings()
{
OpenWeatherUri = new Uri(Environment.GetEnvironmentVariable("OPEN_WEATHER_URI")),
OpenWeatherKey = Environment.GetEnvironmentVariable("OPEN_WEATHER_KEY"),
OpenWeatherQueries = Environment.GetEnvironmentVariable("OPEN_WEATHER_QUERIES"),
CdsUri = new Uri(Environment.GetEnvironmentVariable("Cds_URI")),
CdsTenantId = Environment.GetEnvironmentVariable("Cds_TENANT_ID"),
CdsNamespaceId = Environment.GetEnvironmentVariable("Cds_NAMESPACE_ID"),
CdsClientId = Environment.GetEnvironmentVariable("Cds_CLIENT_ID"),
CdsClientSecret = Environment.GetEnvironmentVariable("Cds_CLIENT_SECRET"),
};
}
}
/// <summary>
/// Configure ADH/OMF Services
/// </summary>
private static IOmfIngressService ConfigureAdhOmf(Uri address, string tenantId, string namespaceId, string clientId, string clientSecret)
{
AuthenticationHandler deviceAuthenticationHandler = new(address, clientId, clientSecret);
OmfIngressService deviceBaseOmfIngressService = new(address, HttpCompressionMethod.None, deviceAuthenticationHandler);
return deviceBaseOmfIngressService.GetOmfIngressService(tenantId, namespaceId);
}
/// <summary>
/// Runs a generic HTTP GET request against the GitHub API
/// </summary>
private static string HttpGet(string url)
{
using HttpRequestMessage request = new(HttpMethod.Get, url);
return Send(request).Result;
}
/// <summary>
/// Send message using HttpRequestMessage
/// </summary>
private static async Task<string> Send(HttpRequestMessage request)
{
HttpResponseMessage response = await _client.SendAsync(request).ConfigureAwait(false);
string responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
throw new Exception($"Error sending OMF response code:{response.StatusCode}. Response {responseString}");
return responseString;
}
/// <summary>
/// Sends a message to the ADH OMF endpoint
/// </summary>
private static object SendOmfMessage(IOmfIngressService service, OmfMessage omfMessage)
{
SerializedOmfMessage serializedOmfMessage = OmfMessageSerializer.Serialize(omfMessage);
return service.SendOmfMessageAsync(serializedOmfMessage).Result;
}
}
}