-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparams.go
60 lines (53 loc) · 1.29 KB
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package yeelight
import (
"encoding/json"
"log"
"strconv"
"strings"
)
type YeelightParams struct {
Model string `json:"model"`
Support []string `json:"support"`
Power string `json:"power"`
Bright int `json:"bright"`
ColorMode int `json:"color_mode"`
RGB int `json:"rgb"`
Hue int `json:"hue"`
Sat int `json:"sat"`
Name string `json:"name"`
}
func parseAnswer(msg string) *YeelightParams {
dict := make(map[string]interface{})
arr := strings.Split(msg, crlf)
for _, line := range arr {
if strings.Contains(line, ":") {
//fmt.Println(line)
lineArr := strings.Split(line, ": ")
key := lineArr[0]
value := lineArr[1]
switch key {
case "support":
valueArr := strings.Split(value, " ")
dict[key] = valueArr
case "fw_ver", "bright", "color_mode", "rgb", "hue", "sat":
intValue, err := strconv.Atoi(value)
if err != nil {
log.Println("Error convert to int", key)
}
dict[key] = intValue
default:
dict[key] = value
}
}
}
j, err := json.Marshal(dict)
if err != nil {
log.Println("Error convert params dict to JSON")
}
params := new(YeelightParams)
err = json.Unmarshal(j, ¶ms)
if err != nil {
log.Println("Error convert JSON to param struct")
}
return params
}