From 7277a23ca81123a95cbf60cbda65ca2fd6e27c17 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期五, 05 六月 2020 18:26:27 +0800
Subject: [PATCH] get sdks from remote shop

---
 service/SdkInstall.go |  209 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 154 insertions(+), 55 deletions(-)

diff --git a/service/SdkInstall.go b/service/SdkInstall.go
index b930aab..43f29a3 100644
--- a/service/SdkInstall.go
+++ b/service/SdkInstall.go
@@ -9,6 +9,8 @@
 	"os"
 	"path"
 	"path/filepath"
+	"strconv"
+	"strings"
 	"webserver/extend/config"
 	"webserver/extend/util"
 )
@@ -23,17 +25,21 @@
 	if config.Server.PatchPath != "" {
 		configPatchPath = config.Server.PatchPath
 	} else {
-		configPatchPath = "/opt/vasystem/patch"
+		configPatchPath = "../patch"
 	}
 
 	filenameWithSuffix := path.Base(filename)
 	ext := path.Ext(filenameWithSuffix)
 
 	zipFilePath := configPatchPath + "/"+identifier+ext
+	unPackTargetPath := configPatchPath+"/"+identifier+"_basic/"
 	if util.Exists(zipFilePath) {
 		strMd5, e := util.FileMd5(zipFilePath)
 		if e !=nil || strMd5 == "" {
 			go os.Remove(zipFilePath)
+			if util.DirExists(unPackTargetPath) {
+				go os.RemoveAll(unPackTargetPath)
+			}
 			return false,errors.New("鑾峰彇瀹夎鍖卪d5澶辫触")
 		}
 		if strMd5 == identifier {
@@ -44,6 +50,9 @@
 
 		} else {
 			go os.Remove(zipFilePath)
+			if util.DirExists(unPackTargetPath) {
+				go os.RemoveAll(unPackTargetPath)
+			}
 			logger.Debug("strMd5 is", strMd5,"identifier is",identifier,"not equal")
 			return false,errors.New("鏍¢獙瀹夎鏂囦欢澶辫触")
 		}
@@ -58,7 +67,7 @@
 	if config.Server.PatchPath != "" {
 		configPatchPath = config.Server.PatchPath
 	} else {
-		configPatchPath = "/opt/vasystem/patch"
+		configPatchPath = "../patch"
 	}
 	//1.瑙e帇缂╂洿鏂板寘
 	unPackTargetPath := configPatchPath+"/"+identifier+"_basic/"
@@ -122,44 +131,76 @@
 						//3.灏嗙畻娉晄o銆佷緷璧栨枃浠躲�亃conf銆�
 						soM := make(map[string]SdkDef)
 						var skDefArr []SdkDef
+						nInsM := make(map[string]string)
+						aInsM := make(map[string]string)
 						if err = json.Unmarshal(defB, &skDefArr);err == nil {
-							//娉ㄥ唽绠楁硶淇℃伅鍜岀畻娉曞弬鏁板埌dbserver
 							var sdkApi dbapi.SdkApi
 							for _,skd := range skDefArr {
+								//娉ㄥ唽绠楁硶淇℃伅鍜岀畻娉曞弬鏁板埌dbserver
+								//鍏堢湅姝ょ畻娉曟湁娌℃湁娉ㄥ唽锛屽鏋滃凡瀹夎锛屾瘮瀵圭増鏈彿
+								bf, curDbSdk := sdkApi.GetById(skd.Def.Id)
+								if bf {
+									bytes, _ := json.Marshal(curDbSdk)
+									var sm map[string]interface{}
+									unE :=  json.Unmarshal(bytes, &sm)
+									if unE != nil {
+										continue
+									}
+									sdkVersion, ok := sm["version"]
+									if ok { //鏈夌増鏈彿
+										vStr := sdkVersion.(string)
+										if shouldVersionBeUpgrade(vStr, skd.Def.Version) {
+											nInsM[skd.Def.SdkType] = skd.Def.SdkType
+											aInsM[skd.Def.Id] = skd.Def.Id
+										}
+									} else {
+										nInsM[skd.Def.SdkType] = skd.Def.SdkType
+										aInsM[skd.Def.Id] = skd.Def.Id
+									}
+								} else {
+									nInsM[skd.Def.SdkType] = skd.Def.SdkType
+									aInsM[skd.Def.Id] = skd.Def.Id
+								}
+
+
 								if _,ok := soM[skd.Def.SdkType];!ok {
 									soM[skd.Def.SdkType] = skd
 								}
-								srv := SdkRegisterVo{
-									Id: skd.Def.Id,
-									SdkType: skd.Def.SdkType,
-									SdkName: skd.Def.SdkName,
-									Icon: skd.Def.Icon,
-									Url: skd.Def.Url,
-								}
-								for _,ag := range skd.Args {
-									sra := SdkRegisterArgVo{
-										Scope: ag.Scope,
+								if _,ok := aInsM[skd.Def.Id]; ok {
+									srv := SdkRegisterVo{
+										Id: skd.Def.Id,
+										SdkType: skd.Def.SdkType,
+										SdkName: skd.Def.SdkName,
+										Icon: skd.Def.Icon,
+										Url: skd.Def.Url,
+										IconBlob: skd.Def.IconBlob,
+										Version: skd.Def.Version,
 									}
-									sra.SdkArg = SdkArg{
-										Alias: ag.Alias,
-										Name: ag.Name,
-										Type: ag.Type,
-										ArgType: ag.ArgType,
-										Must: ag.Must,
-										Unit: ag.Unit,
-										Range: ag.Range,
-										DefaultValue: ag.DefaultValue,
-										DefaultOperator: ag.DefaultOperator,
-										Sort: ag.Sort,
+									for _,ag := range skd.Args {
+										sra := SdkRegisterArgVo{
+											Scope: ag.Scope,
+										}
+										sra.SdkArg = SdkArg{
+											Alias: ag.Alias,
+											Name: ag.Name,
+											Type: ag.Type,
+											ArgType: ag.ArgType,
+											Must: ag.Must,
+											Unit: ag.Unit,
+											Range: ag.Range,
+											DefaultValue: ag.DefaultValue,
+											DefaultOperator: ag.DefaultOperator,
+											Sort: ag.Sort,
+										}
+										srv.Args = append(srv.Args,  sra)
 									}
-									srv.Args = append(srv.Args,  sra)
+									paramBody := util.Struct2Map(srv)
+									logger.Debug("sdkApi.Register paramBody:", paramBody)
+									sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
 								}
-								paramBody := util.Struct2Map(srv)
-								logger.Debug("sdkApi.Register paramBody:", paramBody)
-								sdkApi.Register(paramBody) //灏嗙畻娉曟敞鍐屽埌鏁版嵁搴撲腑
 							}
-							zconfPath := "/opt/vasystem/bin/zconf/"
-							libPath := "/opt/vasystem/libs/"
+							zconfPath := "./zconf/"
+							libPath := "../libs/"
 							if !util.DirExists(zconfPath) {
 								os.MkdirAll(zconfPath, 0777)
 							}
@@ -167,24 +208,28 @@
 								os.MkdirAll(libPath, 0777)
 							}
 							for sdkType,_ := range soM {
-								//鍏堣В鍘婼dkType.tar.gz鏂囦欢
-								if unTarGzE := util.UnTarGz(unPackPath+sdkType+".tar.gz", unPackPath);unTarGzE == nil {
-									//澶嶅埗json鍚姩鏂囦欢
-									if util.Exists(unPackPath+sdkType+"/zconf/"+sdkType+".json") {
-										util.CopyFile(unPackPath+sdkType+"/zconf/"+sdkType+".json", zconfPath+sdkType+".json")
-									}
-									if util.DirExists(unPackPath+sdkType) {
-										if _,cE := util.CopyDirByCmd(unPackPath+sdkType, libPath);cE != nil {
-											return false, cE
+								if _,iOk := nInsM[sdkType];iOk {
+									//鍏堣В鍘婼dkType.tar.gz鏂囦欢
+									if unTarGzE := util.UnTarGz(unPackPath+sdkType+".tar.gz", unPackPath);unTarGzE == nil {
+										//澶嶅埗json鍚姩鏂囦欢
+										if util.Exists(unPackPath+sdkType+"/zconf/"+sdkType+".json") {
+											util.CopyFile(unPackPath+sdkType+"/zconf/"+sdkType+".json", zconfPath+sdkType+".json")
 										}
-									}
-									if util.DirExists(unPackPath+sdkType+"/models") {
-										if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "/opt/vasystem/bin/zconf"); cE != nil {
-											return false, cE
+										if util.DirExists(unPackPath+sdkType) {
+											if _,cE := util.CopyDirByCmd(unPackPath+sdkType, libPath);cE != nil {
+												return false, cE
+											}
 										}
+										if util.DirExists(unPackPath+sdkType+"/models") {
+											if _,cE := util.CopyDirByCmd(unPackPath+sdkType+"/models", "./zconf"); cE != nil {
+												return false, cE
+											}
+										}
+									} else {
+										logger.Debug("unTarGzE sdkType.tar.gz err:", unTarGzE)
 									}
 								} else {
-									logger.Debug("unTarGzE sdkType.tar.gz err:", unTarGzE)
+									logger.Debug("upper version has been installed")
 								}
 							}
 
@@ -208,6 +253,49 @@
 	}
 
 	return true, nil
+}
+
+func shouldVersionBeUpgrade(curVersion,dstVersion string) bool {
+	if curVersion == "" {
+		return true
+	}
+	if dstVersion == "" {
+		return false
+	}
+	curIVArr := strings.Split(curVersion, ".")
+	dstIVArr := strings.Split(dstVersion, ".")
+	if len(curIVArr) !=3 || len(dstIVArr) != 3{
+		return false
+	}
+	cH,cEH := strconv.Atoi(curIVArr[0])
+	cM,cEM := strconv.Atoi(curIVArr[1])
+	cL,cEL := strconv.Atoi(curIVArr[2])
+
+	dH,dEH := strconv.Atoi(curIVArr[0])
+	dM,dEM := strconv.Atoi(curIVArr[1])
+	dL,dEL := strconv.Atoi(curIVArr[2])
+	if cEH !=nil || cEM != nil || cEL != nil || dEH != nil ||dEM !=nil || dEL !=nil {
+		return false
+	}
+	if cH > dH {
+		return false
+	} else if cH == dH {
+		if cM > dM {
+			return false
+		} else if cM == dM {
+			if cL > dL {
+				return false
+			} else if cL == dL {
+				return false
+			} else {
+				return true
+			}
+		} else {
+			return true
+		}
+	} else {
+		return true
+	}
 }
 
 
@@ -243,6 +331,15 @@
 	Enable 			bool 		`gorm:"column:enable;default:1" json:"enable"`
 	DelFlag 		int 		`gorm:"column:del_flag;default:0" json:"del_flag"`
 	Env 			string 		`gorm:"column:env" json:"env"` //杩愯鐜鍙婄粨鏋滆鏄庯紝json鏍煎紡锛屽寘鍚玸o_file_path,runtime,param,depends(cuda鐗堟湰锛宱pencv鐗堟湰锛宼ensorflow鐗堟湰绛�)
+	IconBlob 		string 		`gorm:"column:iconBlob;type:text;" json:"iconBlob"` //鍥剧墖
+	Version 		string 		`gorm:"column:version;type:varchar(50);default:'1.0';" json:"version"` //鐗堟湰鍙�
+}
+
+type SdkInsOrUpgrade struct {
+	Sdk
+	RemoteVersion 	string 		 `json:"remoteVersion"` //鍟嗗煄浠撳簱鐗堟湰鍙�
+	Installed 		bool 		 `json:"installed"` //鏄惁宸插畨瑁�
+	IsUpgrade 		bool 		 `json:"isUpgrade"` //鏄惁闇�瑕佸崌绾�
 }
 
 type SdkArgEntity struct {
@@ -267,23 +364,25 @@
 
 
 type SdkRegisterVo struct {
-	Id 		string `json:"id"`
-	SdkType string `json:"sdk_type"`//浜鸿劯妫�娴嬶細FaceDetect,浜鸿劯鎻愬彇锛欶aceExtract,浜鸿劯姣斿锛欶aceCompare,琛屼负锛歒olo
-	SdkName string `json:"sdk_name"`    //绠楁硶鍚嶇О
-	Args    []SdkRegisterArgVo `json:"args"` //绠楁硶鍙傛暟
-	Icon    string `json:"icon"`       //绠楁硶鍥炬爣
-	Url     string `json:"url"`                       //绠楁硶涓嬭浇鍦板潃
+	Id 				string 				`json:"id"`
+	SdkType 		string 				`json:"sdk_type"`//浜鸿劯妫�娴嬶細FaceDetect,浜鸿劯鎻愬彇锛欶aceExtract,浜鸿劯姣斿锛欶aceCompare,琛屼负锛歒olo
+	SdkName 		string 				`json:"sdk_name"`    //绠楁硶鍚嶇О
+	Args    		[]SdkRegisterArgVo 	`json:"args"` //绠楁硶鍙傛暟
+	Icon    		string 				`json:"icon"`       //绠楁硶鍥炬爣
+	Url     		string 				`json:"url"`        //绠楁硶涓嬭浇鍦板潃
+	IconBlob 		string 				`json:"iconBlob"` //鍥剧墖
+	Version 		string 				`json:"version"` //鐗堟湰鍙�
 }
 
 type SdkRegisterArgVo struct {
-	Scope string `json:"scope"`
+	Scope 			string 			`json:"scope"`
 	SdkArg
 
-	Dics []SdkArgDic `json:"dics"` //濡傛灉姝ょ畻娉曞弬鏁版槸琚�夐」锛岄渶瑕佸皢鍙傛暟鏋氫妇鍊煎啓鍏ュ埌瀛楀吀琛ㄤ腑
+	Dics 			[]SdkArgDic 	`json:"dics"` //濡傛灉姝ょ畻娉曞弬鏁版槸琚�夐」锛岄渶瑕佸皢鍙傛暟鏋氫妇鍊煎啓鍏ュ埌瀛楀吀琛ㄤ腑
 }
 
 type SdkArgDic struct {
-	Value string `json:"value"`
-	Name string `json:"name"`
-	Sort int `json:"sort"`
+	Value 			string 			`json:"value"`
+	Name 			string 			`json:"name"`
+	Sort 			int 			`json:"sort"`
 }
\ No newline at end of file

--
Gitblit v1.8.0