Skip to content

Commit

Permalink
Lots
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed Apr 23, 2023
1 parent 2e75567 commit 9c3b339
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 90 deletions.
14 changes: 12 additions & 2 deletions examples/get_info/get_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,18 @@ func main() {
fmt.Println("MAC Address:", speaker.MacAddress)
volume, _ := speaker.GetVolume()
fmt.Println("Volume:", volume)
source, _ := speaker.GetSource()
source, _ := speaker.Source()
fmt.Println("Source:", source)
powerstate, _ := speaker.GetPowerState()
powerstate, _ := speaker.IsPoweredOn()
fmt.Println("Powered on:", powerstate)
// speaker.PowerOff()
// err = speaker.Unmute()
// if err != nil {
// log.Fatal(err)
// }
speaker.PlayPause()
err = speaker.SetSource(kefw2.SourceTV)
if err != nil {
fmt.Println(err)
}
}
25 changes: 25 additions & 0 deletions kefw2/eqprofilev2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kefw2

type EQProfileV2 struct {
SubwooferCount int `json:"subwooferCount"` // 0, 1, 2
TrebleAmount float32 `json:"trebleAmount"`
DeskMode bool `json:"deskMode"`
BassExtension string `json:"bassExtension"` // less, standard, more
HighPassMode bool `json:"highPassMode"`
AudioPolarity string `json:"audioPolarity"`
IsExpertMode bool `json:"isExpertMode"`
DeskModeSetting int `json:"deskModeSetting"`
SubwooferPreset string `json:"subwooferPreset"`
HighPassModeFreq int `json:"highPassModeFreq"`
WallModeSetting float32 `json:"wallModeSetting"`
Balance int `json:"balance"`
SubEnableStereo bool `json:"subEnableStereo"`
SubwooferPolarity string `json:"subwooferPolarity"`
SubwooferGain int `json:"subwooferGain"`
IsKW1 bool `json:"isKW1"`
PhaseCorrection bool `json:"phaseCorrection"`
WallMode bool `json:"wallMode"`
ProfileId string `json:"profileId"`
ProfileName string `json:"profileName"`
SubOutLPFreq float32 `json:"subOutLPFreq"`
}
134 changes: 126 additions & 8 deletions kefw2/http.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,73 @@
package kefw2

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

log "github.com/sirupsen/logrus"
)

type KEFPostRequest struct {
Path string `json:"path"`
Roles string `json:"roles"`
Value *json.RawMessage `json:"value"`
}

func (s KEFSpeaker) getData(path string) ([]byte, error) {
// log.SetLevel(log.DebugLevel)
client := &http.Client{}

req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/getData", s.IPAddress), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")

q := req.URL.Query()
q.Add("path", path)
q.Add("roles", "value")
req.URL.RawQuery = q.Encode()

log.Debug("Request:", req.URL.String())
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Debug("Response:", resp.StatusCode, resp.Body)
return nil, fmt.Errorf("HTTP Status Code: %d\n%s", resp.StatusCode, resp.Body)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
log.Debug("Response:", resp.StatusCode, resp.Body)
return nil, fmt.Errorf("HTTP Status Code: %d\n%s", resp.StatusCode, resp.Body)
}

return body, nil
}

func (s KEFSpeaker) getRows(path string, params map[string]string) ([]byte, error) {
// log.SetLevel(log.DebugLevel)
client := &http.Client{}

req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/getRows", s.IPAddress), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")

q := req.URL.Query()
q.Add("path", path) // Always add the path
for key, value := range params {
q.Add(key, value)
}
req.URL.RawQuery = q.Encode()

log.Debug("Request:", req.URL.String())
resp, err := client.Do(req)
if err != nil {
return nil, err
Expand All @@ -74,3 +86,109 @@ func (s KEFSpeaker) getRows(path string, params map[string]string) ([]byte, erro

return body, nil
}

func (s KEFSpeaker) setActivate(path, item, value string) error {
client := &http.Client{}

jsonStr, _ := json.Marshal(
map[string]string{
item: value,
})
rawValue := json.RawMessage(jsonStr)

reqbody, _ := json.Marshal(KEFPostRequest{
Path: path,
Roles: "activate",
Value: &rawValue,
})

req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/api/setData", s.IPAddress), bytes.NewBuffer(reqbody))
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Debug("Response:", resp.StatusCode, resp.Body)
return fmt.Errorf("HTTP Status Code: %d\n%s", resp.StatusCode, resp.Body)
}

// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return nil, err
// }

return nil
}

