c_cloud.go 5.8 KB

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