Adds persistence

This commit is contained in:
itsscb 2022-12-09 17:13:31 +01:00
parent ccd9734ef6
commit 97c239d874
6 changed files with 146 additions and 64 deletions

BIN
cmd/cmd Executable file

Binary file not shown.

View File

@ -1,13 +1,15 @@
package main package main
import ( import (
"time" // "time"
marlinbox "github.com/itsscb/marlinbox" marlinbox "github.com/itsscb/marlinbox"
) )
func main() { func main() {
mb := marlinbox.New("playlist.json") mb := marlinbox.New("playlist.json")
mb.Run() mb.Run()
time.Sleep(time.Minute * 5) /* for {
time.Sleep(time.Second * 1)
}
*/
} }

BIN
cmd/marlinbox Executable file

Binary file not shown.

View File

@ -3,26 +3,26 @@
"volume": 1.0, "volume": 1.0,
"playlist": [ "playlist": [
{ {
"id": "0002693373", "id": "0004106397",
"file": "./music/ACDC - Back In Black.mp3" "file": "./music/Badewannensitzpirat.mp3"
}, },
{ {
"id": "0011415256", "id": "0017411992",
"file": "./music/ACDC - TNT.mp3" "file": "./music/Leo_Lausemaus_Titelsong.mp3"
}, },
{ {
"id": "0011394336", "id": "0017356608",
"file": "./music/ACDC - Thunderstruck.mp3" "file": "./music/Party_im_Schneckenhaus.mp3"
} }
], ],
"controlcards": [ "controlcards": [
{ {
"id": "0011462127", "id": "0017458863",
"function": "vol+" "function": "vol+"
}, },
{ {
"id": "0011394129", "id": "0017356401",
"function": "vol-" "function": "vol-"
} }
] ]
} }

View File

