Skip to content

Commit

Permalink
Integrate with router for pausing and stopping RDS/DLS
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-grace committed Sep 7, 2024
1 parent bef0c30 commit 1177608
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 28 deletions.
2 changes: 2 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Author: Michael Grace <[email protected]>
package main

import (
"flag"
"os"

"github.com/UniversityRadioYork/myradio-go"
Expand All @@ -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

Expand All @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions pkg/common/config.go
Original file line number Diff line number Diff line change
@@ -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"`

Expand Down
90 changes: 75 additions & 15 deletions pkg/data/session.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
package data

import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"

"github.com/UniversityRadioYork/myradio-go"
"github.com/UniversityRadioYork/radiotext/pkg/common"
"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
Expand Down Expand Up @@ -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)
}

Expand Down
14 changes: 8 additions & 6 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1177608

Please sign in to comment.