Skip to content

Commit 58584b7

Browse files
Add lots of improvements
- Stop on using `/tmp/cache.mp3` as a hardcoded string; - Defer an os.Remove() for `/tmp/cache.mp3` in `exec()` function; - Unpair bluetooth device on exiting; - Fix bluetooth device auto pairing (a.k.a pairing restore);
1 parent dd7a5ed commit 58584b7

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

src/eutherpe.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ package main
99

1010
import (
1111
"internal/bluebraces"
12-
"fmt"
1312
"internal/vars"
1413
"internal/webui"
15-
"os"
16-
"time"
1714
"internal/wifi"
1815
"internal/options"
16+
"internal/mplayer"
17+
"fmt"
18+
"os"
19+
"time"
1920
"strings"
2021
)
2122

@@ -68,13 +69,16 @@ func exec() {
6869
eutherpeVars.CachedDevices.BlueDevId)
6970
}
7071
fmt.Printf("info: Listen at %s:%s\n", eutherpeVars.HTTPd.Addr, eutherpeVars.HTTPd.Port)
71-
os.Remove("/tmp/cache.mp3")
72+
defer os.Remove(vars.EutherpeCachedMP3FilePath)
7273
webui.RunWebUI(eutherpeVars)
7374
eutherpeVars.HTTPd.AuthWatchdog.Off()
7475
eutherpeVars.SaveSession()
7576
if len(eutherpeVars.HostName) > 0 {
7677
eutherpeVars.MDNS.GoinHome <- true
7778
}
79+
if len(eutherpeVars.CachedDevices.BlueDevId) > 0 {
80+
bluebraces.UnpairDevice(eutherpeVars.CachedDevices.BlueDevId)
81+
}
7882
}
7983

8084
func version() {
@@ -99,7 +103,7 @@ func tryToPairWithPreviousBluetoothDevice(eutherpeVars *vars.EutherpeVars,
99103
eutherpeVars.Unlock()
100104
return
101105
}
102-
blueDevs, _ := bluebraces.ScanDevices(3 * time.Second)
106+
blueDevs, _ := bluebraces.ScanDevices(time.Duration(10 * time.Second))
103107
found := false
104108
for _, blueDev := range blueDevs {
105109
found = (blueDev.Id == previousDevice)
@@ -112,6 +116,9 @@ func tryToPairWithPreviousBluetoothDevice(eutherpeVars *vars.EutherpeVars,
112116
err = bluebraces.PairDevice(previousDevice)
113117
if err == nil {
114118
err = bluebraces.ConnectDevice(previousDevice)
119+
if err == nil {
120+
eutherpeVars.CachedDevices.MixerControlName, err = bluebraces.GetBlueAlsaMixerControlName()
121+
}
115122
}
116123
} else {
117124
err = fmt.Errorf("Previous device powered off.")
@@ -121,5 +128,8 @@ func tryToPairWithPreviousBluetoothDevice(eutherpeVars *vars.EutherpeVars,
121128
if shouldTryAgain {
122129
time.Sleep(3 * time.Second)
123130
go tryToPairWithPreviousBluetoothDevice(eutherpeVars, previousDevice)
131+
} else {
132+
mplayer.SetVolume(int(eutherpeVars.Player.VolumeLevel),
133+
eutherpeVars.CachedDevices.MixerControlName)
124134
}
125135
}

src/internal/actions/music_play.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func MusicPlay(eutherpeVars *vars.EutherpeVars, userData *url.Values) error {
6666
// I believe that some nosy compiler "optimzation" was causing the
6767
// inconsistence, not sure. I have been facing this issue on Go 1.19. :S
6868
shouldSetVolume := eutherpeVars.Player.Stopped || len(customPath) > 0
69-
createCache(eutherpeVars.Player.NowPlaying.FilePath, "/tmp/cache.mp3")
70-
eutherpeVars.Player.Handle, err = mplayer.Play("/tmp/cache.mp3", len(eutherpeVars.CachedDevices.BlueDevId) > 0, customPath)
69+
createCache(eutherpeVars.Player.NowPlaying.FilePath, vars.EutherpeCachedMP3FilePath)
70+
eutherpeVars.Player.Handle, err = mplayer.Play(vars.EutherpeCachedMP3FilePath, len(eutherpeVars.CachedDevices.BlueDevId) > 0, customPath)
7171
eutherpeVars.Player.Stopped = (err != nil)
7272
if eutherpeVars.Player.Stopped {
7373
return err
@@ -112,4 +112,3 @@ func createCache(src, dest string) {
112112
input, _ := ioutil.ReadFile(src)
113113
ioutil.WriteFile(dest, input, 0644)
114114
}
115-

src/internal/actions/probe_bluetooth_devices.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"flag"
1414
"net/url"
1515
"time"
16-
"fmt"
1716
)
1817

1918
func ProbeBluetoothDevices(eutherpeVars *vars.EutherpeVars,
@@ -26,7 +25,6 @@ func ProbeBluetoothDevices(eutherpeVars *vars.EutherpeVars,
2625
}
2726
blueDevs, err := bluebraces.ScanDevices(time.Duration(10 * time.Second), customPath)
2827
if err != nil {
29-
fmt.Print(err)
3028
return err
3129
}
3230
eutherpeVars.BluetoothDevices = blueDevs

src/internal/vars/vars.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,5 @@ const EutherpeMusicDevRootDir = ".eutherpe"
693693
const EutherpeMusicDevPlaylistsDir = "playlists"
694694

695695
const EutherpeOptionListenPort = "listen-port"
696+
697+
const EutherpeCachedMP3FilePath = "/tmp/cache.mp3"

0 commit comments

Comments
 (0)