package service
|
|
import (
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/go-aiot.git/aiotProto/aiot"
|
"basic.com/valib/go-aiot.git/client"
|
"basic.com/valib/logger.git"
|
"encoding/json"
|
"vamicro/saas-service/service/nodeService"
|
"vamicro/saas-service/util"
|
)
|
|
// 同步摄像机数据
|
func syncCamera(cli *client.Client) {
|
deviceList := nodeService.Node.NodeLists
|
if deviceList == nil {
|
return
|
}
|
lock, err := SetGlobalLock(util.CameraRuleDispatch)
|
defer DelGlobalLock(util.CameraRuleDispatch)
|
if err != nil {
|
logger.Error("fail to lock "+util.CameraRuleDispatch, err)
|
return
|
}
|
if !lock {
|
// 上锁失败
|
logger.Error("lock exists "+util.CameraRuleDispatch, err)
|
return
|
}
|
for _, node := range deviceList {
|
// 同步普通相机
|
SyncCameraData(cli, node.Id)
|
|
// 同步普通摄像机传感器
|
SyncSensorData(cli, node.Id)
|
|
// 同步国标摄像机
|
SyncGbCameraData(cli, node.Id)
|
|
// 同步场景信息
|
SyncSceneData(cli, node.Id)
|
|
// 同步算力设置
|
SyncPollConfigData(cli, node.Id)
|
}
|
}
|
|
func SyncAllCameraData(cli *client.Client, nodeId string) {
|
// 同步普通相机
|
SyncCameraData(cli, nodeId)
|
|
// 同步普通摄像机传感器
|
SyncSensorData(cli, nodeId)
|
|
// 同步国标摄像机
|
SyncGbCameraData(cli, nodeId)
|
|
// 同步场景信息
|
SyncSceneData(cli, nodeId)
|
|
// 同步算力设置
|
SyncPollConfigData(cli, nodeId)
|
}
|
|
// 同步普通相机
|
func SyncCameraData(cli *client.Client, nodeId string) {
|
logger.Debug("同步摄像机信息")
|
// 获取普通摄像机树
|
cameraTopic := "/data/api-v/camera/getSyncCameraData"
|
param := make(map[string]interface{})
|
data, err := DoBusReq(cameraTopic, nodeId, aiot.RequestMethod_Post, aiot.RequestContentType_ApplicationJson, param)
|
if err != nil {
|
// 查询错误
|
logger.Error("fail to do bus request topic", cameraTopic, err)
|
return
|
}
|
cameraDataResp := bhomeclient.Reply{}
|
err = json.Unmarshal(data, &cameraDataResp)
|
if err != nil {
|
logger.Error("fail to Unmarshal topic data", cameraTopic, err)
|
return
|
}
|
cameraData, err := json.Marshal(cameraDataResp.Data)
|
if err != nil {
|
logger.Error("fail to Marshal topic data", cameraTopic, cameraDataResp, err)
|
return
|
}
|
|
cameraReport := aiot.DataReport{
|
DataKey: CameraDataReport,
|
Data: cameraData,
|
}
|
cameraReportData, _ := json.Marshal(cameraReport)
|
cameraSyncData := &aiot.Protocol{
|
Receiver: aiot.RECEIVER_TO_SAAS,
|
SenderId: nodeId,
|
MsgType: aiot.MSG_TYPE_DATA_REPORT,
|
ReqType: aiot.REQ_TYPE_REQUEST,
|
MsgProto: client.GetMsgProto(""),
|
Data: cameraReportData,
|
}
|
// 上报集群数据
|
_ = cli.WriteBody(cameraSyncData)
|
}
|
|
// 同步普通摄像机传感器信息
|
func SyncSensorData(cli *client.Client, nodeId string) {
|
logger.Debug("同步传感器信息")
|
// 获取普通摄像机传感器
|
sensorTopic := "/data/api-v/camera/getSyncSensorData"
|
param := make(map[string]interface{})
|
data, err := DoBusReq(sensorTopic, nodeId, aiot.RequestMethod_Post, aiot.RequestContentType_ApplicationJson, param)
|
if err != nil {
|
// 查询错误
|
logger.Error("fail to do bus request topic", sensorTopic, err)
|
return
|
}
|
sensorDataResp := bhomeclient.Reply{}
|
err = json.Unmarshal(data, &sensorDataResp)
|
if err != nil {
|
logger.Error("fail to Unmarshal topic data", sensorTopic, err)
|
return
|
}
|
sensorData, err := json.Marshal(sensorDataResp.Data)
|
if err != nil {
|
logger.Error("fail to Marshal topic data", sensorTopic, sensorData, err)
|
return
|
}
|
|
sensorReport := aiot.DataReport{
|
DataKey: SensorDataReport,
|
Data: sensorData,
|
}
|
sensorReportData, _ := json.Marshal(sensorReport)
|
sensorSyncData := &aiot.Protocol{
|
Receiver: aiot.RECEIVER_TO_SAAS,
|
SenderId: nodeId,
|
MsgType: aiot.MSG_TYPE_DATA_REPORT,
|
ReqType: aiot.REQ_TYPE_REQUEST,
|
MsgProto: client.GetMsgProto(""),
|
Data: sensorReportData,
|
}
|
// 上报集群数据
|
_ = cli.WriteBody(sensorSyncData)
|
}
|
|
// 同步国标摄像机
|
func SyncGbCameraData(cli *client.Client, nodeId string) {
|
logger.Debug("同步摄像机信息")
|
// 获取普通摄像机树
|
cameraTopic := "/data/api-v/gb28181/getSyncCameraData"
|
param := make(map[string]interface{})
|
data, err := DoBusReq(cameraTopic, nodeId, aiot.RequestMethod_Post, aiot.RequestContentType_ApplicationJson, param)
|
if err != nil {
|
// 查询错误
|
logger.Error("fail to do bus request topic", cameraTopic, err)
|
return
|
}
|
cameraDataResp := bhomeclient.Reply{}
|
err = json.Unmarshal(data, &cameraDataResp)
|
if err != nil {
|
logger.Error("fail to Unmarshal topic data", cameraTopic, err)
|
return
|
}
|
cameraData, err := json.Marshal(cameraDataResp.Data)
|
if err != nil {
|
logger.Error("fail to Marshal topic data", cameraTopic, cameraDataResp, err)
|
return
|
}
|
|
cameraReport := aiot.DataReport{
|
DataKey: CameraGb28181DataReport,
|
Data: cameraData,
|
}
|
cameraReportData, _ := json.Marshal(cameraReport)
|
cameraSyncData := &aiot.Protocol{
|
Receiver: aiot.RECEIVER_TO_SAAS,
|
SenderId: nodeId,
|
MsgType: aiot.MSG_TYPE_DATA_REPORT,
|
ReqType: aiot.REQ_TYPE_REQUEST,
|
MsgProto: client.GetMsgProto(""),
|
Data: cameraReportData,
|
}
|
// 上报集群数据
|
_ = cli.WriteBody(cameraSyncData)
|
}
|
|
// 同步国标摄像机
|
func SyncSceneData(cli *client.Client, nodeId string) {
|
logger.Debug("同步摄像机信息")
|
// 获取普通摄像机树
|
cameraTopic := "/data/api-v/polygon/getSyncSceneData"
|
param := make(map[string]interface{})
|
data, err := DoBusReq(cameraTopic, nodeId, aiot.RequestMethod_Post, aiot.RequestContentType_ApplicationJson, param)
|
if err != nil {
|
// 查询错误
|
logger.Error("fail to do bus request topic", cameraTopic, err)
|
return
|
}
|
cameraDataResp := bhomeclient.Reply{}
|
err = json.Unmarshal(data, &cameraDataResp)
|
if err != nil {
|
logger.Error("fail to Unmarshal topic data", cameraTopic, err)
|
return
|
}
|
cameraData, err := json.Marshal(cameraDataResp.Data)
|
if err != nil {
|
logger.Error("fail to Marshal topic data", cameraTopic, cameraDataResp, err)
|
return
|
}
|
|
cameraReport := aiot.DataReport{
|
DataKey: ScenesDataReport,
|
Data: cameraData,
|
}
|
cameraReportData, _ := json.Marshal(cameraReport)
|
cameraSyncData := &aiot.Protocol{
|
Receiver: aiot.RECEIVER_TO_SAAS,
|
SenderId: nodeId,
|
MsgType: aiot.MSG_TYPE_DATA_REPORT,
|
ReqType: aiot.REQ_TYPE_REQUEST,
|
MsgProto: client.GetMsgProto(""),
|
Data: cameraReportData,
|
}
|
// 上报集群数据
|
_ = cli.WriteBody(cameraSyncData)
|
}
|
|
// 同步算力设置
|
func SyncPollConfigData(cli *client.Client, nodeId string) {
|
pollConfigTopic := "/data/api-v/pollConfig/getPollConfig"
|
param := make(map[string]interface{})
|
data, err := DoBusReq(pollConfigTopic, nodeId, aiot.RequestMethod_Post, aiot.RequestContentType_ApplicationJson, param)
|
if err != nil {
|
// 查询错误
|
logger.Error("fail to do bus request topic", pollConfigTopic, err)
|
return
|
}
|
pollConfigDataResp := bhomeclient.Reply{}
|
err = json.Unmarshal(data, &pollConfigDataResp)
|
if err != nil {
|
logger.Error("fail to Unmarshal topic data", pollConfigTopic, err)
|
return
|
}
|
pollConfigData, err := json.Marshal(pollConfigDataResp.Data)
|
if err != nil {
|
logger.Error("fail to Marshal topic data", pollConfigTopic, pollConfigData, err)
|
return
|
}
|
pollConfigReport := aiot.DataReport{
|
DataKey: PollConfigDataReport,
|
Data: pollConfigData,
|
}
|
pollConfigReportData, _ := json.Marshal(pollConfigReport)
|
pollConfigSyncData := &aiot.Protocol{
|
Receiver: aiot.RECEIVER_TO_SAAS,
|
SenderId: nodeId,
|
MsgType: aiot.MSG_TYPE_DATA_REPORT,
|
ReqType: aiot.REQ_TYPE_REQUEST,
|
MsgProto: client.GetMsgProto(""),
|
Data: pollConfigReportData,
|
}
|
// 上报集群数据
|
_ = cli.WriteBody(pollConfigSyncData)
|
}
|