@ -2,7 +2,6 @@ package marlinbox
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"log" "log"
"os" "os"
@ -14,16 +13,17 @@ import (
) )
type MarlinBox struct { type MarlinBox struct {
DeviceName string `json:"devicename"` DeviceName string `json:"devicename"`
DevicePath string `json:"devicepath,omitempty"` DevicePath string `json:"devicepath,omitempty"`
CurrentID string ConfigPath string `json:"configpath,omitempty"`
Volume float64 `json:"volume,omitempty"` CurrentID string `json:"-"`
Playlist []*PlayCard `json:"playlist,omitempty"` Volume float64 `json:"-"`
ControlCards []*ControlCard `json:"controlcards,omitempty"` Playlist []*PlayCard `json:"playlist,omitempty"`
Device *evdev.InputDevice ControlCards []*ControlCard `json:"controlcards,omitempty"`
CurrentPlayCard *PlayCard Device *evdev.InputDevice `json:"-"`
Player oto.Player CurrentPlayCard *PlayCard `json:"-"`
PlayerContext *oto.Context Player oto.Player `json:"-"`
PlayerContext *oto.Context `json:"-"`
} }
type RFIDCard struct { type RFIDCard struct {
@ -75,6 +75,9 @@ func New(path string) *MarlinBox {
} }
devices, err := evdev.ListInputDevices() devices, err := evdev.ListInputDevices()
if err != nil {
log.Panicf("Error listing Input Devices: %s", err)
}
for _, dev := range devices { for _, dev := range devices {
if dev.Name == mb.DeviceName { if dev.Name == mb.DeviceName {
@ -86,6 +89,8 @@ func New(path string) *MarlinBox {
panic("Device not found") panic("Device not found")
} }
mb.ConfigPath = path
return mb return mb
} }
@ -97,45 +102,37 @@ func (mb *MarlinBox) Run() {
} }
mb.Device.Grab() mb.Device.Grab()
go func() { // go func() {
for { for {
events, err := mb.Device.Read() events, err := mb.Device.Read()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
}
for _, ev := range events {
if ev.Value != 0x1 {
continue
}
val, ok := KeyMap[ev.Code]
if !ok {
continue
}
if val == "ENTER" {
err := mb.GetCurrentCard()
if err != nil {
log.Println(err)
}
fmt.Println(mb.CurrentPlayCard)
mb.CurrentID = ""
// err =
// if err != nil {
// log.Println(err)
// panic(err)
// }
continue
}
mb.CurrentID += val
}
} }
}()
for _, ev := range events {
if ev.Value != 0x1 {
continue
}
val, ok := KeyMap[ev.Code]
if !ok {
continue
}
if val == "ENTER" || len(mb.CurrentID) == 9 {
if val != "ENTER" {
mb.CurrentID += val
}
mb.GetCurrentCard()
mb.CurrentID = ""
continue
}
mb.CurrentID += val
}
}
} }
func (mb *MarlinBox) GetCurrentCard() error { func (mb *MarlinBox) GetCurrentCard() {
fmt.Println(mb.ControlCards)
for _, c := range mb.ControlCards { for _, c := range mb.ControlCards {
if mb.CurrentID == c.ID { if mb.CurrentID == c.ID {
switch c.Function { switch c.Function {
@ -146,13 +143,12 @@ func (mb *MarlinBox) GetCurrentCard() error {
} }
case "vol-": case "vol-":
fmt.Println("vol-") fmt.Println("vol-")
if mb.Volume > 0 { if mb.Volume >= 0.2 {
mb.Volume = mb.Volume - 0.2 mb.Volume = mb.Volume - 0.2
} }
default: default:
return nil return
} }
return nil
} }
} }
for _, c := range mb.Playlist { for _, c := range mb.Playlist {
@ -162,10 +158,29 @@ func (mb *MarlinBox) GetCurrentCard() error {
mb.Player.Reset() mb.Player.Reset()
} }
mb.Play() mb.Play()
return nil return
} }
} }
return errors.New("Card not found: " + mb.CurrentID) mb.Playlist = append(mb.Playlist, &PlayCard{
RFIDCard: RFIDCard{
ID: mb.CurrentID,
},
})
config, err := json.MarshalIndent(&mb, "", " ")
if err != nil {
log.Printf("\nCould not marshal output '%+v': %s", config, err)
}
jsonFile, err := os.Create(mb.ConfigPath)
if err != nil {
log.Printf("\nCould not create json-File '%s': %s", mb.ConfigPath, err)
}
_, err = jsonFile.Write(config)
if err != nil {
log.Printf("\nCould not write json-File '%s': %s", mb.ConfigPath, err)
}
} }
func (mb *MarlinBox) Play() { func (mb *MarlinBox) Play() {

65
playlist.json Normal file
View File

@ -0,0 +1,65 @@
{
"devicename": "Sycreader RFID Technology Co., Ltd SYC ID\u0026IC USB Reader",
"devicepath": "/dev/input/event1",
"configpath": "playlist.json",
"playlist": [
{
"id": "0004106397",
"file": "./music/01-01-Traditional-Der_Mond_ist_aufgegangen-320.mp3"
},
{
"id": "0017411992",
"file": "./music/01-02-Heino_Gaze-La-Le-Lu-320.mp3"
},
{
"id": "0017356608",
"file": "./music/01-01-Chris_Buseck-Party_im_Schneckenhaus-320.mp3"
},
{
"id": "0004148198",
"file": "./music/01-01-Heiko_Russe-Leo_Lausemaus_Titelsong-320.mp3"
},
{
"id": "0008323849",
"file": "./music/01-05-Markus_Schurjann-Ich_bin_ein_Einhorn-320.mp3"
},
{
"id": "0004152218",
"file": "./music/01-06-Traditional-Die_Rader_vom_Bus-320.mp3"
},
{
"id": "0012528320",
"file": "./music/01-06-Traditional-Schlaf_Kindlein_schlaf-320.mp3"
},
{
"id": "0008324420",
"file": "./music/01-07-Georges_Moustaki-Das_rote_Pferd-320.mp3"
},
{
"id": "0008324422",
"file": "./music/01-09-Markus_Schurjann-Wer_hat_den_Keks_aus_der_Dose_geklaut-320.mp3"
},
{
"id": "0008324475",
"file": "./music/01-10-Traditional-Funf_kleine_Fische-320.mp3"
},
{
"id": "0005409503",
"file": "./music/01-11-Traditional-Old_Mac_Donald_Had_a_Farm-320.mp3"
},
{
"id": "0004148375",
"file": "./music/01-14-Traditional-Meine_Oma_fahrt_im_Huhnerstall_Motorrad-320.mp3"
}
],
"controlcards": [
{
"id": "0017458863",
"function": "vol+"
},
{
"id": "0017356401",
"function": "vol-"
}
]
}