Browse Source

Removing dependency on glom

  Thanks to meow-bot we now process all xml queries into their
apropriate structures, thus not needing glom any longer.
Apollo 2 years ago
parent
commit
b1c28e1ec2
4 changed files with 41 additions and 99 deletions
  1. 0 1
      main.go
  2. 13 0
      media-player.go
  3. 25 95
      poster.go
  4. 3 3
      webserver.go

+ 0 - 1
main.go

@@ -72,7 +72,6 @@ func main() {
 	}
 
 	var web WebServer = WebServer{}
-	//web.Init(bind, "", strconv.Itoa(port))
 	web.Init("192.168.254.75", bind, strconv.Itoa(port))
 
 	var e *echo.Echo = echo.New()

+ 13 - 0
media-player.go

@@ -0,0 +1,13 @@
+package main
+
+type MediaPlugin struct {
+	Bandwidth string `xml:"bandwidth,attr"`
+	Id        int    `xml:"id,attr"`
+	Name      string `xml:"name,attr"`
+}
+
+type MediaPlayer struct {
+	Error  bool        `xml:"error,attr"`
+	State  string      `xml:"state,attr"`
+	Plugin MediaPlugin `xml:"plugin"` // Might not exist in xml
+}

+ 25 - 95
poster.go

@@ -5,10 +5,8 @@ import (
 	"io"
 	"log"
 	"net/http"
-	"os"
 	"strings"
 
-	"github.com/beanzilla/glom"
 	"github.com/beego/x2j"
 )
 
@@ -133,7 +131,7 @@ func PerformKey(ip, command string) bool {
 	return true
 }
 
