From 20ed84cad4fd237cca40619d0fe7ed39c5e030cd Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期日, 19 一月 2020 20:00:51 +0800
Subject: [PATCH] feat: add sdk download and regiter

---
 service/SdkDownLoad.go |  162 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 137 insertions(+), 25 deletions(-)

diff --git a/service/SdkDownLoad.go b/service/SdkDownLoad.go
index 00715d9..ed364ca 100644
--- a/service/SdkDownLoad.go
+++ b/service/SdkDownLoad.go
@@ -4,63 +4,78 @@
 	"bytes"
 	"crypto/md5"
 	"encoding/hex"
+	"encoding/json"
 	"fmt"
 	"io"
 	"io/ioutil"
 	"net/http"
 	"os"
+	"path/filepath"
 	"strings"
-	"basic.com/valib/logger.git"
 
 	"webserver/util"
+
+	"basic.com/dbapi.git"
+	"basic.com/valib/logger.git"
 )
+
+func DownSdk(id string) map[string]interface{} {
+	// 涓嬭浇绠楁硶
+	if ok, err := DownSo(id); !ok {
+		logger.Error("涓嬭浇绠楁硶澶辫触", err)
+		return nil
+	}
+
+	return FindLocalSdkSoById(id)
+}
+
 // 浠庢湇鍔″櫒涓婁笅杞芥枃浠跺埌涓存椂鐩綍锛屾牎楠屼箣鍚庡鏋滃畬鏁村皢鍏舵嫹璐濆埌鐩爣鐩綍涓�
