liuxiaolong
2021-01-20 2b337fa6e928c209aafe616dcdde351595c63c1e
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
package bhomedbapi
 
import (
    "basic.com/pubsub/protomsg.git"
    "encoding/json"
)
 
type AppApi struct{
 
}
 
func (api AppApi) FindAll(appName string) (list []protomsg.App) {
    url := DATA_URL_PREFIX + "/app/findAllApp"
    netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url))
    logPrint("netNode:", netNode)
    if netNode == nil {
        return nil
    }
    client := NewClient(WithNodes(netNode))
    paramMap := make(map[string]string, 0)
    paramMap["appName"] = appName
    respBody, err := client.DoGetRequest(url, paramMap, nil)
    logPrint("DoGetRequest err:", err)
    if err !=nil {
        return nil
    }
    var res Result
    if err = json.Unmarshal(respBody, &res); err != nil {
        logPrint(err)
        return nil
    }
    bytes, _ := json.Marshal(res.Data)
    err = json.Unmarshal(bytes, &list)
    if err == nil {
        return list
    } else {
        logPrint("unmarshal err:", err)
        return nil
    }
}
 
func (api AppApi) Save(paramBody map[string]interface{}) bool {
    url := DATA_URL_PREFIX + "/app/save"
    netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url))
    if netNode == nil {
        return false
    }
    client := NewClient(WithNodes(netNode))
    body, err := client.DoPostRequest(url,CONTENT_TYPE_JSON,paramBody,nil,nil)
    if err != nil {
        return false
    }
 
    var res Result
    if err = json.Unmarshal(body, &res); err != nil {
        return false
    }
    return res.Success
}
 
//获取所有已安装的app列表,以package为key
func (api AppApi) FindAppMap() map[string]protomsg.App {
    m := make(map[string]protomsg.App)
    apps := api.FindAll("")
    if apps != nil {
        for _,a := range apps {
            m[a.Package] = a
        }
    }
 
    return m
}