-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
220 lines (175 loc) · 4.85 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"bytes"
"encoding/json"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/tarm/serial"
)
var (
serverURL = kingpin.Arg("server", "URL of server to POST results to").Required().String()
fileDir = "/home/pi/backup_readings/"
backupFileName = "backup_readings.json"
seq = Readings{}
)
type Readings []Reading
type Reading struct {
Value string `json:"value"`
Time int64 `json:"time"`
Host string `json:"host"`
}
func main() {
kingpin.Parse()
r := Reading{}
r.setHostname()
r.setReadingTime()
r.setValueFromSerial()
status := r.sendJSONToServer()
log.Printf("Status Code: %v", status)
if strings.HasPrefix(status, "200") {
log.Print("Successfully POSTed reading to server")
hasBackupReadings, backupReadings := r.hasBackupReadingsInJSONFile()
if hasBackupReadings {
log.Print(fmt.Sprint("There are ", len(backupReadings), " readings in backup JSON file"))
sendBackupReadingsToServer(backupReadings)
}
} else {
log.Print("Couldn't POST to server, saving in backup file to try again later")
r.writeReadingToFile(fileDir)
}
}
func sendBackupReadingsToServer(backupReadings Readings) {
for index, reading := range backupReadings {
log.Print(fmt.Sprint("Sending Reading ", index+1, " of ", len(backupReadings)))
status := reading.sendJSONToServer()
if strings.HasPrefix(status, "200") {
log.Print("Successfully sent reading to server")
reading.removeReadingFromBackupFile()
}
}
}
func check(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
func writeJSONToFile(json []byte) error {
path := fmt.Sprint(fileDir, backupFileName)
werr := ioutil.WriteFile(path, json, os.ModePerm)
return werr
}
func readSerialValue() (value string) {
log.Print("Attempting to read from serial device...")
c := &serial.Config{Name: "/dev/ttyAMA0", Baud: 9600, ReadTimeout: time.Second * 10}
s, err := serial.OpenPort(c)
check(err)
buf := make([]byte, 128)
n, err := s.Read(buf)
check(err)
return strings.Trim(string(buf[:n]), "\r")
}
func (readings Readings) pos(reading Reading) int {
for index, v := range readings {
if v.Time == reading.Time {
return index
}
}
return -1
}
func (r *Reading) removeReadingFromBackupFile() {
path := fmt.Sprint(fileDir, backupFileName)
readingsFromJSONFile := Readings{}
file, e := ioutil.ReadFile(path)
check(e)
dec := json.NewDecoder(strings.NewReader(string(file)))
decErr := dec.Decode(&readingsFromJSONFile)
check(decErr)
indexOfReading := readingsFromJSONFile.pos(*r)
// Remove the current reading from the slice of readings in the file
readingsFromJSONFile = append(readingsFromJSONFile[:indexOfReading], readingsFromJSONFile[indexOfReading+1:]...)
j, err := json.Marshal(readingsFromJSONFile)
check(err)
log.Print("Removing reading from backup file...")
werr := writeJSONToFile(j)
check(werr)
}
func (r *Reading) hasBackupReadingsInJSONFile() (bool, Readings) {
readingsFromJSONFile := Readings{}
path := fmt.Sprint(fileDir, backupFileName)
file, e := ioutil.ReadFile(path)
check(e)
dec := json.NewDecoder(strings.NewReader(string(file)))
decErr := dec.Decode(&readingsFromJSONFile)
check(decErr)
if len(readingsFromJSONFile) > 0 {
return true, readingsFromJSONFile
} else {
return false, readingsFromJSONFile
}
}
func (r *Reading) writeReadingToFile(dir string) {
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
os.Mkdir(dir, 0755)
} else {
log.Println(err)
}
}
path := fmt.Sprint(dir, backupFileName)
file, e := ioutil.ReadFile(path)
check(e)
dec := json.NewDecoder(strings.NewReader(string(file)))
decErr := dec.Decode(&seq)
check(decErr)
seq = append(seq, *r)
j, err := json.Marshal(seq)
check(err)
werr := writeJSONToFile(j)
check(werr)
}
func (r *Reading) setReadingTime() {
r.Time = time.Now().Unix()
}
func (r *Reading) setHostname() {
h, err := os.Hostname()
check(err)
r.Host = h
}
func (r *Reading) setValueFromSerial() {
var success bool
var retries int
// Retry up to 5 times getting a correct reading from the serial pin
for success != true && retries < 4 {
retries++
val := readSerialValue()
// A correct reading should always look like this: 'R0569'
// i.e. an "R", and 4 ints after the "R"
match, _ := regexp.MatchString("^R[0-9]{4}", val)
if match && len(val) == 5 {
success = true
r.Value = val
} else {
log.Print("Couldn't get a correct reading from sensor, retrying...")
}
}
if success != true {
panic("Was unable to get a correct reading from sensor")
}
}
func (r *Reading) sendJSONToServer() (status string) {
b, err := json.Marshal(r)
req, err := http.NewRequest("POST", *serverURL, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
check(err)
defer resp.Body.Close()
return resp.Status
}