-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (160 loc) · 4.29 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
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
//
// author: armaan roshani
//
// some portions derived from:
// - github.com/go-ble/ble/examples/basic/scanner
// - github.com/eclipse/paho.mqtt.golang/cmd/sample
// - socketloop.com/tutorials/golang-force-your-program-to-run-with-root-permissions
//
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os/exec" // needed for checkRoot()
"strconv" //
"time"
"github.com/armaanhammer/ble"
"github.com/armaanhammer/ble/examples/lib/dev"
"github.com/pkg/errors"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
type advType struct {
c chan string
name string
}
// globals
// before v1.0, need to determine which can safely remain globals
var (
// BLE
device = flag.String("device", "default", "implementation of ble")
du = flag.Duration("du", 5*time.Second, "scanning duration")
dup = flag.Bool("dup", true, "allow duplicate reported")
// MQTT
topic = flag.String("topic", "ble/test", "The topic name to publish to. (default ble/test)")
broker = flag.String("broker", "tcp://127.0.0.1:1883", "The broker URI. ex: tcp://10.10.1.1:1883")
password = flag.String("password", "", "The password (optional)")
user = flag.String("user", "", "The User (optional)")
id = flag.String("id", "bleTest", "The ClientID (optional)")
cleansess = flag.Bool("clean", false, "Set Clean Session (default false)")
qos = flag.Int("qos", 0, "The Quality of Service 0,1,2 (default 0)")
store = flag.String("store", ":memory:", "The Store Directory (default use memory store)")
)
// values to convert to JSON
type BleDev struct {
Addr string
Rssi int64
Connectable bool
Name string
Services []string
MData []byte
//Scanned time.Time
}
func main() {
flag.Parse()
checkRoot()
// BLE setup
d, err := dev.NewDevice(*device)
if err != nil {
log.Fatalf("can't new device : %s", err)
}
ble.SetDefaultDevice(d)
// MQTT setup
opts := MQTT.NewClientOptions()
opts.AddBroker(*broker)
opts.SetClientID(*id)
opts.SetUsername(*user)
opts.SetPassword(*password)
opts.SetCleanSession(*cleansess)
if *store != ":memory:" {
opts.SetStore(MQTT.NewFileStore(*store))
}
client := MQTT.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
myChan := make(chan string, 1000)
// create new object with channel
newAdv := &advType{
c: myChan,
name: "main advertiser", // give this advertiser a name
}
go bleScan(*newAdv)
mqttSend(client, *newAdv)
}
func checkRoot() {
cmd := exec.Command("id", "-u")
output, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
// output has trailing \n
// need to remove the \n
// otherwise it will cause error for strconv.Atoi
// log.Println(output[:len(output)-1])
// 0 = root, 501 = non-root user
i, err := strconv.Atoi(string(output[:len(output)-1]))
if err != nil {
log.Fatal(err)
}
if i != 0 {
log.Fatal("This program must be run as root! (sudo)")
}
}
func bleScan(newAdv advType) {
// BLE Scan for specified duration, or until interrupted by user.
fmt.Printf("Scanning for %s...\n", *du)
ctx := ble.WithSigHandler(context.WithTimeout(context.Background(), *du))
chkErr(ble.Scan(ctx, *dup, newAdv.advHandler, nil)) // pass adv method to scan func
defer close(newAdv.c)
}
// make advHandler method of adv type
func (adv *advType) advHandler(a ble.Advertisement) {
bDev := BleDev{
Addr: a.Addr().String(),
Rssi: int64(a.RSSI()),
Connectable: false,
}
if a.Connectable() {
bDev.Connectable = true
}
if len(a.LocalName()) > 0 {
bDev.Name = a.LocalName()
//fmt.Println(a.LocalName)
}
if len(a.Services()) > 0 {
//bDev.Services = string(a.Services())
}
if len(a.ManufacturerData()) > 0 {
bDev.MData = a.ManufacturerData()
}
var jsonData []byte
jsonData, err := json.Marshal(bDev)
if err != nil {
log.Println(err)
}
//fmt.Println(string(jsonData))
adv.c <- string(jsonData)
}
func mqttSend(client MQTT.Client, adv advType) {
var i int
for m := range adv.c {
client.Publish(*topic, byte(*qos), false, m)
i++
//fmt.Println("In mqttSend: ", m)
}
fmt.Println("Sent ", i, " MQTT messages")
}
func chkErr(err error) {
switch errors.Cause(err) {
case nil:
case context.DeadlineExceeded:
fmt.Printf("done\n")
case context.Canceled:
fmt.Printf("canceled\n")
default:
log.Fatalf(err.Error())
}
}