123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package main
- import (
- "fmt"
- "io"
- "log"
- "net/http"
- "strings"
- "github.com/beego/x2j"
- )
- func Post(where string) *http.Response {
- resp, err := http.PostForm(where, nil)
- if err != nil {
- log.Printf("POST got non 200 code, %v", err)
-
- return nil
- }
- return resp
- }
- func Get(where string) *http.Response {
- resp, err := http.Get(where)
- if err != nil {
- log.Printf("GET got non 200 code, %v", err)
- return nil
- } else {
- return resp
- }
- }
- func KeyPress(ip, command string) string {
- return fmt.Sprintf("http://%s:8060/keypress/%s", ip, command)
- }
- func Query(ip, what string) string {
- return fmt.Sprintf("http://%s:8060/query/%s", ip, what)
- }
- func PerformKey(ip, command string) bool {
- r := Post(KeyPress(ip, command))
- if r == nil {
- return false
- }
- defer r.Body.Close()
- return true
- }
- func GetQuery(ip, command string) []byte {
- var resp *http.Response = Get(Query(ip, command))
- if resp == nil {
- return nil
- }
- var body []byte
- var err error
- body, err = io.ReadAll(resp.Body)
- if err != nil {
- log.Printf("Reading from body got error, %v", err)
- }
- defer resp.Body.Close()
- return body
- }
- 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 &mp
- }
- 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.Printf("Error occurred processing XML, %v", err)
- }
- return &di
- }
- func GetDeviceInfo(ip string) *DeviceInfo {
- var di *DeviceInfo = ObtainDeviceInfo(ip)
-
- return di
- }
- func GetDevices(wake_on_lan map[string]string) (*[]string, map[string]string) {
- var str []string
- resp := strings.Split(PostFind(), "\n")
-
- ip := ""
- for _, line := range resp {
- if strings.Contains(line, "LOCATION") {
- st := strings.ReplaceAll(line, "LOCATION: http://", "")
- st = strings.ReplaceAll(st, ":8060/", "")
- st = strings.ReplaceAll(st, "\r", "")
-
- str = append(str, st)
- ip = st
- } else if strings.Contains(line, "WAKEUP") {
- st := strings.ReplaceAll(line, "WAKEUP: MAC=", "")
- st = strings.ReplaceAll(st, ";Timeout=10", "")
- st = strings.ReplaceAll(st, "\r", "")
-
- wake_on_lan[ip] = st
- }
- }
- return &str, wake_on_lan
- }
- func DebugInfo(ip string) string {
-
- var mp *MediaPlayer = GetCurrentPlay(ip)
- var name string
- var id string
-
- 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)
- }
-
- fmt.Printf("Name: %v (%v)\r\n", name, id)
-
- return fmt.Sprintf("%v (%v)", name, id)
- }
|