From 4324306f529b9bc62d7e818c0b12ff822687bb47 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期一, 20 一月 2020 11:14:33 +0800
Subject: [PATCH] update

---
 run.go           |   14 ++--
 common/helper.go |   96 ++++++++++++++++++++++++++++++++
 common/reboot.go |   64 +++++++++++++++++++++
 3 files changed, 167 insertions(+), 7 deletions(-)

diff --git a/common/helper.go b/common/helper.go
new file mode 100644
index 0000000..29fa255
--- /dev/null
+++ b/common/helper.go
@@ -0,0 +1,96 @@
+package common
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"strconv"
+	"time"
+
+	"basic.com/pubsub/protomsg.git"
+	"github.com/gogo/protobuf/proto"
+)
+
+// GetIpcAddress get ipc
+func GetIpcAddress(shm bool, id string) string {
+	if shm {
+		return id
+	}
+	return `ipc:///tmp/` + id + `.ipc`
+}
+
+// SubConfig sub
+type SubConfig struct {
+	SoFile string            `json:"so_file_path"`
+	Env    string            `json:"runtime"`
+	Param  map[string]string `json:"param"`
+}
+
+// SdkConfig sdk
+type SdkConfig struct {
+	SoFile string            `json:"so_file_path"`
+	Env    string            `json:"runtime"`
+	Param  map[string]string `json:"param"`
+	Sub    *SubConfig        `json:"sub"`
+}
+
+// ReadConfig conf
+func ReadConfig(file string) (SdkConfig, error) {
+	data, err := ioutil.ReadFile(file)
+	if err != nil {
+		return SdkConfig{}, fmt.Errorf("READ SDK CONFIG FILE %s ERROR", file)
+	}
+
+	//璇诲彇鐨勬暟鎹负json鏍煎紡锛岄渶瑕佽繘琛岃В鐮�
+	var v SdkConfig
+	err = json.Unmarshal(data, &v)
+
+	return v, err
+}
+
+// Atoi atoi
+func Atoi(s string) int {
+	i, _ := strconv.Atoi(s)
+	return i
+}
+
+// UnserilizeProto un
+func UnserilizeProto(ctx context.Context, data <-chan []byte, out chan<- protomsg.SdkMessage, fn func(...interface{})) {
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		case d := <-data:
+			if len(d) < 100 {
+				continue
+			}
+			msg := protomsg.SdkMessage{}
+			if err := proto.Unmarshal(d, &msg); err != nil {
+				fn(err, " msg 澶勭悊寮傚父")
+				continue
+			}
+
+			out <- msg
+
+		default:
+			time.Sleep(10 * time.Millisecond)
+		}
+	}
+}
+
+// UnpackImage unpack
+func UnpackImage(msg protomsg.SdkMessage, fnName string, fn func(...interface{})) *protomsg.Image {
+	// 鍙嶅簭鍒楀寲鏁版嵁寰楀埌sdk鍏ュ弬
+	i := &protomsg.Image{}
+	err := proto.Unmarshal(msg.Data, i)
+	if err != nil {
+		fn(fnName, " protobuf decode CameraImage error: ", err.Error())
+		return nil
+	}
+	if i.Data == nil {
+		fn(fnName, " protomsg.Image data null")
+		return nil
+	}
+	return i
+}
diff --git a/common/reboot.go b/common/reboot.go
new file mode 100644
index 0000000..e4a9e4c
--- /dev/null
+++ b/common/reboot.go
@@ -0,0 +1,64 @@
+package common
+
+import (
+	"context"
+	"os"
+	"sync"
+	"time"
+)
+
+// Disturber stop
+type Disturber struct {
+	mtx    sync.Mutex
+	live   bool
+	until  int
+	maxTry int
+}
+
+// NewDisturber new
+func NewDisturber(maxTry int) *Disturber {
+	return &Disturber{
+		live:   true,
+		until:  0,
+		maxTry: maxTry,
+	}
+}
+
+// Prevent prevent
+func (d *Disturber) Prevent() {
+	d.mtx.Lock()
+	defer d.mtx.Unlock()
+	d.live = true
+}
+
+// MaybeReboot reboot
+func (d *Disturber) MaybeReboot(ctx context.Context, fn func(...interface{})) {
+	d.live = true
+	for {
+		select {
+		case <-ctx.Done():
+			return
+		default:
+			d.mtx.Lock()
+			running := d.live
+			d.mtx.Unlock()
+
+			if running {
+				d.until = 0
+
+				d.mtx.Lock()
+				d.live = false
+				d.mtx.Unlock()
+
+			} else {
+				d.until++
+				fn("!!!!!!No Running: ", d.until)
+				if d.until > d.maxTry {
+					fn("!!!!!!Too Long Running, Reboot: ", d.maxTry)
+					os.Exit(0)
+				}
+			}
+			time.Sleep(time.Second)
+		}
+	}
+}
diff --git a/run.go b/run.go
index 18b7544..90de849 100644
--- a/run.go
+++ b/run.go
@@ -48,7 +48,7 @@
 
 	"reid/rpc"
 
-	"basic.com/libgowrapper/sdkhelper.git"
+	"reid/common"
 
 	"basic.com/valib/gogpu.git"
 
@@ -68,7 +68,7 @@
 // Create Reid
 func Create(config string, typ, id string, gpu int, shm bool, ipc2Rule string, ruleMaxSize int, fn func(...interface{}), reserved map[string]interface{}) interface{} {
 
-	cfg, err := sdkhelper.ReadConfig(config)
+	cfg, err := common.ReadConfig(config)
 	if err != nil {
 		fn("Reid SDK Create Error When Read Config: ", err)
 		return nil
@@ -88,7 +88,7 @@
 		}
 	}
 
-	gpuM := sdkhelper.Atoi(cfg.Param[sGPU])
+	gpuM := common.Atoi(cfg.Param[sGPU])
 
 	rGPU := gpu
 
@@ -123,8 +123,8 @@
 	ipcSnd := s.ipc + postPush
 	ipcRcv := s.ipc + postPull
 
-	sndURL := sdkhelper.GetIpcAddress(true, ipcSnd)
-	rcvURL := sdkhelper.GetIpcAddress(true, ipcRcv)
+	sndURL := common.GetIpcAddress(true, ipcSnd)
+	rcvURL := common.GetIpcAddress(true, ipcRcv)
 
 	chSnd := make(chan []byte, 3)
 	chRcv := make(chan []byte, 3)
@@ -133,7 +133,7 @@
 	go recv.Run(ctx)
 
 	chMsg := make(chan protomsg.SdkMessage, 3)
-	go sdkhelper.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger)
+	go common.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger)
 
 	send := rpc.NewSender(sndURL, chSnd, true, s.fnLogger)
 	go send.Run(ctx)
@@ -147,7 +147,7 @@
 				s.fnLogger("reid !!!!!! Recv Msg From Humantrack Error")
 				continue
 			}
-			i := sdkhelper.UnpackImage(msg, "reid", s.fnLogger)
+			i := common.UnpackImage(msg, "reid", s.fnLogger)
 			if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
 				s.fnLogger("reid !!!!!! Unpack Image From Humantrack Msg Failed")
 				continue

--
Gitblit v1.8.0