c_cloud.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package config
  2. import (
  3. "cicv-data-closedloop/common/config/c_log"
  4. "cicv-data-closedloop/common/util"
  5. "gopkg.in/yaml.v3"
  6. "os"
  7. "strings"
  8. "sync"
  9. "time"
  10. )
  11. type MonitorStruct struct {
  12. Url string `yaml:"url"`
  13. }
  14. type platform struct {
  15. UrlDeviceAuth string `yaml:"url-device-auth"`
  16. UrlTaskPoll string `yaml:"url-task-poll"`
  17. UrlTask string `yaml:"url-task"`
  18. }
  19. type rosbagStruct struct {
  20. Path string `yaml:"path"`
  21. Envs []string `yaml:"envs"`
  22. }
  23. type hostStruct struct {
  24. Name string `yaml:"name"`
  25. Ip string `yaml:"ip"`
  26. Topics []string `yaml:"topics"`
  27. Rosbag rosbagStruct `yaml:"rosbag"`
  28. }
  29. type ros struct {
  30. MasterAddress string `yaml:"master-address"`
  31. Nodes []string `yaml:"nodes"`
  32. }
  33. type disk struct {
  34. Name string `yaml:"name"`
  35. Used uint64 `yaml:"used"`
  36. }
  37. type trigger struct {
  38. Label string `yaml:"label"`
  39. Topics []string `yaml:"topics"`
  40. }
  41. type cloudConfig struct {
  42. FullCollect bool `yaml:"full-collect"`
  43. ConfigRefreshInterval int `yaml:"config-refresh-interval"` // 配置刷新时间间隔
  44. BagNumber int `yaml:"bag-number"`
  45. TimeWindowSendGap int `yaml:"time-window-send-gap"` // 主节点向从节点发送窗口的最小时间间隔
  46. MapBagPath string `yaml:"map-bag-path"`
  47. BagDataDir string `yaml:"bag-data-dir"`
  48. BagCopyDir string `yaml:"bag-copy-dir"`
  49. TriggersDir string `yaml:"triggers-dir"`
  50. RpcPort string `yaml:"rpc-port"`
  51. Triggers []trigger `yaml:"triggers"`
  52. Hosts []hostStruct `yaml:"hosts"`
  53. Ros ros `yaml:"ros"`
  54. Platform platform `yaml:"platform"`
  55. Disk disk `yaml:"disk"`
  56. Monitor MonitorStruct `yaml:"monitor"`
  57. }
  58. var (
  59. CloudConfig cloudConfig
  60. CloudConfigMutex sync.RWMutex
  61. )
  62. // InitCloudConfig 初始化业务配置
  63. func InitCloudConfig() {
  64. // history20240401:朴津机器人额外加一个获取sn码
  65. var snCode string
  66. for {
  67. time.Sleep(time.Duration(2) * time.Second)
  68. var command []string
  69. command = append(command, "get")
  70. command = append(command, "sn")
  71. _, snOutput, err := util.ExecuteSync(LocalConfig.RosparamPath, command...)
  72. if err != nil {
  73. c_log.GlobalLogger.Error("执行获取sn码命令", command, "出错:", err)
  74. continue
  75. }
  76. c_log.GlobalLogger.Error("执行获取sn码命令", command, "成功,结果为:", snOutput)
  77. snCode = strings.Replace(strings.Replace(snOutput, " ", "", -1), "\n", "", -1)
  78. LocalConfig.SecretKey = snCode
  79. LocalConfig.EquipmentNo = "pjibot-" + snCode
  80. break
  81. }
  82. c_log.GlobalLogger.Info("本地机器人sn码为:", snCode)
  83. c_log.GlobalLogger.Info("初始化OSS配置文件 - 开始。")
  84. // 获取文件的目录
  85. _ = util.CreateParentDir(LocalConfig.CloudConfigLocalPath)
  86. // 3 ------- 获取 yaml 字符串 -------
  87. cloudConfigObjectKey := LocalConfig.OssBasePrefix + LocalConfig.EquipmentNo + "/" + LocalConfig.CloudConfigFilename
  88. for {
  89. time.Sleep(time.Duration(2) * time.Second)
  90. OssMutex.Lock()
  91. err := OssBucket.GetObjectToFile(cloudConfigObjectKey, LocalConfig.CloudConfigLocalPath)
  92. OssMutex.Unlock()
  93. if err != nil {
  94. c_log.GlobalLogger.Error("下载 OSS 上的配置文件 "+cloudConfigObjectKey+" 失败,请尽快在 OSS 上传配置文件。", err)
  95. continue
  96. }
  97. break
  98. }
  99. content, err := os.ReadFile(LocalConfig.CloudConfigLocalPath)
  100. if err != nil {
  101. c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 读取失败:", err)
  102. os.Exit(-1)
  103. }
  104. // 4 ------- 解析YAML内容 -------
  105. var newCloudConfig cloudConfig
  106. err = yaml.Unmarshal(content, &newCloudConfig)
  107. if err != nil {
  108. c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
  109. os.Exit(-1)
  110. }
  111. // 5 ------- 校验 yaml -------
  112. if checkCloudConfig(newCloudConfig) {
  113. CloudConfigMutex.RLock()
  114. CloudConfig = newCloudConfig
  115. CloudConfigMutex.RUnlock()
  116. } else {
  117. c_log.GlobalLogger.Error("程序崩溃,配置文件格式错误:", newCloudConfig)
  118. os.Exit(-1)
  119. }
  120. c_log.GlobalLogger.Info("初始化OSS配置文件 - 成功。")
  121. util.CreateDir(CloudConfig.BagDataDir)
  122. util.CreateDir(CloudConfig.BagCopyDir)
  123. }
  124. // refreshCloudConfig 更新业务配置
  125. func refreshCloudConfig() {
  126. // 获取文件的目录
  127. _ = util.CreateParentDir(LocalConfig.CloudConfigLocalPath)
  128. // 3 ------- 获取 yaml 字符串 -------
  129. var content []byte
  130. cloudConfigObjectKey := LocalConfig.OssBasePrefix + LocalConfig.EquipmentNo + "/" + LocalConfig.CloudConfigFilename
  131. OssMutex.Lock()
  132. err := OssBucket.GetObjectToFile(cloudConfigObjectKey, LocalConfig.CloudConfigLocalPath)
  133. OssMutex.Unlock()
  134. if err != nil {
  135. c_log.GlobalLogger.Error("下载oss上的配置文件"+cloudConfigObjectKey+"失败。", err)
  136. //os.Exit(-1)
  137. }
  138. content, err = os.ReadFile(LocalConfig.CloudConfigLocalPath)
  139. if err != nil {
  140. c_log.GlobalLogger.Error("配置文件 ", LocalConfig.CloudConfigLocalPath, " 读取失败:", err)
  141. os.Exit(-1)
  142. }
  143. // 4 ------- 解析YAML内容 -------
  144. var newCloudConfig cloudConfig
  145. err = yaml.Unmarshal(content, &newCloudConfig)
  146. if err != nil {
  147. c_log.GlobalLogger.Error("配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
  148. os.Exit(-1)
  149. }
  150. // 5 ------- 校验 yaml -------
  151. if checkCloudConfig(newCloudConfig) {
  152. CloudConfigMutex.RLock()
  153. CloudConfig = newCloudConfig
  154. CloudConfigMutex.RUnlock()
  155. } else {
  156. c_log.GlobalLogger.Error("配置文件格式错误:", newCloudConfig)
  157. os.Exit(-1)
  158. }
  159. util.CreateDir(CloudConfig.BagDataDir)
  160. util.CreateDir(CloudConfig.BagCopyDir)
  161. }
  162. // RefreshCloudConfig 轮询oss上的配置文件更新到本地
  163. func RefreshCloudConfig() {
  164. for {
  165. time.Sleep(time.Duration(CloudConfig.ConfigRefreshInterval) * time.Second)
  166. refreshCloudConfig()
  167. }
  168. }
  169. // CheckConfig 校验 cfg.yaml 文件
  170. func checkCloudConfig(check cloudConfig) bool {
  171. if len(check.Hosts) != 1 {
  172. c_log.GlobalLogger.Error("cloud-config.yaml中配置的hosts必须为1。")
  173. os.Exit(-1)
  174. }
  175. return true
  176. }