/main.go

https://gitlab.com/brucealdridge/driver-go-blecombined · Go · 122 lines · 93 code · 22 blank · 7 comment · 28 complexity · 0a58c4835d357d97386dfeb14a920d3c MD5 · raw file

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "os/signal"
  7. "regexp"
  8. "strings"
  9. "github.com/ninjasphere/gatt"
  10. "github.com/ninjasphere/go-ninja/logger"
  11. )
  12. var log = logger.GetLogger("driver-go-blecombined")
  13. var fpDriver *FlowerPowerDriver
  14. var wpDriver *WaypointDriver
  15. var tagDriver *BLETagDriver
  16. var client *gatt.Client //kill me
  17. var sent = false
  18. func main() {
  19. log.Infof("BLE Driver Starting")
  20. // reset BLE layer
  21. out, err := exec.Command("/opt/ninjablocks/bin/sphere-ble-reset", "ble-startup").Output()
  22. if err != nil {
  23. log.Errorf(fmt.Sprintf("Error: %s", err))
  24. }
  25. // use hciconfig to the get the mac address
  26. out, err = exec.Command("hciconfig").Output()
  27. if err != nil {
  28. log.Errorf(fmt.Sprintf("Error: %s", err))
  29. }
  30. re := regexp.MustCompile("([0-9A-F]{2}\\:{0,1}){6}")
  31. mac := strings.Replace(re.FindString(string(out)), ":", "", -1)
  32. log.Infof("The local mac is %s\n", mac)
  33. client = &gatt.Client{
  34. StateChange: func(newState string) {
  35. log.Infof("Client state change: %s", newState)
  36. },
  37. }
  38. fpDriver, err = NewFlowerPowerDriver(client)
  39. if err != nil {
  40. log.Errorf("Failed to create FlowerPower driver: ", err)
  41. }
  42. wpDriver, err = NewWaypointDriver(client)
  43. if err != nil {
  44. log.FatalError(err, "Failed to create waypoint driver")
  45. }
  46. tagDriver, err = NewBLETagDriver(client)
  47. if err != nil {
  48. log.FatalError(err, "Failed to create BLE Tag driver")
  49. }
  50. client.Advertisement = handleAdvertisement
  51. client.Rssi = func(address string, name string, rssi int8) {
  52. //log.Printf("Rssi update address:%s rssi:%d", address, rssi)
  53. wpDriver.sendRssi(strings.Replace(address, ":", "", -1), name, mac, rssi, true)
  54. //spew.Dump(device);
  55. }
  56. log.Infof("Starting client scan")
  57. err = client.Start()
  58. if err != nil {
  59. log.FatalError(err, "Failed to start client")
  60. }
  61. err = client.StartScanning(true)
  62. if err != nil {
  63. log.FatalError(err, "Failed to start scanning")
  64. }
  65. //----------------------------------------------------------------------------------------
  66. c := make(chan os.Signal, 1)
  67. signal.Notify(c, os.Interrupt, os.Kill)
  68. // Block until a signal is received.
  69. s := <-c
  70. fmt.Println("Got signal:", s)
  71. }
  72. func handleAdvertisement(device *gatt.DiscoveredDevice) {
  73. if device.Advertisement.LocalName == "NinjaSphereWaypoint" {
  74. log.Infof("Found waypoint %s", device.Address)
  75. wpDriver.handleSphereWaypoint(device)
  76. }
  77. for uuid := range device.Advertisement.ServiceUuids {
  78. if uuid == flowerPowerServiceUuid {
  79. if fpDriver.announcedFlowerPowers[device.Address] {
  80. return
  81. }
  82. log.Infof("Found Flower Power %s", device.Address)
  83. err := NewFlowerPower(fpDriver, device)
  84. if err != nil {
  85. log.Errorf("Error creating FlowerPower device ", err)
  86. }
  87. }
  88. }
  89. // look for tags which are CLOSE to the sphere!!
  90. for uuid := range device.Advertisement.ServiceUuids {
  91. if uuid == stickNFindServiceUuid {
  92. if device.Rssi > minRSSI {
  93. err := NewBLETag(tagDriver, device)
  94. if err != nil {
  95. log.Errorf("Error creating BLE Tag device ", err)
  96. }
  97. }
  98. }
  99. }
  100. }