|
@@ -9,6 +9,7 @@ import (
|
|
|
"github.com/cloudwego/hertz/pkg/protocol/consts"
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
"io"
|
|
|
+ "io/ioutil"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
@@ -16,8 +17,10 @@ import (
|
|
|
"pji_desktop_http/biz/model"
|
|
|
"pji_desktop_http/common/config"
|
|
|
"pji_desktop_http/common/config/c_log"
|
|
|
+ "pji_desktop_http/common/config/c_pji"
|
|
|
"pji_desktop_http/common/entity"
|
|
|
"pji_desktop_http/common/util"
|
|
|
+ "pji_desktop_http/pji_client"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
@@ -398,6 +401,192 @@ func QueryMapUpdateRecord(ctx context.Context, c *app.RequestContext) {
|
|
|
c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图更新查询成功", Data: string(output), Total: int(count)})
|
|
|
}
|
|
|
|
|
|
+// TestUploadUpdateMap 测试朴津地图上传接口
|
|
|
+// @router /map/upload/update/map [GET]
|
|
|
+func TestUploadUpdateMap(ctx context.Context, c *app.RequestContext) {
|
|
|
+ header, err := c.FormFile("file")
|
|
|
+ if err != nil {
|
|
|
+ c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fileName := header.Filename
|
|
|
+ fmt.Println("filename", fileName)
|
|
|
+
|
|
|
+ f, _ := header.Open()
|
|
|
+ defer f.Close()
|
|
|
+
|
|
|
+ paramMap := make(map[string]interface{})
|
|
|
+
|
|
|
+ // Map<String, String> paramMap=new HashMap<>();
|
|
|
+ // paramMap.put("mapId","2247aeb31ad34wwddfd9c3ba24536e52859");
|
|
|
+ // paramMap.put("mapName","合肥演示");
|
|
|
+ // paramMap.put("buildId"," 7b4e8d01c08f472fabf7ccff2b2336s");
|
|
|
+
|
|
|
+ // paramMap.put("floorId"," 695abbcfdf99471sdfff3b9b1d0818164e7");
|
|
|
+ // paramMap.put("updateType"," 7b4e8d01c08f472fabf7ccff2b2336s");
|
|
|
+ // paramMap.put("floor","1");
|
|
|
+ // paramMap.put("buildName"," 2");
|
|
|
+ // paramMap.put("customAreaId"," 355");
|
|
|
+ // paramMap.put("snCode","P1YTYD1M233M00224");
|
|
|
+ // paramMap.put("fileName","1702284791385137152.zip");
|
|
|
+ // paramMap.put("mapType","0");
|
|
|
+
|
|
|
+ paramMap["snCode"] = "P1YTXS1M22AM00001"
|
|
|
+ paramMap["mapId"] = "553d9d47f8404734997e11175f0e63f5"
|
|
|
+ paramMap["updateType"] = "0"
|
|
|
+ paramMap["mapType"] = "0"
|
|
|
+ paramMap["mapName"] = "北京办公区20230224"
|
|
|
+
|
|
|
+ bytes, err := io.ReadAll(f)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 调用 朴津地图上传 接口
|
|
|
+ resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiSitBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, bytes)
|
|
|
+ fmt.Println(string(resp.Body()))
|
|
|
+
|
|
|
+ c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传地图测试"})
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateDeviceMapById 根据场景id维护设备地图表
|
|
|
+// @router /map/update/deviceMap/record [GET]
|
|
|
+func UpdateDeviceMapById(ctx context.Context, c *app.RequestContext) {
|
|
|
+ sceneId := c.Query("sceneId")
|
|
|
+ fmt.Println("sceneId", sceneId)
|
|
|
+ deviceNo := c.Query("deviceNo")
|
|
|
+ fmt.Println("deviceNo", deviceNo)
|
|
|
+ deviceName := c.Query("deviceName")
|
|
|
+ fmt.Println("deviceName", deviceName)
|
|
|
+ deviceType := c.Query("deviceType")
|
|
|
+ fmt.Println("deviceType", deviceType)
|
|
|
+
|
|
|
+ // 根据场景id查询地图id
|
|
|
+ mapId, err := getMapIdById(sceneId)
|
|
|
+ if err != nil || mapId == "" {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("mapId", mapId)
|
|
|
+
|
|
|
+ // 查询朴津地图列表
|
|
|
+ mapInfoList, err := getMapInfoList()
|
|
|
+ if err != nil || mapInfoList == nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("mapInfoList", mapInfoList)
|
|
|
+
|
|
|
+ // 判断map id是否存在于朴津地图列表中
|
|
|
+ mapId = "fee84db56d254d79b85971cb4ab2e7f1" // 存在
|
|
|
+ //mapId = "553d9d47f8404734997e11175f0e63f5" // 不存在
|
|
|
+ exist, record := checkMapIdExist(mapId, mapInfoList)
|
|
|
+ fmt.Println("exist", exist)
|
|
|
+ fmt.Println("record", record)
|
|
|
+
|
|
|
+ if !exist { // map id不存在于朴津地图列表中
|
|
|
+ // 查询地图设备表中是否存在对应map id且有效的记录
|
|
|
+ records, err := mysql.QueryValidDeviceMapByMapId(ctx, mapId)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("records", records)
|
|
|
+ if len(records) > 0 { // 存在,
|
|
|
+ fmt.Println("设置无效")
|
|
|
+ // 将对应的map id的记录设置为无效
|
|
|
+ update, err := mysql.UpdateDeviceMapValidFlagByMapId(ctx, mapId, config.MAP_NOT_VALID_FLAG)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("update", update)
|
|
|
+ }
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1001", Message: "map id不存在地图列表中"})
|
|
|
+ } else { // map id存在于朴津地图列表中
|
|
|
+ snCodeList := record.SnCodeList
|
|
|
+ for _, snCode := range snCodeList { // snCodeList 代表配置了对应map id地图的机器人sn码列表, 上限为机器人数量
|
|
|
+ fmt.Println("snCode", snCode)
|
|
|
+ deviceMap := model.DeviceMap{DeviceNo: snCode, DeviceName: deviceName,
|
|
|
+ DeviceType: deviceType, MapID: record.MapId, MapType: record.MapType, MapName: record.MapName, FloorID: record.FloorId,
|
|
|
+ Floor: strconv.Itoa(record.Floor), BuildID: record.BuildId, BuildName: record.BuildName, MapCreateTime: record.CreateTime, MapUpdateTime: record.UpdateTime,
|
|
|
+ MapVersion: strconv.Itoa(record.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: record.ZipUrl, CustomAreaID: int32(record.CustomAreaId)}
|
|
|
+ fmt.Println("deviceMap", deviceMap)
|
|
|
+
|
|
|
+ deviceMap.MapVersion = "123456"
|
|
|
+
|
|
|
+ // 查询[对应设备]是否存在对应map id且有效的记录
|
|
|
+ records, err := mysql.QueryValidDeviceMapByDevice(ctx, mapId, snCode)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("records", records)
|
|
|
+
|
|
|
+ if len(records) == 0 { // 对应设备不存在对应map id且有效的记录, 说明该设备部署了新的地图(重采或新的区域地图)
|
|
|
+ // 添加记录
|
|
|
+ deviceMap.ID = uuid.NewV1().String()
|
|
|
+ err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else if len(records) == 1 { // 对应设备存在1个对应map id且有效的记录, 说明该设备之前部署过该地图, 需要检测版本号来判断更新记录(版本号一致)还是添加记录(版本号不一致)
|
|
|
+ fmt.Println("record", record)
|
|
|
+ if deviceMap.MapVersion == records[0].MapVersion { // 地图列表查询到的版本与设备地图表中版本一致, 则更新记录
|
|
|
+ // 更新记录
|
|
|
+ deviceMap.ID = records[0].ID
|
|
|
+ update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("update", update)
|
|
|
+ } else { // 地图列表查询到的版本与设备地图表中版本不一致, 则先将之前的记录状态设置为无效, 然后添加记录
|
|
|
+ // 将对应设备存在的对应map id的记录设置为无效
|
|
|
+ update, err := mysql.UpdateDeviceMapValidFlagByDevice(ctx, mapId, snCode, config.MAP_NOT_VALID_FLAG)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("update", update)
|
|
|
+ // 添加记录
|
|
|
+ deviceMap.ID = uuid.NewV1().String()
|
|
|
+ err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
|
|
|
+ if err != nil {
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //if len(records) > 0 { // 对应设备存在对应map id且有效的记录
|
|
|
+ // // 设置之前记录地图标志为无效
|
|
|
+ // fmt.Println("设置无效")
|
|
|
+ // // 将对应设备存在的对应map id的记录设置为无效
|
|
|
+ // update, err := mysql.UpdateDeviceMapValidFlagByDevice(ctx, mapId, snCode, config.MAP_NOT_VALID_FLAG)
|
|
|
+ // if err != nil {
|
|
|
+ // c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ // fmt.Println("update", update)
|
|
|
+ // // 更新记录
|
|
|
+ // mysql.UpdateDeviceMapOneRecord(ctx, deviceMap)
|
|
|
+ //} else { // 对应设备不存在对应map id且有效的记录
|
|
|
+ // // 添加记录
|
|
|
+ // deviceMap.ID = uuid.NewV1().String()
|
|
|
+ // err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
|
|
|
+ // if err != nil {
|
|
|
+ // c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
|
|
|
+ // return
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"})
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 计算oss中文件列表的总大小
|
|
|
func calculateTotalFileSize(fileList []string) int {
|
|
|
var totalSize int
|
|
@@ -410,3 +599,92 @@ func calculateTotalFileSize(fileList []string) int {
|
|
|
}
|
|
|
return totalSize
|
|
|
}
|
|
|
+
|
|
|
+// 调用 朴津地图列表查询 接口
|
|
|
+func getMapInfoList() ([]model.MapInfo, error) {
|
|
|
+ paramMap := make(map[string]interface{})
|
|
|
+ paramMap["mapType"] = "0"
|
|
|
+ resp, err := pji_client.ApiClient.GetRequest(c_pji.PjiApiSitBaseUrl+"mapList?mapType=0", paramMap, pji_client.MapSecretId)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ //fmt.Println(string(resp.Body()))
|
|
|
+
|
|
|
+ // 解析json响应
|
|
|
+ body := resp.Body()
|
|
|
+ var mapRes model.MapInfoRes
|
|
|
+ err = json.Unmarshal(body, &mapRes)
|
|
|
+ if err != nil || mapRes.Code != 200 {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ //fmt.Println("mapRes", mapRes)
|
|
|
+ return mapRes.Data, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 查询地图id是否存在于地图列表中
|
|
|
+func checkMapIdExist(mapId string, mapInfoList []model.MapInfo) (bool, model.MapInfo) {
|
|
|
+ // 转为map
|
|
|
+ IdMap := make(map[string]model.MapInfo)
|
|
|
+ for _, info := range mapInfoList {
|
|
|
+ IdMap[info.MapId] = info
|
|
|
+ }
|
|
|
+ record, exist := IdMap[mapId]
|
|
|
+ return exist, record
|
|
|
+}
|
|
|
+
|
|
|
+// 根据场景id获取数据采集时的地图id
|
|
|
+func getMapIdById(id string) (string, error) {
|
|
|
+ var mapId string
|
|
|
+ // 下载map.json
|
|
|
+ // 根据id获取对应的oss文件列表
|
|
|
+ allFileList, err := util.GetExactedMapFileById(id)
|
|
|
+ fmt.Println("allFileList", allFileList)
|
|
|
+
|
|
|
+ // 过滤特定后缀的文件列表
|
|
|
+ fileList := util.FilterBySuffixes(allFileList, config.MapJsonFiltersuffixes...)
|
|
|
+ fmt.Println("fileList", fileList)
|
|
|
+ // 创建临时文件夹
|
|
|
+ tmpDir, err := os.MkdirTemp("", "temp-download-*")
|
|
|
+
|
|
|
+ fmt.Println("tmpDir:", tmpDir)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error creating temporary directory:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
|
|
|
+
|
|
|
+ for _, file := range fileList {
|
|
|
+ path := filepath.Join(tmpDir, filepath.Base(file))
|
|
|
+ err = config.OssBucket.GetObjectToFile(file, path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error downloading file:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ c_log.GlobalLogger.Info("下载map.json文件 - 成功")
|
|
|
+ // 读取json文件 - mapId
|
|
|
+ // 打开文件
|
|
|
+ file, err := os.Open(path)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error opening file:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ defer file.Close()
|
|
|
+
|
|
|
+ // 读取文件内容
|
|
|
+ fileData, err := ioutil.ReadAll(file)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("Error reading file:", err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ // Json转换为map
|
|
|
+ var mapData map[string]interface{}
|
|
|
+ err = json.Unmarshal(fileData, &mapData)
|
|
|
+ data := mapData["map"].(map[string]interface{})
|
|
|
+ // 读取mapId
|
|
|
+ mapId = data["mapId"].(string)
|
|
|
+ fmt.Println("mapId:", mapId)
|
|
|
+ }
|
|
|
+
|
|
|
+ return mapId, err
|
|
|
+}
|