|
@@ -3,42 +3,52 @@ package config
|
|
|
import (
|
|
|
"cicv-data-closedloop/common/config/c_log"
|
|
|
"cicv-data-closedloop/common/util"
|
|
|
+ "crypto/md5"
|
|
|
+ "encoding/hex"
|
|
|
"encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "sort"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type taskTrigger struct {
|
|
|
- TriggerId int `json:"triggerId"`
|
|
|
- TriggerName string `json:"triggerName"`
|
|
|
- TriggerScriptPath string `json:"triggerScriptPath"`
|
|
|
- TriggerType string `json:"triggerType"`
|
|
|
+ TriggerId int `json:"triggerId"` // 触发器id
|
|
|
+ TriggerName string `json:"triggerName"` // 触发器名称
|
|
|
+ TriggerScriptPath string `json:"triggerScriptPath"` // 脚本在oss上的path
|
|
|
+ ListenTopic string `json:"listenTopic"` // 触发器监听的topic
|
|
|
+ CollectionTopic string `json:"collectionTopic"` // 触发器采集的topic
|
|
|
+ CollectionPreSeconds string `json:"collectionPreSeconds"` // 采集前几秒的数据
|
|
|
+ CollectionAfterSeconds string `json:"collectionAfterSeconds"` // 采集后几秒的数据
|
|
|
+ Priority string `json:"priority"` // 优先级, 数值越小越优先
|
|
|
}
|
|
|
|
|
|
type PlatformConfigStruct struct {
|
|
|
- TaskConfigId string `json:"taskConfigId"` // 配置ID
|
|
|
- TaskConfigName string `json:"taskConfigName"` // 配置名称
|
|
|
- DropUploadData bool `json:"dropUploadData"` // 更新任务时 true 先上传旧任务 false 删除旧任务
|
|
|
- TaskMaxTime int `json:"taskMaxTime"`
|
|
|
- TaskBeforeTime int `json:"taskBeforeTime"`
|
|
|
- TaskAfterTime int `json:"taskAfterTime"`
|
|
|
- TaskCachePolicy string `json:"taskCachePolicy"`
|
|
|
- EquipmentTopic string `json:"equipmentTopic"` // topic序列
|
|
|
- Lru []string `json:"LRU"`
|
|
|
- TaskTriggers []taskTrigger `json:"taskTriggers"`
|
|
|
+ TaskConfigId string `json:"taskConfigId"` // 任务ID
|
|
|
+ TaskConfigName string `json:"taskConfigName"` // 任务名称
|
|
|
+ TaskType string `json:"taskType"` // 任务类型
|
|
|
+ StartTime string `json:"startTime"` // 开始时间(taskType为“single”有效)
|
|
|
+ EndTime string `json:"endTime"` // 结束时间(taskType为“single”有效)
|
|
|
+ DailyStartTime string `json:"dailyStartTime"` // 开始时间(taskType为“daily/weekly/monthly”有效)
|
|
|
+ DailyEndTime string `json:"dailyEndTime"` // 结束时间(taskType为“daily/weekly/monthly”有效)
|
|
|
+ CollectionStrategy int `json:"collectionStrategy"` // 采集策略
|
|
|
+ UploadStrategy int `json:"uploadStrategy"` // 上传策略
|
|
|
+ CompressionType int `json:"compressionType"` // 压缩类型
|
|
|
+ TaskTriggers []taskTrigger `json:"taskTriggers"` // 触发器信息
|
|
|
}
|
|
|
|
|
|
type response struct {
|
|
|
- Data PlatformConfigStruct `json:"data"`
|
|
|
- Success bool `json:"success"`
|
|
|
- Message string `json:"message"`
|
|
|
- Code int `json:"code"`
|
|
|
- NowTime string `json:"nowTime"`
|
|
|
+ Data []PlatformConfigStruct `json:"data"`
|
|
|
+ Success bool `json:"success"`
|
|
|
+ Message string `json:"message"`
|
|
|
+ Code int `json:"code"`
|
|
|
+ NowTime string `json:"nowTime"`
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
- PlatformConfig PlatformConfigStruct
|
|
|
- RecordTopics []string
|
|
|
+ PlatformConfig PlatformConfigStruct
|
|
|
+ PlatformConfig1 []PlatformConfigStruct
|
|
|
+ RecordTopics []string
|
|
|
)
|
|
|
|
|
|
// InitPlatformConfig 初始化数据闭环平台的配置
|
|
@@ -51,21 +61,21 @@ func InitPlatformConfig() {
|
|
|
// 1 如果车辆没有配置任务,则阻塞在这里,不启动任务
|
|
|
for {
|
|
|
time.Sleep(time.Duration(2) * time.Second)
|
|
|
- PlatformConfig, err = getConfig()
|
|
|
+ PlatformConfig1, err = GetConfig()
|
|
|
if err != nil {
|
|
|
c_log.GlobalLogger.Error("获取配置status失败:", err)
|
|
|
continue
|
|
|
}
|
|
|
- if checkPlatformConfig() {
|
|
|
- RecordTopics = strings.Split(PlatformConfig.EquipmentTopic, ",")
|
|
|
- // 去掉首尾空格
|
|
|
- for i, topic := range RecordTopics {
|
|
|
- RecordTopics[i] = strings.TrimSpace(topic)
|
|
|
- }
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- c_log.GlobalLogger.Infof("获取数据闭环平台配置 - 成功。【触发前】=【%v 秒】,触发后=【%v 秒】,【最大窗口】=【%v 秒】。", PlatformConfig.TaskBeforeTime, PlatformConfig.TaskAfterTime, PlatformConfig.TaskMaxTime)
|
|
|
+ //if checkPlatformConfig() {
|
|
|
+ // RecordTopics = strings.Split(PlatformConfig.EquipmentTopic, ",")
|
|
|
+ // // 去掉首尾空格
|
|
|
+ // for i, topic := range RecordTopics {
|
|
|
+ // RecordTopics[i] = strings.TrimSpace(topic)
|
|
|
+ // }
|
|
|
+ // break
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ //c_log.GlobalLogger.Infof("获取数据闭环平台配置 - 成功。【触发前】=【%v 秒】,触发后=【%v 秒】,【最大窗口】=【%v 秒】。", PlatformConfig.TaskBeforeTime, PlatformConfig.TaskAfterTime, PlatformConfig.TaskMaxTime)
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -155,32 +165,39 @@ func GetStatus(taskConfigId string) (string, error) {
|
|
|
return dataMap["status"].(string), nil
|
|
|
}
|
|
|
|
|
|
-func getConfig() (PlatformConfigStruct, error) {
|
|
|
- token, err := GetAccessToken()
|
|
|
- if err != nil {
|
|
|
- return PlatformConfigStruct{}, err
|
|
|
- }
|
|
|
- // 下载插件和获取配置
|
|
|
- // 2 访问配置获取接口
|
|
|
+func GetConfig() ([]PlatformConfigStruct, error) {
|
|
|
+ queryParams := map[string]string{
|
|
|
+ "equipmentNo": LocalConfig.SecretKey,
|
|
|
+ }
|
|
|
+ // 1. 生成签名
|
|
|
+ sign := GenerateSign(CloudConfig.Platform.Ak, CloudConfig.Platform.Sk, queryParams)
|
|
|
+ fmt.Println("sign:", sign)
|
|
|
+ if sign == "" {
|
|
|
+ return []PlatformConfigStruct{}, fmt.Errorf("生成签名失败。")
|
|
|
+ }
|
|
|
+ headers := map[string]string{
|
|
|
+ "ak": CloudConfig.Platform.Ak,
|
|
|
+ "timestamp": fmt.Sprintf("%d", time.Now().Unix()),
|
|
|
+ "sign": sign,
|
|
|
+ }
|
|
|
+ // 2 访问任务配置获取接口
|
|
|
resp, err := util.HttpGetStringAddHeadersResponseString(
|
|
|
CloudConfig.Platform.UrlTask,
|
|
|
- map[string]string{
|
|
|
- "authorization": token,
|
|
|
- },
|
|
|
- map[string]string{
|
|
|
- "equipmentNo": LocalConfig.EquipmentNo,
|
|
|
- },
|
|
|
+ headers,
|
|
|
+ queryParams,
|
|
|
)
|
|
|
if err != nil {
|
|
|
c_log.GlobalLogger.Error("访问接口", CloudConfig.Platform.UrlTask, "失败:", err)
|
|
|
- return PlatformConfigStruct{}, err
|
|
|
+ return []PlatformConfigStruct{}, err
|
|
|
}
|
|
|
var result response
|
|
|
err = json.Unmarshal([]byte(resp), &result)
|
|
|
+ fmt.Println("result", result)
|
|
|
if err != nil {
|
|
|
c_log.GlobalLogger.Error("解析【返回结果】", resp, "失败:", err)
|
|
|
- return PlatformConfigStruct{}, err
|
|
|
+ return []PlatformConfigStruct{}, err
|
|
|
}
|
|
|
+
|
|
|
return result.Data, nil
|
|
|
}
|
|
|
|
|
@@ -189,9 +206,37 @@ func checkPlatformConfig() bool {
|
|
|
c_log.GlobalLogger.Error("数据闭环平台没有配置任务。")
|
|
|
return false
|
|
|
}
|
|
|
- if PlatformConfig.EquipmentTopic == "" {
|
|
|
- c_log.GlobalLogger.Error("数据闭环平台没有配置topic序列。")
|
|
|
- return false
|
|
|
- }
|
|
|
+ //if PlatformConfig.EquipmentTopic == "" {
|
|
|
+ // c_log.GlobalLogger.Error("数据闭环平台没有配置topic序列。")
|
|
|
+ // return false
|
|
|
+ //}
|
|
|
return true
|
|
|
}
|
|
|
+
|
|
|
+// GenerateSign 生成签名
|
|
|
+func GenerateSign(ak, sk string, params map[string]string) string {
|
|
|
+ // 1. 添加时间戳
|
|
|
+ params["timestamp"] = fmt.Sprintf("%d", time.Now().Unix())
|
|
|
+
|
|
|
+ // 2. 参数排序
|
|
|
+ keys := make([]string, 0, len(params))
|
|
|
+ for k := range params {
|
|
|
+ keys = append(keys, k)
|
|
|
+ }
|
|
|
+ sort.Strings(keys)
|
|
|
+
|
|
|
+ // 3. 拼接参数
|
|
|
+ var buffer strings.Builder
|
|
|
+ for _, k := range keys {
|
|
|
+ buffer.WriteString(k)
|
|
|
+ buffer.WriteByte('=')
|
|
|
+ buffer.WriteString(params[k])
|
|
|
+ buffer.WriteByte('&')
|
|
|
+ }
|
|
|
+ paramStr := buffer.String()[:len(buffer.String())-1]
|
|
|
+
|
|
|
+ // 4. 计算MD5
|
|
|
+ signStr := paramStr + sk
|
|
|
+ hash := md5.Sum([]byte(signStr))
|
|
|
+ return hex.EncodeToString(hash[:])
|
|
|
+}
|