-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Initial implementation for
play
(#9)
implements #8
- Loading branch information
Showing
13 changed files
with
400 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2024 Fabian `xx4h` Sylvester | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/xx4h/hctl/pkg" | ||
) | ||
|
||
// toggleCmd represents the toggle command | ||
func newPlayCmd(h *pkg.Hctl, _ io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "play", | ||
Short: "Play music from url on media player", | ||
Aliases: []string{"p"}, | ||
Args: cobra.MatchAll(cobra.ExactArgs(2)), | ||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
if len(args) != 0 { | ||
return noMoreArgsComp() | ||
} | ||
return compListStates(toComplete, args, "play_media", "", h) | ||
}, | ||
Run: func(_ *cobra.Command, args []string) { | ||
h.PlayMusic(args[0], args[1]) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 Fabian `xx4h` Sylvester | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rest | ||
|
||
import "fmt" | ||
|
||
func (h *Hass) play(mediaURL string, mediaType string, sub string, obj string) error { | ||
hasDomain, err := h.hasDomainWithService(sub, "play_media") | ||
if err != nil { | ||
return err | ||
} else if !hasDomain { | ||
return fmt.Errorf("No such Domain with Service: %s with %s", sub, "play_media") | ||
} | ||
if !h.hasEntityInDomain(obj, sub) { | ||
return fmt.Errorf("No such Entity in Domain: %s in %s", obj, sub) | ||
} | ||
payload := map[string]any{"entity_id": fmt.Sprintf("%s.%s", sub, obj), "media_content_id": mediaURL, "media_content_type": mediaType} | ||
res, err := h.api("POST", fmt.Sprintf("/services/%s/play_media", sub), payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := h.getResult(res); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *Hass) PlayMusic(obj string, mediaURL string, name string) (string, string, string, error) { | ||
sub, obj, err := h.entityArgHandler([]string{obj}, "play_media") | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
return obj, fmt.Sprintf("playing %s", name), sub, h.play(mediaURL, "music", sub, obj) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2024 Fabian `xx4h` Sylvester | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package serve | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
/* | ||
Big thanks to https://stackoverflow.com/a/62766994/1922402 | ||
*/ | ||
|
||
type ConnectionWatcher struct { | ||
// mu protects remaining fields | ||
mu sync.Mutex | ||
|
||
// open connections are keys in the map | ||
m map[net.Conn]struct{} | ||
} | ||
|
||
// OnStateChange records open connections in response to connection | ||
// state changes. Set net/http Server.ConnState to this method | ||
// as value. | ||
func (cw *ConnectionWatcher) OnStateChange(conn net.Conn, state http.ConnState) { | ||
switch state { | ||
case http.StateNew: | ||
cw.mu.Lock() | ||
if cw.m == nil { | ||
cw.m = make(map[net.Conn]struct{}) | ||
} | ||
cw.m[conn] = struct{}{} | ||
cw.mu.Unlock() | ||
case http.StateHijacked, http.StateClosed: | ||
cw.mu.Lock() | ||
delete(cw.m, conn) | ||
cw.mu.Unlock() | ||
} | ||
} | ||
|
||
// Connections returns the open connections at the time | ||
// the call. | ||
func (cw *ConnectionWatcher) Connections() []net.Conn { | ||
var result []net.Conn | ||
cw.mu.Lock() | ||
for conn := range cw.m { | ||
result = append(result, conn) | ||
} | ||
cw.mu.Unlock() | ||
return result | ||
} |
Oops, something went wrong.