private static async Task SendDataToCwop(AprsWeatherDataPacket packet, ILogger log) { using (var client = new TcpClient("cwop.aprs.net", 14580)) using (var stream = client.GetStream()) { await Send("user EW9714 pass -1 vers custom 1.00\r\n", stream, log); await ReceiveResponse(stream, log); await Task.Delay(TimeSpan.FromSeconds(3)); await Send(packet.ToString(), stream, log); await Task.Delay(TimeSpan.FromSeconds(3)); } }
public static async Task RunAsync([TimerTrigger("45 */5 * * * *")] TimerInfo myTimer, ILogger log) { log.LogInformation($"Loading data from ElasticSearch..."); try { var elasticSearchCredentials = GetEnvironmentVariable("ElasticSearchCredentials"); var httpClient = new HttpClient(); var byteArray = Encoding.ASCII.GetBytes(elasticSearchCredentials); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); var uri = $"http://elasticsearch.saintgimp.org/logstash-temperatures/_search"; var query = @"{ ""query"": { ""match_all"": {} }, ""size"": ""1"", ""sort"": [ { ""@timestamp"": { ""order"": ""desc"" } } ] }"; var response = await httpClient.PostAsync(uri, new StringContent(query, Encoding.ASCII, "application/json")); var responseContent = await response.Content.ReadAsStringAsync(); log.LogInformation(responseContent); dynamic data = JObject.Parse(responseContent); var mostRecentRecord = data.hits.hits[0]._source; DateTime timestamp = mostRecentRecord["@timestamp"]; var timeSinceLastUpdate = DateTime.UtcNow - timestamp; log.LogInformation($"Timestamp is {timestamp}, difference is {timeSinceLastUpdate}"); if (timeSinceLastUpdate > TimeSpan.FromMinutes(30)) { throw new ApplicationException("No new data"); } var temperature = (int)Math.Round((double)mostRecentRecord.t3 * 9.0 / 5.0 + 32.0); log.LogInformation($"Temperature is {temperature} F"); var packet = new AprsWeatherDataPacket( accountNumber: "EW9714", equipmentIdentifier: "custom", latitudeInDegrees: 47.697201f, longitudeInDegrees: -122.063844f, temperatureInFahrenheit: temperature); await SendDataToCwop(packet, log); log.LogInformation("Everything's fine here, we're all fine, how are you?"); } catch (Exception e) { log.LogInformation(e.ToString()); SendTwitterNotification("Hey, I think the temperature sensor is offline!", log); } }