-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharmstrong.go
More file actions
94 lines (81 loc) · 2.44 KB
/
armstrong.go
File metadata and controls
94 lines (81 loc) · 2.44 KB
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
// Based on https://www.kluenter.de/garmin-ephemeris-files-and-linux/ and
// EPO_Downloader.rb in https://github.com/scrapper/postrunner (GPLv2)
// and information in this Garmin forum post:
// https://forums.garmin.com/outdoor-recreation/outdoor-recreation-archive/f/fenix-5-series/207977/epo-expired/1166435
// = EPO_Downloader.rb -- PostRunner - Manage the data from your Garmin sport devices.
//
// Copyright (c) 2015 by Chris Schlaeger <cs@taskjuggler.org>
//
// armstrong.go
//
// Copyright (c) 2016 by Steven Maude
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as
// published by the Free Software Foundation.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
// Garmin forum post:
// "…each EPO SET is 2304 bytes"
const epoLength = 2304
// retrieveData makes a HTTP request to get data and returns the body as []byte if successful.
func retrieveData() ([]byte, error) {
url := "https://epodownload.mediatek.com/EPO.DAT"
c := &http.Client{
Timeout: 20 * time.Second,
}
resp, err := c.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// checkDataLength checks the EPO data length; if not as expected, returns an error.
func checkDataLength(data []byte) error {
dataLength := len(data)
if dataLength != 120*epoLength {
return fmt.Errorf("EPO data has unexpected length: %v", dataLength)
}
return nil
}
// trimEPOData trims the MediaTek data sufficiently for a Garmin watch.
func trimEPOData(data []byte) []byte {
// Garmin forum post:
// "…even with such a clean file, the Garmin watches use only a max of one-digit
// days, ie 9 days of data…"
const days = 9
// Garmin forum post:
// "…each EPO SET … has 6 hours of satellite locations…"
const epoSetsCount = 4 * days
return data[:(epoSetsCount * epoLength)]
}
// main retrieves EPO data, checks it, cleans it and writes it to disk.
func main() {
fmt.Println("Retrieving EPO data from Mediatek's servers...")
rawEPOData, err := retrieveData()
if err != nil {
log.Fatal(err)
}
fmt.Println("Processing EPO.BIN...")
err = checkDataLength(rawEPOData)
if err != nil {
log.Fatal(err)
}
outData := trimEPOData(rawEPOData)
err = ioutil.WriteFile("EPO.BIN", outData, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Done! EPO.BIN saved.")
}