Bläddra i källkod

feat: 轮询修改更新地图状态&

HeWang 7 månader sedan
förälder
incheckning
b2d94c39d7

+ 13 - 0
biz/dal/mysql/map_update.go

@@ -78,6 +78,19 @@ func QueryMapUpdateRecordsByRescanFlag(ctx context.Context, flag int32) ([]*mode
 	return records, err
 }
 
+func QueryMapUpdateRecordsByUpdateFlag(ctx context.Context, flag int32) ([]*model.MapUpdate, error) {
+	r := query.MapUpdate
+
+	var records []*model.MapUpdate
+	records, err := r.WithContext(ctx).Where(r.UpdateFlag.Eq(flag)).Find()
+	if err != nil {
+		return records, err
+	}
+
+	fmt.Println("Query map update records successfully:", len(records))
+	return records, err
+}
+
 func QueryMapRescanList(ctx context.Context, record *model.MapUpdate, pageFlag bool, page, pageSize int) ([]*model.MapUpdate, int64, error) {
 	r := query.MapUpdate
 	q := r.WithContext(ctx)

+ 120 - 3
biz/handler/map_service/map_service.go

@@ -686,6 +686,51 @@ func CheckDeviceMapStatus(ctx context.Context, c *app.RequestContext) {
 	c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1000", Message: "记录存在,符合地图更新条件"})
 }
 
+// CheckUpdateMapStatus 根据设备号及map id查询地图状态
+// @router /map/check/updateMap/status [GET]
+func CheckUpdateMapStatus(ctx context.Context, c *app.RequestContext) {
+	deviceNo := c.Query("deviceNo")
+	fmt.Println("deviceNo", deviceNo)
+	mapId := c.Query("mapId")
+	fmt.Println("mapId", mapId)
+	updateTime := c.Query("updateTime")
+	fmt.Println("updateTime", updateTime)
+
+	// 查询 设备sn码 是否存在于 设备表中
+	device, err := mysql.QueryDeviceByDeviceNo(ctx, deviceNo)
+	if err != nil {
+		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备记录失败"})
+		return
+	}
+	fmt.Println("device", device)
+
+	// 查询[对应设备]是否存在对应map id且有效的记录
+	records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo)
+	if err != nil {
+		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"})
+		return
+	}
+	fmt.Println("records", records)
+
+	if len(records) == 0 { // [对应设备]不存在对应map id且有效的记录
+		// 地图已重采/删除
+		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1--001", Message: "地图已重采/删除, 不符合地图上传条件"})
+		return
+	}
+
+	// 判断记录时间戳(多个记录取最晚时间戳)是否晚于地图版本时间戳,即判断记录采集后,地图是否发生更新
+	record := records[0]
+	versionTimeVal, _ := strconv.Atoi(record.MapVersion)
+	fmt.Println("versionTimeVal", versionTimeVal)
+	updateTimeVal, _ := strconv.Atoi(updateTime)
+	fmt.Println("updateTimeVal", updateTimeVal)
+	if versionTimeVal > updateTimeVal { // 地图已更新
+		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1003", Message: "数据采集后地图已更新版本, 不符合地图上传条件"})
+		return
+	}
+	c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1000", Message: "记录存在,符合地图上传条件"})
+}
+
 // UpdateDeviceMap 根据地图列表及设备列表维护设备地图表
 // @router /map/update/deviceMap/record [GET]
 func UpdateDeviceMap(ctx context.Context, c *app.RequestContext) {
@@ -1203,7 +1248,7 @@ func UploadPjiMapAndNotify(ctx context.Context, c *app.RequestContext) {
 
 }
 
-// AddMapRescanNotifyWithHttp 查询累积地图更新率于续扫阈值的记录,并通知运维人员
+// AddMapRescanNotifyWithHttp 查询累积地图更新率大于等于续扫阈值的记录,并通知运维人员
 // @router /map/rescan/notify/add [GET]
 func AddMapRescanNotifyWithHttp(ctx context.Context, c *app.RequestContext) {
 	// 查询朴津地图列表
@@ -1277,7 +1322,7 @@ func AddMapRescanNotifyWithHttp(ctx context.Context, c *app.RequestContext) {
 	c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "更新地图续扫提醒成功"})
 }
 