-func GetQuery(ip, command string) map[string]interface{} {
+func GetQuery(ip, command string) []byte {
 	var resp *http.Response = Get(Query(ip, command))
 	if resp == nil {
 		return nil
@@ -144,68 +142,38 @@ func GetQuery(ip, command string) map[string]interface{} {
 	if err != nil {
 		log.Printf("Reading from body got error, %v", err)
 	}
-	// Change this so it's xml parsing it into a real struct (bye glom, your too ugly)
-	var r map[string]interface{} = make(map[string]interface{})
-	err = x2j.Unmarshal(body, &r)
-	if err != nil {
-		log.Printf("Got a error parsing XML, %v", err)
-	}
 	defer resp.Body.Close()
-	// Save to file, for real pros
-	if !FileExists("xmls") {
-		os.Mkdir("xmls", 0660)
-	}
-	var filename string = fmt.Sprintf("xmls/%s.xml", command)
-	err = os.WriteFile(filename, body, 0660)
-	if err != nil {
-		log.Printf("Got a error printing out xml to file '%s', %v", filename, err)
-	}
-	return r
+	return body
 }
 
-func GetCurrentPlay(ip string) map[string]interface{} {
-	resp := GetQuery(ip, "media-player")
-	if resp == nil {
-		return nil
-	}
-	return resp
-}
-
-func ObtainDeviceInfo(ip string) interface{} {
-	resp := GetQuery(ip, "device-info")
-	if resp == nil {
-		return nil
+func GetCurrentPlay(ip string) *MediaPlayer {
+	var resp []byte = GetQuery(ip, "media-player")
+	var mp MediaPlayer
+	var err error = x2j.Unmarshal(resp, &mp)
+	if err != nil {
+		log.Printf("Error occurred processing XML, %v", err)
 	}
-	return resp
+	return &mp
 }
 
-func ParseDeviceInfo(dev_info interface{}) map[string]interface{} {
-	dev, err := glom.Glom(dev_info, "device-info.*")
+func ObtainDeviceInfo(ip string) *DeviceInfo {
+	var resp []byte = GetQuery(ip, "device-info")
+	var di DeviceInfo
+	var err error = x2j.Unmarshal(resp, &di)
 	if err != nil {
-		log.Print(err)
-		return nil
-	}
-	data := make(map[string]interface{})
-	keys := glom.GetPossible(dev)
-	for _, key := range keys {
-		val, err := glom.Glom(dev, key)
-		if err != nil {
-			log.Printf("%s -> %v", key, err)
-		} else {
-			data[key] = val
-		}
+		log.Printf("Error occurred processing XML, %v", err)
 	}
-	return data
+	return &di
 }
 
 // Returns a map of device-infos
-func GetDeviceInfo(ip string) map[string]interface{} {
-	dev := ParseDeviceInfo(ObtainDeviceInfo(ip))
+func GetDeviceInfo(ip string) *DeviceInfo {
+	var di *DeviceInfo = ObtainDeviceInfo(ip)
 	/*keys := glom.GetPossible(dev)
 	for _, key := range keys {
 		log.Printf("%s = %v", key, dev[key])
 	}*/
-	return dev
+	return di
 }
 
 func GetDevices(wake_on_lan map[string]string) (*[]string, map[string]string) {
@@ -234,19 +202,19 @@ func GetDevices(wake_on_lan map[string]string) (*[]string, map[string]string) {
 
 func DebugInfo(ip string) string {
 	// Query the current playing thing, and get the tv channels
-	r := GetQuery(ip, "media-player")
-	if r == nil {
-		return ""
-	}
-	name, _ := glom.Glom(r, "player.plugin.-name")
-	id, _ := glom.Glom(r, "player.plugin.-id")
+	var mp *MediaPlayer = GetCurrentPlay(ip)
+	var name string
+	var id string
 	/*r1 := GetQuery(ip, "tv-channels")
 	chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
 	chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
 	chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")*/
-	if name == nil && id == nil {
+	if mp.Plugin.Name == "" && mp.Plugin.Id == 0 {
 		name = "TV"
 		id = "No Connection"
+	} else {
+		name = mp.Plugin.Name
+		id = fmt.Sprintf("%d", mp.Plugin.Id)
 	}
 
 	// Debug print
@@ -254,41 +222,3 @@ func DebugInfo(ip string) string {
 	//fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
 	return fmt.Sprintf("%v (%v)", name, id)
 }
-
-func examplePost() {
-	keys := GetKeys()
-	_ = keys // Keep alive
-	PerformKey("192.168.254.75", keys["mute"])
-	//Post("http://192.168.254.75:8060/launch/74519")
-	//PerformKey("192.168.254.75", keys["home"])
-
-	/*
-		//resp := Post("http://192.168.254.75:8060/keypress/volumeMute")
-		//resp := Post(KeyPress("192.168.254.75", "volumeMute"))
-		//resp := Get(Query("192.168.254.75", "device-info"))
-		//resp := GetQuery("192.168.254.75", "tv-active-channel")
-		resp := GetQuery("192.168.254.75", "tv-channels")
-
-		//result, err := glom.Glom(resp, "tv-channels.channel")
-		_, err := glom.Glom(resp, "*")
-		if err != nil {
-			log.Println(err)
-		} else {
-			//fmt.Println(result)
-			fmt.Println(glom.GetPossible(result))
-		}
-	*/
-
-	// Query the current playing thing, and get the tv channels
-	r := GetQuery("192.168.254.75", "media-player")
-	name, _ := glom.Glom(r, "player.plugin.-name")
-	id, _ := glom.Glom(r, "player.plugin.-id")
-	r1 := GetQuery("192.168.254.75", "tv-channels")
-	chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
-	chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
-	chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")
-
-	// Debug print
-	fmt.Printf("Name: %v (%v)\r\n", name, id)
-	fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
-}

+ 3 - 3
webserver.go

@@ -223,13 +223,13 @@ func (w *WebServer) tvcmd(c echo.Context) error {
 			sendEtherWake(w.WakeOnLan[w.Tv_ip])
 			w.At = DebugInfo(w.Tv_ip)
 		}
-		device := GetDeviceInfo(w.Tv_ip)
-		if device["power-mode"] == "PowerOn" {
+		var device *DeviceInfo = GetDeviceInfo(w.Tv_ip)
+		if device.PowerMode == "PowerOn" {
 			w.Power = true
 		} else {
 			w.Power = false
 		}
-		data := make(map[string]interface{})
+		var data map[string]interface{} = make(map[string]interface{})
 		data["at"] = w.At
 		data["power"] = w.Power
 		return c.JSON(http.StatusOK, data)