Add "add" html page

This commit is contained in:
Alexey Khit
2023-03-19 16:38:36 +03:00
parent e728643aad
commit bd79b24db3
10 changed files with 272 additions and 231 deletions
+20
View File
@@ -144,3 +144,23 @@ func exitHandler(w http.ResponseWriter, r *http.Request) {
code, _ := strconv.Atoi(s)
os.Exit(code)
}
type Stream struct {
Name string `json:"name"`
URL string `json:"url"`
}
func ResponseStreams(w http.ResponseWriter, streams []Stream) {
if len(streams) == 0 {
http.Error(w, "no streams", http.StatusNotFound)
return
}
var response struct {
Streams []Stream `json:"streams"`
}
response.Streams = streams
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
+16 -8
View File
@@ -1,7 +1,6 @@
package device
import (
"encoding/json"
"github.com/AlexxIT/go2rtc/cmd/api"
"github.com/AlexxIT/go2rtc/cmd/app"
"github.com/AlexxIT/go2rtc/pkg/core"
@@ -72,12 +71,21 @@ func handle(w http.ResponseWriter, r *http.Request) {
loadMedias()
}
data, err := json.Marshal(medias)
if err != nil {
log.Error().Err(err).Msg("[api.ffmpeg]")
return
}
if _, err = w.Write(data); err != nil {
log.Error().Err(err).Msg("[api.ffmpeg]")
var items []api.Stream
var iv, ia int
for _, media := range medias {
var source string
switch media.Kind {
case core.KindVideo:
source = "ffmpeg:device?video=" + strconv.Itoa(iv)
iv++
case core.KindAudio:
source = "ffmpeg:device?audio=" + strconv.Itoa(ia)
ia++
}
items = append(items, api.Stream{Name: media.ID, URL: source})
}
api.ResponseStreams(w, items)
}
+10
View File
@@ -3,11 +3,13 @@ package hass
import (
"encoding/json"
"fmt"
"github.com/AlexxIT/go2rtc/cmd/api"
"github.com/AlexxIT/go2rtc/cmd/app"
"github.com/AlexxIT/go2rtc/cmd/roborock"
"github.com/AlexxIT/go2rtc/cmd/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/rs/zerolog"
"net/http"
"os"
"path"
)
@@ -39,6 +41,14 @@ func Init() {
urls := map[string]string{}
api.HandleFunc("api/hass", func(w http.ResponseWriter, r *http.Request) {
var items []api.Stream
for name, url := range urls {
items = append(items, api.Stream{Name: name, URL: url})
}
api.ResponseStreams(w, items)
})
streams.HandleFunc("hass", func(url string) (core.Producer, error) {
if hurl := urls[url[5:]]; hurl != "" {
return streams.GetProducer(hurl)
+3 -23
View File
@@ -1,7 +1,6 @@
package roborock
import (
"encoding/json"
"fmt"
"github.com/AlexxIT/go2rtc/cmd/api"
"github.com/AlexxIT/go2rtc/cmd/streams"
@@ -85,17 +84,7 @@ func apiHandle(w http.ResponseWriter, r *http.Request) {
return
}
if len(devices) == 0 {
http.Error(w, "no devices in the account", http.StatusNotFound)
return
}
var response struct {
Devices []struct {
Name string `json:"name"`
Source string `json:"source"`
} `json:"devices"`
}
var items []api.Stream
for _, device := range devices {
source := fmt.Sprintf(
@@ -104,17 +93,8 @@ func apiHandle(w http.ResponseWriter, r *http.Request) {
Auth.UserData.IoT.User, Auth.UserData.IoT.Pass, Auth.UserData.IoT.Domain,
device.DID, device.Key,
)
response.Devices = append(response.Devices, struct {
Name string `json:"name"`
Source string `json:"source"`
}{
Name: device.Name,
Source: source,
})
items = append(items, api.Stream{Name: device.Name, URL: source})
}
if err = json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
api.ResponseStreams(w, items)
}