-
-
Save Mnkai/ca16a55e7b8b473db01ad347527f6127 to your computer and use it in GitHub Desktop.
Hack for getting wx reports via inreach messaging.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"strings" | |
) | |
func main() { | |
for _, forecastSection := range weatherForecast()[0:1] { | |
sendInReachMessage(forecastSection) | |
} | |
} | |
func sendInReachMessage(message string) { | |
trimmed := strings.TrimSpace(message) | |
Igo | |
var toSend []string | |
messageFragment := "" | |
for _, line := range strings.Split(trimmed, "\n") { | |
for _, word := range strings.Split(line, " ") { | |
trimmedWord := strings.TrimSpace(word) | |
if (len(messageFragment) + len(trimmedWord)) > 160 { | |
toSend = append(toSend, messageFragment) | |
messageFragment = trimmedWord | |
} else { | |
messageFragment = messageFragment + " " + trimmedWord | |
} | |
} | |
} | |
toSend = append(toSend, messageFragment) | |
fmt.Printf("Sending %d inreach messages.\n", len(toSend)) | |
for _, fragmet := range toSend { | |
trimmedFragment := strings.TrimSpace(fragmet) | |
if trimmedFragment != "" { | |
fmt.Printf("Sending inreach message : %s\n---\n", trimmedFragment) | |
data := url.Values{ | |
"ReplyAddress" : []string {"[email protected]"}, | |
"ReplyMessage" : []string {trimmedFragment}, | |
"Guid" : []string {"guid grabbed from watching traffic in the inreach messaging web ui"}, | |
} | |
resp, err := http.PostForm("https://us0.explore.garmin.com/TextMessage/TxtMsg", data) | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Errorf("Unable to reach the InReach messaging service : %s", body, err) | |
} | |
} | |
} | |
} | |
func weatherForecast() []string { | |
// Change EKA out with local forecast options | |
resp, err := http.Get("https://forecast.weather.gov/product.php?site=EKA&issuedby=EKA&product=AFD&format=txt&version=1&glossary=0") | |
if err != nil { | |
fmt.Errorf("Unable to reach the NWS", err) | |
return nil | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
strBody := string(body) | |
start := strings.Index(strBody, "AREA FORECAST DISCUSSION") | |
end := strings.LastIndex(strBody, "$$") | |
wxReport := strBody[start:end] | |
fmt.Printf("Got weather report : %s", wxReport) | |
return strings.Split(wxReport, "&&") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment