Skip to content

Commit

Permalink
Merge pull request #33 from richbl/dev
Browse files Browse the repository at this point in the history
refactor: Refactor of the logger package to address formatting issues; reorganized other packages
  • Loading branch information
richbl authored Dec 17, 2024
2 parents a6c2742 + e3c29dc commit fce1337
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 613 deletions.
155 changes: 79 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,72 +194,69 @@ go run cmd/main.go
At this point, you should see the following output:

```console
2024/12/11 22:02:28 Starting BLE Sync Cycle 0.6.2
2024/12/11 22:02:28 INFO [BLE] Created new BLE central controller
2024/12/11 22:02:28 INFO [BLE] Now scanning the ether for BLE peripheral UUID of F1:42:D8:DE:35:16...
2024/12/11 22:02:42 INFO [BLE] Found BLE peripheral F1:42:D8:DE:35:16
2024/12/11 22:02:42 INFO [BLE] Connecting to BLE peripheral device F1:42:D8:DE:35:16
2024/12/11 22:02:44 INFO [BLE] BLE peripheral device connected
2024/12/11 22:02:44 INFO [BLE] Discovering CSC services 00001816-0000-1000-8000-00805f9b34fb
2024/12/11 22:02:54 ERROR [BLE] CSC services discovery failed: timeout on DiscoverServices
2024/12/11 22:02:54 ERROR [BLE] BLE peripheral scan failed: timeout on DiscoverServices
2024/12/11 22:02:54 INFO [APP] Application shutdown complete. Goodbye!
2024/12/16 15:08:56 Starting BLE Sync Cycle 0.6.2
2024/12/16 15:08:56 [INFO] [BLE] created new BLE central controller
2024/12/16 15:08:56 [INFO] [BLE] now scanning the ether for BLE peripheral UUID of F1:42:D8:DE:35:16...
2024/12/16 15:08:58 [DEBUG] [BLE] found BLE peripheral F1:42:D8:DE:35:16
2024/12/16 15:08:58 [DEBUG] [BLE] connecting to BLE peripheral device F1:42:D8:DE:35:16
2024/12/16 15:09:00 [INFO] [BLE] BLE peripheral device connected
2024/12/16 15:09:00 [DEBUG] [BLE] discovering CSC services 00001816-0000-1000-8000-00805f9b34fb
2024/12/16 15:09:10 [ERROR] [BLE] CSC services discovery failed: timeout on DiscoverServices
2024/12/16 15:09:10 [ERROR] [BLE] BLE peripheral scan failed: timeout on DiscoverServices
2024/12/16 15:09:10 [INFO] [APP] application shutdown complete... goodbye!
```

In this first example, while the application was able to find the BLE peripheral, it failed to discover the CSC services and characteristics before timing out. Depending on the BLE peripheral, it may take some time before a BLE peripheral advertises both its device services and characteristics. If the peripheral is not responding, you may need to increase the timeout in the `config.toml` file.

```console
2024/12/11 22:03:59 Starting BLE Sync Cycle 0.6.2
2024/12/11 22:03:59 INFO [BLE] Created new BLE central controller
2024/12/11 22:03:59 INFO [BLE] Now scanning the ether for BLE peripheral UUID of F1:42:D8:DE:35:16...
2024/12/11 22:03:59 INFO [BLE] Found BLE peripheral F1:42:D8:DE:35:16
2024/12/11 22:03:59 INFO [BLE] Connecting to BLE peripheral device F1:42:D8:DE:35:16
2024/12/11 22:03:59 INFO [BLE] BLE peripheral device connected
2024/12/11 22:03:59 INFO [BLE] Discovering CSC services 00001816-0000-1000-8000-00805f9b34fb
2024/12/11 22:03:59 INFO [BLE] Found CSC service 00001816-0000-1000-8000-00805f9b34fb
2024/12/11 22:03:59 INFO [BLE] Discovering CSC characteristics 00002a5b-0000-1000-8000-00805f9b34fb
2024/12/11 22:03:59 INFO [BLE] Found CSC characteristic 00002a5b-0000-1000-8000-00805f9b34fb
2024/12/11 22:03:59 INFO [BLE] Starting real-time monitoring of BLE sensor notifications...
2024/12/11 22:03:59 INFO [VIDEO] Starting MPV video player...
2024/12/11 22:03:59 INFO [VIDEO] Loading video file: cycling_test.mp4
2024/12/11 22:03:59 INFO [VIDEO] Entering MPV playback loop...
2024/12/11 22:04:00 INFO [VIDEO] Sensor speed buffer: [0.00 0.00 0.00 0.00 0.00]
2024/12/11 22:04:00 INFO [VIDEO] Smoothed sensor speed: 0.00 mph
2024/12/11 22:04:00 INFO [VIDEO] No speed detected, so pausing video
2024/12/11 22:04:00 INFO [VIDEO] Video paused successfully
2024/12/16 15:09:47 Starting BLE Sync Cycle 0.6.2
2024/12/16 15:09:47 [INFO] [BLE] created new BLE central controller
2024/12/16 15:09:47 [INFO] [BLE] now scanning the ether for BLE peripheral UUID of F1:42:D8:DE:35:16...
2024/12/16 15:09:47 [DEBUG] [BLE] found BLE peripheral F1:42:D8:DE:35:16
2024/12/16 15:09:47 [DEBUG] [BLE] connecting to BLE peripheral device F1:42:D8:DE:35:16
2024/12/16 15:09:47 [INFO] [BLE] BLE peripheral device connected
2024/12/16 15:09:47 [DEBUG] [BLE] discovering CSC services 00001816-0000-1000-8000-00805f9b34fb
2024/12/16 15:09:47 [DEBUG] [BLE] found CSC service 00001816-0000-1000-8000-00805f9b34fb
2024/12/16 15:09:47 [DEBUG] [BLE] discovering CSC characteristics 00002a5b-0000-1000-8000-00805f9b34fb
2024/12/16 15:09:47 [DEBUG] [BLE] found CSC characteristic 00002a5b-0000-1000-8000-00805f9b34fb
2024/12/16 15:09:47 [INFO] [VIDEO] starting MPV video player...
2024/12/16 15:09:47 [DEBUG] [BLE] starting real-time monitoring of BLE sensor notifications...
2024/12/16 15:09:47 [DEBUG] [VIDEO] loading video file: cycling_test.mp4
2024/12/16 15:09:47 [DEBUG] [VIDEO] entering MPV playback loop...
2024/12/16 15:09:47 [DEBUG] [VIDEO] sensor speed buffer: [0.00 0.00 0.00 0.00 0.00]
2024/12/16 15:09:47 [INFO] [VIDEO] smoothed sensor speed: 0.00 mph
2024/12/16 15:09:47 [DEBUG] [VIDEO] no speed detected, so pausing video
2024/12/16 15:09:47 [DEBUG] [VIDEO] video paused successfully
```

In the example above, the application is now running in a loop, periodically querying the BLE peripheral for speed data. The application will also update the video player to match the speed of the sensor. Here, since the video has just begun, its speed is set to 0.0 (paused).

```console
...
024/12/11 22:05:08 INFO [SPEED] BLE sensor speed: 8.54 mph
2024/12/11 22:05:09 INFO [VIDEO] Sensor speed buffer: [9.06 8.28 8.11 8.20 8.54]
2024/12/11 22:05:09 INFO [VIDEO] Smoothed sensor speed: 8.44 mph
2024/12/11 22:05:09 INFO [VIDEO] Updating video playback speed to 0.51
2024/12/11 22:05:09 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:09 INFO [VIDEO] Sensor speed buffer: [8.28 8.11 8.20 8.54 0.00]
2024/12/11 22:05:09 INFO [VIDEO] Smoothed sensor speed: 6.63 mph
2024/12/11 22:05:09 INFO [VIDEO] Updating video playback speed to 0.40
2024/12/11 22:05:09 INFO [SPEED] BLE sensor speed: 8.44 mph
2024/12/11 22:05:10 INFO [VIDEO] Sensor speed buffer: [8.11 8.20 8.54 0.00 8.44]
2024/12/11 22:05:10 INFO [VIDEO] Smoothed sensor speed: 6.66 mph
2024/12/11 22:05:10 INFO [SPEED] BLE sensor speed: 7.84 mph
2024/12/11 22:05:10 INFO [VIDEO] Sensor speed buffer: [8.20 8.54 0.00 8.44 7.84]
2024/12/11 22:05:10 INFO [VIDEO] Smoothed sensor speed: 6.61 mph
2024/12/11 22:05:11 INFO [VIDEO] Sensor speed buffer: [8.20 8.54 0.00 8.44 7.84]
2024/12/11 22:05:11 INFO [VIDEO] Smoothed sensor speed: 6.61 mph
2024/12/11 22:05:11 INFO [SPEED] BLE sensor speed: 6.56 mph
2024/12/11 22:05:11 INFO [SPEED] BLE sensor speed: 6.65 mph
2024/12/11 22:05:11 INFO [VIDEO] Sensor speed buffer: [0.00 8.44 7.84 6.56 6.65]
2024/12/11 22:05:11 INFO [VIDEO] Smoothed sensor speed: 5.90 mph
2024/12/11 22:05:11 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:12 INFO [VIDEO] Sensor speed buffer: [8.44 7.84 6.56 6.65 0.00]
2024/12/11 22:05:12 INFO [VIDEO] Smoothed sensor speed: 5.90 mph
2024/12/11 22:05:12 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:12 INFO [VIDEO] Sensor speed buffer: [7.84 6.56 6.65 0.00 0.00]
2024/12/11 22:05:12 INFO [VIDEO] Smoothed sensor speed: 4.21 mph
2024/12/11 22:05:12 INFO [VIDEO] Updating video playback speed to 0.25
2024/12/16 15:13:26 [INFO] [SPEED] BLE sensor speed: 24.14 mph
2024/12/16 15:13:26 [DEBUG] [VIDEO] sensor speed buffer: [0.00 3.17 5.54 13.53 24.14]
2024/12/16 15:13:26 [INFO] [VIDEO] smoothed sensor speed: 9.28 mph
2024/12/16 15:13:26 [DEBUG] [VIDEO] last playback speed: 1.74 mph
2024/12/16 15:13:26 [DEBUG] [VIDEO] sensor speed delta: 7.54 mph
2024/12/16 15:13:26 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:26 [INFO] [VIDEO] updating video playback speed to 0.56
2024/12/16 15:13:27 [DEBUG] [VIDEO] sensor speed buffer: [0.00 3.17 5.54 13.53 24.14]
2024/12/16 15:13:27 [INFO] [VIDEO] smoothed sensor speed: 9.28 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] last playback speed: 9.28 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] sensor speed delta: 0.00 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] sensor speed buffer: [0.00 3.17 5.54 13.53 24.14]
2024/12/16 15:13:27 [INFO] [VIDEO] smoothed sensor speed: 9.28 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] last playback speed: 9.28 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] sensor speed delta: 0.00 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:27 [INFO] [SPEED] BLE sensor speed: 27.35 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] sensor speed buffer: [3.17 5.54 13.53 24.14 27.35]
2024/12/16 15:13:27 [INFO] [VIDEO] smoothed sensor speed: 14.75 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] last playback speed: 9.28 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] sensor speed delta: 5.47 mph
2024/12/16 15:13:27 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:27 [INFO] [VIDEO] updating video playback speed to 0.88
...
```

Expand All @@ -269,26 +266,32 @@ In this last example, **BLE Sync Cycle** is coordinating with both the BLE perip

