-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (116 loc) · 3.15 KB
/
main.go
File metadata and controls
138 lines (116 loc) · 3.15 KB
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
package main
import (
"context"
"flag"
"fmt"
"github.com/go-ble/ble"
"github.com/go-ble/ble/examples/lib/dev"
"github.com/go-ble/ble/linux"
"github.com/go-ble/ble/linux/hci/cmd"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
func main() {
var listen = flag.String("listen", ":2112", "metrics listen address")
flag.Parse()
// https://github.com/go-ble/ble/tree/master/examples
d, err := dev.NewDevice("default")
if err != nil {
log.Fatalf("can't new device : %s", err)
}
ble.SetDefaultDevice(d)
dev := d.(*linux.Device)
if err := dev.HCI.Send(&cmd.LESetScanParameters{
LEScanType: 0x01, // 0x00: passive, 0x01: active
LEScanInterval: 0x0004, // 0x0004 - 0x4000; N * 0.625msec
LEScanWindow: 0x0004, // 0x0004 - 0x4000; N * 0.625msec
OwnAddressType: 0x01, // 0x00: public, 0x01: random
ScanningFilterPolicy: 0x00, // 0x00: accept all, 0x01: ignore non-white-listed.
}, nil); err != nil {
panic(err)
}
collector := &SwitchBotCollector{}
prometheus.MustRegister(collector)
ctx, cancel := context.WithCancel(context.TODO())
go func() {
http.Handle("/metrics", promhttp.Handler())
fmt.Printf("listen in %s\n", *listen)
err := http.ListenAndServe(*listen, nil)
if err != nil {
panic(err)
}
cancel()
}()
ble.Scan(ctx, true, advHandler, nil)
}
var deviceStatuses map[string]DeviceStatus = map[string]DeviceStatus{}
type DeviceStatus struct {
Temperature float64
Humidity int
Battery int
Updated time.Time
}
func advHandler(a ble.Advertisement) {
found := false
for _, uuid := range a.Services() {
if uuid.String() == "cba20d00224d11e69fb80002a5d5c51b" {
found = true
}
}
if !found {
return
}
for _, data := range a.ServiceData() {
temp := float64(data.Data[4] & 0x7f)
temp += float64(data.Data[3]) / 10
humidity := int(data.Data[5] & 0x7f)
battery := int(data.Data[2])
fmt.Printf("[%s] temp: %.1f humidity: %d battery: %d\n", a.Addr(), temp, humidity, battery)
deviceStatuses[a.Addr().String()] = DeviceStatus{
Temperature: temp,
Humidity: humidity,
Battery: battery,
Updated: time.Now(),
}
}
}
var ns = "temperature_collector"
type SwitchBotCollector struct {
}
func (*SwitchBotCollector) Describe(chan<- *prometheus.Desc) {
}
func (*SwitchBotCollector) Collect(ch chan<- prometheus.Metric) {
current := time.Now()
for addr, status := range deviceStatuses {
if current.Sub(status.Updated) > 1*time.Minute {
continue
}
labels := map[string]string{
"hw": addr,
}
tmpGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Name: "temperature",
ConstLabels: labels,
})
tmpGauge.Set(status.Temperature)
humGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Name: "humidity",
ConstLabels: labels,
})
humGauge.Set(float64(status.Humidity))
batteryGauge := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: ns,
Name: "battery",
ConstLabels: labels,
})
batteryGauge.Set(float64(status.Battery))
ch <- tmpGauge
ch <- humGauge
ch <- batteryGauge
}
}