func (s KEFSpeaker) setTypedValue(path string, value any) error {
client := &http.Client{}

var myType string
var myValue string
switch theType := value.(type) {
case int:
myType = "i32_"
myValue = fmt.Sprintf("%d", value.(int))
case string:
myType = "string_"
myValue = fmt.Sprintf("\"%s\"", value.(string))
case bool:
myType = "bool_"
myValue = fmt.Sprintf("%t", value.(bool))
case Source:
myType = "kefPhysicalSource"
myValue = fmt.Sprintf("\"%s\"", value.(Source))
case SpeakerStatus:
myType = "kefSpeakerStatus"
myValue = fmt.Sprintf("\"%s\"", value.(SpeakerStatus))
default:
return fmt.Errorf("type %s is not supported", theType)
}

// Build the JSON
jsonStr, _ := json.Marshal(
map[string]string{
"type": myType,
myType: myValue,
})
rawValue := json.RawMessage(jsonStr)
pr := KEFPostRequest{
Path: path,
Roles: "value",
Value: &rawValue,
}

reqbody, _ := json.MarshalIndent(pr, "", " ")
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s/api/setData", s.IPAddress), bytes.NewBuffer(reqbody))
if err != nil {
return err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Debug("Response:", resp.StatusCode, resp.Body)
return fmt.Errorf("HTTP Status Code: %d\n%s", resp.StatusCode, resp.Body)
}

// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return nil, err
// }

return nil
}
44 changes: 0 additions & 44 deletions kefw2/json-parsing.go

This file was deleted.

78 changes: 78 additions & 0 deletions kefw2/json_parsing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package kefw2

import (
"encoding/json"
"errors"
)

func JSONStringValue(data []byte, err error) (value string, err2 error) {
if err != nil {
return "", err
}
var jsonData []map[string]interface{}
err2 = json.Unmarshal(data, &jsonData)
if err2 != nil {
return "", err
}
value = jsonData[0]["string_"].(string)
return value, nil
}

func JSONStringValueByKey(data []byte, key string, err error) (value string, err2 error) {
if err != nil {
return "", err
}
var jsonData []map[string]string
err2 = json.Unmarshal(data, &jsonData)
if err2 != nil {
return "", err
}
value = jsonData[0]["value"]
return value, nil
}

func JSONIntValue(data []byte, err error) (value int, err2 error) {
if err != nil {
return 0, err
}
var jsonData []map[string]interface{}
err2 = json.Unmarshal(data, &jsonData)
if err2 != nil {
return 0, err
}
fvalue, _ := jsonData[0]["i32_"].(float64)
return int(fvalue), nil
}

func JSONUnmarshalValue(data []byte, err error) (value any, err2 error) {
// Easing the call chain
if err != nil {
return 0, err
}

// Unmarshal the JSON data into a map of strings to any
var jsonData []map[string]any
err2 = json.Unmarshal(data, &jsonData)
if err2 != nil {
return 0, err
}
// Locate the value and set the type
tvalue := jsonData[0]["type"].(string)
switch tvalue {
case "i32_":
value = jsonData[0]["i32_"].(int)
case "i64_":
value = jsonData[0]["i64_"].(int)
case "string_":
value = jsonData[0]["string_"].(string)
case "bool_":
value = jsonData[0]["bool_"].(bool)
case "kefPhysicalSource":
value = Source(jsonData[0]["kefPhysicalSource"].(string))
case "kefSpeakerStatus":
value = SpeakerStatus(jsonData[0]["kefSpeakerStatus"].(string))
default:
return nil, errors.New("Unknown type: " + tvalue)
}
return value, nil
}
Loading

0 comments on commit 9c3b339

Please sign in to comment.