```console
...
2024/12/11 22:05:11 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:12 INFO [VIDEO] Sensor speed buffer: [8.44 7.84 6.56 6.65 0.00]
2024/12/11 22:05:12 INFO [VIDEO] Smoothed sensor speed: 5.90 mph
2024/12/11 22:05:12 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:12 INFO [VIDEO] Sensor speed buffer: [7.84 6.56 6.65 0.00 0.00]
2024/12/11 22:05:12 INFO [VIDEO] Smoothed sensor speed: 4.21 mph
2024/12/11 22:05:12 INFO [VIDEO] Updating video playback speed to 0.25
2024/12/11 22:05:13 INFO [SPEED] BLE sensor speed: 7.29 mph
2024/12/11 22:05:13 INFO [VIDEO] Sensor speed buffer: [6.56 6.65 0.00 0.00 7.29]
2024/12/11 22:05:13 INFO [VIDEO] Smoothed sensor speed: 4.10 mph
2024/12/11 22:05:13 INFO [VIDEO] Sensor speed buffer: [6.56 6.65 0.00 0.00 7.29]
2024/12/11 22:05:13 INFO [VIDEO] Smoothed sensor speed: 4.10 mph
2024/12/11 22:05:13 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:13 INFO [SPEED] BLE sensor speed: 0.00 mph
2024/12/11 22:05:14 INFO [VIDEO] Sensor speed buffer: [0.00 0.00 7.29 0.00 0.00]
2024/12/11 22:05:14 INFO [VIDEO] Smoothed sensor speed: 1.46 mph
2024/12/11 22:05:14 INFO [VIDEO] Updating video playback speed to 0.09
2024/12/11 22:05:14 INFO [APP] Shutdown signal received
2024/12/11 22:05:14 INFO [VIDEO] Context cancelled. Shutting down video player component
2024/12/11 22:05:14 INFO [APP] Application shutdown complete. Goodbye!
2024/12/16 15:13:32 [INFO] [VIDEO] updating video playback speed to 0.41
2024/12/16 15:13:32 [DEBUG] [VIDEO] sensor speed buffer: [11.62 10.58 11.68 0.00 0.00]
2024/12/16 15:13:32 [INFO] [VIDEO] smoothed sensor speed: 6.78 mph
2024/12/16 15:13:32 [DEBUG] [VIDEO] last playback speed: 6.78 mph
2024/12/16 15:13:32 [DEBUG] [VIDEO] sensor speed delta: 0.00 mph
2024/12/16 15:13:32 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:33 [INFO] [SPEED] BLE sensor speed: 12.42 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] sensor speed buffer: [10.58 11.68 0.00 0.00 12.42]
2024/12/16 15:13:33 [INFO] [VIDEO] smoothed sensor speed: 6.94 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] last playback speed: 6.78 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] sensor speed delta: 0.16 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] sensor speed buffer: [10.58 11.68 0.00 0.00 12.42]
2024/12/16 15:13:33 [INFO] [VIDEO] smoothed sensor speed: 6.94 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] last playback speed: 6.78 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] sensor speed delta: 0.16 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] sensor speed buffer: [10.58 11.68 0.00 0.00 12.42]
2024/12/16 15:13:33 [INFO] [VIDEO] smoothed sensor speed: 6.94 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] last playback speed: 6.78 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] sensor speed delta: 0.16 mph
2024/12/16 15:13:33 [DEBUG] [VIDEO] playback speed update threshold: 0.25 mph
2024/12/16 15:13:33 [INFO] [SPEED] BLE sensor speed: 0.00 mph
2024/12/16 15:13:33 [INFO] [VIDEO] context cancelled, stopping video player...
2024/12/16 15:13:33 [ERROR] [APP] context canceled
2024/12/16 15:13:33 [INFO] [APP] application shutdown complete... goodbye!
```

