-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalk.go
137 lines (111 loc) · 3.32 KB
/
alk.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"github.com/codyja/alkatronic/api"
"io/ioutil"
"log"
"os"
"sync"
"time"
)
// alkLoop starts up a ticker that calls GetAlkData every X minutes
func alkLoop(c *api.AlkatronicClient, pg *PostgresAlkatronic) {
ticker := time.NewTicker(30 * time.Minute)
for {
GetAlkData(c, pg)
<-ticker.C
}
}
// alkAuth performs initial authentication or reauthentication as needed
func alkAuth(c *api.AlkatronicClient, username string, password string, wg *sync.WaitGroup) {
// Read in home directory to read and write token file to
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Error getting home directory: %s", err)
}
// Location to store token inside file
tokenFileLocation := fmt.Sprintf("%s/.alkatronic", home)
// Read existing token from token storage file
existingToken, err := ioutil.ReadFile(tokenFileLocation)
if err != nil {
log.Printf("Checking for existing token file: %s", err)
}
// Authenticate if there's no existing token file, otherwise call SetAccessToken to reuse existing token on subsequent calls
if string(existingToken) == "" {
c.Authenticate(username, password)
// write token locally
tokenBytes := []byte(fmt.Sprintf("%s",c.AccessToken()))
err = ioutil.WriteFile(tokenFileLocation, tokenBytes, 0600)
if err != nil {
log.Fatalf("Error writting token file: %s", err)
}
} else {
log.Printf("Using existing access token read from ~/.alkatronic")
c.SetAccessToken(string(existingToken))
}
wg.Done()
}
// GetAlkData calls GetLatestResult to get the latest record then calls InsertRecord to log to the DB
func GetAlkData(c *api.AlkatronicClient, pg *PostgresAlkatronic) {
devices, err := c.GetDevices()
if err != nil {
log.Fatalf("Error getting devices: %s", err)
}
for _, d := range devices.Data {
r, err := c.GetLatestResult(d.DeviceID)
if err != nil {
log.Fatalf("Error getting last test result: %s", err)
}
log.Printf(
"Latest test result: Device Name: %s, Record ID: %d, KH_Value: %.2f, Create Time: %s\n",
d.FriendlyName,
r.RecordID,
api.ConvertKh(r.KhValue),
time.Unix(r.CreateTime, 0).Format(time.RFC822Z))
r.KhValue = api.ConvertKh(r.KhValue)
device := api.Device {
UpperKh: api.ConvertKh(d.UpperKh),
LowerKh: api.ConvertKh(d.LowerKh),
}
err = pg.InsertRecord(r, device)
if err != nil {
log.Fatalf("error calling InsertRecord(): %s", err)
}
}
}
func GetAllAlkData(c *api.AlkatronicClient, pg *PostgresAlkatronic, days int) {
devices, err := c.GetDevices()
if err != nil {
log.Fatalf("Error getting devices: %s", err)
}
for _, d := range devices.Data {
records, err := c.GetRecords(d.DeviceID, days)
if err != nil {
log.Fatalf("Error getting device records: %s", err)
}
for _, r := range records.Data {
r.KhValue = api.ConvertKh(r.KhValue)
device := api.Device {
UpperKh: api.ConvertKh(d.UpperKh),
LowerKh: api.ConvertKh(d.LowerKh),
}
log.Printf(
"Latest test result: Device Name: %s, Record ID: %d, KH_Value: %.2f, Create Time: %s\n",
d.FriendlyName,
r.RecordID,
api.ConvertKh(r.KhValue),
time.Unix(r.CreateTime, 0).Format(time.RFC822Z))
err = pg.InsertRecord(r, device)
if err != nil {
log.Fatalf("error calling InsertRecord(): %s", err)
}
}
}
}
func boolConverter(i int) bool {
if i == 0 {
return false
} else {
return true
}
}