-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfakebluetooth.go
74 lines (53 loc) · 1.42 KB
/
fakebluetooth.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
package main
import (
"bufio"
"encoding/hex"
"os"
"time"
log "github.com/sirupsen/logrus"
)
func fakeBluetoothLogger() log.FieldLogger {
return log.WithField("component", "fake-bluetooth")
}
/*
FakeBluetooth fakes receiving data, triggering the plugins
It will pretend to receive data from a scale, which
should trigger all the (configured) plugins.
You can use this to test configurations and/or new plugins
*/
func FakeBluetooth() {
fakeBluetoothLogger().Infoln("Sending fake data from 'testdata' to the indicator parser... (waiting 5 seconds)")
f, err := os.Open("testdata")
if err != nil {
fakeBluetoothLogger().Fatalf("error opening file: %s", err)
}
r := bufio.NewReader(f)
s, e := readln(r)
time.Sleep(5 * time.Second)
for e == nil {
fakeBluetoothLogger().Infoln("Sending data: ", s)
h, err := hex.DecodeString(s)
if err != nil {
fakeBluetoothLogger().Fatalf("error decoding line: %v", err)
}
go decodeData(h)
time.Sleep(100 * time.Millisecond)
s, e = readln(r)
}
time.Sleep(1 * time.Second)
fakeBluetoothLogger().Infoln("Finished sending fake data from 'testdata' to the indicator parser. Waiting in an infinite loop now.")
go debounce()
select {}
}
func readln(r *bufio.Reader) (string, error) {
var (
isPrefix = true
err error
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}