## FAQ
Expand All @@ -300,7 +303,7 @@ Q: Do all Bluetooth devices work with **BLE Sync Cycle**?
A: Not necessarily. The Bluetooth package used by **BLE Sync Cycle**, [called Go Bluetooth by TinyGo.org](https://github.com/tinygo-org/bluetooth), is based on the [Bluetooth Low Energy (BLE) standard](https://en.wikipedia.org/wiki/Bluetooth_Low_Energy). Some Bluetooth devices may not be compatible with this protocol.

Q: Can I disable the log messages in **BLE Sync Cycle**?
A: While you cannot disable all log messages, check out the `logging_level` parameter in the `config.toml` file (see the [Editing the TOML File](#editing-the-toml-file) section above). This parameter can be set to "debug", "info", "warn", or "error", where "debug" is the most verbose and "error" is least verbose. When set to "error", only error/fatal messages will be displayed which, under normal circumstances, should be none.
A: While you cannot disable all log messages, check out the `logging_level` parameter in the `config.toml` file (see the [Editing the TOML File](#editing-the-toml-file) section above). This parameter can be set to "debug", "info", "warn", or "error", where "debug" is the most verbose and "error" is least verbose.

Q: How do I use **BLE Sync Cycle**?
A: See the [Basic Usage](#basic-usage) section above
Expand Down
119 changes: 58 additions & 61 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"os"
"os/signal"
"sync"
"syscall"

ble "github.com/richbl/go-ble-sync-cycle/internal/ble"
Expand All @@ -31,33 +30,64 @@ func main() {
// Load configuration
cfg, err := config.LoadFile("config.toml")
if err != nil {
log.Fatal("FATAL - Failed to load TOML configuration: " + err.Error())
log.Fatal("[FATAL]: failed to load TOML configuration: " + err.Error())
}

// Initialize logger
logger.Initialize(cfg.App.LogLevel)
if _, err := logger.Initialize(cfg.App.LogLevel); err != nil {
log.Printf("[WARN]: logger initialization warning: %v", err)
}

// Create contexts for managing goroutines and cancellations
rootCtx, rootCancel := context.WithCancel(context.Background())
defer rootCancel()

// Create component controllers
controllers, err := setupAppControllers(*cfg)
controllers, componentType, err := setupAppControllers(*cfg)
if err != nil {
logger.Fatal(logger.APP, "Failed to create controllers: "+err.Error())
logger.Fatal(componentType, "failed to create controllers: "+err.Error())
}

// Run the application
if err := startAppControllers(rootCtx, controllers); err != nil {
logger.Error(err.Error())
if componentType, err := startAppControllers(rootCtx, controllers); err != nil {
logger.Error(componentType, err.Error())
}

// Shutdown the application... buh bye!
logger.Info(logger.APP, "Application shutdown complete. Goodbye!")
logger.Info(logger.APP, "application shutdown complete... goodbye!")
}

// setupAppControllers creates and initializes the application controllers
func setupAppControllers(cfg config.Config) (appControllers, logger.ComponentType, error) {

// Create speed and video controllers
speedController := speed.NewSpeedController(cfg.Speed.SmoothingWindow)
videoPlayer, err := video.NewPlaybackController(cfg.Video, cfg.Speed)
if err != nil {
return appControllers{}, logger.VIDEO, errors.New("failed to create video player: " + err.Error())
}

// Create BLE controller
bleController, err := ble.NewBLEController(cfg.BLE, cfg.Speed)
if err != nil {
return appControllers{}, logger.BLE, errors.New("failed to create BLE controller: " + err.Error())
}

return appControllers{
speedController: speedController,
videoPlayer: videoPlayer,
bleController: bleController,
}, logger.APP, nil
}

// startAppControllers is responsible for starting and managing the component controllers
func startAppControllers(ctx context.Context, controllers appControllers) error {
func startAppControllers(ctx context.Context, controllers appControllers) (logger.ComponentType, error) {

// componentErr holds the error type and component type used for logging
type componentErr struct {
componentType logger.ComponentType
err error
}

// Create shutdown signal
ctx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
Expand All @@ -66,74 +96,41 @@ func startAppControllers(ctx context.Context, controllers appControllers) error
// Scan for BLE peripheral of interest
bleSpeedCharacter, err := scanForBLESpeedCharacteristic(ctx, controllers)
if err != nil {
return errors.New(string(logger.BLE) + " BLE peripheral scan failed: " + err.Error())
return logger.BLE, errors.New("BLE peripheral scan failed: " + err.Error())
}

// Start component controllers concurrently
var wg sync.WaitGroup
errs := make(chan error, 2)
errs := make(chan componentErr, 1)

// Start BLE sensor speed monitoring
wg.Add(1)
// Monitor BLE speed
go func() {
defer wg.Done()
if err := monitorBLESpeed(ctx, controllers, bleSpeedCharacter); err != nil {
errs <- errors.New(string(logger.BLE) + "Speed monitoring failed: " + err.Error())
errs <- componentErr{logger.BLE, err}
return
}
errs <- componentErr{logger.BLE, nil}
}()

// Start video playback
wg.Add(1)
// Play video
go func() {
defer wg.Done()
if err := playVideo(ctx, controllers); err != nil {
errs <- errors.New(string(logger.VIDEO) + "Playback failed: " + err.Error())
errs <- componentErr{logger.VIDEO, err}
return
}
errs <- componentErr{logger.VIDEO, nil}
}()

// Wait for shutdown signals from go routines
go func() {
<-ctx.Done()
logger.Info(logger.APP, "Shutdown signal received")
}()

// Wait for goroutines to finish or an error to occur
go func() {
wg.Wait()
close(errs)
}()

// Check for errors from components
if err := <-errs; err != nil {
return err
}

return nil
}

// setupAppControllers creates and initializes the application controllers
func setupAppControllers(cfg config.Config) (appControllers, error) {

// Create speed controller
speedController := speed.NewSpeedController(cfg.Speed.SmoothingWindow)

// Create video player
videoPlayer, err := video.NewPlaybackController(cfg.Video, cfg.Speed)
if err != nil {
return appControllers{}, errors.New(string(logger.VIDEO) + "Failed to create video player: " + err.Error())
}

// Create BLE controller
bleController, err := ble.NewBLEController(cfg.BLE, cfg.Speed)
if err != nil {
return appControllers{}, errors.New(string(logger.BLE) + "Failed to create BLE controller: " + err.Error())
// Wait for context cancellation or error
select {
case <-ctx.Done():
return logger.APP, ctx.Err()
case compErr := <-errs:
if compErr.err != nil {
return compErr.componentType, compErr.err
}
}

return appControllers{
speedController: speedController,
videoPlayer: videoPlayer,
bleController: bleController,
}, nil
return logger.APP, nil
}

// scanForBLESpeedCharacteristic scans for the BLE CSC speed characteristic
Expand Down
Loading

0 comments on commit fce1337

Please sign in to comment.