-
Notifications
You must be signed in to change notification settings - Fork 0
/
templogger.go
146 lines (125 loc) · 3.4 KB
/
templogger.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
package main
import (
"database/sql"
"flag"
"fmt"
"github.com/magiconair/properties"
_ "github.com/mattn/go-sqlite3"
"github.com/pkg/profile"
"github.com/yryz/ds18b20"
"log"
"net/http"
_ "net/http/pprof"
"runtime"
"strconv"
"strings"
"time"
)
var db *sql.DB
var cfg Config
type Config struct {
DbFile string `properties:"dbfile"`
Interval int `properties:"interval"`
SensorNames string `properties:"sensornames"`
ExpectedSensor string `properties:"expectedsensors"`
SensorCalibration string `properties:"sensorcalibration"`
}
func Check(e error) {
if e != nil {
panic(e)
}
}
func initdb() {
var err error
db, err = sql.Open("sqlite3", cfg.DbFile)
Check(err)
statement, prepError := db.Prepare("CREATE TABLE IF NOT EXISTS reading (Id INTEGER PRIMARY KEY AUTOINCREMENT, Sensor INTEGER, Temperature NUMERIC, Datetime DATETIME)")
Check(prepError)
_, err = statement.Exec()
Check(err)
if err := db.Ping(); err != nil {
log.Fatalf("unable to reach database: %v", err)
}
fmt.Printf("%s: Database is reachable\n", time.Now().Format("2006-01-02-15:04:05"))
}
func closedb() {
err := db.Close()
Check(err)
}
func saveToDatabase(Sensor int, Temperature float64) {
statement, err := db.Prepare("INSERT INTO reading (Sensor, Temperature, Datetime) VALUES (?,?,CURRENT_TIMESTAMP)")
Check(err)
_, err = statement.Exec(Sensor, Temperature)
Check(err)
}
func main() {
defer profile.Start(profile.MemProfile).Stop()
go func() {
err := http.ListenAndServe(":8080", nil)
Check(err)
}()
propPtr := flag.String("prop", "templogger.properties", "a string")
flag.Parse()
p := properties.MustLoadFile(*propPtr, properties.ISO_8859_1)
if err := p.Decode(&cfg); err != nil {
log.Fatal(err)
}
sensorNames := strings.Split(cfg.SensorNames, ",")
expectedSensors := strings.Split(cfg.ExpectedSensor, ",")
sensorCalibration := strings.Split(cfg.SensorCalibration, ",")
calib := make(map[string]float64)
dbinitialized := false
initTime := time.Now()
sensors, err := ds18b20.Sensors()
if err != nil {
panic(err)
}
if len(expectedSensors) != len(sensorCalibration) {
fmt.Printf("Expected equal number sensors and calibration, setting calibration to 0")
for _, c := range sensors {
calib[c] = 0
}
} else {
for k, c := range sensors {
if c != expectedSensors[k] {
fmt.Printf("Expected sensor:%s, found sensor: %s ", expectedSensors[k], c)
}
}
for k, c := range sensorCalibration {
x, err := strconv.ParseFloat(c, 64)
if err != nil {
panic(err)
}
calib[expectedSensors[k]] = x
}
}
fmt.Printf("sensor IDs: %v\n", sensorNames)
fmt.Printf("sensor IDs: %v\n", sensorCalibration)
fmt.Printf("sensor IDs: %v\n", sensors)
/*i:= 1 ; i<10;i++*/
for {
if !dbinitialized {
initdb()
dbinitialized = true
initTime = time.Now()
}
loopStart := time.Now()
for key, sensor := range sensors {
t, err := ds18b20.Temperature(sensor)
t = t - calib[sensor]
if err == nil {
//fmt.Printf("sensor: %s key: %v temperature: %.2f°C \n", sensor, key, t)
saveToDatabase(key, t)
}
}
// TODO Workaround for Memory leak in database - find root cause
if time.Since(initTime) > 24*time.Hour {
closedb()
time.Sleep(time.Second)
runtime.GC()
dbinitialized = false
}
loopTime := time.Since(loopStart)
time.Sleep(time.Duration(cfg.Interval)*time.Second - loopTime) //TODO Should be possible with better time drift control
}
}