c_cloud.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. BagDataDir string `yaml:"bag-data-dir"`
  47. BagCopyDir string `yaml:"bag-copy-dir"`
  48. TriggersDir string `yaml:"triggers-dir"`
  49. RpcPort string `yaml:"rpc-port"`
  50. Triggers []trigger `yaml:"triggers"`
  51. Hosts []hostStruct `yaml:"hosts"`
  52. Ros ros `yaml:"ros"`
  53. Platform platform `yaml:"platform"`
  54. Disk disk `yaml:"disk"`
  55. Monitor MonitorStruct `yaml:"monitor"`
  56. }
  57. var (
  58. CloudConfig cloudConfig
  59. CloudConfigMutex sync.RWMutex
  60. )
  61. // InitCloudConfig 初始化业务配置
  62. func InitCloudConfig() {
  63. // history20240401:朴津机器人额外加一个获取sn码
  64. var snCode string
  65. for {
  66. time.Sleep(time.Duration(2) * time.Second)
  67. var command []string
  68. command = append(command, "get")
  69. command = append(command, "sn")
  70. _, snOutput, err := util.ExecuteSync(LocalConfig.RosparamPath, command...)
  71. if err != nil {
  72. c_log.GlobalLogger.Error("执行获取sn码命令", command, "出错:", err)
  73. continue
  74. }
  75. c_log.GlobalLogger.Error("执行获取sn码命令", command, "成功,结果为:", snOutput)
  76. snCode = strings.Replace(strings.Replace(snOutput, " ", "", -1), "\n", "", -1)
  77. LocalConfig.SecretKey = snCode
  78. LocalConfig.EquipmentNo = "pjibot-" + snCode
  79. break
  80. }
  81. c_log.GlobalLogger.Info("本地机器人sn码为:", snCode)
  82. c_log.GlobalLogger.Info("初始化OSS配置文件 - 开始。")
  83. // 获取文件的目录
  84. _ = util.CreateParentDir(LocalConfig.CloudConfigLocalPath)
  85. // 3 ------- 获取 yaml 字符串 -------
  86. cloudConfigObjectKey := LocalConfig.OssBasePrefix + LocalConfig.EquipmentNo + "/" + LocalConfig.CloudConfigFilename
  87. for {
  88. time.Sleep(time.Duration(2) * time.Second)
  89. OssMutex.Lock()
  90. err := OssBucket.GetObjectToFile(cloudConfigObjectKey, LocalConfig.CloudConfigLocalPath)
  91. OssMutex.Unlock()
  92. if err != nil {
  93. c_log.GlobalLogger.Error("下载 OSS 上的配置文件 "+cloudConfigObjectKey+" 失败,请尽快在 OSS 上传配置文件。", err)
  94. continue
  95. }
  96. break
  97. }
  98. content, err := os.ReadFile(LocalConfig.CloudConfigLocalPath)
  99. if err != nil {
  100. c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 读取失败:", err)
  101. os.Exit(-1)
  102. }
  103. // 4 ------- 解析YAML内容 -------
  104. var newCloudConfig cloudConfig
  105. err = yaml.Unmarshal(content, &newCloudConfig)
  106. if err != nil {
  107. c_log.GlobalLogger.Error("程序崩溃,配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
  108. os.Exit(-1)
  109. }
  110. // 5 ------- 校验 yaml -------
  111. if checkCloudConfig(newCloudConfig) {
  112. CloudConfigMutex.RLock()
  113. CloudConfig = newCloudConfig
  114. CloudConfigMutex.RUnlock()
  115. } else {
  116. c_log.GlobalLogger.Error("程序崩溃,配置文件格式错误:", newCloudConfig)
  117. os.Exit(-1)
  118. }
  119. c_log.GlobalLogger.Info("初始化OSS配置文件 - 成功。")
  120. util.CreateDir(CloudConfig.BagDataDir)
  121. util.CreateDir(CloudConfig.BagCopyDir)
  122. }
  123. // refreshCloudConfig 更新业务配置
  124. func refreshCloudConfig() {
  125. // 获取文件的目录
  126. _ = util.CreateParentDir(LocalConfig.CloudConfigLocalPath)
  127. // 3 ------- 获取 yaml 字符串 -------
  128. var content []byte
  129. cloudConfigObjectKey := LocalConfig.OssBasePrefix + LocalConfig.EquipmentNo + "/" + LocalConfig.CloudConfigFilename
  130. OssMutex.Lock()
  131. err := OssBucket.GetObjectToFile(cloudConfigObjectKey, LocalConfig.CloudConfigLocalPath)
  132. OssMutex.Unlock()
  133. if err != nil {
  134. c_log.GlobalLogger.Error("下载oss上的配置文件"+cloudConfigObjectKey+"失败。", err)
  135. //os.Exit(-1)
  136. }
  137. content, err = os.ReadFile(LocalConfig.CloudConfigLocalPath)
  138. if err != nil {
  139. c_log.GlobalLogger.Error("配置文件 ", LocalConfig.CloudConfigLocalPath, " 读取失败:", err)
  140. os.Exit(-1)
  141. }
  142. // 4 ------- 解析YAML内容 -------
  143. var newCloudConfig cloudConfig
  144. err = yaml.Unmarshal(content, &newCloudConfig)
  145. if err != nil {
  146. c_log.GlobalLogger.Error("配置文件 ", LocalConfig.CloudConfigLocalPath, " 解析失败:", err)
  147. os.Exit(-1)
  148. }
  149. // 5 ------- 校验 yaml -------
  150. if checkCloudConfig(newCloudConfig) {
  151. CloudConfigMutex.RLock()
  152. CloudConfig = newCloudConfig
  153. CloudConfigMutex.RUnlock()
  154. } else {
  155. c_log.GlobalLogger.Error("配置文件格式错误:", newCloudConfig)
  156. os.Exit(-1)
  157. }
  158. util.CreateDir(CloudConfig.BagDataDir)
  159. util.CreateDir(CloudConfig.BagCopyDir)
  160. }
  161. // RefreshCloudConfig 轮询oss上的配置文件更新到本地
  162. func RefreshCloudConfig() {
  163. for {
  164. time.Sleep(time.Duration(CloudConfig.ConfigRefreshInterval) * time.Second)
  165. refreshCloudConfig()
  166. }
  167. }
  168. // CheckConfig 校验 cfg.yaml 文件
  169. func checkCloudConfig(check cloudConfig) bool {
  170. if len(check.Hosts) != 1 {
  171. c_log.GlobalLogger.Error("cloud-config.yaml中配置的hosts必须为1。")
  172. os.Exit(-1)
  173. }
  174. return true
  175. }