-// AddMapRescanNotifyWithoutHttp 查询累积地图更新率于续扫阈值的记录,并通知运维人员
+// AddMapRescanNotifyWithoutHttp 查询累积地图更新率大于等于续扫阈值的记录,并通知运维人员
 func AddMapRescanNotifyWithoutHttp(ctx context.Context) bool {
 	// 查询朴津地图列表
 	mapInfoList, err := getMapInfoList()
@@ -1351,6 +1396,54 @@ func AddMapRescanNotifyWithoutHttp(ctx context.Context) bool {
 	return true
 }
 
+// UpdateMapDeployStatusWithoutHttp 根据地图id和版本号查询朴津地图下发情况,并更新对应地图状态
+func UpdateMapDeployStatusWithoutHttp(ctx context.Context) bool {
+	// 查询 update_flag 为1的更新地图列表
+	mapUpdates, err := mysql.QueryMapUpdateRecordsByUpdateFlag(ctx, config.UPDATE_MAP_UPLOAD_FLAG)
+	if err != nil {
+		fmt.Println("获取更新地图列表失败")
+		return false
+	}
+
+	for _, mapUpdate := range mapUpdates {
+
+		//// 查询地图id是否存在于地图列表中
+		//exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList)
+		//fmt.Println("exist", exist)
+		//fmt.Println("record", record)
+		//if !exist {
+		//	fmt.Println("地图列表中不存在该地图id,跳过...")
+		//	continue
+		//}
+
+		mapDeployRes, err := checkMapDeployStatus(mapUpdate.MapID, *mapUpdate.UploadVersion)
+		if err != nil {
+			return false
+		}
+		fmt.Println("mapDeployRes", mapDeployRes)
+		changeFlag := false
+		if mapDeployRes.Code == 500 { // 地图不存在,将更新地图状态标记为无效
+			mapUpdate.UpdateFlag = config.UPDATE_MAP_NOT_VALID_FLAG
+			changeFlag = true
+		} else if mapDeployRes.Code == 200 {
+			if mapDeployRes.Data.Status == 1 {
+				mapUpdate.UpdateFlag = config.UPDATE_MAP_DEPLOY_FLAG
+				changeFlag = true
+			}
+		}
+
+		if changeFlag {
+			_, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
+			if err != nil {
+				fmt.Println("修改更新地图状态失败")
+				return false
+			}
+		}
+		time.Sleep(2 * time.Second)
+	}
+	return true
+}
+
 // QueryMapRescanNotifyList 查询地图续扫提醒列表
 // @router /map/rescan/notify/list [GET]
 func QueryMapRescanNotifyList(ctx context.Context, c *app.RequestContext) {
@@ -1370,7 +1463,6 @@ func QueryMapRescanNotifyList(ctx context.Context, c *app.RequestContext) {
 
 	records, count, err := mysql.QueryMapRescanList(ctx, &record, pageFlag, page, pageSize)
 	if err != nil {
-		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
 		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
 		return
 	}
@@ -1441,6 +1533,31 @@ func getMapInfoList() ([]model.MapInfo, error) {
 	return mapRes.Data, nil
 }
 
+// 调用 朴津地图下发状态查询 接口
+func checkMapDeployStatus(mapId string, version string) (model.MapDeployRes, error) {
+	paramMap := make(map[string]interface{})
+	paramMap["mapId"] = mapId
+	paramMap["version"] = version
+	fmt.Println("paramMap", paramMap)
+	resp, err := pji_client.ApiClient.GetRequestWithForm(c_pji.PjiApiBaseUrl+"getMapVersionStatus?mapId="+mapId+"&version="+version, paramMap, pji_client.MapSecretId)
+	if err != nil {
+		return model.MapDeployRes{}, err
+	}
+	//fmt.Println("resp", resp)
+	// 解析json响应
+	body := resp.Body()
+
+	//fmt.Println("body", body)
+	var mapRes model.MapDeployRes
+	err = json.Unmarshal([]byte(body), &mapRes)
+	fmt.Println("mapRes", mapRes)
+	if err != nil {
+		return mapRes, err
+	}
+
+	return mapRes, nil
+}
+
 // 查询地图id是否存在于地图列表中
 func checkMapIdExist(mapId string, mapInfoList []model.MapInfo) (bool, model.MapInfo) {
 	// 转为map

+ 11 - 0
biz/model/map_info.go

@@ -21,3 +21,14 @@ type MapInfo struct {
 	CustomAreaId int      `json:"customAreaId"`
 	SnCodeList   []string `json:"snCodeList"`
 }
+
+type MapDeployRes struct {
+	Msg  string          `json:"msg"`
+	Code int             `json:"code"`
+	Data MapDeployStatus `json:"data"`
+}
+
+type MapDeployStatus struct {
+	MapId  string `json:"mapId"`
+	Status int    `json:"status"`
+}

+ 8 - 4
common/config/c_map.go

@@ -11,8 +11,12 @@ var (
 )
 
 const (
-	MAP_VALID_FLAG      = 1
-	MAP_NOT_VALID_FLAG  = 0
-	MAP_RESCAN_FLAG     = 1
-	MAP_NOT_RESCAN_FLAG = 0
+	MAP_VALID_FLAG             = 1
+	MAP_NOT_VALID_FLAG         = 0
+	MAP_RESCAN_FLAG            = 1
+	MAP_NOT_RESCAN_FLAG        = 0
+	UPDATE_MAP_NOT_UPLOAD_FLAG = 0
+	UPDATE_MAP_UPLOAD_FLAG     = 1
+	UPDATE_MAP_DEPLOY_FLAG     = 2
+	UPDATE_MAP_NOT_VALID_FLAG  = -1
 )

+ 1 - 1
common/config/c_pji/c_pji.go

@@ -3,7 +3,7 @@ package c_pji
 var (
 	//SenceOssDownUrl = "http://36.110.106.156:11121/open/scene/oss/down/" // 外网
 	SenceOssDownUrl = "http://10.14.86.147:9081/open/scene/oss/down/" // 内网
-	//SenceInfoUrl    = "http://36.110.106.156:11121/open/scene/" // 外网
+	//SenceInfoUrl = "http://36.110.106.156:11121/open/scene/" // 外网
 	SenceInfoUrl = "http://10.14.86.147:9081/open/scene/" // 内网
 
 	Token = "4773hd92ysj54paflw2jem3onyhywxt2"

+ 8 - 0
main.go

@@ -40,6 +40,14 @@ func main() {
 			fmt.Println("轮询上传原始地图结束")
 		}
 	})
+
+	_, err = c.AddFunc("@every 10m", func() {
+		flag := map_service.UpdateMapDeployStatusWithoutHttp(ctx)
+		if flag {
+			fmt.Println("轮询更新地图下发状态成功")
+		}
+	})
+
 	if err != nil {
 		return
 	}

+ 34 - 2
pji_client/pji_client.go

@@ -140,10 +140,10 @@ func (c *SysUserApiClient) generateSignature(val string) (string, error) {
 	hashed := hash.Sum(nil)
 	//fmt.Println("hashed", hashed)
 	str := hex.EncodeToString(hashed)
-	bytes := []byte(str)
+	bytes1 := []byte(str)
 	//fmt.Println("str", str)
 	//fmt.Println("bytes", bytes)
-	signedData, err := rsa.SignPKCS1v15(rand.Reader, c.PrivateKey, 0, bytes)
+	signedData, err := rsa.SignPKCS1v15(rand.Reader, c.PrivateKey, 0, bytes1)
 	str = hex.EncodeToString(signedData)
 	if err != nil {
 		return "", err
@@ -221,6 +221,38 @@ func (c *SysUserApiClient) GetRequest(url string, paramMap map[string]interface{
 	return resp, err
 }
 
+// GetRequestWithForm 发送GET请求
+func (c *SysUserApiClient) GetRequestWithForm(url string, paramMap map[string]interface{}, secretId string) (*protocol.Response, error) {
+	paramMap = sortMapByKey(paramMap)
+	jsonData, err := json.Marshal(paramMap)
+	fmt.Println("jsonData", string(jsonData))
+	if err != nil {
+		return nil, err
+	}
+	str := string(jsonData)
+	if str == "{}" {
+		str = ""
+	}
+	signature, err := c.generateSignature(str)
+	if err != nil {
+		return nil, err
+	}
+
+	newParamMap := make(map[string]string)
+	for k, v := range paramMap {
+		newParamMap[k] = fmt.Sprintf("%v", v)
+	}
+
+	req := c.getHttpRequest("GET", url, secretId, signature)
+	req.SetHeader("Content-Type", "multipart/form-data")
+	req.SetFormData(newParamMap)
+
+	resp := protocol.AcquireResponse()
+	ctx := context.Background()
+	err = c.HttpClient.Do(ctx, req, resp)
+	return resp, err
+}
+
 // UploadRequest 上传文件
 func (c *SysUserApiClient) UploadRequest(url string, paramMap map[string]interface{}, secretId string, file []byte) (*protocol.Response, error) {
 	paramMap = sortMapByKey(paramMap)

+ 1 - 0
router.go

@@ -21,6 +21,7 @@ func customizedRegister(r *server.Hertz) {
 	r.POST("/map/check/mapbuf/consistency", map_service.CheckMapBufConsistency)
 
 	r.GET("/map/check/deviceMap/status", map_service.CheckDeviceMapStatus)
+	r.GET("/map/check/updateMap/status", map_service.CheckUpdateMapStatus)
 
 	r.GET("/map/download/oss", map_service.DownloadOSSFile)