Initial commit

This commit is contained in:
nemunaire 2023-11-12 21:25:57 +01:00
commit 5d0a210e6d
16 changed files with 998 additions and 0 deletions

View file

@ -0,0 +1,73 @@
package amp1gpio
import (
"bytes"
"io"
"log"
"os"
"path"
"git.nemunai.re/nemunaire/hathoris/sources"
)
type AMP1GPIOSource struct {
Path string
}
const GPIODirectory = "/sys/class/gpio/gpio46/"
func init() {
if _, err := os.Stat(GPIODirectory); err == nil {
sources.SoundSources["amp1"] = &AMP1GPIOSource{
Path: GPIODirectory,
}
}
}
func (s *AMP1GPIOSource) GetName() string {
return "entrée analogique"
}
func (s *AMP1GPIOSource) read() ([]byte, error) {
fd, err := os.Open(path.Join(s.Path, "value"))
if err != nil {
return nil, err
}
defer fd.Close()
return io.ReadAll(fd)
}
func (s *AMP1GPIOSource) IsActive() bool {
return s.IsEnabled()
}
func (s *AMP1GPIOSource) IsEnabled() bool {
b, err := s.read()
if err != nil {
log.Println("Unable to get amp1 GPIO state:", err.Error())
return false
}
return bytes.Compare(b, []byte{'1'}) == 0
}
func (s *AMP1GPIOSource) write(value string) error {
fd, err := os.Create(path.Join(s.Path, "value"))
if err != nil {
return err
}
defer fd.Close()
_, err = fd.Write([]byte(value))
return err
}
func (s *AMP1GPIOSource) Enable() error {
return s.write("1")
}
func (s *AMP1GPIOSource) Disable() error {
return s.write("0")
}

13
sources/interfaces.go Normal file
View file

@ -0,0 +1,13 @@
package sources
import ()
var SoundSources = map[string]SoundSource{}
type SoundSource interface {
GetName() string
IsActive() bool
IsEnabled() bool
Enable() error
Disable() error
}

69
sources/mpv/source.go Normal file
View file

@ -0,0 +1,69 @@
package mpv
import (
"fmt"
"os/exec"
"git.nemunai.re/nemunaire/hathoris/sources"
)
type MPVSource struct {
process *exec.Cmd
Options []string
File string
}
func init() {
sources.SoundSources["mpv"] = &MPVSource{
Options: []string{"--no-video"},
File: "https://mediaserv38.live-streams.nl:18030/stream",
}
}
func (s *MPVSource) GetName() string {
return "Radio 1"
}
func (s *MPVSource) IsActive() bool {
return s.process != nil
}
func (s *MPVSource) IsEnabled() bool {
return s.process != nil
}
func (s *MPVSource) Enable() (err error) {
if s.process != nil {
return fmt.Errorf("Already running")
}
var opts []string
opts = append(opts, s.Options...)
opts = append(opts, s.File)
s.process = exec.Command("mpv", opts...)
if err = s.process.Start(); err != nil {
return
}
go func() {
err := s.process.Wait()
if err != nil {
s.process.Process.Kill()
}
s.process = nil
}()
return
}
func (s *MPVSource) Disable() error {
if s.process != nil {
if s.process.Process != nil {
s.process.Process.Kill()
}
}
return nil
}