Build launcher cho PC theo user PC
This commit is contained in:
parent
8b370c5c8a
commit
40b5fab0f9
BIN
Check_engine/Check_engine
Normal file
BIN
Check_engine/Check_engine
Normal file
Binary file not shown.
661
Check_engine/Check_engine.go
Normal file
661
Check_engine/Check_engine.go
Normal file
|
@ -0,0 +1,661 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"beetai_lattepanda/file"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
//"log"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
//"path/filepath"
|
||||
"encoding/json"
|
||||
"strconv" // convert string to int
|
||||
)
|
||||
|
||||
// scp -P 2224 -r -o 'ProxyJump dh.duyhai@ssh_tunnel.beetai.com' Check_engine admin@localhost:/admin/monitor/check/
|
||||
var name_dir_engine string = ""
|
||||
var state_read_ecf bool = false
|
||||
|
||||
// var path_base string = "/home/biface/monitor"
|
||||
// var path_base = "/home/aibox/monitor"
|
||||
var path_base = "/home/svr-bi-security/monitor"
|
||||
|
||||
// var path_base = "/home/ubuntu/monitor"
|
||||
|
||||
// var path_base = "/home/administrator/monitor"
|
||||
// var path_base = "/home/adminars/monitor"
|
||||
|
||||
// var path_base string = "/home/admin/monitor"
|
||||
var Path_base_engine string = path_base + "/engine/" + name_dir_engine //"count_people_face" // Name dir engine
|
||||
var Path_log_engine string = path_base + "/log/log_engine"
|
||||
var Path_log_luncher string = path_base + "/log/log_launcher"
|
||||
var Path_engine_config string = path_base + "/setup/engine.json" // dia chi duoi file Config.txt
|
||||
// var Path_config string = Path_base_engine + "/data/Config.txt"
|
||||
var Path_config string = Path_base_engine + "/data/config.json"
|
||||
|
||||
// var path = "/admin/monitor/log/log_engine.txt"
|
||||
|
||||
//var time_sts int = 0
|
||||
//var cam_sts int = 0
|
||||
|
||||
//var Path_config string = "/admin/monitor/face_recognition/data/Config.txt"
|
||||
|
||||
//engine
|
||||
var engine_status int = 0
|
||||
|
||||
var Path_time string = path_base + "/setup/time_run.txt"
|
||||
var time_start int = 0
|
||||
var time_end int = 0
|
||||
var time_start_defaut int = 5
|
||||
var time_end_defaut int = 23
|
||||
var Debug int = 1
|
||||
|
||||
var number_run int = 0 // Check so lan run lai engine khi engine loi
|
||||
|
||||
var message Message
|
||||
|
||||
type Message struct {
|
||||
Error_code int `json:"error_code"`
|
||||
Data Datas `json:"data"`
|
||||
SD_CARD int `json:"sd_card"`
|
||||
}
|
||||
|
||||
// User struct which contains a name
|
||||
// a type and a list of social links
|
||||
type Datas struct {
|
||||
Box_id int `json:"box_id"`
|
||||
//Box_name string `json:"box_name"`
|
||||
Engines []Engine `json:"engines"`
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
IsConfig string `json:"isConfig,omitempty"`
|
||||
Active int `json:"active"`
|
||||
Engine_id int `json:"engine_id"`
|
||||
Id int `json:"id"`
|
||||
Link string `json:"link"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Version string `json:"version"`
|
||||
Time_run string `json:"time_run,omitempty"`
|
||||
}
|
||||
|
||||
//============================> Main <==========================
|
||||
|
||||
func main() {
|
||||
fmt.Println("------------------ Start --------------------")
|
||||
//GetDir() // Check thu muc
|
||||
fmt.Println("--------------------------")
|
||||
file.Check_path(Path_log_engine)
|
||||
file.Write_log("Begin Check, Restart program\n", Path_log_engine)
|
||||
fmt.Printf("Begin Check, Restart program\n")
|
||||
ReadEngineConfig(Path_engine_config) // Pharse engine.json to struct Message
|
||||
// Loop:
|
||||
for {
|
||||
fmt.Println("Path base:" + path_base)
|
||||
fmt.Println("-------------------- 0 ---------------------")
|
||||
//Check_dir_engine() // Check directory engine , Create Path_base engine
|
||||
// ReadTimeRun()
|
||||
//CheckCam()
|
||||
//CheckTime()
|
||||
ActionEngine()
|
||||
fmt.Println("-------------------- 1 ---------------------")
|
||||
Sleep_ms(5000)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//========================> END MAIN <====================
|
||||
//------------------ Function ----------------------------
|
||||
// func Check_dir_engine() {
|
||||
// if Debug == 1 {
|
||||
// fmt.Println("Function Check_dir_engine ()")
|
||||
// }
|
||||
|
||||
// name_dir_engine = strings.Replace(message.Data.Engines[0].Path, ".zip", "", -1)
|
||||
|
||||
// Path_base_engine = path_base + "/engine/" + name_dir_engine
|
||||
// Path_config = Path_base_engine + "/data/Config.txt"
|
||||
// Path_log_engine = path_base + "/log/log_engine"
|
||||
// Path_log_luncher = path_base + "/log/log_launcher"
|
||||
// Path_engine_config = path_base + "/setup/engine.json" // dia chi duoi file Config.txt
|
||||
// Path_config = Path_base_engine + "/data/Config.txt"
|
||||
// if Debug == 1 {
|
||||
// fmt.Printf("Name_dir_engine :" + name_dir_engine + "\t")
|
||||
// fmt.Println("Path_base_engine :" + Path_base_engine)
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
func GetDir() {
|
||||
if file.ReadFile("/admin/ver_sys.txt") == "1" {
|
||||
path_base = "/home/aibox/Documents/monitor"
|
||||
}
|
||||
}
|
||||
func ReadEngineConfig(path string) {
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function ReadEngineConfig ()")
|
||||
}
|
||||
|
||||
// Open our jsonFile
|
||||
jsonFile, err := os.Open(path)
|
||||
// if we os.Open returns an error then handle it
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("ReadEngineConfig 1 () "+err.Error(), Path_log_engine)
|
||||
}
|
||||
if Debug == 1 {
|
||||
fmt.Println("Successfully Opened engine.json")
|
||||
}
|
||||
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, err := ioutil.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("ReadEngineConfig 2 () "+err.Error(), Path_log_engine)
|
||||
}
|
||||
json.Unmarshal(byteValue, &message)
|
||||
state_read_ecf = true
|
||||
|
||||
file.Println("\nError_code: " + strconv.Itoa(message.Error_code))
|
||||
file.Println("Box_id: " + strconv.Itoa(message.Data.Box_id))
|
||||
// file.Println("---------------------------------------------")
|
||||
// List engine
|
||||
for i := 0; i < len(message.Data.Engines); i++ {
|
||||
file.Println("Isconfig: " + message.Data.Engines[i].IsConfig)
|
||||
file.Println("Active: " + strconv.Itoa(message.Data.Engines[i].Active))
|
||||
file.Println("Engine_id: " + strconv.Itoa(message.Data.Engines[i].Engine_id))
|
||||
file.Println("Id: " + strconv.Itoa(message.Data.Engines[i].Id))
|
||||
file.Println("Link: " + message.Data.Engines[i].Link)
|
||||
file.Println("Name: " + message.Data.Engines[i].Name)
|
||||
file.Println("Path: " + message.Data.Engines[i].Path)
|
||||
file.Println("Version: " + message.Data.Engines[i].Version)
|
||||
file.Println("Time_run: " + message.Data.Engines[i].Time_run)
|
||||
file.Println("---------------------------------------------")
|
||||
}
|
||||
}
|
||||
|
||||
func ReadTimeRun(time string) (int, int) {
|
||||
var t_start int
|
||||
var t_end int
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function ReadTimeRun ()")
|
||||
}
|
||||
// var value = message.Data.Engines[i].Time_run
|
||||
fmt.Println("\nTime_run:" + time)
|
||||
var list = strings.Split(time, ":")
|
||||
if Debug == 1 {
|
||||
fmt.Println(list)
|
||||
for i := 0; i < 2; i++ {
|
||||
fmt.Print(i)
|
||||
fmt.Print("_")
|
||||
fmt.Printf("%T", list[i])
|
||||
fmt.Print("_")
|
||||
fmt.Printf("%v\n", list[i])
|
||||
}
|
||||
}
|
||||
|
||||
var x, _ = strconv.Atoi(list[0])
|
||||
t_start = x
|
||||
if t_start < 0 {
|
||||
t_start = 0
|
||||
} else if t_start > 23 {
|
||||
t_start = 23
|
||||
}
|
||||
var z, _ = strconv.Atoi(list[1])
|
||||
t_end = z
|
||||
if t_end < 0 {
|
||||
t_end = 0
|
||||
} else if t_end > 23 {
|
||||
t_end = 23
|
||||
}
|
||||
return t_start, t_end
|
||||
}
|
||||
func ActionEngine() {
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function ActionEngine ()")
|
||||
}
|
||||
for i := 0; i < len(message.Data.Engines); i++ {
|
||||
var t_stt = CheckTime(message.Data.Engines[i].Time_run)
|
||||
//var cam_sts = CheckCam(message.Data.Engines[i].Path + "/data/Config.txt")
|
||||
var cam_sts = 1
|
||||
var enable, _ = strconv.Atoi(message.Data.Engines[i].IsConfig)
|
||||
if cam_sts*t_stt*enable == 1 {
|
||||
Status_engine()
|
||||
fmt.Println(" Setup Run Engine")
|
||||
} else {
|
||||
fmt.Println("Setup off Engine")
|
||||
|
||||
if message.Data.Engines[i].Active == 1 {
|
||||
//var cmd string = "ps -A | grep " + message.Data.Engines[i].Name
|
||||
|
||||
var cmd string = "pidof " + message.Data.Engines[i].Name
|
||||
out2, err := exec.Command("sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
//file.Write_log("ActionEngine () "+err.Error(), Path_log_engine)
|
||||
}
|
||||
fmt.Println("Lengh :"+cmd+"%v", len(out2))
|
||||
if len(out2) != 0 {
|
||||
// sudo kill $(pgrep FaceRecognition)
|
||||
|
||||
//var cmd string = "sudo kill $(pgrep " + message.Data.Engines[i].Name + ")"
|
||||
//var cmd string = "pidof " + message.Data.Engines[i].Name
|
||||
var cmd string = "sudo kill -9 $(pidof " + message.Data.Engines[i].Name + ")"
|
||||
fmt.Println("CMD Kill_engine:" + cmd)
|
||||
_, err := exec.Command("sh", "-c", cmd).Output() // kill $(pgrep FaceRecognition)
|
||||
//log.Fatal(err)
|
||||
if err != nil {
|
||||
file.Write_log("sudo kill engine of ActionEngine() Error \n", Path_log_engine)
|
||||
fmt.Println("sudo kill engine of ActionEngine() Error")
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Println("Engine kill")
|
||||
}
|
||||
out1, _ := exec.Command("sh", "-c", "ps -A | grep loop_run").Output()
|
||||
if len(out1) != 0 {
|
||||
_, err := exec.Command("sh", "-c", " sudo kill $(pgrep loop_run)").Output()
|
||||
if err != nil {
|
||||
file.Write_log("sudo kill $(pgrep loop_run) of CheckActionEngine() \n", Path_log_engine)
|
||||
}
|
||||
//log.Fatal(err) sudo systemctl restart S905X_BI rm /admin/monitor/launcher/S905X_BI
|
||||
} else {
|
||||
fmt.Println("loop_run kill")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Engine setting off on engine.json")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
func CheckTime(time_run string) int {
|
||||
var time_sts = 0
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function CheckTime ()")
|
||||
}
|
||||
//ReadTimeRun(time string) (int, int)
|
||||
|
||||
t_start, t_end := ReadTimeRun(time_run)
|
||||
var time_now = time.Now().Hour()
|
||||
if Debug == 1 {
|
||||
//fmt.Printf("Kieu time : %T\n", time)
|
||||
fmt.Printf("time = %v\n", time_now)
|
||||
}
|
||||
if time_now >= t_start && time_now <= t_end {
|
||||
time_sts = 1
|
||||
//Status_engine()
|
||||
fmt.Println(" Time Engine's running")
|
||||
} else {
|
||||
time_sts = 0
|
||||
}
|
||||
return time_sts
|
||||
}
|
||||
func FloatToString(input_num float64) string {
|
||||
fmt.Println("Function FloatToString ()")
|
||||
// to convert a float number to a string
|
||||
return strconv.FormatFloat(input_num, 'f', 3, 64)
|
||||
}
|
||||
func KillLoopRun() {
|
||||
fmt.Println("Function KillLoopRun()")
|
||||
out1, err := exec.Command("sh", "-c", "ps -A | grep loop_run").Output()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("KillLoopRun() "+err.Error(), Path_log_engine)
|
||||
}
|
||||
if len(out1) != 0 {
|
||||
_, err := exec.Command("sh", "-c", "sudo kill $(pgrep loop_run)").Output() // kill $(pgrep FaceRecognition)
|
||||
//log.Fatal(err)
|
||||
if err != nil {
|
||||
file.Write_log("sudo kill $(pgrep loop_run) of KillLoopRun() \n", Path_log_engine)
|
||||
}
|
||||
file.Write_log("KillLoopRun()\n", Path_log_engine)
|
||||
Sleep_ms(500)
|
||||
}
|
||||
fmt.Printf("Kill loop run and Run engine\n")
|
||||
|
||||
}
|
||||
func Status_engine() {
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function Status_engine ()")
|
||||
}
|
||||
for i := 0; i < len(message.Data.Engines); i++ {
|
||||
if message.Data.Engines[i].Active == 1 {
|
||||
//var cmd string = "ps -A | grep " + message.Data.Engines[i].Name
|
||||
var cmd string = "pidof " + message.Data.Engines[i].Name
|
||||
fmt.Println(cmd)
|
||||
//file.Write_log(cmd, Path_log_engine)
|
||||
out, err := exec.Command("sh", "-c", cmd).Output()
|
||||
fmt.Println(out)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("Status_engine () "+err.Error(), Path_log_engine)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
fmt.Printf(message.Data.Engines[i].Name + "isn't running\n")
|
||||
file.Write_log(message.Data.Engines[i].Name+"isn't running\n", Path_log_engine)
|
||||
//KillLoopRun()
|
||||
RunEngine(message.Data.Engines[i].Name)
|
||||
} else {
|
||||
number_run = 0
|
||||
fmt.Printf(message.Data.Engines[i].Name + " is running\n")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Engine setting off on engine.json")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func CheckCam(path string) int {
|
||||
var cam_sts int
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function CheckCam ()")
|
||||
}
|
||||
for i := 0; i < len(message.Data.Engines); i++ {
|
||||
var ip string = ""
|
||||
ip = GetIPCam(path)
|
||||
fmt.Println("Ip camera: " + ip)
|
||||
out, err := exec.Command("ping", ip, "-c 5", "-i 0.2", "-w 5").Output()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("CheckCam () "+err.Error(), Path_log_engine)
|
||||
cam_sts = 0
|
||||
} else {
|
||||
|
||||
}
|
||||
if strings.Contains(string(out), "Destination Host Unreachable") {
|
||||
fmt.Println("Disconnect camera...")
|
||||
file.Write_log("Disconnect camera", Path_log_engine)
|
||||
cam_sts = 0
|
||||
} else {
|
||||
fmt.Println("Connected camera...")
|
||||
cam_sts = 1
|
||||
}
|
||||
|
||||
}
|
||||
return cam_sts
|
||||
//fmt.Println("End Function CheckCam")
|
||||
}
|
||||
|
||||
func GetIPCam(path string) string {
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function GetIPCam ()")
|
||||
}
|
||||
|
||||
var ip string = ""
|
||||
str := ReadFile(path)
|
||||
j := strings.Index(str, "@")
|
||||
if j > -1 {
|
||||
fmt.Println(j)
|
||||
index := str[j+1 : j+20]
|
||||
t := strings.Index(index, "@")
|
||||
if t > -1 {
|
||||
index := str[t+1 : t+15]
|
||||
fmt.Println(index)
|
||||
list_0 := strings.Split(index, ":")
|
||||
ip = list_0[0]
|
||||
} else {
|
||||
fmt.Println(index)
|
||||
list_0 := strings.Split(index, ":")
|
||||
ip = list_0[0]
|
||||
}
|
||||
|
||||
} else {
|
||||
i := strings.Index(str, "192")
|
||||
|
||||
//fmt.Println("Index: ", i)
|
||||
if i > -1 {
|
||||
if Debug == 1 {
|
||||
fmt.Println(str[i : i+15])
|
||||
}
|
||||
|
||||
index := str[i : i+15]
|
||||
list := strings.Split(index, ".")
|
||||
//fmt.Println(list)
|
||||
k := strings.Index(list[3], "\n")
|
||||
if k > -1 {
|
||||
x := strings.Split(list[3], "\n")
|
||||
list[3] = x[0]
|
||||
} else {
|
||||
j := strings.Index(list[3], "/")
|
||||
if j > -1 {
|
||||
x := strings.Split(list[3], "/")
|
||||
list[3] = x[0]
|
||||
} else {
|
||||
t := strings.Index(list[3], ":")
|
||||
if t > -1 {
|
||||
x := strings.Split(list[3], ":")
|
||||
list[3] = x[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ip = list[0]
|
||||
for i := 1; i < 4; i++ {
|
||||
if Debug == 1 {
|
||||
fmt.Printf("%v_", list[i])
|
||||
}
|
||||
|
||||
ip = ip + "." + list[i]
|
||||
}
|
||||
|
||||
} else {
|
||||
file.Write_log("cam_id not found", Path_log_engine)
|
||||
fmt.Println("cam_id not found")
|
||||
}
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
func RunEngine(name string) {
|
||||
fmt.Println("Function RunEngine ()")
|
||||
number_run = number_run + 1
|
||||
if number_run > 25 {
|
||||
time.Sleep(10000)
|
||||
// RebootBox()
|
||||
number_run = 0
|
||||
file.Write_log(" Number run engine > 25 ----> reset count_number=0 && not reset ", Path_log_engine)
|
||||
} else {
|
||||
//while true; do /home/beetsoft/Desktop/face_recognition_20190308/build/FaceRecognition/FaceRecognition ;done
|
||||
// FaceRecognition/FaceRecognition ../data/Config.txt > /dev/null 2>&1 &
|
||||
//var cmd = "cd " + path_base + "/engine/" + name + "/build/ && " + "screen -dm -S Engine " + name + "/" + name + " ../data/Config.txt > /dev/null 2>&1 &"
|
||||
//var cmd = "export DISPLAY=:0&&cd " + path_base + "/engine/" + name + "/build/ " + "&& " + name + "/" + name + " ../data/config.json > /dev/null 2>&1 &"
|
||||
var cmd = "export DISPLAY=:0&&cd " + path_base + "/engine/" + name + "/build/ " + "&& " + name + "/" + name + " ../data/Config.txt > /dev/null 2>&1 &"
|
||||
|
||||
// var cmd = "cd " + path_base + "/engine/" + name + "/build/ " + "&& " + name + "/" + name + " ../data/config.json > /dev/null 2>&1 &"
|
||||
if Debug == 1 {
|
||||
fmt.Println("cmd run engine", cmd)
|
||||
}
|
||||
out, err := exec.Command("sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
//log.Fatal(err)
|
||||
engine_status = 1
|
||||
file.Write_log("RunEngine () "+err.Error(), Path_log_engine)
|
||||
fmt.Printf("RunEngine() not run \n")
|
||||
} else {
|
||||
fmt.Printf("RunEngine() ok \n")
|
||||
file.Write_log("RunEngine() ok \n", Path_log_engine)
|
||||
engine_status = 0
|
||||
var out1 = string(out)
|
||||
fmt.Printf(out1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
func RebootBox() {
|
||||
fmt.Println("Function RebootBox()")
|
||||
_, err := exec.Command("sh", "-c", "reboot").Output()
|
||||
if err == nil {
|
||||
file.Write_log("RebootBox()", Path_log_luncher)
|
||||
}
|
||||
fmt.Println("End Function RebootBox()")
|
||||
}
|
||||
|
||||
func Sleep_ms(ms time.Duration) {
|
||||
time.Sleep(time.Millisecond * ms) // sleep(nano second)
|
||||
}
|
||||
|
||||
// Thao tac file
|
||||
func CreateLogfile(path string) {
|
||||
fmt.Println("Function CreateLogfile ()")
|
||||
// detect if file exists
|
||||
var _, err = os.Stat(path)
|
||||
|
||||
// create file if not exists
|
||||
if os.IsNotExist(err) {
|
||||
var file, err = os.Create(path)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
//file.Write_log("CreateLogfile() "+err.Error(), Path_log_engine)
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
fmt.Println("==> done creating file", path)
|
||||
}
|
||||
|
||||
// func file.file.Write_log(msg string, path string) {
|
||||
// fmt.Println("Function file.Write_log ()")
|
||||
// // open file using READ & WRITE permission
|
||||
// // fmt.Println("==>begin writting ")
|
||||
// var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// file.Write_log("file.Write_log 1 () "+err.Error(), Path_log_engine)
|
||||
// }
|
||||
// defer file.Close()
|
||||
|
||||
// _, err = file.WriteString(time.Now().String())
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// file.Write_log("file.Write_log 2 () "+err.Error(), Path_log_engine)
|
||||
// }
|
||||
// // write some text line-by-line to file
|
||||
// _, err = file.WriteString("\n")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// file.Write_log("file.Write_log 3 () "+err.Error(), Path_log_engine)
|
||||
// }
|
||||
// _, err = file.WriteString("Service Check_engine : " + msg)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// file.Write_log("file.Write_log 4 () "+err.Error(), Path_log_engine)
|
||||
// }
|
||||
// _, err = file.WriteString("\n")
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// file.Write_log("file.Write_log 5 () "+err.Error(), Path_log_engine)
|
||||
// }
|
||||
|
||||
// // save changes
|
||||
// err = file.Sync()
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// file.Write_log("file.Write_log 6 () "+err.Error(), Path_log_engine)
|
||||
// }
|
||||
|
||||
// //fmt.Println("==> done writing to file")
|
||||
// }
|
||||
|
||||
// func readFile(path string) string {
|
||||
// if Debug == 1 {
|
||||
// fmt.Println("Function readFile ()")
|
||||
// }
|
||||
|
||||
// // re-open file
|
||||
// var msg string = ""
|
||||
// var file, err = os.OpenFile(path, os.O_RDWR, 0644)
|
||||
// if err != nil {
|
||||
// //file.Write_log("readFile 1 () "+err.Error(), Path_log_engine)
|
||||
// time_start = time_start_defaut
|
||||
// time_end = time_end_defaut
|
||||
// } else {
|
||||
// defer file.Close()
|
||||
// // read file, line by line
|
||||
// var text = make([]byte, 1024)
|
||||
// for {
|
||||
// _, err = file.Read(text)
|
||||
// // break if finally arrived at end of file
|
||||
// if err == io.EOF {
|
||||
// //file.Write_log("readFile 2() "+err.Error(), Path_log_engine)
|
||||
// //break
|
||||
|
||||
// }
|
||||
|
||||
// // break if error occured
|
||||
// if err != nil && err != io.EOF {
|
||||
// //file.Write_log("readFile 3() "+err.Error(), Path_log_engine)
|
||||
// //break
|
||||
// }
|
||||
// }
|
||||
// //fmt.Println(string(text))
|
||||
// msg = string(text)
|
||||
// }
|
||||
// fmt.Println(time_start)
|
||||
// fmt.Println(time_end)
|
||||
// fmt.Println("read file done")
|
||||
// return msg
|
||||
// }
|
||||
|
||||
func DeleteFile(path string) {
|
||||
fmt.Println("Function DeleteFile ()")
|
||||
// delete file
|
||||
var err = os.Remove(path)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("DeleteFile () "+err.Error(), Path_log_engine)
|
||||
}
|
||||
|
||||
fmt.Println("==> done deleting file")
|
||||
}
|
||||
|
||||
func IsError(err error) bool {
|
||||
//fmt.Println("Function IsError ()")
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
return (err != nil)
|
||||
}
|
||||
|
||||
func ReadFile(path string) string {
|
||||
if Debug == 1 {
|
||||
fmt.Println("Function ReadFile ()")
|
||||
}
|
||||
|
||||
// re-open file
|
||||
var msg string = ""
|
||||
var file, err = os.OpenFile(path, os.O_RDWR, 0644)
|
||||
if err != nil {
|
||||
fmt.Println(" Open file error")
|
||||
//file.Write_log("ReadFile 1 () "+err.Error(), Path_log_engine)
|
||||
|
||||
}
|
||||
defer file.Close()
|
||||
// read file, line by line
|
||||
var text = make([]byte, 2048)
|
||||
for {
|
||||
_, err = file.Read(text)
|
||||
// break if finally arrived at end of file
|
||||
if err == io.EOF {
|
||||
//file.Write_log("ReadFile 2 () "+err.Error(), Path_log_engine)
|
||||
break
|
||||
}
|
||||
|
||||
// break if error occured
|
||||
if err != nil && err != io.EOF {
|
||||
//file.Write_log("ReadFile 3 () "+err.Error(), Path_log_engine)
|
||||
break
|
||||
}
|
||||
}
|
||||
msg = string(text)
|
||||
//fmt.Println(msg)
|
||||
return msg
|
||||
}
|
4
Check_engine/build_pc.bat
Normal file
4
Check_engine/build_pc.bat
Normal file
|
@ -0,0 +1,4 @@
|
|||
set GOOS=linux
|
||||
set GOARCH=amd64
|
||||
go build
|
||||
pause
|
20
Check_engine/engine.json
Normal file
20
Check_engine/engine.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"data": {
|
||||
"engines": [
|
||||
{
|
||||
"time_run": "5:23:",
|
||||
"path": "/root/monitor/engine/FaceRecog_5",
|
||||
"isConfig": "1",
|
||||
"engine_id": 7,
|
||||
"name": "FaceRecog_5",
|
||||
"id": 5,
|
||||
"active": 1,
|
||||
"version": "1.0",
|
||||
"link": ""
|
||||
}
|
||||
],
|
||||
"box_id": 5
|
||||
},
|
||||
"error_code": 10000,
|
||||
"sd_card": 0
|
||||
}
|
1
Check_engine/scp.txt
Normal file
1
Check_engine/scp.txt
Normal file
|
@ -0,0 +1 @@
|
|||
scp -P 2222 -r -o 'ProxyJump dh.duyhai@ssh_tunnel.beetai.com' Check_engine root@localhost:/root/monitor/check/
|
BIN
S905X_BI/S905X_BI
Normal file
BIN
S905X_BI/S905X_BI
Normal file
Binary file not shown.
426
S905X_BI/S905X_BI.go
Normal file
426
S905X_BI/S905X_BI.go
Normal file
|
@ -0,0 +1,426 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
_ "beetai_lattepanda/action_engine"
|
||||
"beetai_lattepanda/box"
|
||||
"beetai_lattepanda/file"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
// AutoServer : Bat che do update engine tu dong
|
||||
var AutoServer = 1
|
||||
|
||||
// MqttCmsBi : Khoi tao doi tuong mqtt
|
||||
var MqttCmsBi mqtt.Client
|
||||
|
||||
// CmsHostBi : host MQTT
|
||||
const CmsHostBi string = "tcp://broker.beetai.com:21883"
|
||||
|
||||
// CmsAccessTokenBi : User
|
||||
const CmsAccessTokenBi = "beeetai"
|
||||
|
||||
// CmsPassBi : Password
|
||||
const CmsPassBi = "5ige8TdfTEHoTgJz"
|
||||
|
||||
var idBox = ""
|
||||
|
||||
// CmsTopicIn : Server to Box
|
||||
var CmsTopicIn = "v1/devices/" + idBox + "/telemetry"
|
||||
|
||||
// CmsTopicOut : Box to Server
|
||||
var CmsTopicOut = "v1/devices/" + idBox + "/request/+"
|
||||
|
||||
// StatusSSH : Cho phep bat tat ssh tu xa
|
||||
var StatusSSH = 0
|
||||
|
||||
// SttUpdateEngine : Trang thai update engine
|
||||
// var SttUpdateEngine = false
|
||||
|
||||
// MqttStt : Conect mqtt server yes? no
|
||||
var MqttStt = false
|
||||
|
||||
// SttUpdate: Quyet dinh dung gui message len server mqtt broker
|
||||
var SttUpdate = false
|
||||
var SttPub = true
|
||||
|
||||
// Counter number reconnect publish
|
||||
var number_repub = 0
|
||||
|
||||
// Counter number reconnect subcriber
|
||||
var number_resub = 0
|
||||
|
||||
// Pwd : Password
|
||||
type Pwd struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
type Ports struct {
|
||||
Port string `json:"port"`
|
||||
}
|
||||
|
||||
var port Ports
|
||||
|
||||
//============================> Main <==========================
|
||||
|
||||
func main() {
|
||||
//box.GetDir() // Check thu muc goc
|
||||
file.Check_path(box.Path_log_luncher)
|
||||
file.Check_path(box.Path_log_engine)
|
||||
fmt.Println("----------------------START MAIN -------------------------")
|
||||
|
||||
box.Stop_check_engine()
|
||||
box.Stop_engine()
|
||||
go MqttServer() // Check connect tới server MQTT 5p 1 lần
|
||||
// Loop:
|
||||
for {
|
||||
if AutoServer == 1 {
|
||||
// Đã được khai báo trên trang quan lý chưa
|
||||
if box.Check_dir(box.Path_engine_config) == 0 { // Chưa được khai báo
|
||||
if box.SetUp() == false {
|
||||
fmt.Println("Box not register on CMS")
|
||||
time.Sleep(7 * time.Second) // Waitting 7 second for next request
|
||||
} else {
|
||||
// KHoi tao connect MQTT cms.beetai.com
|
||||
if MqttStt == false {
|
||||
if CheckConnectionMQTT() == true { // Server mqtt running ?
|
||||
CreateTopic()
|
||||
MqttBegin()
|
||||
// MqttCmsBi.Subscribe(CmsTopicOut, 1, MqttMessageHandler)
|
||||
MqttStt = true
|
||||
}
|
||||
}
|
||||
if MqttStt == true && SttUpdate == false && SttPub == true {
|
||||
box.RunCheck() // Check Check_engine running ???
|
||||
PublishData()
|
||||
}
|
||||
//file.Println("Publish data done")
|
||||
}
|
||||
} else { // Đã đươc khai báo trên cms
|
||||
// KHoi tao connect MQTT cms.beetai.com
|
||||
if MqttStt == false { // Chưa connect đến MQTT or mất connect mqtt
|
||||
if CheckConnectionMQTT() == true { // Server mqtt running ?
|
||||
CreateTopic()
|
||||
MqttBegin()
|
||||
// MqttCmsBi.Subscribe(CmsTopicOut, 1, MqttMessageHandler)
|
||||
MqttStt = true
|
||||
}
|
||||
}
|
||||
if MqttStt == true && SttUpdate == false && SttPub == true { // Check khởi tạo done mqtt and no update and cho phép publish message
|
||||
box.RunCheck()
|
||||
PublishData()
|
||||
}
|
||||
//file.Println("Publish data done")
|
||||
}
|
||||
box.Sleep_ms(3000)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// CreateTopic : Function
|
||||
//***********************************************//
|
||||
//------------------ Function ---------------------
|
||||
// CreateTopic : Function
|
||||
func CreateTopic() {
|
||||
//idBox = box.Get_id()
|
||||
idBox = box.Get_idBox()
|
||||
CmsTopicIn = "v1/devices/" + idBox + "/telemetry"
|
||||
CmsTopicOut = "v1/devices/" + idBox + "/request/+"
|
||||
file.Println("idBox: " + idBox)
|
||||
file.Println("TOPIC IN :" + CmsTopicIn)
|
||||
file.Println("TOPIC OUT :" + CmsTopicOut)
|
||||
|
||||
}
|
||||
|
||||
// PublishData : Function
|
||||
func PublishData() {
|
||||
|
||||
//var payload string = "{" + "\"ip_private\":" + "\"" + ip + "\"" + "," + "\"box_id\":" + "\"" + id_cam + "\"" + "," + "\"engine_status\":" + "\"" + sts_engine + "\"" + "," + "\"temperature\":" + "\"" + temp + "\"" + "," + "\"ip_public\":" + "\"" + ip_public + "\"" + "," + "\"mac_address\":" + "\"" + ether + "\"" + "," + "\"boot_storage\":" + "\"" + sum_boot_stg + "\"" + "," + "\"boot_storage_usage\":" + "\"" + boot_stg + "\"" + "," + "\"memory\":" + "\"" + sum_mem + "\"" + "," + "\"memory_usage\":" + "\"" + mem + "\"" + "," + "\"cpu_usage\":" + "\"" + FloatToString(avg) + "\"" + "," + "\"storage_usage\":" + "\"" + box_stg + "\"" + "," + "\"storage\":" + "\"" + sum_storage + "\"" + "}"
|
||||
var payload = box.Message_pub()
|
||||
//file.Println(payload)
|
||||
//fmt.Printf("Payload = %v\n\n", payload)
|
||||
Token1 := MqttCmsBi.Publish(CmsTopicIn, 1, false, payload)
|
||||
if Token1.Wait() && Token1.Error() != nil {
|
||||
fmt.Printf("Error Publish message CMS BI: %v\n", Token1.Error())
|
||||
file.Write_log("Error Publish message CMS BI", box.Path_log_luncher)
|
||||
} else {
|
||||
fmt.Println("Send message CMS BI")
|
||||
//file.Write_log("Send message CMS BI", box.Path_log_luncher)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MqttBegin : Khoi tao MQTT
|
||||
func MqttBegin() {
|
||||
|
||||
OptsCmsBI := mqtt.NewClientOptions()
|
||||
OptsCmsBI.AddBroker(CmsHostBi)
|
||||
OptsCmsBI.SetUsername(CmsAccessTokenBi)
|
||||
OptsCmsBI.SetPassword(CmsPassBi)
|
||||
OptsCmsBI.SetCleanSession(true)
|
||||
OptsCmsBI.SetConnectionLostHandler(MQTTLostConnectHandler)
|
||||
OptsCmsBI.SetOnConnectHandler(MQTTOnConnectHandler)
|
||||
|
||||
MqttCmsBi = mqtt.NewClient(OptsCmsBI)
|
||||
if Token1 := MqttCmsBi.Connect(); Token1.Wait() && Token1.Error() == nil {
|
||||
file.Println("MQTT CMS Beetsoft Connected\n")
|
||||
file.Write_log("MQTT CMS BeetsoftConnected\n", box.Path_log_luncher)
|
||||
MqttCmsBi.Subscribe(CmsTopicOut, 1, MqttMessageHandler)
|
||||
} else {
|
||||
file.Write_log("MQTT CMS Beetsoft cant not Connected\n", box.Path_log_luncher)
|
||||
MqttStt = false
|
||||
file.Println("MQTT CMS Beetsoft cant not Connected\n")
|
||||
fmt.Printf("Loi CMS Beetsoft : %v \n", Token1.Error())
|
||||
file.Println("-------------------\n")
|
||||
}
|
||||
}
|
||||
|
||||
// Check lost connect mqtt server - can't publish msg
|
||||
func MQTTLostConnectHandler(c mqtt.Client, err error) {
|
||||
c.Disconnect(10)
|
||||
MqttStt = false
|
||||
number_repub = number_repub + 1
|
||||
file.Write_log("MQTT CMS Beetsoft Lost Connect\n", box.Path_log_luncher)
|
||||
file.Write_log("Number reconnect publish msg: "+strconv.Itoa(number_repub)+"\n", box.Path_log_luncher)
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
// Check lost connect mqtt server - can't subcriber msg from cms
|
||||
func MQTTOnConnectHandler(client mqtt.Client) {
|
||||
number_resub = number_resub + 1
|
||||
file.Write_log("Lostconnect chanel subcriber: MQTT_OnConnectHandler\n", box.Path_log_luncher)
|
||||
file.Write_log("Number reconnect subcriber msg: "+strconv.Itoa(number_resub)+"\n", box.Path_log_luncher)
|
||||
client.Unsubscribe(CmsTopicOut)
|
||||
time.Sleep(10)
|
||||
client.Subscribe(CmsTopicOut, 1, MqttMessageHandler)
|
||||
file.Write_log("Recall function Subscribe MQTT\n", box.Path_log_luncher)
|
||||
}
|
||||
func MqttServer() {
|
||||
for {
|
||||
if CheckConnectionMQTT() == true {
|
||||
//
|
||||
} else {
|
||||
MqttStt = false
|
||||
}
|
||||
time.Sleep(5 * time.Minute)
|
||||
}
|
||||
}
|
||||
|
||||
// CheckConnectionMQTT: Check connection to MQTT server Beetsoft
|
||||
func CheckConnectionMQTT() bool {
|
||||
var SttMQTTServer = false // Biến trang thái kết nối MQTT với server
|
||||
out, err := exec.Command("ping", "broker.beetai.com", "-c 5", "-i 0.2", "-w 5").Output()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
file.Write_log("Recall function Subscribe MQTT\n", box.Path_log_luncher)
|
||||
SttMQTTServer = false
|
||||
} else {
|
||||
|
||||
}
|
||||
if strings.Contains(string(out), "Destination Host Unreachable") {
|
||||
fmt.Println("Disconnect Server MQTT...")
|
||||
file.Write_log("Disconnect Server MQTT...\n", box.Path_log_luncher)
|
||||
SttMQTTServer = false
|
||||
} else {
|
||||
fmt.Println("Connected Server MQTT...")
|
||||
SttMQTTServer = true
|
||||
}
|
||||
return SttMQTTServer
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
|
||||
// MqttMessageHandler : mqtt callback server beetswoft
|
||||
func MqttMessageHandler(MqttBI mqtt.Client, message mqtt.Message) {
|
||||
fmt.Println("=================== MqttMessageHandler ====================")
|
||||
fmt.Printf("Message %s\n", message)
|
||||
fmt.Printf("TOPIC: %s\n", message.Topic())
|
||||
fmt.Printf("MSG:\n %s\n", message.Payload())
|
||||
fmt.Println("=========================== + = + ==========================")
|
||||
dec := json.NewDecoder(bytes.NewReader(message.Payload()))
|
||||
var list map[string]interface{}
|
||||
if err := dec.Decode(&list); err != nil {
|
||||
fmt.Printf("Error:%v\n", err)
|
||||
file.Write_log("Message:Loi form message\n", box.Path_log_luncher)
|
||||
} else {
|
||||
//***********************************************//
|
||||
if list["method"] == "change_passwd" { // {"method" : "change_passwd","params": {"password": "123456@"}}
|
||||
file.Println("Changing password \n")
|
||||
s, err := json.Marshal(list["params"])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
var pwd Pwd
|
||||
json.Unmarshal([]byte(string(s)), &pwd)
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
pass := pwd.Password
|
||||
fmt.Print(topic, "\n\n")
|
||||
fmt.Printf("New password:%v\n", pass)
|
||||
//box.Set_passwd(pass)
|
||||
msg := `{"method":"change_passwd","status": ` + strconv.Itoa(1) + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
|
||||
} else if list["method"] == "open_ssh" {
|
||||
file.Println("Open port ssh\n")
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
p, err := json.Marshal(list["params"])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
var pVal = port.Port
|
||||
if StatusSSH == 0 {
|
||||
json.Unmarshal([]byte(string(p)), &port)
|
||||
pVal = port.Port
|
||||
fmt.Println(pVal)
|
||||
box.Ssh_open(pVal)
|
||||
}
|
||||
StatusSSH = 1
|
||||
msg := `{"method":"open_ssh","status": ` + strconv.Itoa(1) + `,"params": {"port": "` + pVal + `"}}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Open Done ssh\n")
|
||||
|
||||
} else if list["method"] == "close_ssh" {
|
||||
file.Println("Close port ssh\n")
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
if StatusSSH == 1 {
|
||||
box.Ssh_close()
|
||||
StatusSSH = 0
|
||||
}
|
||||
msg := `{"method":"close_ssh","status": ` + strconv.Itoa(1) + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Close Done ssh\n")
|
||||
|
||||
} else if list["method"] == "update_engine" {
|
||||
file.Println("Update engine\n")
|
||||
UpdateEngine()
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
msg := `{"method":"update_engine","status": ` + strconv.Itoa(1) + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Update engine done\n")
|
||||
|
||||
} else if list["method"] == "inactive_box" {
|
||||
file.Println("Inactive Box\n")
|
||||
SttPub = false
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
msg := `{"method":"inactive_box","status": ` + strconv.Itoa(1) + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Inactive Box done\n")
|
||||
|
||||
} else if list["method"] == "active_box" {
|
||||
file.Println("Active Box\n")
|
||||
SttPub = true
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
msg := `{"method":"active_box","status": ` + strconv.Itoa(1) + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Active Box done\n")
|
||||
|
||||
} else if list["method"] == "delete_box" {
|
||||
file.Println("Delete box\n")
|
||||
SttPub = false
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
msg := `{"method":"delete_box","status": ` + strconv.Itoa(1) + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Delete box done\n")
|
||||
|
||||
} else if list["method"] == "getVersion" {
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
//msg := `{"version":` + box.Get_version() + `}`
|
||||
msg := `{"method":"getVersion","params":` + box.Get_version() + `}`
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
file.Println("Get version done\n")
|
||||
} else if list["method"] == "reboot" {
|
||||
file.Write_log("Message:reboot\n", box.Path_log_luncher)
|
||||
//file.Println("=======================>> Reboot BOX <<=====================")
|
||||
box.Reboot_box()
|
||||
} else if list["method"] == "cmd" {
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
msg := `{"method":"cmd","status": ` + strconv.Itoa(1) + `}`
|
||||
//file.Println(topic)
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
//CmsResponse(MqttBI, topic, msg)
|
||||
//file.Printf("%T_%v", list["params"], list["params"])
|
||||
m, _ := list["params"].(string)
|
||||
var out string
|
||||
k := strings.Index(m, "nano")
|
||||
if k > -1 {
|
||||
if strings.Index(m, "Config.txt") > -1 {
|
||||
path := m[k+5:]
|
||||
file.Println(path)
|
||||
out = file.ReadFile(path)
|
||||
fmt.Println("0")
|
||||
} else {
|
||||
out = box.Cmd_Box(m)
|
||||
fmt.Println("1")
|
||||
}
|
||||
|
||||
} else {
|
||||
out = box.Cmd_Box(m)
|
||||
fmt.Println("2")
|
||||
}
|
||||
//fmt.Println(out)
|
||||
// out := box.Cmd_Box(m)
|
||||
mg := `{"method":"cmd","status": "` + "\n" + out + "\n" + `"}`
|
||||
fmt.Println(mg)
|
||||
CmsResponse(MqttBI, topic, mg)
|
||||
} else if list["method"] == "upgrade_engine" {
|
||||
file.Println(`list["method"] == "upgrade_engine"`)
|
||||
s, err := json.Marshal(list["params"])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
topic := strings.Replace(message.Topic(), "request", "response", 1)
|
||||
msg := `{"method":"upgrade_engine","status":` + strconv.Itoa(1) + `}`
|
||||
//file.Println(topic)
|
||||
file.Write_log("Message:"+msg+" \n", box.Path_log_luncher)
|
||||
CmsResponse(MqttBI, topic, msg)
|
||||
params := string(s)
|
||||
UpgradeEngine(params)
|
||||
} else {
|
||||
fmt.Println(list)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
func UpdateEngine() {
|
||||
SttUpdate = true
|
||||
box.Stop_check_engine()
|
||||
box.Stop_engine()
|
||||
box.RequestUpdate(box.Url)
|
||||
SttUpdate = false
|
||||
}
|
||||
|
||||
// CmsResponse : Phan hoi message tu box to Server
|
||||
func CmsResponse(c mqtt.Client, topic string, msg string) {
|
||||
c.Publish(topic, 0, false, msg)
|
||||
file.Println("Publish message done")
|
||||
}
|
||||
|
||||
// UpgradeEngine : Nang cap engine
|
||||
func UpgradeEngine(params string) {
|
||||
//file.Println("0")
|
||||
box.Check_path(box.Path_upgrade)
|
||||
//file.Println("1")
|
||||
file.Create_file(box.Path_engine_update)
|
||||
file.Write_file(params, box.Path_engine_update)
|
||||
//file.Println("2")
|
||||
SttUpdate = true
|
||||
box.Stop_check_engine()
|
||||
//file.Println("3")
|
||||
box.Stop_engine()
|
||||
//file.Println("4")
|
||||
}
|
BIN
S905X_BI/aibox@192.168.3.160
Normal file
BIN
S905X_BI/aibox@192.168.3.160
Normal file
Binary file not shown.
4
S905X_BI/build_pc.bat
Normal file
4
S905X_BI/build_pc.bat
Normal file
|
@ -0,0 +1,4 @@
|
|||
set GOOS=linux
|
||||
set GOARCH=amd64
|
||||
go build
|
||||
pause
|
2
S905X_BI/scp.txt
Normal file
2
S905X_BI/scp.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
|
||||
scp -P 2332 -r -o 'ProxyJump dh.duyhai@ssh_tunnel.beetai.com' S905X_BI root@localhost:/root/monitor/check/
|
124
action_engine/action_engine.go
Normal file
124
action_engine/action_engine.go
Normal file
|
@ -0,0 +1,124 @@
|
|||
package action_engine
|
||||
|
||||
import (
|
||||
// "bytes"
|
||||
// "encoding/json"
|
||||
|
||||
"fmt"
|
||||
// "github.com/eclipse/paho.mqtt.golang"
|
||||
//"io"
|
||||
// "log"
|
||||
// "net"
|
||||
// "os"
|
||||
"os/exec"
|
||||
// "time"
|
||||
//"io/ioutil"
|
||||
//"path/filepath"
|
||||
// "strconv" // convert string to int
|
||||
//"strings"
|
||||
)
|
||||
|
||||
var debug int = 1
|
||||
|
||||
func Kill_engine() {
|
||||
if debug == 1 {
|
||||
fmt.Printf("Kill_engine()\n")
|
||||
}
|
||||
out1, _ := exec.Command("sh", "-c", "ps -A | grep loop_run").Output()
|
||||
if len(out1) != 0 {
|
||||
_, err := exec.Command("sh", "-c", " kill $(pgrep loop_run)").Output()
|
||||
if err != nil {
|
||||
//log.Fatal(err)
|
||||
}
|
||||
}
|
||||
out2, _ := exec.Command("sh", "-c", "ps -A | grep FaceRecognition").Output()
|
||||
if len(out2) != 0 {
|
||||
_, err := exec.Command("sh", "-c", " kill $(pgrep FaceRecognition)").Output() // kill $(pgrep FaceRecognition)
|
||||
if err != nil {
|
||||
//log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
func Stop_check_engine() {
|
||||
fmt.Println("Kill Check_engine")
|
||||
if debug == 1 {
|
||||
fmt.Printf("Stop_check_engine()\n")
|
||||
}
|
||||
_, err := exec.Command("sh", "-c", "kill $(pgrep Check_engine)").Output()
|
||||
if err != nil {
|
||||
//log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Tranfer_engine() {
|
||||
if debug == 1 {
|
||||
fmt.Printf("Tranfer_engine()\n")
|
||||
fmt.Printf("Begin backup engine\n")
|
||||
}
|
||||
_, err := exec.Command("sh", "-c", "cd /root/monitor/backup").Output()
|
||||
if err != nil {
|
||||
_, err1 := exec.Command("sh", "-c", "mkdir /root/monitor/backup").Output()
|
||||
if err1 != nil {
|
||||
//log.Fatal(err)
|
||||
fmt.Printf(" Backup engine Error\n")
|
||||
}
|
||||
}
|
||||
|
||||
if debug == 1 {
|
||||
fmt.Printf("Create forder backup\n")
|
||||
}
|
||||
// Xoa file engine cu trong backup
|
||||
_, err0 := exec.Command("sh", "-c", "cd /root/monitor/backup/monitor/").Output()
|
||||
if err0 == nil {
|
||||
_, err1 := exec.Command("sh", "-c", "rm -rf /root/monitor/backup/monitor/").Output()
|
||||
if err1 != nil {
|
||||
//log.Fatal(err1)
|
||||
fmt.Printf(" Xoa file engine cu trong backup Error\n")
|
||||
}
|
||||
}
|
||||
_, err2 := exec.Command("sh", "-c", "mv /root/monitor/face_recognition/ /root/monitor/backup/").Output()
|
||||
if err2 != nil {
|
||||
//log.Fatal(err2)
|
||||
fmt.Printf("Move mv /root/monitor/face_recognition/ /root/monitor/backup/ error\n")
|
||||
|
||||
}
|
||||
// fmt.Printf("Done copy\n")
|
||||
}
|
||||
func Backup_engine() {
|
||||
if debug == 1 {
|
||||
fmt.Printf("Backup_engine()\n")
|
||||
}
|
||||
_, err := exec.Command("sh", "-c", "rm -rf /root/monitor/face_recognition/").Output()
|
||||
if err != nil {
|
||||
//log.Fatal(err)
|
||||
} else {
|
||||
_, err1 := exec.Command("sh", "-c", "mv /root/monitor/backup/face_recognition/ /root/monitor/").Output()
|
||||
if err1 != nil {
|
||||
//log.Fatal(err1)
|
||||
} else {
|
||||
_, err2 := exec.Command("sh", "-c", "sudo chmod +x /root/monitor/face_recognition/build/FaceRecognition/FaceRecognition").Output()
|
||||
if err2 != nil {
|
||||
//log.Fatal(err2)
|
||||
} else {
|
||||
//Start_engine()
|
||||
}
|
||||
}
|
||||
}
|
||||
//fmt.Printf("Done Backup_engine \n")
|
||||
}
|
||||
func Download_engine() {
|
||||
if debug == 1 {
|
||||
fmt.Printf("Download_engine()\n")
|
||||
}
|
||||
|
||||
}
|
||||
func Upgrade_engine() {
|
||||
if debug == 1 {
|
||||
fmt.Printf("Upgrade_engine()\n")
|
||||
}
|
||||
Stop_check_engine()
|
||||
Kill_engine()
|
||||
Tranfer_engine()
|
||||
//Start_engine()
|
||||
}
|
1539
box/box.go
Normal file
1539
box/box.go
Normal file
File diff suppressed because it is too large
Load Diff
217
file/file.go
Normal file
217
file/file.go
Normal file
|
@ -0,0 +1,217 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Debug int = 0
|
||||
|
||||
//List function
|
||||
|
||||
// Thao tac file
|
||||
func Create_file(path string) {
|
||||
// detect if file exists
|
||||
var _, err = os.Stat(path)
|
||||
|
||||
// create file if not exists
|
||||
if os.IsNotExist(err) {
|
||||
var file, err = os.Create(path)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
//Println("==> done creating file", path)
|
||||
}
|
||||
func Write_file(msg string, path string) {
|
||||
// open file using READ & WRITE permission
|
||||
//Check_dir(path)
|
||||
// Println("==>begin writting ")
|
||||
//var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
var file, err = os.OpenFile(path, os.O_RDWR, 0777)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(msg)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
// save changes
|
||||
err = file.Sync()
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
//Println("==> done writing to file")
|
||||
}
|
||||
|
||||
func Write_log(msg string, path string) {
|
||||
Check_path(path)
|
||||
year, month, day := time.Now().Date()
|
||||
new_path := strconv.Itoa(year) + "-" + strconv.Itoa(int(month)) + "-" + strconv.Itoa(day) + ".txt"
|
||||
path = path + "/" + new_path
|
||||
Check_dir(path)
|
||||
var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
if IsError(err) {
|
||||
////return
|
||||
if Debug == 1 {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(time.Now().String())
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
// write some text line-by-line to file
|
||||
_, err = file.WriteString("\n")
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
_, err = file.WriteString(msg)
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
_, err = file.WriteString("\n")
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
|
||||
// save changes
|
||||
err = file.Sync()
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
|
||||
//Println("==> done writing to file")
|
||||
}
|
||||
func Println(msg string) {
|
||||
if Debug == 1 {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
}
|
||||
func DeleteFile(path string) {
|
||||
// delete file
|
||||
var err = os.Remove(path)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
|
||||
Println("==> done deleting file")
|
||||
|
||||
}
|
||||
func DeleteForder(path string) {
|
||||
var cmd = "rm -rf " + path
|
||||
var log = "/admin/monitor/log/log_launcher"
|
||||
_, err := exec.Command("sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
fmt.Println("Delete forder error,Path:" + path)
|
||||
Write_log("Delete forder file error,Path:"+path, log)
|
||||
} else {
|
||||
Println("Delete forder Done ")
|
||||
}
|
||||
}
|
||||
func CoppyFile(path string, id int) {
|
||||
var cmd = "mv " + path + " /admin/monitor/backup/Config_" + strconv.Itoa(id) + ".txt"
|
||||
var log = "/admin/monitor/log/log_launcher"
|
||||
_, err := exec.Command("sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
fmt.Println("Move file error,Path:" + path)
|
||||
Write_log("Move file error,Path:"+path, log)
|
||||
} else {
|
||||
fmt.Println("Move file error,Path:" + path)
|
||||
Write_log("Move file error,Path:"+path, log)
|
||||
}
|
||||
}
|
||||
func IsError(err error) bool {
|
||||
if err != nil {
|
||||
if Debug == 1 {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (err != nil)
|
||||
}
|
||||
func Check_path(path string) {
|
||||
// Check directory ?
|
||||
_, err := exec.Command("sh", "-c", "cd "+path).Output()
|
||||
if err != nil {
|
||||
|
||||
fmt.Println(path + " not Exits\n")
|
||||
|
||||
_, err1 := exec.Command("sh", "-c", "mkdir "+path).Output()
|
||||
if err1 != nil {
|
||||
|
||||
Println(" Cant create " + path)
|
||||
|
||||
} else {
|
||||
|
||||
Println(" Create forder " + path)
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
Println(path + " ok \n")
|
||||
|
||||
}
|
||||
}
|
||||
func Check_dir(path string) {
|
||||
|
||||
Println("Function Check_dir ()")
|
||||
|
||||
//Println("------------>Check_dir <------------")
|
||||
var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
//Printf("Kieu file : %T\t", file)
|
||||
//Printf("Kieu file : %v\n", file)
|
||||
if IsError(err) {
|
||||
if Debug == 1 {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
|
||||
}
|
||||
if file == nil {
|
||||
Create_file(path)
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
func ReadFile(path string) string {
|
||||
// re-open file
|
||||
var msg string = ""
|
||||
var file, err = os.OpenFile(path, os.O_RDWR, 0777)
|
||||
if err != nil {
|
||||
if Debug == 1 {
|
||||
Println(" Open file error")
|
||||
}
|
||||
|
||||
} else {
|
||||
// read file, line by line
|
||||
var text = make([]byte, 2048)
|
||||
for {
|
||||
_, err = file.Read(text)
|
||||
// break if finally arrived at end of file
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
// break if error occured
|
||||
if err != nil && err != io.EOF {
|
||||
IsError(err)
|
||||
break
|
||||
}
|
||||
}
|
||||
msg = string(text)
|
||||
}
|
||||
defer file.Close()
|
||||
return msg
|
||||
}
|
5
test/build_pi.bat
Normal file
5
test/build_pi.bat
Normal file
|
@ -0,0 +1,5 @@
|
|||
set GOOS=linux
|
||||
set GOARCH=arm
|
||||
set GOARM=5
|
||||
go build
|
||||
pause
|
1
test/path.txt
Normal file
1
test/path.txt
Normal file
|
@ -0,0 +1 @@
|
|||
/home/beetsoft/
|
BIN
test/test.exe
Normal file
BIN
test/test.exe
Normal file
Binary file not shown.
33
test/test.go
Normal file
33
test/test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var path_base = "/home/ademax/"
|
||||
|
||||
func main() {
|
||||
|
||||
Setpath()
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
func test() {
|
||||
var path string
|
||||
year, month, day := time.Now().Date()
|
||||
path = strconv.Itoa(year) + "-" + strconv.Itoa(int(month)) + "-" + strconv.Itoa(day) + ".txt"
|
||||
|
||||
fmt.Println(path)
|
||||
}
|
||||
func Setpath() {
|
||||
if os.Args != nil {
|
||||
fmt.Println(os.Args)
|
||||
fmt.Println(path_base)
|
||||
path_base = os.Args[1]
|
||||
fmt.Println("Thay the path_base")
|
||||
fmt.Println("Path_base:", path_base)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user