-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempserver.go
191 lines (161 loc) · 5.28 KB
/
tempserver.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
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
"github.com/gorilla/mux"
"github.com/magiconair/properties"
_ "github.com/mattn/go-sqlite3"
"log"
"net/http"
"os"
"time"
)
var db *sql.DB
var cfg Config
type Config struct {
DbFile string `properties:"dbfile"`
Port string `properties:"port"`
}
type Reading struct {
Id int `json:"id"`
Sensor int `json:"sensor"`
TimeStamp time.Time `json:"timeStamp"`
Temperature float64 `json:"temperature"`
}
func generateLineItems(name int, count int) []opts.LineData { //nolint:unparam
items := make([]opts.LineData, 0)
rows, err := db.Query("SELECT Id FROM reading ORDER BY ID DESC LIMIT 1")
Check(err)
defer rows.Close()
lastid := 0
for rows.Next() {
read := Reading{}
if err = rows.Scan(&read.Id); err != nil {
log.Fatalf("could not scan row: %v", err)
}
lastid = read.Id
}
rows, err = db.Query("SELECT Sensor, Temperature FROM reading WHERE ID >= $1 AND Sensor = $2 LIMIT $3", lastid-count, name, count)
// rows, err := db.Query("SELECT Sensor, Temperature FROM reading ORDER BY ID DESC LIMIT $1 WHERE Sensor = $2", count, name)
Check(err)
defer rows.Close()
for rows.Next() {
read := Reading{}
// create an instance of `Bird` and write the result of the current row into it
if err = rows.Scan(&read.Sensor, &read.Temperature); err != nil {
log.Fatalf("could not scan row: %v", err)
}
//fmt.Printf("found read: %+v\n", read)
// append the current instance to the slice of birds
items = append(items, opts.LineData{Value: read.Temperature})
}
return items
}
func httpserver(w http.ResponseWriter, _ *http.Request) {
// create a new line instance
line := charts.NewLine()
// set some global options like Title/Legend/ToolTip or anything else
line.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{Theme: types.ThemeWesteros}),
charts.WithTitleOpts(opts.Title{
Title: "Line example in Westeros theme",
Subtitle: "Line chart rendered by the http server this time",
}))
// Put data into instance
line.SetXAxis([]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}).
AddSeries("28-1b61221e64ff", generateLineItems(1, 100)).
AddSeries("28-7167221e64ff", generateLineItems(2, 100)).
AddSeries("28-7167221e64ff", generateLineItems(3, 100)).
AddSeries("28-7167221e64ff", generateLineItems(4, 100)).
AddSeries("28-7167221e64ff", generateLineItems(5, 100)).
SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{Smooth: true}))
err := line.Render(w)
Check(err)
}
func initdb() {
var err error
db, err = sql.Open("sqlite3", cfg.DbFile)
Check(err)
}
func Check(e error) {
if e != nil {
panic(e)
}
}
func returnLast(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnLast")
items := make([]Reading, 0)
rows, err := db.Query("SELECT Id, Sensor, Temperature, Datetime FROM reading ORDER BY ID DESC LIMIT $1", 50)
Check(err)
defer rows.Close()
for rows.Next() {
read := Reading{}
timestring := ""
// create an instance of `Bird` and write the result of the current row into it
if err = rows.Scan(&read.Id, &read.Sensor, &read.Temperature, ×tring); err != nil {
log.Fatalf("could not scan row: %v", err)
}
layout := "2006-01-02T15:04:05Z"
temp, err2 := time.Parse(layout, timestring)
Check(err2)
read.TimeStamp = temp
//fmt.Printf("found read: %+v + %s\n", read, timestring)
// append the current instance to the slice of birds
items = append(items, read)
}
err = json.NewEncoder(w).Encode(items)
Check(err)
}
func getStart(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: getStart")
eventID := mux.Vars(r)["id"]
items := make([]Reading, 0)
rows, err := db.Query("SELECT Id, Sensor, Temperature, Datetime FROM reading WHERE ID >= ? LIMIT $1", eventID, 50)
Check(err)
defer rows.Close()
for rows.Next() {
read := Reading{}
timestring := ""
// create an instance of `Bird` and write the result of the current row into it
if err = rows.Scan(&read.Id, &read.Sensor, &read.Temperature, ×tring); err != nil {
log.Fatalf("could not scan row: %v", err)
}
layout := "2006-01-02T15:04:05Z"
temp, err2 := time.Parse(layout, timestring)
Check(err2)
read.TimeStamp = temp.Add(time.Hour * 2)
//fmt.Printf("found read: %+v + %s\n", read, timestring)
// append the current instance to the slice of birds
items = append(items, read)
}
err = json.NewEncoder(w).Encode(items)
Check(err)
}
func doQuit(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: doQuit")
os.Exit(0)
}
func main() {
propPtr := flag.String("prop", "tempserver.properties", "a string")
flag.Parse()
p := properties.MustLoadFile(*propPtr, properties.ISO_8859_1)
if err := p.Decode(&cfg); err != nil {
log.Fatal(err)
}
initdb()
if err := db.Ping(); err != nil {
log.Fatalf("unable to reach database: %v", err)
}
fmt.Println("database is reachable")
myRouter := mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/", httpserver)
myRouter.HandleFunc("/last", returnLast)
myRouter.HandleFunc("/quit", doQuit)
myRouter.HandleFunc("/start/{id}", getStart).Methods("GET")
log.Fatal(http.ListenAndServe(":" + cfg.Port, myRouter))
}