Change function Download module and format type print log

This commit is contained in:
2020-04-13 10:45:29 +07:00
parent 11a715ed34
commit 56c9eaec33
6 changed files with 116 additions and 326 deletions

View File

@@ -1,7 +1,7 @@
package box
import (
"beetai/file"
"Beetai_Pro/file"
"bufio"
"bytes"
"encoding/json"
@@ -20,6 +20,8 @@ import (
//"path/filepath"
"strconv" // convert string to int
"strings"
"github.com/dustin/go-humanize"
)
// go.lintOnSave": "off"
@@ -155,7 +157,67 @@ type Engine_pub struct {
Engine_id int `json:"engine_id"`
}
// Check size of file download
type WriteCounter struct {
Total uint64
}
//==================== FUNCTION ==============================
// Down load file Url
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
func (wc WriteCounter) PrintProgress() {
// Clear the line by using a character return to go back to the start and remove
// the remaining characters by filling it with spaces
fmt.Printf("\r%s", strings.Repeat(" ", 35))
// Return again and print current status of download
// We use the humanize package to print the bytes in a meaningful way (e.g. 10 MB)
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
}
func DownloadFile(filepath string, url string) error {
// Create the file, but give it a tmp file extension, this means we won't overwrite a
// file until it's downloaded, but we'll remove the tmp extension once downloaded.
out, err := os.Create(filepath + ".tmp")
if err != nil {
return err
}
// Get the data
resp, err := http.Get(url)
if err != nil {
out.Close()
return err
}
defer resp.Body.Close()
// Create our progress reporter and pass it to be used alongside our writer
counter := &WriteCounter{}
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
out.Close()
return err
}
// The progress use the same line so print a new line once it's finished downloading
fmt.Print("\n")
// Close the file without defer so it can happen before Rename()
out.Close()
if err = os.Rename(filepath+".tmp", filepath); err != nil {
return err
}
return nil
}
//---------------------------------------------------------------------------------
func GetDir() {
if file.ReadFile("/admin/ver_sys.txt") == "1" {
path_base = "/home/aibox/Documents/monitor"
@@ -911,18 +973,49 @@ func Res_Server(Url string, id_box int, id_engine int) {
} else {
defer resp.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
file.Write_log("ioutil.ReadAll(resp.Body) Faild of Res_Server ()", Path_log_luncher)
file.Println("ioutil.ReadAll(resp.Body) Faild of Res_Server ()")
}
respon_msg := string(body)
if respon_msg == "{'status': 10000}" {
file.Write_log("Response to server --> update modules Done", Path_log_luncher)
} else {
file.Write_log("Response to server --> update modules Error", Path_log_luncher)
}
// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// file.Write_log("ioutil.ReadAll(resp.Body) Faild of Res_Server ()", Path_log_luncher)
// file.Println("ioutil.ReadAll(resp.Body) Faild of Res_Server ()")
// }
// respon_msg := string(body)
// if respon_msg == "{'status': 10000}" {
// file.Write_log("Response to server --> update modules Done", Path_log_luncher)
// } else {
// file.Write_log("Response to server --> update modules Error", Path_log_luncher)
// }
}
func RequestUpdate_Module(url string, id_engine int) {
//http://api1.dev_dc.beetai.com/api/model/get_model/257
// url = /api/model/get_mode/
fmt.Println("RequestUpdate_Module()")
for i := 0; i < len(message.Data.Engines); i++ {
if message.Data.Engines[i].Id == id_engine {
var id_val = strconv.Itoa(message.Data.Engines[i].Id)
fmt.Println(message)
url = message.Data.Engines[i].Url + url + id_val
path := path_base + "/engine/" + message.Data.Engines[i].Name + "/FaceRecognition/build/features.json"
fmt.Println("API get module :" + url)
file.Write_log("API get module :"+url, Path_log_luncher)
file.Write_log("Path:"+path, Path_log_luncher)
file.Write_log("Download Started", Path_log_luncher)
err := DownloadFile(path, url)
if err != nil {
// panic(err)
file.Write_log("Download Error", Path_log_luncher)
} else {
file.Write_log("Download Finished", Path_log_luncher)
file.Write_log("Save thanh cong modules", Path_log_luncher)
Res_Server(message.Data.Engines[i].Url, message.Data.Box_id, message.Data.Engines[i].Id)
}
}
}
}
/*
func RequestUpdate_Module(url string, id_engine int) {
//http://api1.dev_dc.beetai.com/api/model/get_model/257
// url = /api/model/get_mode/
@@ -964,7 +1057,7 @@ func RequestUpdate_Module(url string, id_engine int) {
}
}
*/
// List function
func GetIDEngine(path string) string {
var id string = ""
@@ -1024,8 +1117,8 @@ func Ssh_close() {
}
func Stop_check_engine() {
file.Println("Stop Check_engine")
file.Write_log("Stop Check_engine \n", Path_log_luncher)
file.Println("Stop_check_engine()\n")
file.Write_log("Stop Check_engine", Path_log_luncher)
file.Println("Stop_check_engine()")
_, err := exec.Command("sh", "-c", "kill $(pgrep Check_engine)").Output()
if err != nil {
//log.Fatal(err)