Skip to content

Commit

Permalink
Merge pull request #86 from wizzomafizzo/wizzo/remote-tapto
Browse files Browse the repository at this point in the history
TapTo support for Remote
  • Loading branch information
wizzomafizzo authored Jan 16, 2024
2 parents 20100a6 + 162d0c6 commit 012ac75
Showing 1 changed file with 58 additions and 25 deletions.
83 changes: 58 additions & 25 deletions cmd/remote/games/nfc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,54 @@ package games

import (
"encoding/json"
"github.com/wizzomafizzo/mrext/pkg/config"
"github.com/wizzomafizzo/mrext/pkg/service"
"fmt"
"net/http"
"os"
"os/exec"
)

// TODO: is it possible to not rely on a specific path to the nfc app?
const nfcBin = config.ScriptsFolder + "/nfc.sh"
"github.com/wizzomafizzo/mrext/pkg/config"
"github.com/wizzomafizzo/mrext/pkg/service"
)

func nfcExists() bool {
_, err := os.Stat(nfcBin)
return !os.IsNotExist(err)
type NfcState struct {
Available bool `json:"installed"`
Running bool `json:"running"`
Name string `json:"name"`
}

func NfcStatus(logger *service.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
payload := struct {
Available bool `json:"available"`
Running bool `json:"running"`
}{}
func getNfcState() NfcState {
state := NfcState{}

if nfcExists() {
payload.Available = true
}
name := "tapto"
// TODO: is it possible to not rely on a specific path to the nfc app?
binTemplate := config.ScriptsFolder + "/%s.sh"
pidTemplate := "/tmp/%s.pid"

_, err := os.Stat("/tmp/nfc.pid")
if err == nil {
payload.Running = true
if _, err := os.Stat(fmt.Sprintf(binTemplate, name)); err == nil {
state.Available = true
} else {
name = "nfc"
if _, err := os.Stat(fmt.Sprintf(binTemplate, name)); err == nil {
state.Available = true
} else {
return state
}
}

state.Name = name

if _, err := os.Stat(fmt.Sprintf(pidTemplate, name)); err == nil {
state.Running = true
}

return state
}

func NfcStatus(logger *service.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
payload := getNfcState()

err = json.NewEncoder(w).Encode(payload)
err := json.NewEncoder(w).Encode(payload)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.Error("nfc status: encoding response: %s", err)
Expand All @@ -55,11 +71,20 @@ func NfcWrite(logger *service.Logger) http.HandlerFunc {
return
}

if !nfcExists() {
http.Error(w, "nfc script not found", http.StatusInternalServerError)
state := getNfcState()

if !state.Available {
http.Error(w, "nfc app not found", http.StatusInternalServerError)
return
}

if !state.Running {
http.Error(w, "nfc service not running", http.StatusInternalServerError)
return
}

nfcBin := fmt.Sprintf(config.ScriptsFolder+"/%s.sh", state.Name)

cmd := exec.Command(nfcBin, "-write", args.Path)
cmd.Env = append(os.Environ(), config.UserAppPathEnv+"="+nfcBin)
err = cmd.Run()
Expand All @@ -74,11 +99,19 @@ func NfcWrite(logger *service.Logger) http.HandlerFunc {

func NfcCancel(logger *service.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
if !nfcExists() {
http.Error(w, "nfc script not found", http.StatusInternalServerError)
state := getNfcState()

if !state.Available {
http.Error(w, "nfc app not found", http.StatusInternalServerError)
return
}

if !state.Running {
return
}

nfcBin := fmt.Sprintf(config.ScriptsFolder+"/%s.sh", state.Name)

cmd := exec.Command(nfcBin, "-service", "restart")
cmd.Env = append(os.Environ(), config.UserAppPathEnv+"="+nfcBin)
err := cmd.Run()
Expand Down

0 comments on commit 012ac75

Please sign in to comment.