-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (47 loc) · 1.25 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
package main
import (
"log"
"time"
"github.com/tarm/serial"
"github.com/dovics/borja/cache"
"github.com/dovics/borja/device/light_sensor"
"github.com/dovics/borja/exporter"
"github.com/dovics/borja/operator"
"github.com/dovics/borja/reporter"
"github.com/dovics/borja/util/trigger"
)
const boltdbName = "bolt"
func main() {
config := &serial.Config{Name: "COM4", Baud: 9600, ReadTimeout: time.Second * 5}
sensor, err := light_sensor.ConnectBySerial(config)
if err != nil {
log.Fatal(err)
}
lightOperator := operator.NewLightOperator(sensor)
c, err := cache.NewCache(boltdbName)
if err != nil {
log.Fatal(err)
}
reporter := reporter.New("")
if err := reporter.SetTrigger(trigger.NewTimeTrigger(time.Second)); err != nil {
log.Fatal(err)
}
if err := reporter.SetCache(c); err != nil {
log.Fatal(err)
}
reporter.Register("light", lightOperator.QueryLight)
go reporter.Run()
exporter := exporter.NewExporter()
exporter.Register("light", lightOperator.QueryLight)
exporter.Register("data", func() (interface{}, error) {
data, err := c.GetAfter(time.Now().Add(-time.Minute * 15))
if err != nil {
return nil, err
}
if err := c.ClearBefore(time.Now()); err != nil {
return nil, err
}
return data, nil
})
exporter.Run()
}