forked from yihong0618/MiService
-
Notifications
You must be signed in to change notification settings - Fork 7
/
command.go
176 lines (164 loc) · 5.22 KB
/
command.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package miservice
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
var template = `Get Props: {prefix}<siid[-piid]>[,...]\n\
{prefix}1,1-2,1-3,1-4,2-1,2-2,3\n\
Set Props: {prefix}<siid[-piid]=[#]value>[,...]\n\
{prefix}2=#60,2-2=#false,3=test\n\
Do Action: {prefix}<siid[-piid]> <arg1|#NA> [...] \n\
{prefix}2 #NA\n\
{prefix}5 Hello\n\
{prefix}5-4 Hello #1\n\n\
Call MIoT: {prefix}<cmd=prop/get|/prop/set|action> <params>\n\
{prefix}action {quote}{{"did":"{did}","siid":5,"aiid":1,"in":["Hello"]}}{quote}\n\n\
Call MiIO: {prefix}/<uri> <data>\n\
{prefix}/home/device_list {quote}{{"getVirtualModel":false,"getHuamiDevices":1}}{quote}\n\n\
Devs List: {prefix}list [name=full|name_keyword] [getVirtualModel=false|true] [getHuamiDevices=0|1]\n\
{prefix}list Light true 0\n\n\
MIoT Spec: {prefix}spec [model_keyword|type_urn] [format=text|python|json]\n\
{prefix}spec\n\
{prefix}spec speaker\n\
{prefix}spec xiaomi.wifispeaker.lx04\n\
{prefix}spec urn:miot-spec-v2:device:speaker:0000A015:xiaomi-lx04:1\n\n\
MIoT Decode: {prefix}decode <ssecurity> <nonce> <data> [gzip]\n\
`
func IOCommandHelp(did string, prefix string) string {
var quote string
if prefix == "" {
prefix = "?"
quote = ""
} else {
quote = "'"
}
tmp := strings.ReplaceAll(template, "{prefix}", prefix)
tmp = strings.ReplaceAll(tmp, "{quote}", quote)
if did == "" {
did = "267090026"
}
tmp = strings.ReplaceAll(tmp, "{did}", did)
return tmp
}
func IOCommand(service *IOService, did string, text string, prefix string) (interface{}, error) {
cmd, arg := twinsSplit(text, " ", "")
//if strings.HasPrefix(cmd, "/") {
// return service.Request(cmd, arg)
//}
//
if strings.HasPrefix(cmd, "prop") || cmd == "action" {
var args map[string]interface{}
if err := json.Unmarshal([]byte(arg), &args); err != nil {
return nil, err
}
return service.Request(cmd, args)
}
argv := strings.Split(arg, " ")
argc := len(argv)
var arg0 string
if argc > 0 {
arg0 = argv[0]
}
var arg1 string
if argc > 1 {
arg1 = argv[1]
}
var arg2 string
if argc > 2 {
arg2 = argv[2]
}
switch cmd {
case "list":
a1 := false
if arg1 != "" {
a1, _ = strconv.ParseBool(arg1)
}
a2 := 0
if arg2 != "" {
a2, _ = strconv.Atoi(arg2)
}
return service.DeviceList(a1, a2) // Implement this method for the IOService
case "spec":
return service.IotSpec(arg0) // Implement this method for the IOService
case "decode":
if argc > 3 && argv[3] == "gzip" {
return service.IotDecode(argv[0], argv[1], argv[2], true) // Implement this method for the IOService
}
return service.IotDecode(argv[0], argv[1], argv[2], false) // Implement this method for the IOService
}
if did == "" || cmd == "" || cmd == "help" || cmd == "-h" || cmd == "--help" {
return IOCommandHelp(did, prefix), nil
}
if !isDigit(did) {
devices, err := service.DeviceList(false, 0) // Implement this method for the IOService
if err != nil {
return nil, err
}
if len(devices) == 0 {
return nil, errors.New("Device not found: " + did)
}
for _, device := range devices {
if device.Name == did {
did = device.Did
break
}
}
}
var props [][]interface{}
setp := true
miot := true
for _, item := range strings.Split(cmd, ",") {
key, value := twinsSplit(item, "=", "")
siid, iid := twinsSplit(key, "-", "1")
var prop []interface{}
if isDigit(siid) && isDigit(iid) {
s, _ := strconv.Atoi(siid)
i, _ := strconv.Atoi(iid)
prop = []interface{}{s, i}
} else {
prop = []interface{}{key}
miot = false
}
if value == "" {
setp = false
} else if setp {
prop = append(prop, stringOrValue(value))
}
props = append(props, prop)
}
if miot && argc > 0 {
var args []interface{}
if arg != "#NA" {
for _, a := range argv {
args = append(args, stringOrValue(a))
}
}
var ids []int
for _, id := range props[0] {
if v, ok := id.(int); ok {
ids = append(ids, v)
} else if v, ok := id.(string); ok {
if v2, err := strconv.Atoi(v); err == nil {
ids = append(ids, v2)
}
}
}
return service.MiotAction(did, ids, args)
}
//if setp {
// if miot {
// return service.MiotSetProps(did, props)
// } else {
// return service.HomeSetProps(did, props)
// }
//} else {
// if miot {
// return service.MiotGetProps(did, props)
// } else {
// return service.HomeGetProps(did, props)
// }
//}
return nil, errors.New("Unknown command: " + cmd)
}