package camera
|
|
import (
|
"errors"
|
|
"github.com/long/test/deliver"
|
"github.com/long/test/httpclient"
|
"github.com/long/test/sdk"
|
|
"context"
|
"encoding/json"
|
"fmt"
|
"time"
|
)
|
|
/*
|
* 1. 获取 cid
|
* 2. 获取 cid和 taskid关系
|
* 3. 获取 cid ipc communication
|
*/
|
var SocketManage = make(map[string]SocketContext)
|
var Initchannel = make(chan Camerdata)
|
var UrlPort = 7000
|
|
type Camerdata struct {
|
Cameraid string
|
Rtsp string
|
}
|
|
type SocketContext struct {
|
Sock deliver.Deliver
|
Context context.Context
|
Cancel context.CancelFunc
|
}
|
|
func Taskdolist(cid string, taskid string, data []byte) {
|
|
fmt.Println("======================================")
|
// 数据加工(打标签)
|
sdkmsg := sdk.SdkData(cid, taskid, data)
|
if sdkmsg.Tasklab == nil {
|
fmt.Println("cid:%s 没有任务%s", cid, taskid)
|
return
|
}
|
|
// 计算分发的主题
|
SendTopic := sdk.SdkSendTopic(sdkmsg)
|
if _, ok := sdk.SdkMap[SendTopic]; ok {
|
fmt.Println("分发的主题存在")
|
sdk.SdkMap[SendTopic] <- sdkmsg
|
//fmt.Println("重新开始循环: ", sdk.SdkMap)
|
} else {
|
fmt.Println("分发的主题不存在")
|
}
|
|
}
|
|
func Init() {
|
|
CameraRelative()
|
fmt.Println()
|
fmt.Println("cid,taskid , sid: ", CtsId)
|
|
url := fmt.Sprintf("%s%d", "tcp://192.168.1.124:", UrlPort)
|
_, socket, err := NewCamerSocketListen(deliver.Pair, "init", url)
|
if err != nil {
|
return
|
}
|
UrlPort++
|
|
go SendRecv(socket, Initchannel)
|
|
var cameratodecode Camerdata
|
for {
|
time.Sleep(2 * time.Second)
|
i := 0
|
for _, cam := range CtsId {
|
|
if i < 2 {
|
cameratodecode.Cameraid = cam.Cameraid
|
cameratodecode.Rtsp = cam.RtspUrl
|
Initchannel <- cameratodecode
|
}
|
i++
|
}
|
}
|
}
|
|
func send(socket SocketContext, cam Camerdata) {
|
b, err := json.Marshal(cam)
|
if err != nil {
|
fmt.Println("can not json convert !", cam)
|
}
|
if err := socket.Sock.Send(b); err != nil {
|
fmt.Println("camera info: failed send")
|
}
|
}
|
|
func SendRecv(socket SocketContext, camera chan Camerdata) {
|
//socket.Sock.SetOption(mangos.OptionRecvDeadline, 5*time.Second)
|
for cam := range camera {
|
send(socket, cam)
|
if _, ok := SocketManage[cam.Cameraid]; !ok {
|
go func(id string, urlport int) {
|
fmt.Println("create cid server: ", id)
|
url := fmt.Sprintf("%s%d", "tcp://192.168.1.124:", urlport)
|
cid, socketlisten, err := NewCamerSocketListen(deliver.PushPull, id, url)
|
|
if err != nil {
|
return
|
}
|
fmt.Println("input id: ", id, " output id :", cid)
|
Recv(cid, socketlisten)
|
|
}(cam.Cameraid, UrlPort)
|
|
UrlPort++
|
}
|
}
|
}
|
|
// 获取 cid , taskid, sdkid 关系
|
var CtsId []httpclient.Camerasingle
|
|
func CameraRelative() {
|
CtsId = httpclient.GetEsDataReq("http://127.0.0.1:8000/data/api-v/camera/queryCameraAndTaskInfo")
|
}
|
|
// 根据cid 获取 所有的任务
|
func GetAlltask(cid string) (tasks []string) {
|
for _, camsingle := range CtsId {
|
if cid == camsingle.Cameraid {
|
for _, tasksingle := range camsingle.TaskList {
|
tasks = append(tasks, tasksingle.Taskid)
|
}
|
return
|
}
|
}
|
return
|
}
|
|
// create server
|
func NewCamerSocketListen(mode int, cameraid string, url string) (cid string, socket SocketContext, err error) {
|
fmt.Println("url is: ", url)
|
ctx, cancel := context.WithCancel(context.Background())
|
|
socket.Context = ctx
|
socket.Cancel = cancel
|
|
socket.Sock = deliver.NewServer(deliver.Mode(mode), url)
|
fmt.Println("new socket.Sock: ", socket.Sock)
|
|
if socket.Sock == nil {
|
return cameraid, socket, errors.New("create listen error")
|
}
|
|
SocketManage[cameraid] = socket
|
return cameraid, socket, nil
|
}
|
|
func Recv(cameraid string, socket SocketContext) {
|
// socket.Sock.SetOption(mangos.OptionRecvDeadline, 5*time.Second)
|
var msg []byte
|
var err error
|
for {
|
select {
|
case <-socket.Context.Done():
|
fmt.Println("listen recv quit")
|
return
|
default:
|
if msg, err = socket.Sock.Recv(); err != nil {
|
//fmt.Printf("%s ", err)
|
continue
|
} else {
|
fmt.Println("cameraid: ", len(msg))
|
for _, taskid := range GetAlltask(cameraid) {
|
Taskdolist(cameraid, taskid, msg)
|
// fmt.Println("receive: ", len(msg), "cameraid: ", cameraid, "taskid: ", taskid)
|
}
|
}
|
}
|
}
|
}
|