-
Notifications
You must be signed in to change notification settings - Fork 0
/
dji_result.go
83 lines (68 loc) · 1.6 KB
/
dji_result.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
package dji
import (
"encoding/json"
"fmt"
)
type DJIResult struct {
key DJIKeys
sequenceNumber uint32
value DJIParamValue
errorCode int64
errorDesc string
}
func NewDJIResult(key DJIKeys) *DJIResult {
return &DJIResult{
key: key,
}
}
func NewDJIResultFromJSON(jsonData []byte) *DJIResult {
r := &DJIResult{}
r.parseJSONData(jsonData)
return r
}
func (r *DJIResult) Key() DJIKeys {
return r.key
}
func (r *DJIResult) SequenceNumber() uint32 {
return r.sequenceNumber
}
func (r *DJIResult) Value() DJIParamValue {
return r.value
}
func (r *DJIResult) ErrorCode() int64 {
return r.errorCode
}
func (r *DJIResult) ErrorDesc() string {
return r.errorDesc
}
func (r *DJIResult) Succeeded() bool {
return r.errorCode == 0
}
func (r *DJIResult) parseJSONData(jsonData []byte) {
if len(jsonData) == 0 {
r.errorCode = -1
r.errorDesc = "empty or nil json data"
return
}
var data map[string]interface{}
err := json.Unmarshal(jsonData, &data)
if err != nil {
r.errorCode = -1
r.errorDesc = fmt.Sprintf("invalid json data: %s", string(jsonData))
return
}
r.sequenceNumber = uint32(data["Tag"].(float64))
r.key = keyByValue(int(data["Key"].(float64)))
r.errorCode = int64(data["Error"].(float64))
switch data["Value"].(type) {
case string:
r.value = data["Value"].(string)
case map[string]interface{}:
r.value = data["Value"].(map[string]interface{})["value"]
default:
panic(fmt.Sprintf("invalid value type: %T", data["Value"]))
}
if r.value == nil {
r.errorCode = -1
}
}