Switch Garmin messaging from IPC Inbound API to MapShare API
This commit is contained in:
38
main.go
38
main.go
@@ -13,12 +13,12 @@ type PHandler struct {
|
||||
tmpl *template.Template
|
||||
pd *pdClient
|
||||
gc *garminClient
|
||||
garminIMEIs []string
|
||||
garminSender string
|
||||
garminDeviceIDs []string
|
||||
garminSender string
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
func NewPHandler(pdRoutingKey, garminAPIKey string, garminIMEIs []string, garminSender string) (*PHandler, error) {
|
||||
func NewPHandler(pdRoutingKey, garminMapshareID string, garminDeviceIDs []string, garminSender string) (*PHandler, error) {
|
||||
tmpl := template.New("index.html")
|
||||
|
||||
tmpl.Funcs(template.FuncMap{
|
||||
@@ -33,13 +33,13 @@ func NewPHandler(pdRoutingKey, garminAPIKey string, garminIMEIs []string, garmin
|
||||
ph := &PHandler{
|
||||
tmpl: tmpl,
|
||||
pd: newPDClient(pdRoutingKey),
|
||||
garminIMEIs: garminIMEIs,
|
||||
garminDeviceIDs: garminDeviceIDs,
|
||||
garminSender: garminSender,
|
||||
mux: http.NewServeMux(),
|
||||
}
|
||||
|
||||
if garminAPIKey != "" {
|
||||
ph.gc = newGarminClient(garminAPIKey)
|
||||
if garminMapshareID != "" {
|
||||
ph.gc = newGarminClient(garminMapshareID)
|
||||
}
|
||||
|
||||
ph.mux.HandleFunc("/{$}", ph.serveRoot)
|
||||
@@ -86,7 +86,7 @@ func (ph *PHandler) sendAlert(m string) error {
|
||||
|
||||
if ph.gc != nil {
|
||||
go func() {
|
||||
err := ph.gc.sendMessage(ph.garminIMEIs, ph.garminSender, m)
|
||||
err := ph.gc.sendMessage(ph.garminDeviceIDs, ph.garminSender, m)
|
||||
if err != nil {
|
||||
res <- fmt.Errorf("Error sending to Garmin: %w", err)
|
||||
} else {
|
||||
@@ -150,28 +150,28 @@ func main() {
|
||||
log.Fatalf("please set PD_ROUTING_KEY")
|
||||
}
|
||||
|
||||
garminAPIKey := os.Getenv("GARMIN_API_KEY")
|
||||
garminMapshareID := os.Getenv("GARMIN_MAPSHARE_ID")
|
||||
|
||||
garminIMEIStr := os.Getenv("GARMIN_IMEI")
|
||||
if garminAPIKey != "" && garminIMEIStr == "" {
|
||||
log.Fatalf("please set GARMIN_IMEI")
|
||||
garminDeviceIDStr := os.Getenv("GARMIN_DEVICE_IDS")
|
||||
if garminMapshareID != "" && garminDeviceIDStr == "" {
|
||||
log.Fatalf("please set GARMIN_DEVICE_IDS")
|
||||
}
|
||||
var garminIMEIs []string
|
||||
if garminIMEIStr != "" {
|
||||
for _, imei := range strings.Split(garminIMEIStr, ",") {
|
||||
imei = strings.TrimSpace(imei)
|
||||
if imei != "" {
|
||||
garminIMEIs = append(garminIMEIs, imei)
|
||||
var garminDeviceIDs []string
|
||||
if garminDeviceIDStr != "" {
|
||||
for _, id := range strings.Split(garminDeviceIDStr, ",") {
|
||||
id = strings.TrimSpace(id)
|
||||
if id != "" {
|
||||
garminDeviceIDs = append(garminDeviceIDs, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
garminSender := os.Getenv("GARMIN_SENDER")
|
||||
if garminAPIKey != "" && garminSender == "" {
|
||||
if garminMapshareID != "" && garminSender == "" {
|
||||
log.Fatalf("please set GARMIN_SENDER")
|
||||
}
|
||||
|
||||
ph, err := NewPHandler(pdRoutingKey, garminAPIKey, garminIMEIs, garminSender)
|
||||
ph, err := NewPHandler(pdRoutingKey, garminMapshareID, garminDeviceIDs, garminSender)
|
||||
if err != nil {
|
||||
log.Fatalf("NewPHandler: %s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user