c_cloud.go 5.9 KB

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