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
import (
"time"
// "time"
marlinbox "github.com/itsscb/marlinbox"
)
func main() {
mb := marlinbox.New("playlist.json")
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,25 +3,25 @@
"volume": 1.0,
"playlist": [
{
"id": "0002693373",
"file": "./music/ACDC - Back In Black.mp3"
"id": "0004106397",
"file": "./music/Badewannensitzpirat.mp3"
},
{
"id": "0011415256",
"file": "./music/ACDC - TNT.mp3"
"id": "0017411992",
"file": "./music/Leo_Lausemaus_Titelsong.mp3"
},
{
"id": "0011394336",
"file": "./music/ACDC - Thunderstruck.mp3"
"id": "0017356608",
"file": "./music/Party_im_Schneckenhaus.mp3"
}
],
"controlcards": [
{
"id": "0011462127",
"id": "0017458863",
"function": "vol+"
},
{
"id": "0011394129",
"id": "0017356401",
"function": "vol-"
}
]

View File

@ -2,7 +2,6 @@ package marlinbox
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
@ -16,14 +15,15 @@ import (
type MarlinBox struct {
DeviceName string `json:"devicename"`
DevicePath string `json:"devicepath,omitempty"`
CurrentID string
Volume float64 `json:"volume,omitempty"`
ConfigPath string `json:"configpath,omitempty"`
CurrentID string `json:"-"`
Volume float64 `json:"-"`
Playlist []*PlayCard `json:"playlist,omitempty"`
ControlCards []*ControlCard `json:"controlcards,omitempty"`
Device *evdev.InputDevice
CurrentPlayCard *PlayCard
Player oto.Player
PlayerContext *oto.Context
Device *evdev.InputDevice `json:"-"`
CurrentPlayCard *PlayCard `json:"-"`
Player oto.Player `json:"-"`
PlayerContext *oto.Context `json:"-"`
}
type RFIDCard struct {
@ -75,6 +75,9 @@ func New(path string) *MarlinBox {
}
devices, err := evdev.ListInputDevices()
if err != nil {
log.Panicf("Error listing Input Devices: %s", err)
}
for _, dev := range devices {
if dev.Name == mb.DeviceName {
@ -86,6 +89,8 @@ func New(path string) *MarlinBox {
panic("Device not found")
}
mb.ConfigPath = path
return mb
}
@ -97,7 +102,7 @@ func (mb *MarlinBox) Run() {
}
mb.Device.Grab()
go func() {
// go func() {
for {
events, err := mb.Device.Read()
if err != nil {
@ -113,29 +118,21 @@ func (mb *MarlinBox) Run() {
continue
}
if val == "ENTER" {
err := mb.GetCurrentCard()
if err != nil {
log.Println(err)
if val == "ENTER" || len(mb.CurrentID) == 9 {
if val != "ENTER" {
mb.CurrentID += val
}
fmt.Println(mb.CurrentPlayCard)
mb.GetCurrentCard()
mb.CurrentID = ""
// err =
// if err != nil {
// log.Println(err)
// panic(err)
// }
continue
}
mb.CurrentID += val
}
}
}()
}
func (mb *MarlinBox) GetCurrentCard() error {
fmt.Println(mb.ControlCards)
func (mb *MarlinBox) GetCurrentCard() {
for _, c := range mb.ControlCards {
if mb.CurrentID == c.ID {
switch c.Function {
@ -146,13 +143,12 @@ func (mb *MarlinBox) GetCurrentCard() error {
}
case "vol-":
fmt.Println("vol-")
if mb.Volume > 0 {
if mb.Volume >= 0.2 {
mb.Volume = mb.Volume - 0.2
}
default:
return nil
return
}
return nil
}
}
for _, c := range mb.Playlist {
@ -162,10 +158,29 @@ func (mb *MarlinBox) GetCurrentCard() error {
mb.Player.Reset()
}
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() {

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-"
}
]
}