-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearable.go
73 lines (61 loc) · 1.32 KB
/
nearable.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
package nearable
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type NearablePacket struct {
ProtocolVersion string
Uuid string
Rssi int
}
func NewNearablePacket(line string) *NearablePacket {
vals := strings.Split(line, " ")
return &NearablePacket{
ProtocolVersion: parseProtocolVersion(vals),
Uuid: parseUuid(vals),
Rssi: parseRssi(vals)}
}
func IsValid(str string) bool {
r, err := regexp.Compile(`^04\ 3E\ .{2}\ 02\ 01\ .{41}\ 0F\ 18\ 17\ FF\ 5D\ 01`)
if err != nil {
fmt.Printf("There is a problem with your regexp.\n")
return false
}
return r.MatchString(str)
}
func parseProtocolVersion(vals []string) string {
return vals[25]
}
func parseUuid(vals []string) string {
return strings.Join(
vals[26:34],
"")
}
// func GetType(str string) string {
// vals := strings.Split(str, " ")
// return parseType(vals)
// }
// func parseType(vals []string) string {
// return vals[25]
// }
func parseRssi(vals []string) int {
rssi, _ := strconv.ParseInt(vals[len(vals)-1], 16, 0)
return int(rssi) - 256
}
// func parsePower(vals []string) int {
// power, _ := strconv.ParseInt(vals[26], 16, 0)
// return int(power) - 256
// }
// func parseMac(vals []string) string {
// return strings.Join(
// []string{
// vals[12],
// vals[11],
// vals[10],
// vals[9],
// vals[8],
// vals[7]},
// "")
// }