From 374b0e375c540a883c22434f08f4823d5e264258 Mon Sep 17 00:00:00 2001
From: Leo Herzog <leo@herzog.tech>
Date: Mon, 16 Sep 2024 17:22:02 -0400
Subject: [PATCH] Add MET (UK) WOW Destination

Closes #27
---
 code.gs | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/code.gs b/code.gs
index 6f7d733..38c61a6 100644
--- a/code.gs
+++ b/code.gs
@@ -60,6 +60,10 @@ const updateWindGuru = false;
 const windGuruStationUID = 'xxxxxxxxxx';
 const windGuruStationPassword = 'xxxxxxxxxxxxxxxx';
 ///
+const updateWOW = false;
+const wowSiteID = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
+const wowAuthKey = 'xxxxxx';
+///
 const updateCWOP = false;
 const cwopStationIDOrHamCallsign = 'CW0001';
 const cwopValidationCode = null;
@@ -75,7 +79,7 @@ const cwopValidationCode = null;
 
 */
 
-let version = 'v2.7.1';
+let version = 'v2.8.0';
 
 function Schedule() {
   ScriptApp.getProjectTriggers().forEach(trigger => ScriptApp.deleteTrigger(trigger));
@@ -115,6 +119,7 @@ function Schedule() {
   if (updateWeatherCloud) ScriptApp.newTrigger('updateWeatherCloud_').timeBased().everyMinutes(hasWeatherCloudPro ? 1 : 10).create();
   if (updateOpenWeatherMap) ScriptApp.newTrigger('updateOpenWeatherMap_').timeBased().everyMinutes(1).create();
   if (updateWindGuru) ScriptApp.newTrigger('updateWindGuru_').timeBased().everyMinutes(1).create();
+  if (updateWOW) ScriptApp.newTrigger('updateWOW_').timeBased().everyMinutes(5).create();
   if (updateCWOP) ScriptApp.newTrigger('updateCWOP_').timeBased().everyMinutes(5).create();
   console.log('Scheduled! Check Executions ☰▶ tab for status.');
   checkGithubReleaseVersion_();
@@ -1025,6 +1030,34 @@ function updateWindGuru_() {
   
 }
 
+// https://wow.metoffice.gov.uk/support/dataformats#automatic
+function updateWOW_() {
+  
+  let conditions = JSON.parse(CacheService.getScriptCache().get('conditions'));
+  
+  let request = 'http://wow.metoffice.gov.uk/automaticreading?';
+  request += 'siteid=' + wowSiteID;
+  request += '&siteAuthenticationKey=' + wowAuthKey;
+  request += '&dateutc=' + encodeURIComponent(Utilities.formatDate(new Date(conditions.time), 'UTC', 'yyyy-MM-dd HH:mm:ss'));
+  if (conditions.temp != null) request += '&tempf=' + conditions.temp.f;
+  if (conditions.dewpoint != null) request += '&dewptf=' + conditions.dewpoint.f;
+  if (conditions.windSpeed != null) request += '&windspeedmph=' + conditions.windSpeed.mph;
+  if (conditions.windGust != null) request += '&windgustmph=' + conditions.windGust.mph;
+  if (conditions.winddir != null) request += '&winddir=' + conditions.winddir;
+  if (conditions.pressure != null) request += '&baromin=' + conditions.pressure.inHg;
+  if (conditions.humidity != null) request += '&humidity=' + conditions.humidity;
+  if (conditions.precipRate != null) request += '&rainin=' + conditions.precipRate.in;
+  if (conditions.precipSinceMidnight != null) request += '&dailyrainin=' + conditions.precipSinceMidnight.in;
+  request += '&softwaretype=appsscriptforwarder' + version;
+
+  let response = UrlFetchApp.fetch(request).getContentText();
+  
+  console.log(response);
+  
+  return response;
+  
+}
+
 // Apps Script has no ability to open a TCP socket directly,
 // so we're using https://cwop.rest/ as an intermediary
 function updateCWOP_() {