From 117760812881eb283ac45fba508b2ea8c7c80236 Mon Sep 17 00:00:00 2001 From: michael-grace Date: Sat, 7 Sep 2024 21:29:43 +0100 Subject: [PATCH] Integrate with router for pausing and stopping RDS/DLS --- config.example.yml | 2 + main.go | 18 +++++++-- pkg/common/config.go | 8 ++-- pkg/data/session.go | 90 ++++++++++++++++++++++++++++++++++++-------- pkg/ssh/ssh.go | 14 ++++--- 5 files changed, 104 insertions(+), 28 deletions(-) diff --git a/config.example.yml b/config.example.yml index e3f35c8..37ed797 100644 --- a/config.example.yml +++ b/config.example.yml @@ -2,6 +2,8 @@ defaultMessage: "By Students, For Students - University Radio York" waitTime: 30 maxTextLength: 64 +endpointForRunningCheck: "http://localhost:5001/v2/programmedata" +runningCheckKey: "fm" apiKey: key diff --git a/main.go b/main.go index 030679f..d076de3 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ Author: Michael Grace package main import ( + "flag" "os" "github.com/UniversityRadioYork/myradio-go" @@ -18,6 +19,9 @@ import ( ) func main() { + dontDoSSHSession := flag.Bool("n", false, "do not start a ssh session") + flag.Parse() + // Load Config var config common.Config @@ -39,11 +43,17 @@ func main() { } // Create SSH Session - session.SSHSession, err = ssh.OpenSSHConnection(config) - if err != nil { - panic(err) + if !*dontDoSSHSession { + session.SSHSession, err = ssh.OpenSSHConnection(config) + if err != nil { + panic(err) + } + defer session.SSHSession.Close() + } else { + session.SSHSession = &ssh.SSHSession{ + Config: config, + } } - defer session.SSHSession.Close() // URY News go session.URYNewsHandler() diff --git a/pkg/common/config.go b/pkg/common/config.go index 8207434..f02b40c 100644 --- a/pkg/common/config.go +++ b/pkg/common/config.go @@ -1,9 +1,11 @@ package common type Config struct { - DefaultMessage string `yaml:"defaultMessage"` - WaitTime int `yaml:"waitTime"` - MaxTextLength int `yaml:"maxTextLength"` + DefaultMessage string `yaml:"defaultMessage"` + WaitTime int `yaml:"waitTime"` + MaxTextLength int `yaml:"maxTextLength"` + EndpointForRunningCheck string `yaml:"endpointForRunningCheck"` + RunningCheckKey string `yaml:"runningCheckKey"` APIKey string `yaml:"apiKey"` diff --git a/pkg/data/session.go b/pkg/data/session.go index 5e07e78..76015dc 100644 --- a/pkg/data/session.go +++ b/pkg/data/session.go @@ -1,8 +1,11 @@ package data import ( + "encoding/json" "fmt" + "io" "log" + "net/http" "time" "github.com/UniversityRadioYork/myradio-go" @@ -10,6 +13,40 @@ import ( "github.com/UniversityRadioYork/radiotext/pkg/ssh" ) +type outputState int + +const ( + OutputRun outputState = iota + OutputPause + OutputStop +) + +var mostRecentMessage string + +func (s *RadiotextSession) checkOutputState() outputState { + res, err := http.Get(s.SSHSession.Config.EndpointForRunningCheck) + if err != nil { + log.Println(err.Error()) + return OutputRun + } + + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + if err != nil { + log.Println(err.Error()) + return OutputRun + } + + var state map[string]int + if err = json.Unmarshal(body, &state); err != nil { + log.Println(err.Error()) + return OutputRun + } + + return []outputState{OutputRun, OutputPause, OutputStop}[state[s.SSHSession.Config.RunningCheckKey]] + +} + type RadiotextSession struct { SSHSession *ssh.SSHSession MyRadioSession *myradio.Session @@ -46,26 +83,49 @@ func (s *RadiotextSession) OutputRadioTextMessage(msg string, highPriority bool) msg = m for _, output := range common.SplitMessageToLength(msg, s.SSHSession.Config.MaxTextLength) { - log.Println(output) - _, err := s.SSHSession.Stdin.Write([]byte( - fmt.Sprintf( - "echo -ne \"%v\\f\" > %v\n", - output, - s.SSHSession.Config.OutputDevice, - ), - )) - - if err != nil { - if err.Error() == "EOF" { - s.SSHSession.Close() - s.SSHSession.CreateConnection() - s.OutputRadioTextMessage(msg, highPriority) + switch s.checkOutputState() { + case OutputRun: + // run + case OutputPause: + time.Sleep(time.Duration(s.SSHSession.Config.WaitTime/3) * time.Second) + return + case OutputStop: + if mostRecentMessage == s.SSHSession.Config.DefaultMessage { + time.Sleep(time.Duration(s.SSHSession.Config.WaitTime/3) * time.Second) return } + if output != s.SSHSession.Config.DefaultMessage { + s.OutputRadioTextMessage(s.SSHSession.Config.DefaultMessage, false) + return + } + } + + log.Println(output) + + if s.SSHSession.DoConnection { + _, err := s.SSHSession.Stdin.Write([]byte( + fmt.Sprintf( + "echo -ne \"%v\\f\" > %v\n", + output, + s.SSHSession.Config.OutputDevice, + ), + )) + + if err != nil { + if err.Error() == "EOF" { + s.SSHSession.Close() + s.SSHSession.CreateConnection() + s.OutputRadioTextMessage(msg, highPriority) + return + } + + fmt.Println(err) + } - fmt.Println(err) } + mostRecentMessage = output + time.Sleep(time.Duration(s.SSHSession.Config.WaitTime) * time.Second) } diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index 57017ea..5a0ec0f 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -10,16 +10,18 @@ import ( ) type SSHSession struct { - Config common.Config - sshConf ssh.ClientConfig - conn *ssh.Client - session *ssh.Session - Stdin io.WriteCloser + DoConnection bool + Config common.Config + sshConf ssh.ClientConfig + conn *ssh.Client + session *ssh.Session + Stdin io.WriteCloser } func OpenSSHConnection(config common.Config) (*SSHSession, error) { s := SSHSession{ - Config: config, + Config: config, + DoConnection: true, sshConf: ssh.ClientConfig{ User: config.SSHUser, HostKeyCallback: ssh.InsecureIgnoreHostKey(),