-func DownSo(url string)(bool,error) {
+func DownSo(url string) (bool, error) {
 	resp, err := http.Get(url)
 	if err != nil {
 		logger.Error("鑾峰彇鏂囦欢澶辫触")
-		return false,err
+		return false, err
 	}
 	// 浠巖esp涓鍑簔ip鏂囦欢瑙e帇缂╋紝瑙e嚭face.so,face.txt,鐒跺悗鎶婅В鍘嬪嚭鐨剆o鐢∕D5缂栫爜鍑轰竴涓猼emp.txt鏂囦欢锛屼笌瑙e帇鍑虹殑so.txt鏂囦欢姣斿锛�
 	body, err := ioutil.ReadAll(resp.Body)
 	if err != nil {
 		logger.Error("璇诲彇resp.body澶辫触")
-		return false,err
+		return false, err
 	}
 	CopyFile(body, "/opt/temp/temp.zip")
 	util.DeCompress("/opt/temp/temp.zip", "/opt/temp")
-	fileName := GetFileNameFromUrl(url,false)
-	md5str,err1 := File2md5("/opt/temp/"+fileName+".so")
+	fileName := GetFileNameFromUrl(url, false)
+	md5str, err1 := File2md5("/opt/temp/" + fileName + ".so")
 	if err1 != nil {
 		logger.Error(err1)
-		return false,err1
+		return false, err1
 	}
-	md5str_origin,err2 := ioutil.ReadFile("/opt/temp/"+fileName+".txt")
+	md5str_origin, err2 := ioutil.ReadFile("/opt/temp/" + fileName + ".txt")
 	if err2 != nil {
 		logger.Error("璇诲彇瑙e帇鍚庣殑md5鏂囦欢澶辫触")
-		return false,err2
+		return false, err2
 	}
-	flag := CompareMd5([]byte(md5str),md5str_origin)
+	flag := CompareMd5([]byte(md5str), md5str_origin)
 	if flag {
 		logger.Info("涓ゆMD5缂栫爜涓�鑷达紒")
 	} else {
 		logger.Debug("涓ゆMD5缂栫爜涓嶄竴鑷达紝璇烽噸鏂颁笅杞�")
-		return false,nil
+		return false, nil
 	}
 	// 浠巙rl涓埅鍙杝oName
-	soName := GetFileNameFromUrl(url,true)
+	soName := GetFileNameFromUrl(url, true)
 	f, err := os.Create("/opt/workspace/ruleprocess/algorithm/" + soName)
 	if err != nil {
 		logger.Error("鍦ㄩ」鐩洰褰曚笅鍒涘缓so鏂囦欢澶辫触")
-		return false,err
+		return false, err
 	}
-	data,_ := ioutil.ReadFile("/opt/temp/"+soName)
-	_,err4 := f.Write(data)
+	data, _ := ioutil.ReadFile("/opt/temp/" + soName)
+	_, err4 := f.Write(data)
 	if err4 != nil {
 		logger.Error("澶嶅埗鏂囦欢鍑洪敊")
-		return false,err4
+		return false, err4
 	}
-	return true,nil
+	return true, nil
 }
 
 func CopyFile(byte []byte, dst string) (w int64, err error) {
@@ -74,7 +89,7 @@
 }
 
 // 鎸囧畾鐩綍鐨勬枃浠剁敓鎴愮浉搴旂殑MD5鐮佹枃浠�
-func File2md5 (filename string) (string, error) {
+func File2md5(filename string) (string, error) {
 	// 鏂囦欢鐢熸垚MD5鍔犲瘑鍔犲瘑鏂囦欢
 	file, err := os.Open(filename)
 	if err != nil {
@@ -88,25 +103,122 @@
 		return "", err
 	}
 	md5Str := hex.EncodeToString(md5.Sum(nil))
-	return md5Str,nil
+	return md5Str, nil
 }
+
 // 浠巙rl涓埅鍙栧嚭鏂囦欢鍚嶏紝鍙傛暟鏄槸鍚﹀甫鍚庣紑
-func GetFileNameFromUrl(url string,withSuffix bool)string {
-	fileName := strings.Split(url,"/")[len(strings.Split(url,"/"))-1]
+func GetFileNameFromUrl(url string, withSuffix bool) string {
+	fileName := strings.Split(url, "/")[len(strings.Split(url, "/"))-1]
 	if withSuffix {
 		return fileName
 	} else {
-		withoutSuffix := strings.Split(fileName,".")[0]
+		withoutSuffix := strings.Split(fileName, ".")[0]
 		return withoutSuffix
 	}
 }
 
 // 姣旇緝涓や釜MD5缂栫爜鏄惁涓�鑷�
-func CompareMd5(value1 []byte,value2 []byte) bool{
-	num := bytes.Compare(value1,value2)
+func CompareMd5(value1 []byte, value2 []byte) bool {
+	num := bytes.Compare(value1, value2)
 	if num == 0 {
 		return true
 	} else {
 		return false
 	}
-}
\ No newline at end of file
+}
+
+func GetSdkList(sdkName string) []map[string]interface{} {
+	// 寰呰繑鍥炵殑鏁版嵁
+	sdks, idsCache := queryDatabase(sdkName)
+
+	// 鏌ヨ鏈湴宸茬粡瀛樺湪鐨勭畻娉曞拰杩滅鐨勫叏閮ㄧ畻娉�, 鐒跺悗鍜屽凡瀹夎鐨勬瘮瀵�, 杩斿洖鍏ㄩ儴鍙敤鐨勭畻娉曠粍. 鏈畨瑁呯殑绠楁硶鍦ㄥ墠绔蛋涓嬭浇娴佺▼
+	localSdks := GetLocalSdks()
+	for _, sdk := range localSdks {
+		id := sdk["id"].(string)
+		if !idsCache[id] {
+			idsCache[id] = true
+
+			sdk["enable"] = false
+			sdk["installed"] = false
+
+			// 涓嶈繑鍥炲弬鏁颁俊鎭�
+			delete(sdk, "args")
+			sdks = append(sdks, sdk)
+		}
+
+	}
+
+	// 鐩墠杩樻病鏈夎繙绔畻娉曞簱,
+	// remoteSdks := getRemoteSdks()
+
+	return sdks
+}
+
+func queryDatabase(sdkName string) ([]map[string]interface{}, map[string]bool) {
+	var api dbapi.SdkApi
+	ids := make(map[string]bool)
+	sdks := []map[string]interface{}{}
+
+	// 鏌ヨ宸茬粡瀹夎鐨勭畻娉�
+	data := api.FindAll(sdkName)
+	for _, sdk := range data {
+		ids[sdk.Id] = true
+		sdks = append(sdks, map[string]interface{}{
+			"id":        sdk.Id,
+			"ipc_id":    sdk.IpcId,
+			"sdk_type":  sdk.SdkType,
+			"sdk_name":  sdk.SdkName,
+			"icon":      sdk.Icon,
+			"enable":    sdk.Enable,
+			"installed": true,
+		})
+	}
+
+	return sdks, ids
+}
+
+func GetLocalSdks() []map[string]interface{} {
+	var algos = []map[string]interface{}{}
+
+	AlgorithmFiles := "/opt/vasystem/bin/algorithm/*.json"
+
+	files, err := filepath.Glob(AlgorithmFiles)
+	if err != nil {
+		fmt.Println("Cannot access algorithm json files: No such file or directory")
+		return algos
+	}
+
+	for _, filename := range files {
+		algo := make(map[string]interface{})
+		f, err := ioutil.ReadFile(filename)
+		if err != nil {
+			return algos
+		}
+
+		if err := json.Unmarshal(f, &algo); err != nil {
+			return algos
+		}
+
+		algos = append(algos, map[string]interface{}{
+			"id":       algo["sdkId"],
+			"ipc_id":   algo["ipcId"],
+			"sdk_type": algo["sdkType"],
+			"sdk_name": algo["sdkName"],
+			"icon":     algo["icon"],
+			"args":     algo["sdkArgs"],
+		})
+	}
+
+	return algos
+}
+
+func FindLocalSdkSoById(id string) map[string]interface{} {
+	localAlgos := GetLocalSdks()
+	for _, sdk := range localAlgos {
+		if sdk["id"].(string) == id {
+			return sdk
+		}
+	}
+
+	return nil
+}

--
Gitblit v1.8.0